user.js 4.46 KB
import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const baseUrl = config.baseUrl

const user = {
  state: {
    token: getToken(),
    name: storage.get(constant.name),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions),
    code: storage.get(constant.code),
    deptPaths: storage.get(constant.deptPaths),
    id: storage.get(constant.id),
    username: storage.get(constant.username),
    roleCodes: storage.get(constant.roleCodes),
    telephone: storage.get(constant.telephone),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
      storage.set(constant.name, name)
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
      storage.set(constant.roles, roles)
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
      storage.set(constant.permissions, permissions)
    },
    SET_CODE: (state, code) => {
      state.code = code
      storage.set(constant.code, code)
    },
    SET_DEPT_PATHS: (state, deptPaths) => {
      state.deptPaths = deptPaths
      storage.set(constant.deptPaths, deptPaths)
    },
    SET_ID: (state, id) => {
      state.id = id
      storage.set(constant.id, id)
    },
    SET_USERNAME: (state, username) => {
      state.username = username
      storage.set(constant.username, username)
    },
    SET_ROLE_CODES: (state, roleCodes) => {
      state.roleCodes = roleCodes
      storage.set(constant.roleCodes, roleCodes)
    },
    SET_TELEPHONE: (state, telephone) => {
      state.telephone = telephone
      storage.set(constant.telephone, telephone)
    }
  },

  actions: {
    // 登录
    Login({ commit, dispatch }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password.trim()
      const captcha = userInfo.captcha.trim()
      const sn = userInfo.sn
	
      return new Promise((resolve, reject) => {

        login(username, password, captcha, sn).then(res => {
          console.log('login_res', res)
          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
		  
          const name = res.data.user.name || '';
          commit('SET_NAME', name)

          if (res.data.roles && res.data.roles.length > 0) {
            commit('SET_ROLES', res.data.roles)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }

          // 登录后拉取最新的用户信息,统一存入 store
          dispatch('GetInfo')
            .then(() => resolve())
            .catch(() => {
              // GetInfo 失败(token 异常或过期),执行登出并回到登录页
              dispatch('LogOut')
                .finally(() => {
                  try { uni.reLaunch({ url: '/pages/login' }) } catch(e) {}
                  resolve()
                })
            })
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const _data = res.data || {};
          console.log('center/info__data', _data)
          // 写入基础信息
          commit('SET_NAME', _data.name)
          commit('SET_ID', _data.id || '')
          commit('SET_USERNAME', _data.username || '')
          commit('SET_CODE', _data.code || '')
          commit('SET_TELEPHONE', _data.telephone || '')
          commit('SET_ROLE_CODES', _data.roleCodes || [])
          commit('SET_DEPT_PATHS', _data.deptPaths || [])
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_NAME', '')
          commit('SET_ID', '')
          commit('SET_USERNAME', '')
          commit('SET_CODE', '')
          commit('SET_TELEPHONE', '')
          commit('SET_ROLE_CODES', [])
          commit('SET_DEPT_PATHS', [])
          removeToken()
          storage.clean()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user