cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. min-height: calc(100vh - 40px - 50px);
  277. overflow: visible;
  278. position: absolute;
  279. top: 50px;
  280. bottom: 0;
  281. left: 15px;
  282. overflow-y: auto;
  283. .shop-row {
  284. width: 100%;
  285. float: left;
  286. background: #FFFFFF;
  287. border-radius: 10px;
  288. margin-bottom: 15px;
  289. .shop-info {
  290. width: 100%;
  291. height: 50px;
  292. float: left;
  293. display: flex;
  294. align-items: center;
  295. border-bottom: 1px solid #EEEEEE;
  296. .shop-name {
  297. font-size: 15px;
  298. font-family: PingFang SC;
  299. color: #333333;
  300. margin: 0 6px;
  301. }
  302. .iconshangjia {
  303. color: #333333;
  304. font-size: 26px;
  305. }
  306. .iconfangxiang {
  307. color: #999999;
  308. font-size: 20px;
  309. }
  310. }
  311. .goods-list {
  312. width: 100%;
  313. float: left;
  314. .goods-row {
  315. width: 100%;
  316. height: 120px;
  317. float: left;
  318. padding-top: 15px;
  319. border-bottom: 1px solid #EEEEEE;
  320. display: flex;
  321. .iconqueding,
  322. .iconfeigouxuan {
  323. margin-top: 26px;
  324. }
  325. .goods-img {
  326. width: 90px;
  327. height: 90px;
  328. margin: 0 15px 0 2px;
  329. border-radius: 5px;
  330. object-fit: cover;
  331. overflow: hidden;
  332. }
  333. .goods-info {
  334. height: 90px;
  335. flex: 1;
  336. .goods-name {
  337. width: 100%;
  338. height: 32px;
  339. float: left;
  340. font-size: 12px;
  341. font-family: PingFang SC;
  342. color: #333333;
  343. line-height: 16px;
  344. overflow: hidden;
  345. text-overflow: ellipsis;
  346. display: -webkit-box;
  347. -webkit-line-clamp: 2;
  348. -webkit-box-orient: vertical;
  349. word-wrap: break-word;
  350. margin-bottom: 8px;
  351. }
  352. .goods-type {
  353. float: left;
  354. height: 20px;
  355. padding: 0 8px;
  356. background: #F0F0F0;
  357. border-radius: 4px;
  358. color: #666666;
  359. font-size: 10px;
  360. line-height: 20px;
  361. margin-bottom: 10px;
  362. }
  363. .goods-handle {
  364. width: 100%;
  365. height: 20px;
  366. box-sizing: border-box;
  367. padding-right: 15px;
  368. float: left;
  369. display: flex;
  370. justify-content: space-between;
  371. align-items: center;
  372. line-height: 20px;
  373. .goods-price {
  374. flex: 1;
  375. font-size: 18px;
  376. font-family: PingFang SC;
  377. color: #52A63A;
  378. line-height: 20px;
  379. }
  380. .handle-box {
  381. flex: 1;
  382. line-height: 20px;
  383. text-align: right;
  384. .good-select {
  385. font-size: 16px;
  386. font-family: PingFang SC;
  387. color: #333333;
  388. margin: 0 10px;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. .shop-handle-box {
  396. width: 100%;
  397. height: 50px;
  398. float: left;
  399. box-sizing: border-box;
  400. padding: 0 16px;
  401. .shop-handle-text {
  402. width: calc(100% - 130px);
  403. height: 50px;
  404. float: left;
  405. white-space: nowrap;
  406. font-size: 15px;
  407. font-family: PingFang SC;
  408. line-height: 50px;
  409. text-align: right;
  410. .shop-text {
  411. color: #333333;
  412. margin-right: 16px;
  413. }
  414. .shop-price {
  415. color: #52A63A;
  416. }
  417. }
  418. .handle-button {
  419. width: 110px;
  420. display: block;
  421. float: right;
  422. // background-color: #52A63A;
  423. margin-top: 7px;
  424. margin-right: 15px;
  425. padding: 0;
  426. text-align: center;
  427. }
  428. }
  429. }
  430. }
  431. .cart-handle-box {
  432. width: 100%;
  433. height: 52px;
  434. position: absolute;
  435. z-index: 1;
  436. bottom: 0;
  437. background: #FFFFFF;
  438. box-sizing: border-box;
  439. border-top: 1px solid #EEEEEE;
  440. padding: 0 15px 0 15px;
  441. .iconfont {
  442. height: 52px;
  443. line-height: 52px;
  444. float: left;
  445. display: block;
  446. }
  447. .icon-text {
  448. height: 52px;
  449. float: left;
  450. font-size: 15px;
  451. font-family: PingFang SC;
  452. color: #666666;
  453. line-height: 52px;
  454. }
  455. .cart-handle-delete {
  456. display: block;
  457. width: 80px;
  458. height: 32px;
  459. line-height: 32px;
  460. padding: 0;
  461. margin: 10px 0 0 0;
  462. float: right;
  463. border-color: #52A63A !important;
  464. background-color: #FFFFFF !important;
  465. color: #52A63A !important;
  466. ;
  467. }
  468. }
  469. }
  470. </style>