index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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">
  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" @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" :value="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. <view slot="slot" class="empty-ipt" @click="onTargetIndex">
  53. <text>去逛逛</text>
  54. </view>
  55. </empty>
  56. <!-- 底部操作栏 -->
  57. <view v-if="list.length" class="footer-fixed">
  58. <label class="all-radio" @click="handleCheckAll">
  59. <radio class="radio" color="#fa2209" :checked="checkedIds.length > 0 && checkedIds.length === list.length" />
  60. <text>全选</text>
  61. </label>
  62. <view class="total-info">
  63. <text>合计:</text>
  64. <view class="goods-price">
  65. <text class="unit">¥</text>
  66. <text class="value">{{ totalPrice }}</text>
  67. </view>
  68. </view>
  69. <view class="cart-action">
  70. <view class="btn-wrapper">
  71. <!-- dev:下面的disabled条件使用checkedIds.join方式判断 -->
  72. <!-- dev:通常情况下vue项目使用checkedIds.length更合理, 但是length属性在微信小程序中不起作用 -->
  73. <view v-if="mode == 'normal'" class="btn-item btn-main" :class="{ disabled: checkedIds.join() == '' }" @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 }" @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 '@/core/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 => {
  256. app.getCartList()
  257. app.handleToggleMode()
  258. })
  259. }
  260. }
  261. }
  262. </script>
  263. <style>
  264. page {
  265. background: #f5f5f5;
  266. }
  267. </style>
  268. <style lang="scss" scoped>
  269. // 页面顶部
  270. .head-info {
  271. display: flex;
  272. justify-content: space-between;
  273. align-items: center;
  274. padding: 4rpx 30rpx;
  275. // background-color: #fff;
  276. height: 80rpx;
  277. .cart-total {
  278. font-size: 28rpx;
  279. color: #333;
  280. .active {
  281. color: #FA2209;
  282. margin: 0 2rpx;
  283. }
  284. }
  285. .cart-edit {
  286. padding-left: 20rpx;
  287. .icon {
  288. margin-right: 12rpx;
  289. }
  290. .edit {
  291. color: #fa2209;
  292. }
  293. }
  294. }
  295. // 购物车列表
  296. .cart-list {
  297. padding: 0 16rpx 110rpx 16rpx;
  298. }
  299. .cart-item {
  300. background: #fff;
  301. border-radius: 12rpx;
  302. display: flex;
  303. align-items: center;
  304. padding: 30rpx 16rpx;
  305. margin-bottom: 24rpx;
  306. .item-radio {
  307. width: 56rpx;
  308. height: 80rpx;
  309. line-height: 80rpx;
  310. margin-right: 10rpx;
  311. text-align: center;
  312. .radio {
  313. transform: scale(0.76)
  314. }
  315. }
  316. .goods-image {
  317. width: 200rpx;
  318. height: 200rpx;
  319. .image {
  320. display: block;
  321. width: 100%;
  322. height: 100%;
  323. border-radius: 8rpx;
  324. }
  325. }
  326. .item-content {
  327. flex: 1;
  328. padding-left: 24rpx;
  329. .goods-title {
  330. font-size: 28rpx;
  331. max-height: 76rpx;
  332. }
  333. .goods-props {
  334. margin-top: 14rpx;
  335. height: 40rpx;
  336. color: #ababab;
  337. font-size: 24rpx;
  338. overflow: hidden;
  339. .goods-props-item {
  340. display: inline-block;
  341. margin-right: 14rpx;
  342. padding: 4rpx 16rpx;
  343. border-radius: 12rpx;
  344. background-color: #F5F5F5;
  345. width: auto;
  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: $uni-text-color-active;
  356. .unit {
  357. font-size: 24rpx;
  358. }
  359. .value {
  360. font-size: 32rpx;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. // 空数据按钮
  367. .empty-ipt {
  368. width: 220rpx;
  369. margin: 0 auto;
  370. font-size: 32rpx;
  371. height: 64rpx;
  372. line-height: 64rpx;
  373. text-align: center;
  374. color: #fff;
  375. border-radius: 50rpx;
  376. background: linear-gradient(to right, #f9211c, #ff6335);
  377. }
  378. // 底部操作栏
  379. .footer-fixed {
  380. display: flex;
  381. align-items: center;
  382. height: 96rpx;
  383. background: #fff;
  384. padding: 0 30rpx;
  385. position: fixed;
  386. bottom: var(--window-bottom);
  387. left: 0;
  388. right: 0;
  389. z-index: 11;
  390. .all-radio {
  391. width: 140rpx;
  392. display: flex;
  393. align-items: center;
  394. .radio {
  395. margin-bottom: -4rpx;
  396. transform: scale(0.76)
  397. }
  398. }
  399. .total-info {
  400. flex: 1;
  401. display: flex;
  402. align-items: center;
  403. justify-content: flex-end;
  404. padding-right: 30rpx;
  405. .goods-price {
  406. vertical-align: bottom;
  407. color: #fa2209;
  408. .unit {
  409. font-size: 24rpx;
  410. }
  411. .value {
  412. font-size: 32rpx;
  413. }
  414. }
  415. }
  416. .cart-action {
  417. width: 200rpx;
  418. .btn-wrapper {
  419. height: 100%;
  420. display: flex;
  421. align-items: center;
  422. }
  423. .btn-item {
  424. flex: 1;
  425. font-size: 28rpx;
  426. height: 72rpx;
  427. line-height: 72rpx;
  428. text-align: center;
  429. color: #fff;
  430. border-radius: 50rpx;
  431. }
  432. // 立即购买按钮
  433. .btn-main {
  434. background: linear-gradient(to right, #f9211c, #ff6335);
  435. // 禁用按钮
  436. &.disabled {
  437. background: #ff9779;
  438. }
  439. }
  440. }
  441. }
  442. </style>