text: import { login, logout, getUserInfo } from '@/api/user'
  import { setToken, getToken } from '@/libs/util'
  
  export default {
    state: {
      userName: '',
      userId: '',
      avatarImgPath: '',
      token: getToken(),
      access: [],
      superAdmin: 0,
      hasGetInfo: false
    },
    mutations: {
      setAvatar (state, avatarPath) {
        state.avatarImgPath = avatarPath
      },
      setUserId (state, id) {
        state.userId = id
      },
      setUserName (state, name) {
        state.userName = name
      },
      setAccess (state, access) {
        state.access = access
      },
      setSuperAdmin (state, bool) {
        state.superAdmin = bool
      },
      setToken (state, token) {
        state.token = token
        setToken(token)
      },
      setHasGetInfo (state, status) {
        state.hasGetInfo = status
      }
    },
    actions: {
      // 登录
      handleLogin ({ commit }, {userName, password}) {
        userName = userName.trim()
        return new Promise((resolve, reject) => {
          login({
            userName,
            password
          }).then(res => {
            const data = res.result;
            commit('setToken', data)
            resolve()
          }).catch(err => {
            reject(err)
          })
        })
      },
      // 退出登录
      handleLogOut ({ state, commit }) {
        return new Promise((resolve, reject) => {
          logout(state.token).then(() => {
            commit('setToken', '')
            commit('setAccess', []),
            commit('setSuperAdmin', 0),
  
            resolve()
          }).catch(err => {
            reject(err)
          })
          // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
          // commit('setToken', '')
          // commit('setAccess', [])
          // resolve()
        })
      },
      // 获取用户相关信息
      getUserInfo ({ state, commit }) {
        return new Promise((resolve, reject) => {
          try {
            getUserInfo(state.token).then(res => {
              if (res.error == 0) {
                  const data = res.result;
  
                  commit('setAvatar', data.avatar)
                  commit('setUserName', data.name)
                  commit('setUserId', data.id)
                  commit('setAccess', data.permissions)
                  commit('setSuperAdmin', data.is_super),
                  commit('setHasGetInfo', true)
  
                  data.access = data.permissions;     // 权限认证
                  resolve(data)
              } else {
                  reject(res)
              }
            }).catch(err => {
              reject(err)
            })
          } catch (error) {
            reject(error)
          }
        })
      }
    }
  }
  