coupon.js 2.7 KB

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