Skip to content

Commit

Permalink
perf(token): 根据nest后台调整token鉴权相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Sep 4, 2020
1 parent d5e0c88 commit bde12f6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NODE_ENV=development
VUE_APP_NAME=BoBo"s Vue Admin

#后端接口地址
VUE_APP_API_URL=http://xxx.xx.xx.xxx:8080/api/
VUE_APP_API_URL=http://localhost:3000/

#后台webSocket地址
VUE_APP_WS_URL=ws://xxx.xx.xx.xxx:8080/api/websocket
Expand Down
12 changes: 6 additions & 6 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ import axios from '@/plugins/axios';
// 用户登录
export function login(userInfo): any {
return axios({
url: '/user/login',
url: '/users/login',
method: 'post',
params: userInfo,
data: userInfo,
});
}

// 获取用户信息
export function getInfo() {
return axios({
url: '/user/userinfo',
url: '/users/userinfo',
method: 'post',
});
}

// 注销
export function logout() {
return axios({
url: '/user/logout',
url: '/users/logout',
method: 'post',
});
}

// 修改密码
export function update(data) {
return axios({
url: '/user/changepassword',
url: '/users/changepassword',
method: 'post',
params: data,
});
Expand All @@ -48,7 +48,7 @@ export function update(data) {

export function findByNames(names) {
return axios({
url: '/users/findByNames',
url: '/userss/findByNames',
method: 'post',
params: {
names,
Expand Down
13 changes: 2 additions & 11 deletions src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ router.beforeEach(async (to, from, next) => {
userName: userInfo.userName,
});
router.addRoutes(store.getters.addRouters); // 动态添加可访问路由表
// 新tab页打开工作流表单的时候不建立ws连接,避免造成多次重复连接.
if (!to.path.includes('task')) {
store.dispatch('initWebSocket', store.getters.name); // 建立ws连接
// 获取菜单待办数量
next({ ...to, replace: true } as any);
} else {
next({ ...to, replace: true } as any);
}
// store.dispatch('initWebSocket', store.getters.name); // 建立ws连接
next({ ...to, replace: true } as any);
} else {
// 此时虽然stroe中有动态路由,但是由于刷新页面导致router中变量已经释放,需要重新导入
// 由于vue-router的BUG:https://github.com/vuejs/vue-router/issues/1859
// 我只能给store做局部持久化
next();
}
} else if (whiteList.includes(to.path)) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ service.interceptors.request.use(
NProgress.start();
if (store.getters.token && store.getters.token !== 'null') {
// 让每个请求携带自定义token
config.headers.auth = store.getters.token;
config.headers.Authorization = store.getters.token;
}
if (encrypt) {
config.data = {
Expand Down
2 changes: 0 additions & 2 deletions src/store/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const app = {
config: {
// 密码长度
passwordLength: '',
// 密码有效时间(天)
passwordTime: '',
// 客户端超时时间(分钟)
clientTimeOut: '',
// 初始密码
Expand Down
10 changes: 1 addition & 9 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const user = {
const promise = await login(userInfo).then((res) => {
// 登录成功
if (res.code === 200) {
commit('SET_TOKEN', res.data.token);
commit('SET_TOKEN', `Bearer ${res.data.token}`);
// 关闭之前打开的标签
commit('DEL_ALL_VIEWS');
return res.data;
Expand Down Expand Up @@ -134,16 +134,8 @@ const user = {
commit('SET_ROLENAME', data.roleName);
commit('SET_DEPTID', data.deptID);
commit('SET_USERID', data.userID);
commit('SET_TEL', data.tel);
commit('SET_CANDIDATEUSER', data.candidateUser);
// 用户头像
commit('SET_PHOTO', data.photo);
// 用户签名
commit('SET_SIGNATURE', data.signature);
// 单位id
commit('SET_COMPANYID', data.companyID);
// 单位名
commit('SET_COMPANYNAME', data.companyName);
// 是否为admin账号
commit('SET_ISADMIN', data.userName === 'admin');

Expand Down
49 changes: 21 additions & 28 deletions src/views/public/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,49 +119,42 @@ export default class Login {
this.btnLoginIsLoading = false;
return null;
}
this.loginForm.passwordTime = this.config.passwordTime;
this.$store
.dispatch('setTokenByLogin', this.loginForm)
.then((res) => {
this.btnLoginIsLoading = false;
// 配置了首页
if (res.homepage) {
let view = null;
this.lodash.forEach(asyncRouterMap, (l1) => {
this.lodash.forEach(l1.children, (l2) => {
if (l2.name === res.homepage) {
view = `${l1.path}/${l2.path}`;
return false;
}
return true;
});
return !view;
});
this.$store.commit('SET_HOME_PAGE_PATH', view);
this.url = view;
} else {
this.url = '/';
}
// if (res.homepage) {
// let view = null;
// this.lodash.forEach(asyncRouterMap, (l1) => {
// this.lodash.forEach(l1.children, (l2) => {
// if (l2.name === res.homepage) {
// view = `${l1.path}/${l2.path}`;
// return false;
// }
// return true;
// });
// return !view;
// });
// this.$store.commit('SET_HOME_PAGE_PATH', view);
// this.url = view;
// } else {
// this.url = '/';
// }
// 进入内部页面
this.$router
.push({
path: this.url,
path: '/',
})
.catch((error) => {
console.info(error.message);
});
})
.catch((error) => {
if (+error.code === 800) {
this.$alert(error.message, '警告', {
confirmButtonText: '重新输入',
});
} else {
this.$alert(error.message, '警告', {
confirmButtonText: '重新输入',
});
}
this.$alert(error.message, '警告', {
confirmButtonText: '重新输入',
});
this.btnLoginIsLoading = false;
});
return null;
Expand Down

0 comments on commit bde12f6

Please sign in to comment.