cart.vue 12 KB

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