From 36a09f9c3d1547a40ba18d7556a9acfedc40c922 Mon Sep 17 00:00:00 2001 From: liyang <1711467488@qq.com> Date: Sat, 17 Nov 2018 22:31:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E9=99=86=E8=B7=B3=E8=BD=AC=E8=BD=AC?= =?UTF-8?q?=E4=BA=A4=E7=99=BB=E9=99=86=E9=A1=B5=E9=9D=A2=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: cfd1438def08f0c214df214e42e8fa54433b29ed [formerly cfd1438def08f0c214df214e42e8fa54433b29ed [formerly cfd1438def08f0c214df214e42e8fa54433b29ed [formerly cfd1438def08f0c214df214e42e8fa54433b29ed [formerly 850c00a248f8dace4cdb42cdda77dfbf528b8e15 [formerly e43dc23c06b5e7addc34b225e0d7512afa4cbf88]]]]] Former-commit-id: b7a006b238aee7cfd6bbcd587fc1826aa53f9ecc Former-commit-id: bba6b8fb7a93cb3363d4109490ea54a3a0066f4c Former-commit-id: bbb7c9d3c44357c023c7194b1c85f529aa048fbc [formerly fa2a2a5a267fe5a96c5e82cc0c1650eb024da71b] Former-commit-id: 383bf4b86f457cff9830e54bf43340e42a2045ab Former-commit-id: 348c1d718cd6c5441c2b721fd6efd30a8f231838 Former-commit-id: 4c9476c29fb68d59150d2b0992dc20bbd2d588ad Former-commit-id: 7bd01ddd895e182be3d7a24267c4721376225bd3 Former-commit-id: 70079fa65f5107867d7bb5b460f3d24563b61e9c --- src/pages/login/page.vue | 10 ++++ src/router/index.js | 9 +-- src/store/modules/d2admin/modules/account.js | 60 +++++++++----------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/pages/login/page.vue b/src/pages/login/page.vue index ad2bdc7a..ea7cc85b 100644 --- a/src/pages/login/page.vue +++ b/src/pages/login/page.vue @@ -141,6 +141,16 @@ export default { username: this.formLogin.username, password: this.formLogin.password }) + .then(() => { + const redirect = this.$route.query.redirect + if (redirect) { + // 重定向到指定的页面 + this.$router.replace(redirect) + } else { + // 重定向到开始路径 + this.$router.replace('/') + } + }) } else { // 登录表单校验失败 this.$message.error('表单校验失败') diff --git a/src/router/index.js b/src/router/index.js index 241e6215..496b9504 100755 --- a/src/router/index.js +++ b/src/router/index.js @@ -36,12 +36,13 @@ router.beforeEach((to, from, next) => { if (token && token !== 'undefined') { next() } else { - // 将当前预计打开的页面完整地址临时存储 登录后继续跳转 - // 这个 cookie(redirect) 会在登录后自动删除 - util.cookies.set('redirect', to.fullPath) // 没有登录的时候跳转到登录界面 + // 携带上登陆成功之后需要跳转的页面完整路径 next({ - name: 'login' + name: 'login', + query: { + redirect: to.fullPath + } }) } } else { diff --git a/src/store/modules/d2admin/modules/account.js b/src/store/modules/d2admin/modules/account.js index b999e7a9..40f20df8 100644 --- a/src/store/modules/d2admin/modules/account.js +++ b/src/store/modules/d2admin/modules/account.js @@ -15,40 +15,36 @@ export default { login ({ dispatch }, { vm, username, - password, - route = { - path: '/' - } + password }) { - // 开始请求登录接口 - AccountLogin({ - username, - password + return new Promise((resolve, reject) => { + // 开始请求登录接口 + AccountLogin({ + username, + password + }) + .then(async res => { + // 设置 cookie 一定要存 uuid 和 token 两个 cookie + // 整个系统依赖这两个数据进行校验和存储 + // uuid 是用户身份唯一标识 用户注册的时候确定 并且不可改变 不可重复 + // token 代表用户当前登录状态 建议在网络请求中携带 token + // 如有必要 token 需要定时更新,默认保存一天 + util.cookies.set('uuid', res.uuid) + util.cookies.set('token', res.token) + // 设置 vuex 用户信息 + await dispatch('d2admin/user/set', { + name: res.name + }, { root: true }) + // 用户登录后从持久化数据加载一系列的设置 + await dispatch('load') + // 结束 + resolve() + }) + .catch(err => { + console.log('err: ', err) + reject(err) + }) }) - .then(async res => { - // 设置 cookie 一定要存 uuid 和 token 两个 cookie - // 整个系统依赖这两个数据进行校验和存储 - // uuid 是用户身份唯一标识 用户注册的时候确定 并且不可改变 不可重复 - // token 代表用户当前登录状态 建议在网络请求中携带 token - // 如有必要 token 需要定时更新,默认保存一天 - util.cookies.set('uuid', res.uuid) - util.cookies.set('token', res.token) - // 设置 vuex 用户信息 - await dispatch('d2admin/user/set', { - name: res.name - }, { root: true }) - // 用户登录后从持久化数据加载一系列的设置 - await dispatch('load') - // 更新路由 尝试去获取 cookie 里保存的需要重定向的页面完整地址 - const path = util.cookies.get('redirect') - // 根据是否存有重定向页面判断如何重定向 - vm.$router.replace(path ? { path } : route) - // 删除 cookie 中保存的重定向页面 - util.cookies.remove('redirect') - }) - .catch(err => { - console.log('err: ', err) - }) }, /** * @description 注销用户并返回登录页面