cart.vue 12 KB

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