Skip to content

Commit 49e26c3

Browse files
committed
✨ 添加 OAuth2 授权码流程相关页面
1 parent ebd3311 commit 49e26c3

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Diff for: src/permission.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ router.beforeEach((to, from, next) => {
2020
if (to.path === '/user/login') {
2121
next({ path: '/' })
2222
NProgress.done()
23+
}
24+
else if (to.path === '/oauth2/authorize') {
25+
next()
26+
NProgress.done()
2327
} else {
2428
if (store.getters.userRouters.length === 0) {
2529
store.dispatch('GenerateRoutes').then(() => {

Diff for: src/router/constantRouter.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const constantRouters = [
99
path: '/redirect/:path*',
1010
component: () => import(/* webpackChunkName: "user" */ '@/views/redirect/index')
1111
},
12+
{
13+
path: '/oauth2/authorize',
14+
component: () => import(/* webpackChunkName: "oauth2" */ '@/views/oauth2/OAuth2Authorize')
15+
},
1216
{
1317
path: '/user',
1418
component: UserLayout,

Diff for: src/views/oauth2/OAuth2Authorize.vue

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<script>
6+
import Vue from 'vue'
7+
import { ACCESS_TOKEN } from '@/store/storage-types'
8+
9+
export default {
10+
name: 'Oauth2Authorize',
11+
mounted () {
12+
debugger
13+
// 校验 token 是否有效,有效则直接 redirect, 如果无效则跳转到 login 页面,return_to 的参数也带过去
14+
// 登录页登录完成后,如果有 return_to 参数,则进行 redirect
15+
16+
// 获取当前地址栏携带的 return_to 参数
17+
const route = this.$route
18+
const returnTo = route.query.return_to
19+
20+
// 获取当前 token
21+
const accessToken = Vue.ls.get(ACCESS_TOKEN)
22+
23+
if (accessToken) {
24+
if (returnTo) {
25+
// 有 return_to 则且已登录则直接重定向到授权地址
26+
// return_to 参数必然会有后缀
27+
window.location.href = returnTo + '&access_token=' + accessToken
28+
} else {
29+
this.$router.push('/')
30+
}
31+
} else {
32+
this.$router.push({
33+
path: '/user/login',
34+
query: returnTo ? { return_to: returnTo } : {}
35+
})
36+
}
37+
}
38+
}
39+
</script>

0 commit comments

Comments
 (0)