index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="container" :style="appThemeStyle">
  3. <!-- 页面顶部 -->
  4. <view v-if="list.length" class="head-info">
  5. <view class="cart-total">
  6. <text>共</text>
  7. <text class="active">{{ total }}</text>
  8. <text>件商品</text>
  9. </view>
  10. <view class="cart-edit" @click="handleToggleMode()">
  11. <view v-if="mode == 'normal'" class="normal">
  12. <text class="icon iconfont icon-bianji"></text>
  13. <text>编辑</text>
  14. </view>
  15. <view v-if="mode == 'edit'" class="edit">
  16. <text>完成</text>
  17. </view>
  18. </view>
  19. </view>
  20. <!-- 购物车商品列表 -->
  21. <view v-if="list.length" class="cart-list">
  22. <view class="cart-item" v-for="(item, index) in list" :key="index">
  23. <view class="item-radio" @click="handleCheckItem(item.id)">
  24. <u-checkbox :modelValue="inArray(item.id, checkedIds)" shape="circle" :activeColor="appTheme.mainBg" />
  25. </view>
  26. <view class="goods-image" @click="onTargetGoods(item.goods_id)">
  27. <image class="image" :src="item.goods.goods_image" mode="scaleToFill"></image>
  28. </view>
  29. <view class="item-content">
  30. <view class="goods-title" @click="onTargetGoods(item.goods_id)">
  31. <text class="twoline-hide">{{ item.goods.goods_name }}</text>
  32. </view>
  33. <view class="goods-props clearfix">
  34. <view class="goods-props-item" v-for="(props, idx) in item.goods.skuInfo.goods_props" :key="idx">
  35. <text>{{ props.value.name }}</text>
  36. </view>
  37. </view>
  38. <view class="item-foot">
  39. <view class="goods-price">
  40. <text class="unit">¥</text>
  41. <text class="value">{{ item.goods.skuInfo.goods_price }}</text>
  42. </view>
  43. <view class="stepper">
  44. <u-number-box :min="1" :modelValue="item.goods_num" :step="1" @change="onChangeStepper($event, item)" />
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 购物车数据为空 -->
  51. <empty v-if="!list.length" :isLoading="isLoading" :custom-style="{ padding: '180rpx 50rpx' }" tips="您的购物车是空的, 快去逛逛吧">
  52. <template v-slot:slot>
  53. <view class="empty-ipt" @click="onTargetIndex()">
  54. <text>去逛逛</text>
  55. </view>
  56. </template>
  57. </empty>
  58. <!-- 底部操作栏 -->
  59. <view v-if="list.length" class="footer-fixed">
  60. <view class="all-radio">
  61. <u-checkbox :modelValue="checkedIds.length > 0 && checkedIds.length === list.length" shape="circle" :activeColor="appTheme.mainBg"
  62. @change="handleCheckAll()">全选</u-checkbox>
  63. </view>
  64. <view class="total-info">
  65. <text>合计:</text>
  66. <view class="goods-price">
  67. <text class="unit">¥</text>
  68. <text class="value">{{ totalPrice }}</text>
  69. </view>
  70. </view>
  71. <view class="cart-action">
  72. <view class="btn-wrapper">
  73. <!-- dev:下面的disabled条件使用checkedIds.join方式判断 -->
  74. <!-- dev:通常情况下vue项目使用checkedIds.length更合理, 但是length属性在微信小程序中不起作用 -->
  75. <view v-if="mode == 'normal'" class="btn-item btn-main" :class="{ disabled: checkedIds.join() == '' }" @click="handleOrder()">
  76. <text>去结算</text>
  77. <!-- <text>去结算 {{ checkedIds.length > 0 ? `(${total})` : '' }}</text> -->
  78. </view>
  79. <view v-if="mode == 'edit'" class="btn-item btn-main" :class="{ disabled: !checkedIds.length }" @click="handleDelete()">
  80. <text>删除</text>
  81. </view>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </template>
  87. <script>
  88. import Empty from '@/components/empty'
  89. import { inArray, arrayIntersect, debounce } from '@/utils/util'
  90. import { checkLogin, setCartTotalNum, setCartTabBadge } from '@/core/app'
  91. import * as CartApi from '@/api/cart'
  92. const CartIdsIndex = 'CartIds'
  93. export default {
  94. components: {
  95. Empty
  96. },
  97. data() {
  98. return {
  99. inArray,
  100. // 正在加载
  101. isLoading: true,
  102. // 当前模式: normal正常 edit编辑
  103. mode: 'normal',
  104. // 购物车商品列表
  105. list: [],
  106. // 购物车商品总数量
  107. total: null,
  108. // 选中的商品ID记录
  109. checkedIds: [],
  110. // 选中的商品总金额
  111. totalPrice: '0.00'
  112. }
  113. },
  114. watch: {
  115. // 监听选中的商品
  116. checkedIds: {
  117. handler(val) {
  118. // 计算合计金额
  119. this.onCalcTotalPrice()
  120. // 记录到缓存中
  121. uni.setStorageSync(CartIdsIndex, val)
  122. },
  123. deep: true,
  124. immediate: false
  125. },
  126. // 监听购物车商品总数量
  127. total(val) {
  128. // 缓存并设置角标
  129. setCartTotalNum(val)
  130. setCartTabBadge()
  131. }
  132. },
  133. /**
  134. * 生命周期函数--监听页面显示
  135. */
  136. onShow() {
  137. // 获取缓存中的选中记录
  138. this.checkedIds = uni.getStorageSync(CartIdsIndex)
  139. // 获取购物车商品列表
  140. checkLogin() ? this.getCartList() : this.isLoading = false
  141. },
  142. methods: {
  143. // 计算合计金额 (根据选中的商品)
  144. onCalcTotalPrice() {
  145. const app = this
  146. // 选中的商品记录
  147. const checkedList = app.list.filter(item => inArray(item.id, app.checkedIds))
  148. // 计算总金额
  149. let tempPrice = 0;
  150. checkedList.forEach(item => {
  151. // 商品单价, 为了方便计算先转换单位为分 (整数)
  152. const unitPrice = item.goods.skuInfo.goods_price * 100
  153. tempPrice += unitPrice * item.goods_num
  154. })
  155. app.totalPrice = (tempPrice / 100).toFixed(2)
  156. },
  157. // 获取购物车商品列表
  158. getCartList() {
  159. const app = this
  160. app.isLoading = true
  161. CartApi.list()
  162. .then(result => {
  163. app.list = result.data.list
  164. app.total = result.data.cartTotal
  165. // 清除checkedIds中无效的ID
  166. app.onClearInvalidId()
  167. })
  168. .finally(() => app.isLoading = false)
  169. },
  170. // 清除checkedIds中无效的ID
  171. onClearInvalidId() {
  172. const app = this
  173. const listIds = app.list.map(item => item.id)
  174. app.checkedIds = arrayIntersect(listIds, app.checkedIds)
  175. },
  176. // 切换当前模式
  177. handleToggleMode() {
  178. this.mode = this.mode == 'normal' ? 'edit' : 'normal'
  179. },
  180. // 监听步进器更改事件
  181. onChangeStepper({ value }, item) {
  182. // 这里是组织首次启动时的执行
  183. if (item.goods_num == value) return
  184. // 记录一个节流函数句柄
  185. if (!item.debounceHandle) {
  186. item.oldValue = item.goods_num
  187. item.debounceHandle = debounce(this.onUpdateCartNum, 500)
  188. }
  189. // 更新商品数量
  190. item.goods_num = value
  191. // 提交更新购物车数量 (节流)
  192. item.debounceHandle(item, item.oldValue, value)
  193. },
  194. // 提交更新购物车数量
  195. onUpdateCartNum(item, oldValue, newValue) {
  196. const app = this
  197. CartApi.update(item.goods_id, item.goods_sku_id, newValue)
  198. .then(result => {
  199. // 更新商品数量
  200. app.total = result.data.cartTotal
  201. // 重新计算合计金额
  202. app.onCalcTotalPrice()
  203. // 清除节流函数句柄
  204. item.debounceHandle = null
  205. })
  206. .catch(err => {
  207. // 还原商品数量
  208. item.goods_num = oldValue
  209. setTimeout(() => app.$toast(err.errMsg), 10)
  210. })
  211. },
  212. // 跳转到商品详情页
  213. onTargetGoods(goodsId) {
  214. this.$navTo('pages/goods/detail', { goodsId })
  215. },
  216. // 点击去逛逛按钮, 跳转到首页
  217. onTargetIndex() {
  218. this.$navTo('pages/index/index')
  219. },
  220. // 选中商品
  221. handleCheckItem(cartId) {
  222. const { checkedIds } = this
  223. const index = checkedIds.findIndex(id => id === cartId)
  224. index < 0 ? checkedIds.push(cartId) : checkedIds.splice(index, 1)
  225. },
  226. // 全选事件
  227. handleCheckAll() {
  228. const { checkedIds, list } = this
  229. this.checkedIds = checkedIds.length === list.length ? [] : list.map(item => item.id)
  230. },
  231. // 结算选中的商品
  232. handleOrder() {
  233. const app = this
  234. if (app.checkedIds.length) {
  235. const cartIds = app.checkedIds.join()
  236. app.$navTo('pages/checkout/index', { mode: 'cart', cartIds })
  237. }
  238. },
  239. // 删除选中的商品弹窗事件
  240. handleDelete() {
  241. const app = this
  242. if (!app.checkedIds.length) {
  243. return false
  244. }
  245. uni.showModal({
  246. title: '友情提示',
  247. content: '您确定要删除该商品吗?',
  248. showCancel: true,
  249. success({ confirm }) {
  250. // 确认删除
  251. confirm && app.onClearCart()
  252. }
  253. })
  254. },
  255. // 确认删除商品
  256. onClearCart() {
  257. const app = this
  258. CartApi.clear(app.checkedIds)
  259. .then(result => {
  260. app.getCartList()
  261. app.handleToggleMode()
  262. })
  263. }
  264. }
  265. }
  266. </script>
  267. <style>
  268. page {
  269. background: #f5f5f5;
  270. }
  271. </style>
  272. <style lang="scss" scoped>
  273. .container {
  274. padding-bottom: 120rpx;
  275. }
  276. // 页面顶部
  277. .head-info {
  278. display: flex;
  279. justify-content: space-between;
  280. align-items: center;
  281. padding: 4rpx 30rpx;
  282. // background-color: #fff;
  283. height: 80rpx;
  284. .cart-total {
  285. font-size: 28rpx;
  286. color: #333;
  287. .active {
  288. color: $main-bg;
  289. margin: 0 2rpx;
  290. }
  291. }
  292. .cart-edit {
  293. padding-left: 20rpx;
  294. .icon {
  295. margin-right: 12rpx;
  296. }
  297. .edit {
  298. color: $main-bg;
  299. }
  300. }
  301. }
  302. // 购物车列表
  303. .cart-list {
  304. padding: 0 16rpx 0 16rpx;
  305. }
  306. .cart-item {
  307. background: #fff;
  308. border-radius: 12rpx;
  309. display: flex;
  310. align-items: center;
  311. padding: 30rpx 16rpx;
  312. margin-bottom: 24rpx;
  313. .item-radio {
  314. width: 56rpx;
  315. height: 80rpx;
  316. margin-right: 10rpx;
  317. display: flex;
  318. justify-content: center;
  319. align-items: center;
  320. }
  321. .goods-image {
  322. width: 200rpx;
  323. height: 200rpx;
  324. .image {
  325. display: block;
  326. width: 100%;
  327. height: 100%;
  328. border-radius: 8rpx;
  329. }
  330. }
  331. .item-content {
  332. flex: 1;
  333. padding-left: 24rpx;
  334. .goods-title {
  335. font-size: 28rpx;
  336. max-height: 76rpx;
  337. }
  338. .goods-props {
  339. margin-top: 14rpx;
  340. color: #ababab;
  341. font-size: 24rpx;
  342. overflow: hidden;
  343. .goods-props-item {
  344. padding: 4rpx 16rpx;
  345. border-radius: 12rpx;
  346. background-color: #fcfcfc;
  347. }
  348. }
  349. .item-foot {
  350. display: flex;
  351. justify-content: space-between;
  352. align-items: center;
  353. margin-top: 20rpx;
  354. .goods-price {
  355. vertical-align: bottom;
  356. color: $main-bg;
  357. .unit {
  358. font-size: 24rpx;
  359. }
  360. .value {
  361. font-size: 32rpx;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. // 空数据按钮
  368. .empty-ipt {
  369. margin: 0 auto;
  370. width: 250rpx;
  371. height: 70rpx;
  372. font-size: 32rpx;
  373. text-align: center;
  374. color: #fff;
  375. border-radius: 50rpx;
  376. background: linear-gradient(to right, $main-bg, $main-bg2);
  377. color: $main-text;
  378. display: flex;
  379. justify-content: center;
  380. align-items: center;
  381. }
  382. // 底部操作栏
  383. .footer-fixed {
  384. display: flex;
  385. align-items: center;
  386. height: 96rpx;
  387. background: #fff;
  388. padding: 0 30rpx;
  389. position: fixed;
  390. bottom: var(--window-bottom);
  391. left: var(--window-left);
  392. right: var(--window-right);
  393. z-index: 11;
  394. .all-radio {
  395. width: 140rpx;
  396. display: flex;
  397. align-items: center;
  398. .radio {
  399. margin-bottom: -4rpx;
  400. transform: scale(0.76)
  401. }
  402. }
  403. .total-info {
  404. flex: 1;
  405. display: flex;
  406. align-items: center;
  407. justify-content: flex-end;
  408. padding-right: 30rpx;
  409. .goods-price {
  410. vertical-align: bottom;
  411. color: $main-bg;
  412. .unit {
  413. font-size: 24rpx;
  414. }
  415. .value {
  416. font-size: 32rpx;
  417. }
  418. }
  419. }
  420. .cart-action {
  421. width: 200rpx;
  422. .btn-wrapper {
  423. height: 100%;
  424. display: flex;
  425. align-items: center;
  426. }
  427. .btn-item {
  428. flex: 1;
  429. font-size: 28rpx;
  430. height: 72rpx;
  431. color: #fff;
  432. border-radius: 50rpx;
  433. display: flex;
  434. justify-content: center;
  435. align-items: center;
  436. }
  437. // 去结算按钮
  438. .btn-main {
  439. background: linear-gradient(to right, $main-bg, $main-bg2);
  440. color: $main-text;
  441. // 禁用按钮
  442. &.disabled {
  443. opacity: 0.6;
  444. }
  445. }
  446. }
  447. }
  448. </style>