Skip to content

Commit 472cbb6

Browse files
committed
add easy-mock
1 parent c528bd8 commit 472cbb6

File tree

11 files changed

+89
-156
lines changed

11 files changed

+89
-156
lines changed

config/dev.env.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ var merge = require('webpack-merge')
22
var prodEnv = require('./prod.env')
33

44
module.exports = merge(prodEnv, {
5-
NODE_ENV: '"development"'
5+
NODE_ENV: '"development"',
6+
BASE_API: '"https://easy-mock.com/mock/5950a2419adc231f356a6636/vue-admin"',
67
})

config/prod.env.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2-
NODE_ENV: '"production"'
2+
NODE_ENV: '"production"',
3+
BASE_API: '"https://easy-mock.com/mock/5950a2419adc231f356a6636/vue-admin"',
34
}

src/api/login.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import fetch from '@/utils/fetch';
22

3-
export function loginByEmail(email, password) {
4-
const data = {
5-
email,
6-
password
7-
};
3+
export function login(email, password) {
84
return fetch({
9-
url: '/login/loginbyemail',
5+
url: '/user/login',
106
method: 'post',
11-
data
12-
});
13-
}
14-
15-
export function logout() {
16-
return fetch({
17-
url: '/login/logout',
18-
method: 'post'
7+
data: {
8+
email,
9+
password
10+
}
1911
});
2012
}
2113

@@ -27,3 +19,12 @@ export function getInfo(token) {
2719
});
2820
}
2921

22+
export function logout() {
23+
return fetch({
24+
url: '/user/logout',
25+
method: 'post'
26+
});
27+
}
28+
29+
30+

src/main.js

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,41 @@ Vue.config.productionTip = false
1515

1616
Vue.use(ElementUI);
1717

18-
router.afterEach(() => {
19-
NProgress.done(); // 结束Progress
20-
});
2118

19+
const whiteList = ['/login'];// 不重定向白名单
20+
router.beforeEach((to, from, next) => {
21+
NProgress.start(); // 开启Progress
22+
if (store.getters.token) { // 判断是否有token
23+
if (to.path === '/login') {
24+
next({ path: '/' });
25+
} else {
26+
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
27+
store.dispatch('GetInfo').then(res => { // 拉取user_info
28+
const roles = res.data.role;
29+
store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
30+
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
31+
next(to.path); // hack方法 确保addRoutes已完成
32+
})
33+
}).catch(err => {
34+
console.log(err);
35+
});
36+
} else {
37+
next();
38+
}
39+
}
40+
} else {
41+
if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
42+
next()
43+
} else {
44+
next('/login'); // 否则全部重定向到登录页
45+
NProgress.done();
46+
}
47+
}
48+
});
2249

23-
// const whiteList = ['/login', '/authredirect', '/reset', '/sendpwd'];// 不重定向白名单
24-
// router.beforeEach((to, from, next) => {
25-
// NProgress.start(); // 开启Progress
26-
// if (store.getters.token) { // 判断是否有token
27-
// if (to.path === '/login') {
28-
// next({ path: '/' });
29-
// } else {
30-
// if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
31-
// store.dispatch('GetInfo').then(res => { // 拉取user_info
32-
// const roles = res.data.role;
33-
// store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
34-
// router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
35-
// next(to.path); // hack方法 确保addRoutes已完成
36-
// })
37-
// }).catch(err => {
38-
// console.log(err);
39-
// });
40-
// } else {
41-
// // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
42-
// if (hasPermission(store.getters.roles, to.meta.role)) {
43-
// next();//
44-
// } else {
45-
// next({ path: '/401', query: { noGoBack: true } });
46-
// }
47-
// // 可删 ↑
48-
// }
49-
// }
50-
// } else {
51-
// if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
52-
// next()
53-
// } else {
54-
// next('/login'); // 否则全部重定向到登录页
55-
// NProgress.done(); // 在hash模式下 改变手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
56-
// }
57-
// }
58-
// });
50+
router.afterEach(() => {
51+
NProgress.done();
52+
});
5953

