index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.goodsId)">
  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.goodsInfo.logo"></image>
  28. </view>
  29. <view class="item-content">
  30. <view class="goods-title twolist-hidden"><text>{{ item.goodsInfo.name }}</text></view>
  31. <view class="goods-props clearfix">
  32. <view class="goods-props-item" v-for="(spec, idx) in item.specList" :key="idx">
  33. <text>{{ spec.specName }}{{ spec.specValue }}</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.goodsInfo.price }}</text>
  40. </view>
  41. <view class="stepper">
  42. <u-number-box :min="1" :value="item.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: 0,
  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.goodsInfo.price * 100
  149. tempPrice += unitPrice * item.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.totalNum;
  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.num == value) return
  180. // 记录一个节流函数句柄
  181. if (!item.debounceHandle) {
  182. item.oldValue = item.num
  183. item.debounceHandle = debounce(this.onUpdateCartNum, 500)
  184. }
  185. // 更新商品数量
  186. item.num = value
  187. // 提交更新购物车数量 (节流)
  188. item.debounceHandle(item, item.oldValue, value)
  189. },
  190. // 提交更新购物车数量
  191. onUpdateCartNum(item, oldValue, newValue) {
  192. const app = this
  193. CartApi.save(item.goodsId, "=", item.skuId, 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.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/settlement/goods', { 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. */
  260. onPullDownRefresh() {
  261. setTimeout(() => {
  262. this.getCartList();
  263. uni.stopPullDownRefresh();
  264. }, 1000)
  265. }
  266. }
  267. }
  268. </script>
  269. <style>
  270. page {
  271. background: #f5f5f5;
  272. }
  273. </style>
  274. <style lang="scss" scoped>
  275. // 页面顶部
  276. .head-info {
  277. display: flex;
  278. justify-content: space-between;
  279. align-items: center;
  280. padding: 4rpx 30rpx;
  281. height: 80rpx;
  282. .cart-total {
  283. font-size: 35rpx;
  284. color: #333;
  285. .active {
  286. color: #f03c3c;
  287. margin: 0 2rpx;
  288. }
  289. }
  290. .cart-edit {
  291. padding-left: 20rpx;
  292. .icon {
  293. margin-right: 12rpx;
  294. }
  295. .edit {
  296. color: #f03c3c;
  297. font-weight: bold;
  298. }
  299. }
  300. }
  301. // 购物车列表
  302. .cart-list {
  303. padding: 0 16rpx 180rpx 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. line-height: 80rpx;
  316. margin-right: 10rpx;
  317. text-align: center;
  318. .radio {
  319. transform: scale(0.76)
  320. }
  321. }
  322. .goods-image {
  323. width: 180rpx;
  324. .image {
  325. display: block;
  326. width: 100%;
  327. height: 160rpx;
  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. height: 40rpx;
  341. color: #ababab;
  342. font-size: 24rpx;
  343. overflow: hidden;
  344. .goods-props-item {
  345. display: inline-block;
  346. margin-right: 14rpx;
  347. padding: 4rpx 16rpx;
  348. border-radius: 12rpx;
  349. background-color: #F5F5F5;
  350. width: auto;
  351. }
  352. }
  353. .item-foot {
  354. display: flex;
  355. justify-content: space-between;
  356. align-items: center;
  357. margin-top: 20rpx;
  358. .goods-price {
  359. vertical-align: bottom;
  360. color: $uni-text-color-active;
  361. .unit {
  362. font-size: 24rpx;
  363. }
  364. .value {
  365. font-size: 32rpx;
  366. color: #f03c3c;
  367. font-weight: bold;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. // 空数据按钮
  374. .empty-ipt {
  375. width: 150rpx;
  376. margin: 0 auto;
  377. font-size: 28rpx;
  378. height: 68rpx;
  379. line-height: 68rpx;
  380. text-align: center;
  381. color: #fff;
  382. border-radius: 5rpx;
  383. background: linear-gradient(to right, $fuint-theme, $fuint-theme);
  384. }
  385. // 底部操作栏
  386. .footer-fixed {
  387. display: flex;
  388. align-items: center;
  389. height: 180rpx;
  390. background: #fff;
  391. padding: 0rpx 0rpx 40rpx 30rpx;
  392. position: fixed;
  393. bottom: var(--window-bottom);
  394. left: 0;
  395. right: 0;
  396. z-index: 11;
  397. .all-radio {
  398. width: 140rpx;
  399. display: flex;
  400. align-items: center;
  401. .radio {
  402. margin-bottom: -4rpx;
  403. transform: scale(0.76)
  404. }
  405. }
  406. .total-info {
  407. flex: 1;
  408. display: flex;
  409. align-items: center;
  410. justify-content: flex-end;
  411. padding-right: 30rpx;
  412. .goods-price {
  413. vertical-align: bottom;
  414. color: #fa2209;
  415. .unit {
  416. font-size: 24rpx;
  417. }
  418. .value {
  419. font-size: 36rpx;
  420. color: #f03c3c;
  421. font-weight: bold;
  422. }
  423. }
  424. }
  425. .cart-action {
  426. width: 200rpx;
  427. .btn-wrapper {
  428. height: 100%;
  429. display: flex;
  430. align-items: center;
  431. }
  432. .btn-item {
  433. flex: 1;
  434. font-size: 28rpx;
  435. height: 92rpx;
  436. line-height: 92rpx;
  437. text-align: center;
  438. color: #fff;
  439. }
  440. // 立即购买按钮
  441. .btn-main {
  442. background: linear-gradient(to right, $fuint-theme, $fuint-theme);
  443. color: #fff;
  444. padding-top: 5rpx;
  445. border-radius: 5rpx;
  446. margin-right: 20rpx;
  447. // 禁用按钮
  448. &.disabled {
  449. background: #cccccc;
  450. }
  451. }
  452. }
  453. }
  454. </style>