|
@@ -7,136 +7,133 @@ import UserService from 'services/user/user-service'; |
|
@@ -7,136 +7,133 @@ import UserService from 'services/user/user-service'; |
7
|
import crypto from 'util/crypto';
|
7
|
import crypto from 'util/crypto';
|
8
|
|
8
|
|
9
|
export default {
|
9
|
export default {
|
10
|
- updateUser(Vue, user, purviews) {
|
|
|
11
|
- Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(user));
|
|
|
12
|
- Vue.prop('purviews', purviews.deep);
|
|
|
13
|
- Vue.prop('oriPurviews', purviews.ori);
|
|
|
14
|
- Vue.prop('user', user);
|
|
|
15
|
- Vue.prop('isLogin', true);
|
|
|
16
|
- },
|
10
|
+ updateUser(Vue, user, purviews) {
|
|
|
11
|
+ Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(user));
|
|
|
12
|
+ Vue.prop('purviews', purviews.deep);
|
|
|
13
|
+ Vue.prop('oriPurviews', purviews.ori);
|
|
|
14
|
+ Vue.prop('user', user);
|
|
|
15
|
+ Vue.prop('isLogin', true);
|
|
|
16
|
+ },
|
17
|
|
17
|
|
18
|
- // 权限验证
|
|
|
19
|
- checkPurview() {
|
|
|
20
|
- return Promise.resolve();
|
18
|
+ // 权限验证
|
|
|
19
|
+ checkPurview() {
|
|
|
20
|
+ return Promise.resolve();
|
21
|
|
21
|
|
22
|
- // let pur = _.find(Vue.$purviews, p => p.menu_url === to.path);
|
22
|
+ // let pur = _.find(Vue.$purviews, p => p.menu_url === to.path);
|
23
|
|
23
|
|
24
|
- // if (pur) {
|
|
|
25
|
- // return Promise.resolve();
|
|
|
26
|
- // }
|
|
|
27
|
- // return Promise.reject();
|
|
|
28
|
- },
|
|
|
29
|
- initPurview(Vue, user) {
|
|
|
30
|
- return this.userService.purviews().then((purviews) => {
|
|
|
31
|
- this.updateUser(Vue, user, purviews);
|
|
|
32
|
- });
|
|
|
33
|
- },
|
|
|
34
|
- initConfig(Vue) {
|
|
|
35
|
- return this.userService.config().then((data) => {
|
|
|
36
|
- Object.assign(Vue.$config, data);
|
|
|
37
|
- Vue.prop('config', Vue.$config);
|
|
|
38
|
- });
|
|
|
39
|
- },
|
|
|
40
|
- install(Vue) {
|
|
|
41
|
- this.userService = new UserService();
|
|
|
42
|
-
|
|
|
43
|
- Vue.beforeRender((next) => {
|
|
|
44
|
- let user = Vue.$store.get(Vue.$config.storeKeys.user);
|
|
|
45
|
- let isLogin = Vue.$cookie.get('_isLogin');
|
|
|
46
|
-
|
|
|
47
|
- if (isLogin && user) {
|
|
|
48
|
- try {
|
|
|
49
|
- user = crypto.aesDecrypt(user, Object);
|
|
|
50
|
- return Promise.all([
|
|
|
51
|
- this.initPurview(Vue, user),
|
|
|
52
|
- this.initConfig(Vue)
|
|
|
53
|
- ]).then(() => {
|
|
|
54
|
- next();
|
|
|
55
|
- }, () => {
|
|
|
56
|
- next();
|
|
|
57
|
- });
|
|
|
58
|
- } catch (e) {
|
|
|
59
|
- Vue.logout();
|
|
|
60
|
- }
|
|
|
61
|
- }
|
|
|
62
|
- next();
|
|
|
63
|
- });
|
24
|
+ // if (pur) {
|
|
|
25
|
+ // return Promise.resolve();
|
|
|
26
|
+ // }
|
|
|
27
|
+ // return Promise.reject();
|
|
|
28
|
+ },
|
|
|
29
|
+ initPurview(Vue, user) {
|
|
|
30
|
+ return this.userService.purviews().then(purviews => {
|
|
|
31
|
+ this.updateUser(Vue, user, purviews);
|
|
|
32
|
+ });
|
|
|
33
|
+ },
|
|
|
34
|
+ initConfig(Vue) {
|
|
|
35
|
+ return this.userService.config().then(data => {
|
|
|
36
|
+ Object.assign(Vue.$config, data);
|
|
|
37
|
+ Vue.prop('config', Vue.$config);
|
|
|
38
|
+ });
|
|
|
39
|
+ },
|
|
|
40
|
+ install(Vue) {
|
|
|
41
|
+ this.userService = new UserService();
|
64
|
|
42
|
|
65
|
- // 路由权限控制
|
|
|
66
|
- Vue.$router.beforeEach((to, from, next) => {
|
43
|
+ Vue.beforeRender(next => {
|
|
|
44
|
+ let user = Vue.$store.get(Vue.$config.storeKeys.user);
|
|
|
45
|
+ const isLogin = Vue.$cookie.get('_isLogin');
|
67
|
|
46
|
|
68
|
- // 需要修改密码去修改密码
|
|
|
69
|
- if (Vue.$store.get('needUpdate') && to.name !== 'login' && to.name !== 'user.password') {
|
|
|
70
|
- return next('/user/password.html');
|
47
|
+ if (isLogin && user) {
|
|
|
48
|
+ try {
|
|
|
49
|
+ user = crypto.aesDecrypt(user, Object);
|
|
|
50
|
+ return Promise.all([this.initPurview(Vue, user), this.initConfig(Vue)]).then(
|
|
|
51
|
+ () => {
|
|
|
52
|
+ next();
|
|
|
53
|
+ },
|
|
|
54
|
+ () => {
|
|
|
55
|
+ next();
|
71
|
}
|
56
|
}
|
|
|
57
|
+ );
|
|
|
58
|
+ } catch (e) {
|
|
|
59
|
+ Vue.logout();
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+ next();
|
|
|
63
|
+ });
|
72
|
|
64
|
|
73
|
- // 无权限控制理由直接pass
|
|
|
74
|
- let authPass = _.get(_.last(to.matched), 'meta.authPass', false);
|
65
|
+ // 路由权限控制
|
|
|
66
|
+ Vue.$router.beforeEach((to, from, next) => {
|
|
|
67
|
+ // 需要修改密码去修改密码
|
|
|
68
|
+ if (Vue.$store.get('needUpdate') && to.name !== 'login' && to.name !== 'user.password') {
|
|
|
69
|
+ return next('/user/password.html');
|
|
|
70
|
+ }
|
75
|
|
71
|
|
76
|
- if (authPass) {
|
|
|
77
|
- // 已登录跳转到首页
|
|
|
78
|
- if (to.name === 'login' && Vue.$isLogin) {
|
|
|
79
|
- return next('/');
|
|
|
80
|
- }
|
|
|
81
|
- return next();
|
|
|
82
|
- }
|
72
|
+ // 无权限控制理由直接pass
|
|
|
73
|
+ const authPass = _.get(_.last(to.matched), 'meta.authPass', false);
|
83
|
|
74
|
|
84
|
- // 未登录去登录
|
|
|
85
|
- if (!Vue.$isLogin) {
|
|
|
86
|
- return next('/login.html');
|
|
|
87
|
- }
|
75
|
+ if (authPass) {
|
|
|
76
|
+ // 已登录跳转到首页
|
|
|
77
|
+ if (to.name === 'login' && Vue.$isLogin) {
|
|
|
78
|
+ return next('/');
|
|
|
79
|
+ }
|
|
|
80
|
+ return next();
|
|
|
81
|
+ }
|
88
|
|
82
|
|
|
|
83
|
+ // 未登录去登录
|
|
|
84
|
+ if (!Vue.$isLogin) {
|
|
|
85
|
+ return next('/login.html');
|
|
|
86
|
+ }
|
|
|
87
|
+ return this.checkPurview(Vue, to).then(
|
|
|
88
|
+ () => {
|
|
|
89
|
+ return next();
|
|
|
90
|
+ },
|
|
|
91
|
+ () => {
|
|
|
92
|
+ return next('/401.html');
|
|
|
93
|
+ }
|
|
|
94
|
+ );
|
|
|
95
|
+ });
|
89
|
|
96
|
|
90
|
- return this.checkPurview(Vue, to).then(() => {
|
|
|
91
|
- return next();
|
|
|
92
|
- }, () => {
|
|
|
93
|
- return next('/401.html');
|
97
|
+ Vue.passport = {
|
|
|
98
|
+ local: (username, password, captcha) => {
|
|
|
99
|
+ return this.userService.login(username, password, captcha).then(res => {
|
|
|
100
|
+ if (res.code === 200) {
|
|
|
101
|
+ return Promise.all([this.initPurview(Vue, res.data), this.initConfig(Vue)]).then(() => {
|
|
|
102
|
+ if (res.data.needUpdate) {
|
|
|
103
|
+ Vue.$store.set('needUpdate', true);
|
|
|
104
|
+ }
|
|
|
105
|
+ return res.data;
|
94
|
});
|
106
|
});
|
|
|
107
|
+ }
|
|
|
108
|
+ return Promise.reject(res);
|
95
|
});
|
109
|
});
|
96
|
-
|
|
|
97
|
- Vue.passport = {
|
|
|
98
|
- local: (username, password, captcha) => {
|
|
|
99
|
- return this.userService.login(username, password, captcha).then((res) => {
|
|
|
100
|
- if (res.code === 200) {
|
|
|
101
|
- return Promise.all([
|
|
|
102
|
- this.initPurview(Vue, res.data),
|
|
|
103
|
- this.initConfig(Vue)
|
|
|
104
|
- ]).then(() => {
|
|
|
105
|
- if (res.data.needUpdate) {
|
|
|
106
|
- Vue.$store.set('needUpdate', true);
|
|
|
107
|
- }
|
|
|
108
|
- return res.data;
|
|
|
109
|
- });
|
|
|
110
|
- }
|
|
|
111
|
- return Promise.reject(res);
|
|
|
112
|
- });
|
|
|
113
|
- }
|
|
|
114
|
- };
|
|
|
115
|
- Vue.switchShop = shopsId => {
|
|
|
116
|
- Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(Vue.$user));
|
|
|
117
|
- Vue.$cookie.set('_sign', shopsId, {
|
|
|
118
|
- path: '/'
|
|
|
119
|
- });
|
|
|
120
|
- };
|
|
|
121
|
- Vue.logout = () => {
|
|
|
122
|
- _.each(Vue.$config.storeKeys, Vue.$store.remove);
|
|
|
123
|
- Vue.prop('user', void 0);
|
|
|
124
|
- Vue.prop('isLogin', void 0);
|
|
|
125
|
- Vue.prop('purviews', void 0);
|
|
|
126
|
- Vue.$store.remove('needUpdate');
|
|
|
127
|
- axios.post('/logout');
|
|
|
128
|
- Vue.$router.push('/login.html');
|
|
|
129
|
- };
|
|
|
130
|
- axios.defaults.validateStatus = (status) => {
|
|
|
131
|
- if (status === 401) {
|
|
|
132
|
- Vue.logout();
|
|
|
133
|
- return false;
|
|
|
134
|
- }
|
|
|
135
|
- if (status === 403) {
|
|
|
136
|
- Vue.$store.set('needUpdate', true);
|
|
|
137
|
- return false;
|
|
|
138
|
- }
|
|
|
139
|
- return true;
|
|
|
140
|
- };
|
|
|
141
|
- }
|
110
|
+ },
|
|
|
111
|
+ };
|
|
|
112
|
+ Vue.switchShop = shopsId => {
|
|
|
113
|
+ Vue.$store.set(Vue.$config.storeKeys.user, crypto.aesEncrypt(Vue.$user));
|
|
|
114
|
+ Vue.$cookie.set('_sign', shopsId, {
|
|
|
115
|
+ path: '/',
|
|
|
116
|
+ });
|
|
|
117
|
+ };
|
|
|
118
|
+ Vue.logout = () => {
|
|
|
119
|
+ _.each(Vue.$config.storeKeys, Vue.$store.remove);
|
|
|
120
|
+ Vue.prop('user', void 0);
|
|
|
121
|
+ Vue.prop('isLogin', void 0);
|
|
|
122
|
+ Vue.prop('purviews', void 0);
|
|
|
123
|
+ Vue.$store.remove('needUpdate');
|
|
|
124
|
+ axios.post('/logout');
|
|
|
125
|
+ Vue.$router.push('/login.html');
|
|
|
126
|
+ };
|
|
|
127
|
+ axios.defaults.validateStatus = status => {
|
|
|
128
|
+ if (status === 401) {
|
|
|
129
|
+ Vue.logout();
|
|
|
130
|
+ return false;
|
|
|
131
|
+ }
|
|
|
132
|
+ if (status === 403) {
|
|
|
133
|
+ Vue.$store.set('needUpdate', true);
|
|
|
134
|
+ return false;
|
|
|
135
|
+ }
|
|
|
136
|
+ return true;
|
|
|
137
|
+ };
|
|
|
138
|
+ },
|
142
|
}; |
139
|
}; |