index.vue 13 KB

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