coupon.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if (!rootGetters.userId) {
  29. const pages = getCurrentPages()
  30. const page = pages[pages.length - 1]
  31. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  32. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  33. return Promise.reject('用户未登录')
  34. }
  35. try {
  36. const res = await receiveCoupon({ couponId, couponShareId, userId: rootGetters.userId })
  37. await dispatch('initCouponCount')
  38. setTimeout(() => {
  39. uni.showToast({
  40. mask: true,
  41. icon: 'success',
  42. title: '领取成功'
  43. })
  44. }, 200)
  45. return res
  46. } catch (e) {
  47. console.log('领取优惠券失败')
  48. return Promise.reject(e)
  49. }
  50. },
  51. // 初始化已领取优惠券数量
  52. async initCouponCount({ commit, rootGetters }) {
  53. try {
  54. const res = await fetchReceivedCouponCount({ userId: rootGetters.userId })
  55. commit('SET_COUPON_COUNT', res.data)
  56. return res
  57. } catch (e) {
  58. console.log('初始化已领取优惠券数量失败')
  59. return Promise.reject(e)
  60. }
  61. },
  62. // 优惠券弹窗验证
  63. async checkCouponAlert({ commit, rootGetters }) {
  64. try {
  65. const result = await checkCouponAlert({ userId: rootGetters.userId })
  66. const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = result.data
  67. // 活动优惠券弹窗(全部用户)
  68. if (actCouponFlag !== 0) {
  69. commit('SET_COUPON_ALERT_TYPE')
  70. }
  71. // 新用户优惠券弹窗(新用户)
  72. if (newUserCouponFlag !== 0) {
  73. commit('SET_COUPON_ALERT_TYPE')
  74. }
  75. // 其它优惠券弹窗(可领取优惠券弹窗)
  76. if (otherCouponFlag !== 0) {
  77. commit('SET_COUPON_NITICE_FLAG', true)
  78. setTimeout(() => {
  79. commit('SET_COUPON_NITICE_FLAG', false)
  80. }, 5000)
  81. }
  82. } catch (e) {
  83. console.log('优惠券弹窗校验失败')
  84. return Promise.reject(e)
  85. }
  86. }
  87. }
  88. export default {
  89. namespaced: true,
  90. state,
  91. mutations,
  92. actions
  93. }