user.js
8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import Vue from 'vue'
import {login, logout, phoneLogin, thirdLogin} from "@/api/login"
import {
ACCESS_TOKEN,
USER_NAME,
USER_INFO,
USER_AUTH,
SYS_BUTTON_AUTH,
UI_CACHE_DB_DICT_DATA,
UI_CACHE_TRANSLATE_DATA,
UI_CACHE_TRANSLATE_DICT_DATA,
TENANT_ID,
CACHE_INCLUDED_ROUTES
} from "@/store/mutation-types"
import {welcome} from "@/utils/util"
import {queryPermissionsByUser, getTranslateItemsFromCache, translateDistItems} from '@/api/api'
import {getAction} from '@/api/manage'
const user = {
state: {
token: '',
username: '',
realname: '',
tenantid: '',
welcome: '',
avatar: '',
permissionList: [],
info: {},
// 系统安全模式
sysSafeMode: null,
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, {username, realname, welcome}) => {
state.username = username
state.realname = realname
state.welcome = welcome
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_PERMISSIONLIST: (state, permissionList) => {
state.permissionList = permissionList
},
SET_INFO: (state, info) => {
state.info = info
},
SET_TENANT: (state, id) => {
state.tenantid = id
},
SET_SYS_SAFE_MODE: (state, sysSafeMode) => {
if (typeof sysSafeMode === 'boolean') {
state.sysSafeMode = sysSafeMode
} else {
state.sysSafeMode = false
}
},
},
actions: {
// CAS验证登录
ValidateLogin({commit}, userInfo) {
return new Promise((resolve, reject) => {
getAction("/sys/cas/client/validateLogin", userInfo).then(response => {
console.log("----cas 登录--------", response);
if (response.success) {
const result = response.result
const userInfo = result.userInfo
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_INFO', userInfo)
commit('SET_NAME', {username: userInfo.username, realname: userInfo.realname, welcome: welcome()})
commit('SET_AVATAR', userInfo.avatar)
resolve(response)
} else {
resolve(response)
}
}).catch(error => {
reject(error)
})
})
},
// 登录
Login({commit}, userInfo) {
return new Promise((resolve, reject) => {
login(userInfo).then(response => {
if (response.code == '200') {
const result = response.result
const userInfo = result.userInfo
const sysTranslateDictItems = result.sysTranslateDictItems
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(UI_CACHE_TRANSLATE_DICT_DATA, sysTranslateDictItems, 7 * 24 * 60 * 60 * 1000)
// 翻译数据字典
const allDictItems = translateDistItems(result.sysAllDictItems);
Vue.ls.set(UI_CACHE_DB_DICT_DATA, allDictItems, 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_INFO', userInfo)
commit('SET_NAME', {username: userInfo.username, realname: userInfo.realname, welcome: welcome()})
commit('SET_AVATAR', userInfo.avatar)
resolve(response)
} else {
reject(response)
}
}).catch(error => {
reject(error)
})
})
},
//手机号登录
PhoneLogin({commit}, userInfo) {
return new Promise((resolve, reject) => {
phoneLogin(userInfo).then(response => {
if (response.code == '200') {
const result = response.result
const userInfo = result.userInfo
const sysTranslateDictItems = result.sysTranslateDictItems
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(UI_CACHE_TRANSLATE_DICT_DATA, sysTranslateDictItems, 7 * 24 * 60 * 60 * 1000)
// 翻译数据字典
const allDictItems = translateDistItems(result.sysAllDictItems);
Vue.ls.set(UI_CACHE_DB_DICT_DATA, allDictItems, 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_INFO', userInfo)
commit('SET_NAME', {username: userInfo.username, realname: userInfo.realname, welcome: welcome()})
commit('SET_AVATAR', userInfo.avatar)
resolve(response)
} else {
reject(response)
}
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetPermissionList({commit}) {
return new Promise((resolve, reject) => {
queryPermissionsByUser().then(response => {
const menuData = response.result.menu;
const authData = response.result.auth;
const allAuthData = response.result.allAuth;
// 设置国际化缓存
Vue.ls.set(UI_CACHE_TRANSLATE_DATA, response.result.sysTranslateItems, 7 * 24 * 60 * 60 * 1000)
sessionStorage.setItem(USER_AUTH, JSON.stringify(authData));
sessionStorage.setItem(SYS_BUTTON_AUTH, JSON.stringify(allAuthData));
if (menuData && menuData.length > 0) {
// 获取缓存语种
let language = Vue.ls.get("language");
menuData.forEach((item) => {
let temp = getTranslateItemsFromCache('sys_permission', item["id"], language, '')
if (temp != null) {
item["meta"].title = temp;
}
if (item["children"]) {
let hasChildrenMenu = item["children"].filter((i) => {
let childrenTemp = getTranslateItemsFromCache('sys_permission', i["id"], language, '')
if (childrenTemp != null) {
i["meta"].title = childrenTemp;
}
return !i.hidden || i.hidden == false
})
if (hasChildrenMenu == null || hasChildrenMenu.length == 0) {
item["hidden"] = true
}
}
})
commit('SET_PERMISSIONLIST', menuData)
// 设置系统安全模式
commit('SET_SYS_SAFE_MODE', response.result.sysSafeMode)
} else {
reject('getPermissionList: permissions must be a non-null array !')
}
resolve(response)
}).catch(error => {
reject(error)
})
})
},
// 登出
Logout({commit, state}) {
return new Promise((resolve) => {
let logoutToken = state.token;
commit('SET_TOKEN', '')
commit('SET_PERMISSIONLIST', [])
Vue.ls.remove(ACCESS_TOKEN)
Vue.ls.remove(USER_INFO)
Vue.ls.remove(USER_NAME)
Vue.ls.remove(UI_CACHE_DB_DICT_DATA)
Vue.ls.remove(UI_CACHE_TRANSLATE_DICT_DATA)
Vue.ls.remove(UI_CACHE_TRANSLATE_DATA)
Vue.ls.remove(CACHE_INCLUDED_ROUTES)
Vue.ls.remove(TENANT_ID)
//console.log('logoutToken: '+ logoutToken)
logout(logoutToken).then(() => {
if (process.env.VUE_APP_SSO == 'true') {
let sevice = 'http://' + window.location.host + '/wms/'
let serviceUrl = encodeURIComponent(sevice)
window.location.href = process.env.VUE_APP_CAS_BASE_URL + '/logout?service=' + serviceUrl
}
resolve()
}).catch(() => {
resolve()
})
})
},
// 第三方登录
ThirdLogin({commit}, param) {
return new Promise((resolve, reject) => {
thirdLogin(param.token, param.thirdType).then(response => {
if (response.code == '200') {
const result = response.result
const userInfo = result.userInfo
Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_NAME, userInfo.username, 7 * 24 * 60 * 60 * 1000)
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
commit('SET_TOKEN', result.token)
commit('SET_INFO', userInfo)
commit('SET_NAME', {username: userInfo.username, realname: userInfo.realname, welcome: welcome()})
commit('SET_AVATAR', userInfo.avatar)
resolve(response)
} else {
reject(response)
}
}).catch(error => {
reject(error)
})
})
},
saveTenant({commit}, id) {
Vue.ls.set(TENANT_ID, id, 7 * 24 * 60 * 60 * 1000)
commit('SET_TENANT', id)
}
}
}
export default user