/** * 用户优惠券仓库 */ import { couponService } from '@/services/index.js' import { msg as showMsg } from '@/common/util.js' let timer = null const state = { showCouponPopup: true, // 是否显示优惠券入口弹窗 activePopupType: 0, // 0 禁用 1 老用户 2 新用户 otherCouponFlag: false, // 有待领取优惠券弹窗 expiredNum: 0, unusedNum: 0, usedNum: 0 } const mutations = { // 设置已领取优惠券数量 setReceiveCouponCount(state, data) { state.unusedNum = data.unusedNum state.usedNum = data.usedNum state.expiredNum = data.expiredNum }, // 更新弹窗类型 updatePopupType(state, type) { state.activePopupType = type <= 0 ? 0 : type }, // 设置优惠券弹窗是否显示 setCouponPopupStatus(state, status) { state.showCouponPopup = status }, // 设置是否有优惠券领取提示 setOtherCouponFlag(state, value) { state.otherCouponFlag = value === 1 } } const actions = { // 领取优惠券 receiveCoupon({ rootGetters, dispatch }, { couponId, couponShareId }) { return couponService.ReceiveCoupon({ couponId, couponShareId, userId: rootGetters.userId }).then(res => { dispatch('initReceiveCouponCount') showMsg('领取成功', 1500, false, 'success') }).catch(error => { showMsg(error.msg, 2000) }) }, // 初始化已领取优惠券数量 initReceiveCouponCount({ commit, rootGetters }) { couponService.CouponReceiveCount({ userId: rootGetters.userId }).then(res => { commit('setReceiveCouponCount', res.data) }) }, // 优惠券活动弹窗信息 getCouponActivity({ commit, rootGetters, state, dispatch }) { // clearTimeout(timer) couponService.CouponActivityPopup({ userId: rootGetters.userId }).then(response => { const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = response.data // debugger // 全部用户弹窗 if (actCouponFlag !== 0) { commit('updatePopupType', 1) } // 是否是新用户 if (newUserCouponFlag !== 0) { commit('updatePopupType', 2) } // 是否有新的优惠券可领取 commit('setOtherCouponFlag', otherCouponFlag) setTimeout(() => { commit('setOtherCouponFlag', false) }, 5000) // 每次5分钟刷新一次 10 * 1000 5 * 60 * 1000 // if (rootGetters.hasLogin) { // timer = setTimeout(() => { // dispatch('getCouponActivity') // }, 5 * 60 * 1000) // } }) } } export default { namespaced: true, state, mutations, actions }