index.vue 13 KB

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