cart.vue 13 KB

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