coupon.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { fetchReceivedCouponCount, receiveCoupon, checkCouponAlert } from '@/services/api/coupon.js'
  2. const state = {
  3. expiredNum: 0, // 已过期
  4. unusedNum: 0, // 未使用
  5. usedNum: 0, // 已使用
  6. couponAlertType: 0, // 优惠券弹窗类型 0: 不弹出 1: 全部用户 2: 新人用户
  7. couponNoticeFlag: false // 新优惠券领取通知
  8. }
  9. const mutations = {
  10. //设置优惠券数量
  11. SET_COUPON_COUNT: (state, data) => {
  12. state.expiredNum = data.expiredNum
  13. state.unusedNum = data.unusedNum
  14. state.usedNum = data.usedNum
  15. },
  16. // 设置优惠券弹窗类型
  17. SET_COUPON_ALERT_TYPE: (state, data) => {
  18. typeof data === 'number' ? state.couponAlertType = data : state.couponAlertType++
  19. },
  20. // 设置新优惠券通知
  21. SET_COUPON_NITICE_FLAG: (state, data) => {
  22. state.couponNoticeFlag = data
  23. }
  24. }
  25. const actions = {
  26. // 领取优惠券
  27. async receiveCoupon({ rootGetters, dispatch }, { couponId, couponShareId }) {
  28. try {
  29. const res = await receiveCoupon({ couponId, couponShareId, userId: rootGetters.userId })
  30. uni.showToast({
  31. icon: 'success',
  32. title: '领取成功'
  33. })
  34. await dispatch('initCouponCount')
  35. return res
  36. } catch (e) {
  37. console.log('领取优惠券失败')
  38. }
  39. },
  40. // 初始化已领取优惠券数量
  41. async initCouponCount({ commit, rootGetters }) {
  42. try {
  43. const res = await fetchReceivedCouponCount({ userId: rootGetters.userId })
  44. commit('SET_COUPON_COUNT', res.data)
  45. return res
  46. } catch (e) {
  47. console.log('初始化已领取优惠券数量失败')
  48. }
  49. },
  50. // 优惠券弹窗验证
  51. async checkCouponAlert({ commit, rootGetters }) {
  52. try {
  53. const result = await checkCouponAlert({ userId: rootGetters.userId })
  54. const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = result.data
  55. // 活动优惠券弹窗(全部用户)
  56. if (actCouponFlag !== 0) {
  57. commit('SET_COUPON_ALERT_TYPE')
  58. }
  59. // 新用户优惠券弹窗(新用户)
  60. if (newUserCouponFlag !== 0) {
  61. commit('SET_COUPON_ALERT_TYPE')
  62. }
  63. // 其它优惠券弹窗(可领取优惠券弹窗)
  64. if (otherCouponFlag !== 0) {
  65. commit('SET_COUPON_NITICE_FLAG', true)
  66. setTimeout(() => {
  67. commit('SET_COUPON_NITICE_FLAG', false)
  68. }, 5000)
  69. }
  70. } catch (e) {
  71. console.log('优惠券弹窗校验失败');
  72. }
  73. }
  74. }
  75. export default {
  76. namespaced: true,
  77. state,
  78. mutations,
  79. actions
  80. }