cart.vue 12 KB

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