coupon.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * 用户优惠券仓库
  3. */
  4. import { couponService } from '@/services/index.js'
  5. import { msg as showMsg } from '@/common/util.js'
  6. let timer = null
  7. const state = {
  8. showCouponPopup: true, // 是否显示优惠券入口弹窗
  9. activePopupType: 0, // 0 禁用 1 老用户 2 新用户
  10. otherCouponFlag: false, // 有待领取优惠券弹窗
  11. expiredNum: 0,
  12. unusedNum: 0,
  13. usedNum: 0
  14. }
  15. const mutations = {
  16. // 设置已领取优惠券数量
  17. setReceiveCouponCount(state, data) {
  18. state.unusedNum = data.unusedNum
  19. state.usedNum = data.usedNum
  20. state.expiredNum = data.expiredNum
  21. },
  22. // 更新弹窗类型
  23. updatePopupType(state, type) {
  24. state.activePopupType = type <= 0 ? 0 : type
  25. },
  26. // 设置优惠券弹窗是否显示
  27. setCouponPopupStatus(state, status) {
  28. state.showCouponPopup = status
  29. },
  30. // 设置是否有优惠券领取提示
  31. setOtherCouponFlag(state, value) {
  32. state.otherCouponFlag = value === 1
  33. }
  34. }
  35. const actions = {
  36. // 领取优惠券
  37. receiveCoupon({ rootGetters, dispatch }, { couponId, couponShareId }) {
  38. return couponService.ReceiveCoupon({ couponId, couponShareId, userId: rootGetters.userId }).then(res => {
  39. dispatch('initReceiveCouponCount')
  40. showMsg('领取成功', 1500, false, 'success')
  41. }).catch(error => {
  42. showMsg(error.msg, 2000)
  43. })
  44. },
  45. // 初始化已领取优惠券数量
  46. initReceiveCouponCount({ commit, rootGetters }) {
  47. couponService.CouponReceiveCount({ userId: rootGetters.userId }).then(res => {
  48. commit('setReceiveCouponCount', res.data)
  49. })
  50. },
  51. // 优惠券活动弹窗信息
  52. getCouponActivity({ commit, rootGetters, state, dispatch }) {
  53. // clearTimeout(timer)
  54. couponService.CouponActivityPopup({
  55. userId: rootGetters.userId
  56. }).then(response => {
  57. const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = response.data
  58. // debugger
  59. // 全部用户弹窗
  60. if (actCouponFlag !== 0) {
  61. commit('updatePopupType', 1)
  62. }
  63. // 是否是新用户
  64. if (newUserCouponFlag !== 0) {
  65. commit('updatePopupType', 2)
  66. }
  67. // 是否有新的优惠券可领取
  68. commit('setOtherCouponFlag', otherCouponFlag)
  69. setTimeout(() => {
  70. commit('setOtherCouponFlag', false)
  71. }, 5000)
  72. // 每次5分钟刷新一次 10 * 1000 5 * 60 * 1000
  73. // if (rootGetters.hasLogin) {
  74. // timer = setTimeout(() => {
  75. // dispatch('getCouponActivity')
  76. // }, 5 * 60 * 1000)
  77. // }
  78. })
  79. }
  80. }
  81. export default {
  82. namespaced: true,
  83. state,
  84. mutations,
  85. actions
  86. }