cart.vue 12 KB

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