123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import { fetchReceivedCouponCount, receiveCoupon, checkCouponAlert } from '@/services/api/coupon.js'
- const state = {
- expiredNum: 0, // 已过期
- unusedNum: 0, // 未使用
- usedNum: 0, // 已使用
- couponAlertType: 0, // 优惠券弹窗类型 0: 不弹出 1: 全部用户 2: 新人用户
- couponNoticeFlag: false // 新优惠券领取通知
- }
- const mutations = {
- //设置优惠券数量
- SET_COUPON_COUNT: (state, data) => {
- state.expiredNum = data.expiredNum
- state.unusedNum = data.unusedNum
- state.usedNum = data.usedNum
- },
- // 设置优惠券弹窗类型
- SET_COUPON_ALERT_TYPE: (state, data) => {
- typeof data === 'number' ? state.couponAlertType = data : state.couponAlertType++
- },
- // 设置新优惠券通知
- SET_COUPON_NITICE_FLAG: (state, data) => {
- state.couponNoticeFlag = data
- }
- }
- const actions = {
- // 领取优惠券
- async receiveCoupon({ rootGetters, dispatch }, { couponId, couponShareId }) {
- if (!rootGetters.userId) {
- const pages = getCurrentPages()
- const page = pages[pages.length - 1]
- uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
- uni.redirectTo({ url: '/pages/authorize/login-custom' })
- return Promise.reject('用户未登录')
- }
- try {
- const res = await receiveCoupon({ couponId, couponShareId, userId: rootGetters.userId })
- await dispatch('initCouponCount')
- setTimeout(() => {
- uni.showToast({
- mask: true,
- icon: 'success',
- title: '领取成功'
- })
- }, 200)
- return res
- } catch (e) {
- console.log('领取优惠券失败')
- return Promise.reject(e)
- }
- },
- // 初始化已领取优惠券数量
- async initCouponCount({ commit, rootGetters }) {
- try {
- const res = await fetchReceivedCouponCount({ userId: rootGetters.userId })
- commit('SET_COUPON_COUNT', res.data)
- return res
- } catch (e) {
- console.log('初始化已领取优惠券数量失败')
- return Promise.reject(e)
- }
- },
- // 优惠券弹窗验证
- async checkCouponAlert({ commit, rootGetters }) {
- try {
- const result = await checkCouponAlert({ userId: rootGetters.userId })
- const { newUserCouponFlag, actCouponFlag, otherCouponFlag } = result.data
- // 活动优惠券弹窗(全部用户)
- if (actCouponFlag !== 0) {
- commit('SET_COUPON_ALERT_TYPE')
- }
- // 新用户优惠券弹窗(新用户)
- if (newUserCouponFlag !== 0) {
- commit('SET_COUPON_ALERT_TYPE')
- }
- // 其它优惠券弹窗(可领取优惠券弹窗)
- if (otherCouponFlag !== 0) {
- commit('SET_COUPON_NITICE_FLAG', true)
- setTimeout(() => {
- commit('SET_COUPON_NITICE_FLAG', false)
- }, 5000)
- }
- } catch (e) {
- console.log('优惠券弹窗校验失败')
- return Promise.reject(e)
- }
- }
- }
- export default {
- namespaced: true,
- state,
- mutations,
- actions
- }
|