text: import { login, logout, getInfo } from '@/api/login'
  import { getToken, setToken, removeToken } from '@/utils/auth'
  
  const user = {
    state: {
      token: getToken(),
      name: '',
      roles: [] // 角色
    },
    mutations: {
      SET_TOKEN: (state, token) => {
        state.token = token
      },
      SET_NAME: (state, name) => {
        state.name = name
      },
      SET_ROLES: (state, roles) => {
        state.roles = roles
      }
  
    },
  
    actions: {
      // 账号登录
      Login({ commit }, userInfo) {
        const username = userInfo.username.trim()// 去掉空格
        return new Promise((resolve, reject) => {
          login(username, userInfo.password).then(response => {
            const data = response.data
            const token = data.token
            setToken(token)
            commit('SET_TOKEN', token)
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
  
      // 获取用户信息
      GetInfo({ commit }) {
        return new Promise((resolve, reject) => {
          getInfo().then(response => {
            const data = response.data
            commit('SET_ROLES', [data.realNameVerified])// 是否通过实名认证
            commit('SET_NAME', data.nickname)
            resolve(data)
          }).catch(error => {
            reject(error)
          })
        })
      },
  
      // 登出
      LogOut({ commit }) {
        return new Promise((resolve, reject) => {
          logout().then(res => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeToken()
            resolve()
          }).catch(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeToken()
            resolve()
            // reject(error)
          })
        })
      },
  
      SetToken({ commit }, token) {
        setToken(token)
        commit('SET_TOKEN', token)
      }
    }
  }
  
  export default user
  