user.js 3.29 KB
import { login, logout, getInfo } from '@/api/auth'
import { getToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: null,
  key: null,
  userType: null,
  orderStatusDict: {}
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USER_TYPE: (state, type) => {
    state.userType = type
  },
  SET_STATUS_DICT: (state, userType) => {
    console.log('userType', userType)
    state.orderStatusDict = {
      CREATE: {
        name: '待确认',
        color: '#9BE405',
        dialogTitle: '订单确认',
        confirmButtonText: userType === 2 ? '保 存' : '确认订单',
        tableButtonTest: userType === 2 ? '编辑' : '确认订单'
      },
      DELIVER_GOODS: {
        name: '待发货',
        color: '#FFAA25',
        dialogTitle: '发货确认',
        confirmButtonText: '确认发货',
        tableButtonTest: '确认发货'
      },
      RECEIVE_GOODS: {
        name: userType === 2 ? '已发货' : '待收货',
        color: '#4DAAF5',
        dialogTitle: '收货确认',
        confirmButtonText: '确认收货',
        tableButtonTest: '确认收货'
      },
      WAITING_BILLING: {
        name: '待开票',
        color: '#005CF2',
        dialogTitle: '录入发票号',
        confirmButtonText: '确认开票',
        tableButtonTest: '确认开票'
      },
      BILLING: {
        name: userType === 2 ? '已开票' : '待收票',
        color: '#3553A4',
        dialogTitle: '收票确认',
        confirmButtonText: '确认收票',
        tableButtonTest: '确认收票'
      },
      RECEIPT: {
        name: userType === 2 ? '待收款' : '待付款',
        color: '#3553A4',
        dialogTitle: '收款确认',
        confirmButtonText: '确认收款',
        tableButtonTest: '确认收款'
      },
      FINISH: {
        name: '已完成',
        color: '#737373'
      }
    }
  }
}

const actions = {
  async login({ commit }, userInfo) {
    const response = await login(userInfo)
    console.log('登录成功', response)
    const { data: { key, userType } } = response
    localStorage.setItem('checkSum', key)
    localStorage.setItem('apiUser', userInfo?.p?.username)
    commit('SET_TOKEN', key)
    commit('SET_NAME', userInfo?.p?.username)
    commit('SET_USER_TYPE', userType)
    commit('SET_STATUS_DICT', userType)
  },

  async getInfo({ commit }) {
    const response = await getInfo({
      checkSum: localStorage.getItem('checkSum'),
      apiUser: localStorage.getItem('apiUser')
    })
    const { data } = response
    if (!data) {
      throw new Error('请重新登录')
    }
    console.log('获取登录信息成功')
    commit('SET_NAME', data.userName)
    commit('SET_USER_TYPE', data.userType)
    commit('SET_STATUS_DICT', data.userType)
    return data
  },

  async logout({ commit, state }) {
    await logout({ apiUser: localStorage.getItem('apiUser'), checkSum: localStorage.getItem('checkSum') })
    localStorage.setItem('apiUser', null)
    commit('SET_TOKEN', null)
    removeToken()
    resetRouter()
  },
  resetToken({ commit }) {
    commit('SET_TOKEN', null)
    commit('SET_NAME', null)
    removeToken()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}