index.vue 12 KB

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