coupon.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. expiredNum: 0,
  10. unusedNum: 0,
  11. usedNum: 0
  12. }
  13. const mutations = {
  14. // 设置已领取优惠券数量
  15. setReceiveCouponCount(state, data) {
  16. state.unusedNum = data.unusedNum
  17. state.usedNum = data.usedNum
  18. state.expiredNum = data.expiredNum
  19. },
  20. // 更新弹窗类型
  21. updatePopupType(state, type) {
  22. state.activePopupType = type <= 0 ? 0 : type
  23. },
  24. // 设置优惠券弹窗是否显示
  25. setCouponPopupStatus(state, status) {
  26. state.showCouponPopup = status
  27. }
  28. }
  29. const actions = {
  30. // 领取优惠券
  31. receiveCoupon({ rootGetters, dispatch }, couponId) {
  32. return couponService.ReceiveCoupon({ couponId, userId: rootGetters.userId }).then(res => {
  33. dispatch('initReceiveCouponCount')
  34. showMsg('领取成功', 1500, false, 'success')
  35. }).catch(error => {
  36. showMsg(error.msg, 2000)
  37. })
  38. },
  39. // 初始化已领取优惠券数量
  40. initReceiveCouponCount({ commit, rootGetters }) {
  41. couponService.CouponReceiveCount({ userId: rootGetters.userId }).then(res => {
  42. commit('setReceiveCouponCount', res.data)
  43. })
  44. },
  45. // 优惠券活动弹窗信息
  46. getCouponActivity({ commit, rootGetters, state }) {
  47. if (!state.showCouponPopup) return
  48. couponService.CouponActivityPopup({
  49. userId: rootGetters.userId
  50. }).then(response => {
  51. const { newUserCouponFlag, actCouponFlag } = response.data
  52. // 全部用户弹窗
  53. if (actCouponFlag !== 0) {
  54. commit('updatePopupType', 1)
  55. }
  56. // 是否是新用户
  57. if (newUserCouponFlag !== 0) {
  58. commit('updatePopupType', 2)
  59. }
  60. })
  61. }
  62. }
  63. export default {
  64. namespaced: true,
  65. state,
  66. mutations,
  67. actions
  68. }