coupon.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. uni.showToast({
  38. icon: 'success',
  39. title: '领取成功'
  40. })
  41. await dispatch('initCouponCount')
  42. return res
  43. } catch (e) {
  44. console.log('领取优惠券失败')
  45. }
  46. },
  47. // 初始化已领取优惠券数量
  48. async initCouponCount({ commit, rootGetters }) {
  49. try {
  50. const res = await fetchReceivedCouponCount({ userId: rootGetters.userId })
  51. commit('SET_COUPON_COUNT', res.data)
  52. return res
  53. } catch (e) {
  54. console.log('初始化已领取优惠券数量失败')
  55. }
  56. },
  57. // 优惠券弹窗验证
  58. async checkCouponAlert({ commit, rootGetters }) {
  59. try {
  60. const result = await checkCouponAlert({ userId: rootGetters.userId })
  61. const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = result.data
  62. // 活动优惠券弹窗(全部用户)
  63. if (actCouponFlag !== 0) {
  64. commit('SET_COUPON_ALERT_TYPE')
  65. }
  66. // 新用户优惠券弹窗(新用户)
  67. if (newUserCouponFlag !== 0) {
  68. commit('SET_COUPON_ALERT_TYPE')
  69. }
  70. // 其它优惠券弹窗(可领取优惠券弹窗)
  71. if (otherCouponFlag !== 0) {
  72. commit('SET_COUPON_NITICE_FLAG', true)
  73. setTimeout(() => {
  74. commit('SET_COUPON_NITICE_FLAG', false)
  75. }, 5000)
  76. }
  77. } catch (e) {
  78. console.log('优惠券弹窗校验失败');
  79. }
  80. }
  81. }
  82. export default {
  83. namespaced: true,
  84. state,
  85. mutations,
  86. actions
  87. }