commonStore.js 2.66 KB
import router from '@/router'
export default {
  state: {
    verificationCount: null,
    verificationFlag: null,
    lastPath: '',
    prevPath: '',
    goodsInfo: {},
    merchantId: null,
    order: {},
    address: {}
  },
  mutations: {
    setUserInfo(state, newVal) {
      Object.keys(newVal).map(key => {
        if(key == 'sessionId' && newVal[key].indexOf('-') == -1) {
          return
        }
        if (newVal[key]) state[key] = newVal[key]
      })
      const ua = navigator.userAgent.toLowerCase()
      if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        let userInfo = sessionStorage.getItem('GL_EQUITY_USER_INFO')
        if (!userInfo) {
          sessionStorage.setItem('GL_EQUITY_USER_INFO', JSON.stringify(state))
          console.log(state.lastPath);
          if (state.lastPath) {
            let route = state.lastPath.replace('.', '/')
            if (route == 'equity/index') {
              route = ''
              history.back()//去掉历史记录,重复页面
              //router.replace({ name: 'common.marketing' })
              //window.location.replace(`${window.location.href.split('?')[0]}#/${route}`)
              return
            } else {
              if (!newVal.phone && route != 'device/index') {
                router.replace({ name: 'common.phoneLogin' })
                return
              }
            }
            window.location.href = `${window.location.href.split('?')[0]}#/${route}`
            return
          }else{
            let route = state.lastPath.replace('.', '/')
            window.location.href = `${window.location.href.split('?')[0]}#/${route}`
          }
        }
      }
    },
    setMerchantId(state, newVal) {
      state.merchantId = newVal;
    },
    setOrder(state, newVal) {
      state.order = newVal;
    },
    setAddress(state, newVal) {
      state.address = newVal;
    },
    setGoodsInfo(state, newVal) {
      state.goodsInfo = newVal;
      sessionStorage.setItem('GL_GOODS_INFO', JSON.stringify(newVal))
    },
    setVerificationCount(state, newVal) {
      state.verificationCount = newVal
    },
    setVerificationFlag(state, newVal) {
      state.verificationFlag = newVal
    },
    jumpPath(state, newVal) {
      state.lastPath = newVal
    },
    findPrevPath(state, newVal) {
      state.prevPath = newVal
    },
    clearSessionId(state) {
      state.sessionId = ''
      router.replace({ name: 'common.marketing' })
    },
    reload(state) {
      console.log(state)
      // router.replace({ name: 'common.nothing' })
      // setTimeout(() => {
      //   state.lastPath?router.replace({ name: state.lastPath }):router.replace({name: 'equity.index'})
      // }, 1000)
    }
  },
}