commit | author | age
|
9bcb19
|
1 |
import Vue from 'vue' |
I |
2 |
import { login, getLoginUser, logout } from '@/api/modular/system/loginManage' |
|
3 |
import { sysDictTypeTree } from '@/api/modular/system/dictManage' |
|
4 |
import { sysMenuChange } from '@/api/modular/system/menuManage' |
|
5 |
import { ACCESS_TOKEN, ALL_APPS_MENU, DICT_TYPE_TREE_DATA } from '@/store/mutation-types' |
|
6 |
|
|
7 |
import { welcome } from '@/utils/util' |
|
8 |
import store from '../index' |
|
9 |
import router from '../../router' |
|
10 |
|
|
11 |
const user = { |
|
12 |
state: { |
|
13 |
token: '', |
|
14 |
name: '', |
|
15 |
welcome: '', |
|
16 |
avatar: '', |
|
17 |
buttons: [], // 按钮权限 |
|
18 |
admintype: '', // 是否是超管 |
|
19 |
roles: [], |
|
20 |
info: {} |
|
21 |
}, |
|
22 |
|
|
23 |
mutations: { |
|
24 |
SET_TOKEN: (state, token) => { |
|
25 |
state.token = token |
|
26 |
}, |
|
27 |
SET_NAME: (state, { name, welcome }) => { |
|
28 |
state.name = name |
|
29 |
state.welcome = welcome |
|
30 |
}, |
|
31 |
SET_AVATAR: (state, avatar) => { |
|
32 |
state.avatar = avatar |
|
33 |
}, |
|
34 |
SET_ROLES: (state, roles) => { |
|
35 |
state.roles = roles |
|
36 |
}, |
|
37 |
SET_INFO: (state, info) => { |
|
38 |
state.info = info |
|
39 |
}, |
|
40 |
SET_BUTTONS: (state, buttons) => { |
|
41 |
state.buttons = buttons |
|
42 |
}, |
|
43 |
SET_ADMINTYPE: (state, admintype) => { |
|
44 |
state.admintype = admintype |
|
45 |
} |
|
46 |
}, |
|
47 |
|
|
48 |
actions: { |
|
49 |
// 登录 |
|
50 |
Login ({ commit }, userInfo) { |
|
51 |
return new Promise((resolve, reject) => { |
|
52 |
login(userInfo).then(response => { |
|
53 |
if (!response.success) { |
|
54 |
reject(response.message) |
|
55 |
return |
|
56 |
} |
|
57 |
const result = response.data |
|
58 |
Vue.ls.set(ACCESS_TOKEN, result, 7 * 24 * 60 * 60 * 1000) |
|
59 |
commit('SET_TOKEN', result) |
|
60 |
resolve() |
|
61 |
// eslint-disable-next-line handle-callback-err |
|
62 |
}).catch(error => { |
|
63 |
// eslint-disable-next-line prefer-promise-reject-errors |
|
64 |
reject('后端未启动或代理错误') |
|
65 |
}) |
|
66 |
}) |
|
67 |
}, |
|
68 |
|
|
69 |
// 获取用户信息 |
|
70 |
GetInfo ({ commit }) { |
|
71 |
return new Promise((resolve, reject) => { |
|
72 |
getLoginUser().then(response => { |
|
73 |
if (response.success) { |
|
74 |
const data = response.data |
|
75 |
commit('SET_ADMINTYPE', data.adminType) |
|
76 |
commit('SET_ROLES', 1) |
|
77 |
commit('SET_BUTTONS', data.permissions) |
|
78 |
commit('SET_INFO', data) |
|
79 |
commit('SET_NAME', { name: data.name, welcome: welcome() }) |
|
80 |
if (data.avatar != null) { |
|
81 |
commit('SET_AVATAR', process.env.VUE_APP_API_BASE_URL + '/sysFileInfo/preview?id=' + data.avatar) |
|
82 |
} |
|
83 |
resolve(data) |
|
84 |
} else { |
|
85 |
// eslint-disable-next-line no-undef |
|
86 |
reject(new Error(data.message)) |
|
87 |
} |
|
88 |
}).catch(error => { |
|
89 |
reject(error) |
|
90 |
}) |
|
91 |
}) |
|
92 |
}, |
|
93 |
|
|
94 |
// 登出 |
|
95 |
Logout ({ commit, state }) { |
|
96 |
return new Promise((resolve) => { |
|
97 |
logout(state.token).then(() => { |
|
98 |
resolve() |
|
99 |
}).catch(() => { |
|
100 |
resolve() |
|
101 |
}).finally(() => { |
|
102 |
commit('SET_TOKEN', '') |
|
103 |
commit('SET_ROLES', []) |
|
104 |
commit('SET_BUTTONS', []) |
|
105 |
commit('SET_ADMINTYPE', '') |
|
106 |
Vue.ls.remove(ACCESS_TOKEN) |
|
107 |
Vue.ls.remove(ALL_APPS_MENU) |
|
108 |
Vue.ls.remove(DICT_TYPE_TREE_DATA) |
|
109 |
}) |
|
110 |
}) |
|
111 |
}, |
|
112 |
|
|
113 |
// 加载所有字典数据 |
|
114 |
dictTypeData () { |
|
115 |
return new Promise((resolve, reject) => { |
|
116 |
sysDictTypeTree().then((data) => { |
|
117 |
if (data.success) { |
|
118 |
const result = data.data |
|
119 |
Vue.ls.set(DICT_TYPE_TREE_DATA, result) |
|
120 |
resolve() |
|
121 |
} else { |
|
122 |
// eslint-disable-next-line no-undef |
|
123 |
reject(new Error(data.message)) |
|
124 |
} |
|
125 |
}).catch(error => { |
|
126 |
reject(error) |
|
127 |
}) |
|
128 |
}) |
|
129 |
}, |
|
130 |
|
|
131 |
// 切换应用菜单 |
|
132 |
MenuChange ({ commit }, application) { |
|
133 |
return new Promise((resolve) => { |
|
134 |
sysMenuChange({ application: application.code }).then((res) => { |
|
135 |
const apps = { 'code': '', 'name': '', 'active': '', 'menu': '' } |
|
136 |
apps.active = true |
|
137 |
apps.menu = res.data |
|
138 |
// eslint-disable-next-line camelcase |
|
139 |
const all_app_menu = Vue.ls.get(ALL_APPS_MENU) |
|
140 |
// eslint-disable-next-line camelcase |
|
141 |
const new_false_all_app_menu = [] |
|
142 |
// 先去除所有默认的,以为此时切换的即将成为前端缓存默认的应用 |
|
143 |
all_app_menu.forEach(item => { |
|
144 |
if (item.active) { |
|
145 |
item.active = false |
|
146 |
} |
|
147 |
new_false_all_app_menu.push(item) |
|
148 |
}) |
|
149 |
// 此时缓存中全部都是不默认的应用 |
|
150 |
Vue.ls.set(ALL_APPS_MENU, new_false_all_app_menu) |
|
151 |
apps.name = application.name |
|
152 |
apps.code = application.code |
|
153 |
const applocationR = [] |
|
154 |
applocationR.push(apps) |
|
155 |
Vue.ls.set(ALL_APPS_MENU, applocationR) |
|
156 |
resolve(res) |
|
157 |
const antDesignmenus = res.data |
|
158 |
store.dispatch('GenerateRoutes', { antDesignmenus }).then(() => { |
|
159 |
router.addRoutes(store.getters.addRouters) |
|
160 |
}) |
|
161 |
// 切换应用刷新整体界面,暂且取消 |
|
162 |
// window.location.reload() |
|
163 |
}).catch(() => { |
|
164 |
resolve() |
|
165 |
}) |
|
166 |
}) |
|
167 |
} |
|
168 |
|
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
export default user |