cart.vue 14 KB

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