coupon.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { fetchReceivedCouponCount, receiveCoupon } from '@/services/api/coupon.js'
  2. const state = {
  3. expiredNum: 0, // 已过期
  4. unusedNum: 0, // 未使用
  5. usedNum: 0 // 已使用
  6. }
  7. const mutations = {
  8. //设置优惠券数量
  9. SET_COUPON_COUNT: (state, data) => {
  10. state.expiredNum = data.expiredNum
  11. state.unusedNum = data.unusedNum
  12. state.usedNum = data.usedNum
  13. }
  14. }
  15. const actions = {
  16. // 领取优惠券
  17. async receiveCoupon({ rootGetters, dispatch }, { couponId, couponShareId }) {
  18. try {
  19. const res = await receiveCoupon({ couponId, couponShareId, userId: rootGetters.userId })
  20. uni.showToast({
  21. icon: 'success',
  22. title: '领取成功'
  23. })
  24. await dispatch('initCouponCount')
  25. return res
  26. } catch (e) {
  27. console.log(e)
  28. }
  29. },
  30. // 初始化已领取优惠券数量
  31. async initCouponCount({ commit, rootGetters }) {
  32. try {
  33. const res = await fetchReceivedCouponCount({ userId: rootGetters.userId })
  34. commit('SET_COUPON_COUNT', res.data)
  35. return res
  36. } catch (e) {
  37. console.log(e)
  38. }
  39. }
  40. }
  41. export default {
  42. namespaced: true,
  43. state,
  44. mutations,
  45. actions
  46. }