index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <template>
  2. <view class="cart">
  3. <template v-if="!isEmpty">
  4. <template v-if="listType !== 'delete'">
  5. <!-- 商品统计 / 批量删除商品 -->
  6. <view class="cart-top">
  7. <view class="count">共{{ kindCount }}件商品</view>
  8. <view class="btn" @click="showDelManager">删除</view>
  9. </view>
  10. <!-- TODO -->
  11. <view class="receive-coupon">
  12. <view class="tip-text">还差¥200可用“满200元减50元”优惠券</view>
  13. <view class="btn" @click="couponVisible = true">领券</view>
  14. </view>
  15. </template>
  16. <view>
  17. <!-- 购物车列表 -->
  18. <template v-for="(cart, index) in goodsList">
  19. <cm-cart-product
  20. class="product-list"
  21. :listType="listType"
  22. :key="index"
  23. :data="cart"
  24. vkey="productList"
  25. @chooseAll="chooseAll"
  26. @chooseOne="chooseOne"
  27. ></cm-cart-product>
  28. </template>
  29. <!-- 失效商品列表 -->
  30. <cm-cart-product
  31. class="product-list"
  32. :key="index"
  33. :data="failureList"
  34. listType="expired"
  35. ></cm-cart-product>
  36. </view>
  37. <!-- 底部按钮 -->
  38. <view class="footer">
  39. <view class="radio" @click="checkAll">
  40. <text class="iconfont" :class="[isCheckAll ? 'icon-xuanze' : 'icon-weixuanze']"></text>
  41. <text class="tip">全选</text>
  42. </view>
  43. <template v-if="!isshowDelbtn">
  44. <view class="center">
  45. <view class="row">
  46. <text>总价:</text> <text class="total-price">¥{{ allPrice | NumFormat }}</text>
  47. </view>
  48. <!-- TODO -->
  49. <view class="row">
  50. <text>共减</text> <text class="discounted-price">¥200.00</text>
  51. <text @click="showDiscountedDetail">优惠明细</text>
  52. <text
  53. class="iconfont"
  54. :class="showDitail ? 'tui-icon-arrowdown' : 'tui-icon-arrowup'"
  55. ></text>
  56. </view>
  57. </view>
  58. <view class="submit" @click="toConfirmation">去结算({{ allCount }})</view>
  59. </template>
  60. <view v-else class="footer-del">
  61. <view class="btn btn-cancel" @tap.stop="hideDelManage">取消</view>
  62. <view class="btn btn-confirm" @tap.stop="deleteList">删除</view>
  63. </view>
  64. </view>
  65. </template>
  66. <view class="empty" v-else>
  67. <cm-empty :image="StaticUrl + 'icon-empty-cart.png'" message="购物车空空的,快去逛逛吧~"></cm-empty>
  68. </view>
  69. <!-- 优惠券列表 TODO-->
  70. <cm-coupon-list
  71. title="优惠券"
  72. listType="search"
  73. :visible="couponVisible"
  74. @close="closeCouponList"
  75. ></cm-coupon-list>
  76. <!-- 优惠明细 -->
  77. <cm-drawer
  78. title="优惠明细"
  79. :visible="showDitail"
  80. position="bottom"
  81. :offset="100"
  82. @close="showDitail = false"
  83. zIndex="99"
  84. >
  85. <template>
  86. <view class="discounted-ditail">
  87. <view class="row"> <text>商品总额</text> <text>¥450.00</text> </view>
  88. <view class="row"> <text>促销满减</text> <text class="red">-¥180.00</text> </view>
  89. <view class="row"> <text>优惠券</text> <text class="red">-¥20.00</text> </view>
  90. <view class="row total"> <text>总计</text> <text>¥250.00</text> </view>
  91. <view class="tip"> 实际订单金额以结算页为准 </view>
  92. </view>
  93. </template>
  94. </cm-drawer>
  95. <!-- 操作弹窗 -->
  96. <tui-modal
  97. :show="modal"
  98. @click="handleClick"
  99. @cancel="hideModal"
  100. :content="contentModalText"
  101. color="#333"
  102. :size="32"
  103. shape="circle"
  104. :maskClosable="false"
  105. ></tui-modal>
  106. </view>
  107. </template>
  108. <script>
  109. import authorize from '@/common/authorize.js'
  110. import CmCouponList from '@/components/cm-module/cm-coupon-list/cm-coupon-list'
  111. import CmDrawer from '@/components/cm-module/cm-drawer/cm-drawer.vue'
  112. import CmCartProduct from '@/components/cm-module/cm-cart-product/cm-cart-product.vue'
  113. import CmEmpty from '@/components/cm-module/cm-empty/cm-empty.vue'
  114. import { mapGetters, mapActions, mapMutations } from 'vuex'
  115. export default {
  116. components: {
  117. CmCouponList,
  118. CmDrawer,
  119. CmCartProduct,
  120. CmEmpty
  121. },
  122. data() {
  123. return {
  124. StaticUrl: this.$Static,
  125. CustomBar: this.CustomBar, // 顶部导航栏高度
  126. popupShow: false,
  127. handlerPros: {}, //监听单挑促销商品
  128. isCheckAll: false, //是否全选
  129. allPrice: 0, //所有价格
  130. totalOriginalPrice: 0, //所有原价价
  131. reducedPrice: 0, //满减
  132. skeletonShow: true,
  133. isshowDelbtn: false,
  134. scrollHeight: 'auto',
  135. hasNextPage: false,
  136. modal: false,
  137. contentModalText: '',
  138. deleteType: 0,
  139. couponVisible: false,
  140. showDitail: false,
  141. listType: 'list'
  142. }
  143. },
  144. onLoad() {
  145. this.setScrollHeight()
  146. },
  147. computed: {
  148. ...mapGetters([
  149. 'hasLogin',
  150. 'isIphoneX',
  151. 'isEmpty',
  152. 'goodsList',
  153. 'failureList',
  154. 'kindCount',
  155. 'userId',
  156. 'cartIds'
  157. ]),
  158. //被选中的产品数量
  159. allCount() {
  160. return this.cartIds.length
  161. }
  162. },
  163. filters: {
  164. NumFormat(value) {
  165. //处理金额
  166. return Number(value).toFixed(2)
  167. },
  168. totalprice(val, count) {
  169. //单件商品的价格 × 数量
  170. return (val * count).toFixed(2)
  171. }
  172. },
  173. watch: {
  174. //深度监听所有数据,每次改变重新计算总价和总数
  175. goodsList: {
  176. deep: true,
  177. handler(val, oldval) {
  178. this.totalPeice()
  179. }
  180. }
  181. },
  182. //下拉刷新
  183. onPullDownRefresh() {
  184. this.initCart().finally(() => {
  185. setTimeout(() => {
  186. uni.stopPullDownRefresh()
  187. }, 2000)
  188. })
  189. },
  190. onShow() {
  191. if (this.hasLogin) {
  192. this.initData()
  193. } else {
  194. this.$api.redirectTo('/pages/login/login')
  195. }
  196. },
  197. onHide() {
  198. this.saveCartIds([])
  199. },
  200. methods: {
  201. ...mapActions('cart', ['initCart', 'updateShoppogCount', 'removeFromCart', 'removeFailureFromCart']),
  202. ...mapMutations('cart', [
  203. 'selectProduct',
  204. 'selectAllShopProduct',
  205. 'selectAllProduct',
  206. 'saveCartIds',
  207. 'selectFailure',
  208. 'selectAllFailure'
  209. ]),
  210. initData() {
  211. this.initCart().finally(() => {
  212. this.skeletonShow = false
  213. })
  214. },
  215. // 关闭优惠券弹窗
  216. closeCouponList() {
  217. this.couponVisible = false
  218. },
  219. // 活动价弹窗
  220. clickPopupShow(pros, type) {
  221. if (pros.ladderList.length > 0) {
  222. this.popupShow = true
  223. this.handlerPros = pros
  224. }
  225. },
  226. // 优惠明细 TODO
  227. showDiscountedDetail() {
  228. this.showDitail = true
  229. },
  230. // 勾选单个失效商品
  231. ischeckFailure(failure) {
  232. this.selectFailure({
  233. productId: failure.productId,
  234. checked: !failure.productsChecked
  235. })
  236. this.getCheckedProductId()
  237. },
  238. // 勾选供应商下所有商品
  239. chooseAll() {
  240. this.isSelectAll()
  241. },
  242. // 勾选单核商品
  243. chooseOne() {
  244. this.isSelectAll()
  245. },
  246. // 勾选全部商品
  247. checkAll() {
  248. this.isCheckAll = !this.isCheckAll
  249. this.selectAllProduct(this.isCheckAll)
  250. // 删除商品的全选也要选中失效商品
  251. if (this.isshowDelbtn) {
  252. this.selectAllFailure(this.isCheckAll)
  253. }
  254. this.isSelectAll()
  255. },
  256. // 判断是否全选商品
  257. isSelectAll() {
  258. this.isCheckAll = this.goodsList.every(shop => shop.checked)
  259. this.getCheckedProductId()
  260. },
  261. // 获取勾选商品的id
  262. getCheckedProductId() {
  263. const cartIds = []
  264. this.goodsList.forEach(shop => {
  265. const ids = shop.productList.reduce((cartIds, prod) => {
  266. if (prod.productsChecked) cartIds.push(prod.cartId)
  267. return cartIds
  268. }, [])
  269. cartIds.push(...ids)
  270. })
  271. if (this.isshowDelbtn) {
  272. const ids = this.failureList.reduce((cartIds, prod) => {
  273. if (prod.productsChecked) cartIds.push(prod.cartId)
  274. return cartIds
  275. }, [])
  276. cartIds.push(...ids)
  277. }
  278. console.log(cartIds)
  279. this.saveCartIds(cartIds)
  280. },
  281. //计算总价格,每次调用此方法,将初始值为0,遍历价格并累加
  282. totalPeice() {
  283. const priceObj = this.goodsList.reduce(
  284. (priceObj, shop) => {
  285. shop.productList.forEach(prod => {
  286. if (prod.productsChecked) {
  287. priceObj.totalOriginalPrice += prod.price * prod.productCount
  288. // 单品满减
  289. if (prod.promotion && prod.promotion.type * 1 === 1 && prod.promotion.mode * 1 === 2) {
  290. // 单品满减-重新计算供应商总价/满减金额
  291. if (prod.price * prod.productCount >= prod.promotion.touchPrice) {
  292. priceObj.reducedPrice += prod.promotion.reducedPrice
  293. }
  294. }
  295. }
  296. })
  297. priceObj.allPrice = priceObj.totalOriginalPrice - priceObj.reducedPrice
  298. return priceObj
  299. },
  300. {
  301. totalOriginalPrice: 0,
  302. reducedPrice: 0,
  303. allPrice: 0
  304. }
  305. )
  306. this.allPrice = priceObj.allPrice
  307. },
  308. //商品数量加加
  309. changeCountAdd(item, pros) {
  310. const productCount = pros.productCount + 1
  311. this.updateShoppogCount({
  312. cartId: pros.cartId,
  313. productCount
  314. })
  315. },
  316. //商品数量减减
  317. changeCountSub(item, pros) {
  318. let productCount = pros.productCount
  319. if (productCount <= 1) {
  320. productCount = 1
  321. this.$util.msg('购买数量不能少于1', 2000)
  322. return
  323. } else {
  324. productCount--
  325. }
  326. this.updateShoppogCount({
  327. cartId: pros.cartId,
  328. productCount
  329. })
  330. },
  331. //输入商品数量更新
  332. changeInput(pros) {},
  333. //输入商品数量更新
  334. changeNumber(e, item, pros) {
  335. let _value = Math.abs(Number(e.detail.value))
  336. let productCount = pros.productCount
  337. if (_value <= 1) {
  338. this.$util.msg('购买数量不能少于1', 2000)
  339. productCount = 1
  340. } else {
  341. productCount = _value
  342. }
  343. this.updateShoppogCount({
  344. cartId: pros.cartId,
  345. productCount
  346. })
  347. },
  348. //显示删除商品管理
  349. showDelManager() {
  350. this.isshowDelbtn = true
  351. this.listType = 'delete'
  352. },
  353. //隐藏删除商品管理
  354. hideDelManage() {
  355. this.selectAllFailure(false)
  356. this.getCheckedProductId()
  357. this.isshowDelbtn = false
  358. this.listType = 'list'
  359. },
  360. //删除购物车商品
  361. deleteList() {
  362. if (this.cartIds.length <= 0) {
  363. return this.$util.msg('请选择要删除的商品~', 2000)
  364. } else {
  365. this.modal = true
  366. this.contentModalText = '确定删除选中的商品吗?'
  367. this.deleteType = 1
  368. }
  369. },
  370. // 清空失效商品
  371. deletefailureList() {
  372. this.modal = true
  373. this.contentModalText = '确定清除所有失效商品吗?'
  374. this.deleteType = 2
  375. },
  376. //从购物车移除商品
  377. handleClick(e) {
  378. if (e.index !== 1) return (this.modal = false)
  379. if (this.deleteType === 1) {
  380. // 删除所有勾选的商品
  381. this.removeFromCart().finally(() => {
  382. this.isshowDelbtn = false
  383. })
  384. } else {
  385. //一键删除失效商品
  386. this.removeFailureFromCart().finally(() => {
  387. this.isshowDelbtn = false
  388. this.listType = 'list'
  389. })
  390. }
  391. this.modal = false
  392. },
  393. //跳转确认订单页面
  394. toConfirmation() {
  395. if (this.cartIds.length <= 0) {
  396. return this.$util.msg('请先选择结算商品~', 2000)
  397. }
  398. let cartPramsData = {
  399. allPrice: this.allPrice,
  400. allCount: this.allCount,
  401. cartIds: this.cartIds.join(','),
  402. productCount: ''
  403. }
  404. this.$api.navigateTo(`/pages/user/order/create-order?data=${JSON.stringify({ data: cartPramsData })}`)
  405. },
  406. hideModal() {
  407. this.modal = false
  408. },
  409. navToListPage(item) {
  410. this.$api.navigateTo(`/pages/goods/product?productId=${item.productId}`)
  411. },
  412. // 窗口高度-footer高度
  413. setScrollHeight() {
  414. const { windowHeight, pixelRatio } = uni.getSystemInfoSync()
  415. setTimeout(() => {
  416. const query = uni.createSelectorQuery().in(this)
  417. query.selectAll('.footer').boundingClientRect()
  418. query.exec(res => {
  419. this.windowHeight = windowHeight
  420. if (res[0][0]) {
  421. this.scrollHeight = windowHeight - res[0][0].height
  422. }
  423. })
  424. }, 500)
  425. }
  426. }
  427. }
  428. </script>
  429. <style lang="scss" scoped>
  430. .cart {
  431. padding-bottom: 124rpx;
  432. background: #f7f7f7;
  433. min-height: 100%;
  434. box-sizing: border-box;
  435. .cart-top {
  436. display: flex;
  437. justify-content: space-between;
  438. align-items: center;
  439. padding: 0 24rpx;
  440. width: 750rpx;
  441. height: 80rpx;
  442. background: #f7f7f7;
  443. box-sizing: border-box;
  444. .count {
  445. font-size: 30rpx;
  446. color: #333333;
  447. }
  448. .btn {
  449. display: flex;
  450. justify-content: center;
  451. align-items: center;
  452. width: 88rpx;
  453. height: 42rpx;
  454. background: #fff8fd;
  455. border: 1rpx solid #ff457b;
  456. border-radius: 24rpx;
  457. font-size: 26rpx;
  458. color: #ff457b;
  459. }
  460. }
  461. .receive-coupon {
  462. display: flex;
  463. justify-content: space-between;
  464. align-items: center;
  465. padding: 24rpx;
  466. background: #fff;
  467. .tip-text {
  468. font-size: 26rpx;
  469. color: #ff457b;
  470. }
  471. .btn {
  472. width: 88rpx;
  473. height: 42rpx;
  474. background: linear-gradient(270deg, #f83c6c 0%, #fc32b4 100%);
  475. border-radius: 28rpx;
  476. font-size: 26rpx;
  477. color: #ffffff;
  478. text-align: center;
  479. line-height: 42rpx;
  480. }
  481. }
  482. .footer {
  483. position: fixed;
  484. bottom: 0;
  485. left: 0;
  486. z-index: 899;
  487. display: flex;
  488. justify-content: space-between;
  489. align-items: center;
  490. width: 100%;
  491. height: 100rpx;
  492. box-sizing: border-box;
  493. padding: 0 24rpx;
  494. border-top: 1rpx solid #eee;
  495. background: #ffffff;
  496. .radio {
  497. font-size: 36rpx;
  498. color: #b2b2b2;
  499. .icon-xuanze {
  500. color: #f83c6c;
  501. }
  502. .tip {
  503. font-size: 30rpx;
  504. color: #333333;
  505. margin-left: 6rpx;
  506. }
  507. }
  508. .row {
  509. &:nth-child(1) {
  510. font-size: 26rpx;
  511. color: #333333;
  512. .total-price {
  513. color: #ff457b;
  514. }
  515. }
  516. &:nth-child(2) {
  517. margin-top: 6rpx;
  518. font-size: 24rpx;
  519. color: #ff457b;
  520. }
  521. .discounted-price {
  522. margin-right: 32rpx;
  523. }
  524. }
  525. .submit {
  526. width: 210rpx;
  527. height: 80rpx;
  528. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  529. border-radius: 40rpx;
  530. font-size: 30rpx;
  531. color: #ffffff;
  532. text-align: center;
  533. line-height: 80rpx;
  534. }
  535. .footer-del {
  536. width: 420rpx;
  537. height: 100rpx;
  538. position: absolute;
  539. padding-left: 200rpx;
  540. background: #ffffff;
  541. right: 0;
  542. top: 0;
  543. z-index: 1000;
  544. box-sizing: border-box;
  545. padding: 10rpx 0;
  546. display: flex;
  547. &.show {
  548. animation: showDelbtn 0s linear both;
  549. }
  550. &.none {
  551. animation: hideDelbtn 0s linear both;
  552. }
  553. .btn {
  554. flex: 1;
  555. margin: 0 8rpx;
  556. height: 100%;
  557. line-height: 80rpx;
  558. font-size: $font-size-28;
  559. color: #ffffff;
  560. text-align: center;
  561. float: left;
  562. border-radius: 40rpx;
  563. }
  564. .btn.btn-cancel {
  565. background: #f7f7f7;
  566. color: #b2b2b2;
  567. }
  568. .btn.btn-confirm {
  569. background: $btn-confirm;
  570. color: #ffffff;
  571. }
  572. @keyframes showDelbtn {
  573. 0% {
  574. transform: translateX(0);
  575. }
  576. 100% {
  577. transform: translateX(-100%);
  578. }
  579. }
  580. @keyframes hideDelbtn {
  581. 0% {
  582. transform: translateX(-100%);
  583. }
  584. 100% {
  585. transform: translateX(0);
  586. }
  587. }
  588. }
  589. }
  590. .discounted-ditail {
  591. padding: 52rpx 8rpx 0;
  592. .row {
  593. display: flex;
  594. justify-content: space-between;
  595. align-items: center;
  596. padding: 12rpx 0;
  597. text {
  598. font-size: 30rpx;
  599. color: #333333;
  600. &.red {
  601. color: #f94b4b;
  602. }
  603. }
  604. &.total {
  605. font-size: 30rpx;
  606. font-weight: 600;
  607. }
  608. }
  609. .tip {
  610. padding: 12rpx 0;
  611. font-size: 26rpx;
  612. color: #999999;
  613. }
  614. }
  615. .empty{
  616. height: 80vh;
  617. }
  618. }
  619. </style>