user.js 4.9 KB
const SESSION_NAME = `${process.env.VUE_APP_STORAGE_PREFIX}-${
  process.env.VUE_APP_VERSION
}-info`
const ACCOUNT_SESSION_NAME = `${process.env.VUE_APP_STORAGE_PREFIX}-${
  process.env.VUE_APP_VERSION
}-account`

export default {
  namespaced: true,
  state: {
    accountInfo: {
      accountList: []
    },
    // 用户信息 采用sessionStorage管理 方便判断用户是否关闭浏览器
    /** userType:(用户类型)
     * 用户类型-员工 - 1
     * 用户类型-嘀加渠道:工厂,广汇渠道:总部 - 5
     * 用户类型-嘀加渠道:省级经销商,广汇渠道:大区 - 4
     * 用户类型-嘀加渠道:市级经销商,广汇渠道:片区 - 3
     * 用户类型-嘀加渠道:门店,广汇渠道:门店 - 2
     * 用户类型-广汇渠道:供应商 - 6 **/
    /** detail.accountType:(账号类型)
     * 商户账号类型-批发/零售商 - 1
     * 商户账号类型-4S店 - 2
     * 商户账号类型-综合汽贸店 - 3
     * 商户账号类型-综合后服务门店 - 4
     * 商户账号类型-厂家 - 5
     * 商户账号类型-园区店 - 6
     * 商户账号类型-服务商 - 7
     * 商户账号类型-虚拟园区店 - 8
     **/
    info: {
      detail: {}
    },
    /** 异步下载excel列表 */
    dowloadList: [
      // {
      //   name: 'xxx店订单补录2022-02-04test',
      //   ifSuccess: false,
      //   id: 1,
      //   status: 'loading',
      //   type: 'upload',
      //   labelText: '导入成功0条, 导入失败1条'
      // }
    ],
    updateList: [
      // {
      //   name: 'xxx店订单补录2022-02-04test',
      //   ifSuccess: false,
      //   id: 1,
      //   status: 'loading',
      //   type: 'upload',
      //   labelText: '成功上传0条, 导入失败1条'
      // }
    ],
    updateRefesh: false
  },
  mutations: {
    setDownloadList(state, { list, type }) {
      // console.log(list)

      // return
      // state[type] = state[type].push(list)
      state[type] = list
      // console.log(state[`${type}`])
      // console.log(state[type])
    },
    setUpdateRefesh(state, data) {
      state.updateRefesh = data
    }
  },
  actions: {
    /**
     * @description 修改下载记录
     * @param {Object} state state
     */
    actionSetDownloadList({ commit, dispatch, state }, { list, type }) {
      // console.log(list, type)
      commit('setDownloadList', { list, type })
      // console.log()
      // console.log(dispatch)
      // console.log(state[type])
      // console.log(`state.${type}`)
      // 持久化
      dispatch(
        'd2admin/db/set',
        {
          dbName: 'sys',
          path: `user.${type}`,
          value: state[type],
          // state.dowloadList,
          user: true
        },
        { root: true }
      )
    },
    /**
     * @description 设置用户数据
     * @param {Object} context
     * @param {*} info info
     */
    set({ state, dispatch }, { info, accountInfo }) {
      return new Promise(async resolve => {
        // store 赋值
        state.info = info
        state.accountInfo = accountInfo
        sessionStorage.setItem(SESSION_NAME, escape(JSON.stringify(info)))
        sessionStorage.setItem(
          ACCOUNT_SESSION_NAME,
          escape(JSON.stringify(accountInfo))
        )
        resolve()
      })
    },
    /**
     * @description 从数据库取用户数据
     * @param {Object} context
     */
    load({ state, dispatch }) {
      return new Promise(async resolve => {
        state.info = JSON.parse(unescape(sessionStorage.getItem(SESSION_NAME)))
        resolve(state.info)
      })
    },
    /**
     * 从持久化数据读下载记录
     * @param {Object} context
     */
    downLoadListLoad({ state, dispatch }, type) {
      return new Promise(async resolve => {
        // store 赋值
        state[type] = await dispatch(
          'd2admin/db/get',
          {
            dbName: 'sys',
            path: `user.${type}`,
            defaultValue: [],
            user: true
          },
          { root: true }
        )
        // end
        resolve()
      })
    },
    loadAcocunt({ state, dispatch }) {
      return new Promise(async resolve => {
        state.accountInfo = JSON.parse(
          unescape(sessionStorage.getItem(ACCOUNT_SESSION_NAME))
        )
        resolve(state.accountInfo)
      })
    },

    clear({ state, dispatch }) {
      return new Promise(async resolve => {
        state.info = null
        sessionStorage.removeItem(SESSION_NAME)
        sessionStorage.removeItem(ACCOUNT_SESSION_NAME)
        dispatch('actionSetDownloadList', { list: [], type: 'updateList' })
        dispatch('actionSetDownloadList', { list: [], type: 'dowloadList' })
        resolve()
      })
    },
    reset({ dispatch }) {
      dispatch('clear')
    }
  }
}