6054
new Vue({
6155
el: '#app',

src/router/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Vue.use(Router);
2828
* meta : { role: ['admin'] } will control the page role
2929
**/
3030
export const constantRouterMap = [
31+
{ path: '/login', component: Login, hidden: true },
3132
{ path: '/404', component: Err404, hidden: true },
3233
{
3334
path: '/',

src/store/modules/user.js

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
1-
import { loginByEmail, logout, getInfo } from '@/api/login';
1+
import { login, logout, getInfo } from '@/api/login';
22
import Cookies from 'js-cookie';
33

44
const user = {
55
state: {
6-
user: '',
7-
status: '',
8-
email: '',
9-
code: '',
10-
uid: undefined,
11-
auth_type: '',
126
token: Cookies.get('Admin-Token'),
137
name: '',
148
avatar: '',
15-
introduction: '',
16-
roles: [],
17-
setting: {
18-
articlePlatform: []
19-
}
9+
roles: []
2010
},
2111

2212
mutations: {
23-
SET_AUTH_TYPE: (state, type) => {
24-
state.auth_type = type;
25-
},
26-
SET_CODE: (state, code) => {
27-
state.code = code;
28-
},
2913
SET_TOKEN: (state, token) => {
3014
state.token = token;
3115
},
32-
SET_UID: (state, uid) => {
33-
state.uid = uid;
34-
},
35-
SET_EMAIL: (state, email) => {
36-
state.email = email;
37-
},
38-
SET_INTRODUCTION: (state, introduction) => {
39-
state.introduction = introduction;
40-
},
41-
SET_SETTING: (state, setting) => {
42-
state.setting = setting;
43-
},
44-
SET_STATUS: (state, status) => {
45-
state.status = status;
46-
},
4716
SET_NAME: (state, name) => {
4817
state.name = name;
4918
},
@@ -52,25 +21,18 @@ const user = {
5221
},
5322
SET_ROLES: (state, roles) => {
5423
state.roles = roles;
55-
},
56-
LOGIN_SUCCESS: () => {
57-
console.log('login success')
58-
},
59-
LOGOUT_USER: state => {
60-
state.user = '';
6124
}
6225
},
6326

6427
actions: {
65-
// 邮箱登录
66-
LoginByEmail({ commit }, userInfo) {
28+
// 登录
29+
Login({ commit }, userInfo) {
6730
const email = userInfo.email.trim();
6831
return new Promise((resolve, reject) => {
69-
loginByEmail(email, userInfo.password).then(response => {
32+
login(email, userInfo.password).then(response => {
7033
const data = response.data;
71-
Cookies.set('Admin-Token', response.data.token);
34+
Cookies.set('Admin-Token', data.token);
7235
commit('SET_TOKEN', data.token);
73-
commit('SET_EMAIL', email);
7436
resolve();
7537
}).catch(error => {
7638
reject(error);
@@ -87,8 +49,6 @@ const user = {
8749
commit('SET_ROLES', data.role);
8850
commit('SET_NAME', data.name);
8951
commit('SET_AVATAR', data.avatar);
90-
commit('SET_UID', data.uid);
91-
commit('SET_INTRODUCTION', data.introduction);
9252
resolve(response);
9353
}).catch(error => {
9454
reject(error);

src/utils/fetch.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,35 @@ service.interceptors.request.use(config => {
2424

2525
// respone拦截器
2626
service.interceptors.response.use(
27-
response => response,
27+
response => {
2828
/**
2929
* 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页
3030
* 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
3131
*/
32-
// const res = response.data;
33-
// if (res.code !== 20000) {
34-
// Message({
35-
// message: res.message,
36-
// type: 'error',
37-
// duration: 5 * 1000
38-
// });
39-
// // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
40-
// if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
41-
// MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
42-
// confirmButtonText: '重新登录',
43-
// cancelButtonText: '取消',
44-
// type: 'warning'
45-
// }).then(() => {
46-
// store.dispatch('FedLogOut').then(() => {
47-
// location.reload();// 为了重新实例化vue-router对象 避免bug
48-
// });
49-
// })
50-
// }
51-
// return Promise.reject(error);
52-
// } else {
53-
// return response.data;
54-
// }
32+
const res = response.data;
33+
if (res.code !== 20000) {
34+
Message({
35+
message: res.data,
36+
type: 'error',
37+
duration: 5 * 1000
38+
});
39+
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
40+
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
41+
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
42+
confirmButtonText: '重新登录',
43+
cancelButtonText: '取消',
44+
type: 'warning'
45+
}).then(() => {
46+
store.dispatch('FedLogOut').then(() => {
47+
location.reload();// 为了重新实例化vue-router对象 避免bug
48+
});
49+
})
50+
}
51+
return Promise.reject(error);
52+
} else {
53+
return response.data;
54+
}
55+
},
5556
error => {
5657
console.log('err' + error);// for debug
5758
Message({

src/views/404.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import img_404_cloud from '@/assets/404_images/404_cloud.png'
2424
2525
export default {
26-
data: {
27-
return: {
26+
data() {
27+
return {
2828
img_404,
2929
img_404_cloud
3030
}

src/views/dashboard/default/index.vue

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/views/dashboard/index.vue

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
<template>
22
<div class="dashboard-container">
3-
<component v-bind:is="currentRole"> </component>
3+
{{name}}
44
</div>
55
</template>
66

77
<script>
88
import { mapGetters } from 'vuex';
9-
import DefaultDashboard from './default/index';
109
export default {
1110
name: 'dashboard',
12-
components: { DefaultDashboard },
13-
data() {
14-
return {
15-
currentRole: 'DefaultDashboard'
16-
}
17-
},
1811
computed: {
1912
...mapGetters([
2013
'name',
21-
'avatar',
22-
'email',
23-
'introduction',
2414
'roles'
2515
])
26-
},
27-
created() {
28-
if (this.roles.indexOf('admin') >= 0) {
29-
return;
30-
}
31-
// const isEditor = this.roles.some(v => v.indexOf('editor') >= 0)
32-
// if (!isEditor) {
33-
// this.currentRole = 'DefaultDashboard';
34-
// }
35-
this.currentRole = 'DefaultDashboard';
3616
}
3717
}
3818
</script>

src/views/login/index.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@
7676
this.$refs.loginForm.validate(valid => {
7777
if (valid) {
7878
this.loading = true;
79-
this.$store.dispatch('LoginByEmail', this.loginForm).then(() => {
79+
this.$store.dispatch('Login', this.loginForm).then(() => {
8080
this.loading = false;
8181
this.$router.push({ path: '/' });
8282
// this.showDialog = true;
83-
}).catch(err => {
84-
this.$message.error(err);
83+
}).catch(() => {
8584
this.loading = false;
8685
});
8786
} else {

0 commit comments

Comments
 (0)