text: import { login, logout, getInfo } from '@/api/login'
  import { getToken, setToken, removeToken } from '@/utils/auth'
  
  const user = {
    state: {
      token: getToken(),
      nickname: '',
      avatar: '',
      uid: '',
      user: {},
      roles: [],
      menus: [], // 菜单权限
      buttons: [] // 安装权限
    },
  
    mutations: {
      SET_TOKEN: (state, token) => {
        state.token = token
      },
      SET_INFO: (state, user) => {
        state.nickname = user.nickname
        state.avatar = user.avatar
        state.uid = user.uid
        state.user = user
      },
      SET_ROLES: (state, roles) => {
        state.roles = roles
      },
      SET_MENUS: (state, menus) => {
        state.menus = menus
      },
      SET_BUTTONS: (state, buttons) => {
        state.buttons = buttons
      }
    },
  
    actions: {
      // 登录
      Login({ commit }, userInfo) {
        const username = userInfo.username.trim()
        return new Promise((resolve, reject) => {
          login(username, userInfo.password).then(res => {
            setToken(res.data)
            commit('SET_TOKEN', res.data)
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
  
      // 获取用户信息
      GetInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
          getInfo(state.token).then(res => {
            const data = res.data
            commit('SET_MENUS', data.menus)
            commit('SET_BUTTONS', data.buttons)
            // 设置用户信息
            commit('SET_INFO', data)
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
  
      // 登出
      LogOut({ commit, state }) {
        return new Promise((resolve, reject) => {
          logout(state.token).then(() => {
            commit('SET_INFO', '')
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
  
      // 前端 登出
      FedLogOut({ commit }) {
        return new Promise(resolve => {
          commit('SET_TOKEN', '')
          removeToken()
          resolve()
        })
      }
    }
  }
  
  export default user
  