cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <view class="container">
  3. <view class="swiper">
  4. <view class="swiper-text">共{{getCartAllGoods()}}件商品</view>
  5. <view class="swiper-text" style="float: right;" @click="manageType = !manageType">{{manageType ? '完成' :'管理'}}</view>
  6. </view>
  7. <view class="cart-box" :style="{bottom: manageType ? '52px' : '0px'}">
  8. <view class="shop-row" v-for="(item, index1) in cartsList" :key="index1">
  9. <view class="shop-info" @click="goToShop(item)">
  10. <view class="iconfont" :class="item.allCheck ? 'iconqueding' : 'iconfeigouxuan'" @click="checkShop(item)"></view>
  11. <view class="iconfont iconshangjia" @click=""></view>
  12. <view class="shop-name" @click="">{{item.supplierName}}</view>
  13. <view class="iconfont iconfangxiang" @click=""></view>
  14. </view>
  15. <view class="goods-list">
  16. <view class="goods-row" v-for="(site, index2) in item.products" :key="index2" @click.stop="goToGoodDetails(site)">
  17. <view class="iconfont" :class="site.check ? 'iconqueding' : 'iconfeigouxuan'" @click.stop="checkGoods(item, site)"></view>
  18. <cover-image class="goods-img" :src="site.imgUrl"></cover-image>
  19. <view class="goods-info">
  20. <view class="goods-name">{{site.productName}}</view>
  21. <view class="goods-type">类型:{{site.productType == 1 ? '普通商品' : '自助采摘'}}</view>
  22. <view class="goods-handle">
  23. <view class="goods-price">
  24. <text style="font-size: 15px;">¥</text>{{site.bizPrice}}
  25. </view>
  26. <view class="handle-box" @click.stop="">
  27. <uni-icons type="minus-filled" size="20" color="#A67A54" @click.native.stop="numSub(item, site)"></uni-icons>
  28. <text class="good-select">{{site.buyNum}}</text>
  29. <uni-icons type="plus-filled" size="20" color="#A67A54" @click.native.stop="numAdd(item, site)"></uni-icons>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. <view class="shop-handle-box">
  36. <view class="shop-handle-text">
  37. <text class="shop-text">合计</text>
  38. <text class="shop-price">¥{{getShopCheckPrice(item)}}</text>
  39. </view>
  40. <u-button type="success" size="medium" shape="circle" :ripple="true" @click="toPay(item)" :disabled="!getShopCheckGoods(item)"
  41. class="handle-button">结算({{getShopCheckGoods(item)}})</u-button>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="cart-handle-box" v-if="manageType">
  46. <view class="iconfont" :class="getAllCartType() ? 'iconqueding' : 'iconfeigouxuan'" @click="checkAllShop()"></view>
  47. <view class="icon-text" @click="checkAllShop()">{{getAllCartType() ? '取消' : ''}}全选</view>
  48. <u-button type="success" size="medium" shape="circle" :ripple="true" :hair-line="false" :plain="true" @click="deleteSelectCarts()"
  49. class="cart-handle-delete">删除</u-button>
  50. </view>
  51. <u-modal v-model="modalShow" content="是否删除勾选商品?" @confirm="submitDeleteData()" :async-close="true"
  52. :show-cancel-button="true"></u-modal>
  53. <u-top-tips ref="uTips"></u-top-tips>
  54. </view>
  55. </template>
  56. <script>
  57. const NET = require('@/utils/request')
  58. const API = require('@/config/api')
  59. export default {
  60. data() {
  61. return {
  62. cartsList: [],
  63. cartNum: 1,
  64. manageType: false,
  65. modalShow: false,
  66. }
  67. },
  68. onShow() {
  69. this.cartsList = []
  70. this.getCartData()
  71. },
  72. methods: {
  73. // 跳转商铺页
  74. goToShop(item) {
  75. uni.navigateTo({
  76. url: '/pagesGood/shopDetails?goodId=' + item.products[0].productId
  77. });
  78. },
  79. // 跳转商品详情
  80. goToGoodDetails(site) {
  81. uni.navigateTo({
  82. url: '/pagesGood/goodDetails?goodId=' + site.productId
  83. });
  84. },
  85. // 获取购物车列表
  86. getCartData() {
  87. NET.request(API.getCartList, {}, 'GET').then(res => {
  88. res.data.merchants.forEach(item => {
  89. item.allCheck = false
  90. item.products.forEach(site => {
  91. site.check = false
  92. })
  93. })
  94. this.cartsList = res.data.merchants
  95. }).catch(error => {
  96. this.$refs.uTips.show({
  97. title: '获取购物车列表失败',
  98. type: 'warning',
  99. })
  100. })
  101. },
  102. // 获取商铺下选中商品价格
  103. getShopCheckPrice(item) {
  104. return item.products.reduce((total, site) => {
  105. return total + (site.check ? (site.buyNum * site.bizPrice) : 0)
  106. }, 0)
  107. },
  108. // 获取全部商铺下商品
  109. getCartAllGoods() {
  110. return this.cartsList.reduce((total1, item) => {
  111. return total1 + item.products.reduce((total2, site) => {
  112. return total2 + site.buyNum
  113. }, 0)
  114. }, 0)
  115. },
  116. // 获取全部商铺下商品
  117. getShopCheckGoods(item) {
  118. return item.products.reduce((total, site) => {
  119. return total + (site.check ? site.buyNum : 0)
  120. }, 0)
  121. },
  122. // 全选店铺
  123. checkShop(item) {
  124. item.allCheck = !item.allCheck
  125. item.products.forEach(site => {
  126. site.check = item.allCheck
  127. })
  128. },
  129. // 勾选商品
  130. checkGoods(item, site) {
  131. site.check = !site.check
  132. item.allCheck = item.products.filter(site => site.check).length == item.products.length
  133. },
  134. // 数量减
  135. numSub(item, site) {
  136. if (site.buyNum > 1) {
  137. site.buyNum--
  138. this.setCartNum(item, site)
  139. }
  140. },
  141. // 数量加
  142. numAdd(item, site) {
  143. site.buyNum++
  144. this.setCartNum(item, site)
  145. },
  146. // 提交购物车变更参数
  147. setCartNum(item, site) {
  148. NET.request(API.editCart, {
  149. vos: [{
  150. buyNum: site.buyNum,
  151. flag: 2,
  152. selected: site.selected,
  153. shoppingcartId: site.shoppingcartId,
  154. }]
  155. }, 'PUT').then(res => {}).catch(error => {
  156. this.$refs.uTips.show({
  157. title: '改变商品数量失败',
  158. type: 'warning',
  159. })
  160. })
  161. },
  162. // 检测所有商铺全选情况
  163. getAllCartType() {
  164. return this.cartsList.filter(site => site.allCheck).length == this.cartsList.length
  165. },
  166. // 设置所有商铺全选状态
  167. checkAllShop() {
  168. let type = this.getAllCartType()
  169. this.cartsList.forEach(item => {
  170. item.allCheck = !type
  171. item.products.forEach(site => {
  172. site.check = !type
  173. })
  174. })
  175. },
  176. // 删除购物车
  177. deleteSelectCarts(item, site) {
  178. this.modalShow = true
  179. },
  180. // 提交删除购物车数据
  181. submitDeleteData() {
  182. let shoppingcartIds = []
  183. this.cartsList.forEach(item => {
  184. item.products.forEach(site => {
  185. if (site.check) {
  186. shoppingcartIds.push(site.shoppingcartId)
  187. }
  188. })
  189. })
  190. if (!shoppingcartIds.length) {
  191. this.modalShow = false
  192. this.$refs.uTips.show({
  193. title: '请勾选商品',
  194. type: 'warning',
  195. })
  196. return false
  197. }
  198. let str = '?'
  199. shoppingcartIds.forEach((site, index) => {
  200. str += (index ? '&' : '') + 'shoppingcartIds=' + site
  201. })
  202. NET.request(API.deleteCart + str, {}, 'POST').then(res => {
  203. this.modalShow = false
  204. this.manageType = false
  205. this.getCartData()
  206. }).catch(error => {
  207. this.modalShow = false
  208. this.$refs.uTips.show({
  209. title: '删除购物车失败',
  210. type: 'warning',
  211. })
  212. })
  213. },
  214. // 提交购物车变更参数
  215. toPay(item) {
  216. uni.removeStorageSync('defaultAddress');
  217. uni.setStorage({
  218. key: 'orderData',
  219. data: {
  220. tenantCode: item.tenantCode,
  221. supplierName: item.supplierName,
  222. areaSize: '',
  223. term: '',
  224. goodsList: item.products.filter(site => site.check).map(site => {
  225. return {
  226. bizPrice: site.bizPrice,
  227. buyNum: site.buyNum,
  228. imgUrl: site.imgUrl,
  229. originalPrice: site.originalPrice,
  230. productId: site.productId,
  231. productName: site.productName,
  232. productType: site.productType,
  233. shoppingcartId: site.shoppingcartId,
  234. }
  235. })
  236. }
  237. });
  238. uni.navigateTo({
  239. url: '/pagesGood/orderPay?flag=1&orderType=1'
  240. });
  241. }
  242. }
  243. }
  244. </script>
  245. <style lang="less" scoped>
  246. page {
  247. width: 100%;
  248. height: 100%;
  249. }
  250. .container {
  251. width: 100%;
  252. height: 100%;
  253. background-color: #F3F3F3;
  254. position: relative;
  255. .iconqueding,
  256. .iconfeigouxuan {
  257. color: #52A63A;
  258. font-size: 36px;
  259. margin-left: 5px;
  260. }
  261. .swiper {
  262. width: 100%;
  263. height: 120px;
  264. float: left;
  265. background-color: #52A63A;
  266. border-radius: 0 0 40px 40px;
  267. .swiper-text {
  268. float: left;
  269. line-height: 16px;
  270. font-size: 15px;
  271. color: #FFFFFF;
  272. padding: 16px 15px;
  273. }
  274. }
  275. .cart-box {
  276. width: calc(100% - 30px);
  277. min-height: calc(100vh - 40px - 50px);
  278. overflow: visible;
  279. position: absolute;
  280. top: 50px;
  281. bottom: 0;
  282. left: 15px;
  283. overflow-y: auto;
  284. .shop-row {
  285. width: 100%;
  286. float: left;
  287. background: #FFFFFF;
  288. border-radius: 10px;
  289. margin-bottom: 15px;
  290. .shop-info {
  291. width: 100%;
  292. height: 50px;
  293. float: left;
  294. display: flex;
  295. align-items: center;
  296. border-bottom: 1px solid #EEEEEE;
  297. .shop-name {
  298. font-size: 15px;
  299. font-family: PingFang SC;
  300. color: #333333;
  301. margin: 0 6px;
  302. }
  303. .iconshangjia {
  304. color: #333333;
  305. font-size: 26px;
  306. }
  307. .iconfangxiang {
  308. color: #999999;
  309. font-size: 20px;
  310. }
  311. }
  312. .goods-list {
  313. width: 100%;
  314. float: left;
  315. .goods-row {
  316. width: 100%;
  317. height: 120px;
  318. float: left;
  319. padding-top: 15px;
  320. border-bottom: 1px solid #EEEEEE;
  321. display: flex;
  322. .iconqueding,
  323. .iconfeigouxuan {
  324. margin-top: 26px;
  325. }
  326. .goods-img {
  327. width: 90px;
  328. height: 90px;
  329. margin: 0 15px 0 2px;
  330. border-radius: 5px;
  331. object-fit: cover;
  332. overflow: hidden;
  333. }
  334. .goods-info {
  335. height: 90px;
  336. flex: 1;
  337. .goods-name {
  338. width: 100%;
  339. height: 32px;
  340. float: left;
  341. font-size: 12px;
  342. font-family: PingFang SC;
  343. color: #333333;
  344. line-height: 16px;
  345. overflow: hidden;
  346. text-overflow: ellipsis;
  347. display: -webkit-box;
  348. -webkit-line-clamp: 2;
  349. -webkit-box-orient: vertical;
  350. word-wrap: break-word;
  351. margin-bottom: 8px;
  352. }
  353. .goods-type {
  354. float: left;
  355. height: 20px;
  356. padding: 0 8px;
  357. background: #F0F0F0;
  358. border-radius: 4px;
  359. color: #666666;
  360. font-size: 10px;
  361. line-height: 20px;
  362. margin-bottom: 10px;
  363. }
  364. .goods-handle {
  365. width: 100%;
  366. height: 20px;
  367. box-sizing: border-box;
  368. padding-right: 15px;
  369. float: left;
  370. display: flex;
  371. justify-content: space-between;
  372. align-items: center;
  373. line-height: 20px;
  374. .goods-price {
  375. flex: 1;
  376. font-size: 18px;
  377. font-family: PingFang SC;
  378. color: #52A63A;
  379. line-height: 20px;
  380. }
  381. .handle-box {
  382. flex: 1;
  383. line-height: 20px;
  384. text-align: right;
  385. .good-select {
  386. font-size: 16px;
  387. font-family: PingFang SC;
  388. color: #333333;
  389. margin: 0 10px;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. .shop-handle-box {
  397. width: 100%;
  398. height: 50px;
  399. float: left;
  400. box-sizing: border-box;
  401. padding: 0 16px;
  402. .shop-handle-text {
  403. width: calc(100% - 130px);
  404. height: 50px;
  405. float: left;
  406. white-space: nowrap;
  407. font-size: 15px;
  408. font-family: PingFang SC;
  409. line-height: 50px;
  410. text-align: right;
  411. .shop-text {
  412. color: #333333;
  413. margin-right: 16px;
  414. }
  415. .shop-price {
  416. color: #52A63A;
  417. }
  418. }
  419. .handle-button {
  420. width: 110px;
  421. display: block;
  422. float: right;
  423. // background-color: #52A63A;
  424. margin-top: 7px;
  425. margin-right: 15px;
  426. padding: 0;
  427. text-align: center;
  428. }
  429. }
  430. }
  431. }
  432. .cart-handle-box {
  433. width: 100%;
  434. height: 52px;
  435. position: absolute;
  436. z-index: 1;
  437. bottom: 0;
  438. background: #FFFFFF;
  439. box-sizing: border-box;
  440. border-top: 1px solid #EEEEEE;
  441. padding: 0 15px 0 15px;
  442. .iconfont {
  443. height: 52px;
  444. line-height: 52px;
  445. float: left;
  446. display: block;
  447. }
  448. .icon-text {
  449. height: 52px;
  450. float: left;
  451. font-size: 15px;
  452. font-family: PingFang SC;
  453. color: #666666;
  454. line-height: 52px;
  455. }
  456. .cart-handle-delete {
  457. display: block;
  458. width: 80px;
  459. height: 32px;
  460. line-height: 32px;
  461. padding: 0;
  462. margin: 10px 0 0 0;
  463. float: right;
  464. border-color: #52A63A !important;
  465. background-color: #FFFFFF !important;
  466. color: #52A63A !important;
  467. ;
  468. }
  469. }
  470. }
  471. </style>