Blame view

src/store/modules/user.js 3.4 KB
yuanshuhui authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import {
  login,
  logout,
  getInfo,
  refreshToken
} from '@/api/login'
import {
  getToken,
  setToken,
  setExpiresIn,
  removeToken
} from '@/utils/auth'
import {
  AESUtil
} from '@/utils/crypto'
yuanshuhui authored
16
17

const user = {
yuanshuhui authored
18
  namespaced: true,
yuanshuhui authored
19
20
21
22
23
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
yuanshuhui authored
24
    permissions: [],
yuanshuhui authored
25
26
    userId: '',
    warehouseCode: ''
yuanshuhui authored
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_EXPIRES_IN: (state, time) => {
      state.expires_in = time
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
yuanshuhui authored
47
48
49
50
51
52
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_WAREHOUSECODE: (state, warehouseCode) => {
      state.warehouseCode = warehouseCode
yuanshuhui authored
53
54
55
56
57
    }
  },

  actions: {
    // 登录
yuanshuhui authored
58
59
60
    Login({
      commit
    }, userInfo) {
yuanshuhui authored
61
62
63
64
      const username = userInfo.username.trim()
      const password = AESUtil.aesEncrypt(userInfo.password)
      const warehouseCode = userInfo.warehouseCode
      return new Promise((resolve, reject) => {
yuanshuhui authored
65
        login(username, password, warehouseCode).then(res => {
yuanshuhui authored
66
yuanshuhui authored
67
68
69
70
71
          let data = res.data
          setToken(data.access_token)
          commit('SET_TOKEN', data.access_token)
          setExpiresIn(data.expires_in)
          commit('SET_EXPIRES_IN', data.expires_in)
yuanshuhui authored
72
73
          commit('SET_USERID', data.userId)
          commit('SET_WAREHOUSECODE', data.warehouseCode)
yuanshuhui authored
74
75
76
77
78
79
80
81
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
yuanshuhui authored
82
83
84
85
    GetInfo({
      commit,
      state
    }) {
yuanshuhui authored
86
87
88
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(res => {
          const user = res.user
yuanshuhui authored
89
          const avatar = user.avatar == "" ? require("@/assets/image/profile.jpg") : user.avatar;
yuanshuhui authored
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 刷新token
yuanshuhui authored
106
107
108
109
    RefreshToken({
      commit,
      state
    }) {
yuanshuhui authored
110
111
112
113
114
115
116
117
118
119
      return new Promise((resolve, reject) => {
        refreshToken(state.token).then(res => {
          setExpiresIn(res.data)
          commit('SET_EXPIRES_IN', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
yuanshuhui authored
120
yuanshuhui authored
121
    // 退出系统
yuanshuhui authored
122
123
124
125
    LogOut({
      commit,
      state
    }) {
yuanshuhui authored
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
yuanshuhui authored
140
141
142
    FedLogOut({
      commit
    }) {
yuanshuhui authored
143
144
145
146
147
148
149
150
151
152
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user