From bde12f64b18e95037b47f9d9167119d9263c4652 Mon Sep 17 00:00:00 2001 From: BoBoooooo <17746714@qq.com> Date: Fri, 4 Sep 2020 11:27:08 +0800 Subject: [PATCH] =?UTF-8?q?perf(token):=20=E6=A0=B9=E6=8D=AEnest=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E8=B0=83=E6=95=B4token=E9=89=B4=E6=9D=83=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.dev | 2 +- src/api/user.ts | 12 +++++----- src/permission.ts | 13 ++-------- src/plugins/axios.ts | 2 +- src/store/modules/app.ts | 2 -- src/store/modules/user.ts | 10 +------- src/views/public/Login.vue | 49 ++++++++++++++++---------------------- 7 files changed, 32 insertions(+), 58 deletions(-) diff --git a/.env.dev b/.env.dev index b792527..7977bb4 100644 --- a/.env.dev +++ b/.env.dev @@ -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 diff --git a/src/api/user.ts b/src/api/user.ts index ccce34f..0a21de9 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -9,16 +9,16 @@ 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', }); } @@ -26,7 +26,7 @@ export function getInfo() { // 注销 export function logout() { return axios({ - url: '/user/logout', + url: '/users/logout', method: 'post', }); } @@ -34,7 +34,7 @@ export function logout() { // 修改密码 export function update(data) { return axios({ - url: '/user/changepassword', + url: '/users/changepassword', method: 'post', params: data, }); @@ -48,7 +48,7 @@ export function update(data) { export function findByNames(names) { return axios({ - url: '/users/findByNames', + url: '/userss/findByNames', method: 'post', params: { names, diff --git a/src/permission.ts b/src/permission.ts index 3e53f4c..8683a7b 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -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)) { diff --git a/src/plugins/axios.ts b/src/plugins/axios.ts index 3a4764d..f02fc5f 100644 --- a/src/plugins/axios.ts +++ b/src/plugins/axios.ts @@ -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 = { diff --git a/src/store/modules/app.ts b/src/store/modules/app.ts index f3d7d22..567264b 100644 --- a/src/store/modules/app.ts +++ b/src/store/modules/app.ts @@ -17,8 +17,6 @@ const app = { config: { // 密码长度 passwordLength: '', - // 密码有效时间(天) - passwordTime: '', // 客户端超时时间(分钟) clientTimeOut: '', // 初始密码 diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index d887b59..22a2b11 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -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; @@ -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'); diff --git a/src/views/public/Login.vue b/src/views/public/Login.vue index 4c384e8..8ea425d 100644 --- a/src/views/public/Login.vue +++ b/src/views/public/Login.vue @@ -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;