Skip to content

Commit e22363d

Browse files
author
puhui999
committed
完善SSO单点登录
1 parent 2d019f1 commit e22363d

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

src/api/login/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ export const reqCheckApi = (data) => {
7676
}
7777

7878
// ========== OAUTH 2.0 相关 ==========
79-
79+
export type scopesType = string[]
80+
export interface paramsType {
81+
responseType: string
82+
clientId: string
83+
redirectUri: string
84+
state: string
85+
scopes: scopesType
86+
}
8087
export const getAuthorize = (clientId) => {
8188
return request.get({ url: '/system/oauth2/authorize?clientId=' + clientId })
8289
}
@@ -87,8 +94,8 @@ export function authorize(
8794
redirectUri: string,
8895
state: string,
8996
autoApprove: boolean,
90-
checkedScopes: any,
91-
uncheckedScopes: any
97+
checkedScopes: scopesType,
98+
uncheckedScopes: scopesType
9299
) {
93100
// 构建 scopes
94101
const scopes = {}

src/views/Login/Login.vue

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
>
1010
<!-- 左上角的 logo + 系统标题 -->
1111
<div class="flex items-center relative text-white">
12-
<img src="@/assets/imgs/logo.png" alt="" class="w-48px h-48px mr-10px" />
12+
<img alt="" class="w-48px h-48px mr-10px" src="@/assets/imgs/logo.png" />
1313
<span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
1414
</div>
1515
<!-- 左边的背景图 + 欢迎语 -->
1616
<div class="flex justify-center items-center h-[calc(100%-60px)]">
1717
<TransitionGroup
1818
appear
19-
tag="div"
2019
enter-active-class="animate__animated animate__bounceInLeft"
20+
tag="div"
2121
>
22-
<img src="@/assets/svgs/login-box-bg.svg" key="1" alt="" class="w-350px" />
23-
<div class="text-3xl text-white" key="2">{{ t('login.welcome') }}</div>
24-
<div class="mt-5 font-normal text-white text-14px" key="3">
22+
<img key="1" alt="" class="w-350px" src="@/assets/svgs/login-box-bg.svg" />
23+
<div key="2" class="text-3xl text-white">{{ t('login.welcome') }}</div>
24+
<div key="3" class="mt-5 font-normal text-white text-14px">
2525
{{ t('login.message') }}
2626
</div>
2727
</TransitionGroup>
@@ -31,7 +31,7 @@
3131
<!-- 右上角的主题、语言选择 -->
3232
<div class="flex justify-between items-center text-white @2xl:justify-end @xl:justify-end">
3333
<div class="flex items-center @2xl:hidden @xl:hidden">
34-
<img src="@/assets/imgs/logo.png" alt="" class="w-48px h-48px mr-10px" />
34+
<img alt="" class="w-48px h-48px mr-10px" src="@/assets/imgs/logo.png" />
3535
<span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
3636
</div>
3737
<div class="flex justify-end items-center space-x-10px">
@@ -52,18 +52,15 @@
5252
<QrCodeForm class="p-20px h-auto m-auto <xl:(rounded-3xl light:bg-white)" />
5353
<!-- 注册 -->
5454
<RegisterForm class="p-20px h-auto m-auto <xl:(rounded-3xl light:bg-white)" />
55-
<!-- 三方登录 v-if触发组件初始化 -->
56-
<SSOLoginVue
57-
v-if="isSSO"
58-
class="p-20px h-auto m-auto <xl:(rounded-3xl light:bg-white)"
59-
/>
55+
<!-- 三方登录 -->
56+
<SSOLoginVue class="p-20px h-auto m-auto <xl:(rounded-3xl light:bg-white)" />
6057
</div>
6158
</Transition>
6259
</div>
6360
</div>
6461
</div>
6562
</template>
66-
<script setup lang="ts">
63+
<script lang="ts" setup>
6764
import { underlineToHump } from '@/utils'
6865
6966
import { useDesign } from '@/hooks/web/useDesign'
@@ -72,23 +69,11 @@ import { ThemeSwitch } from '@/layout/components/ThemeSwitch'
7269
import { LocaleDropdown } from '@/layout/components/LocaleDropdown'
7370
7471
import { LoginForm, MobileForm, QrCodeForm, RegisterForm, SSOLoginVue } from './components'
75-
import { RouteLocationNormalizedLoaded } from 'vue-router'
7672
7773
const { t } = useI18n()
7874
const appStore = useAppStore()
7975
const { getPrefixCls } = useDesign()
8076
const prefixCls = getPrefixCls('login')
81-
// =======SSO======
82-
const isSSO = ref(false)
83-
const router = useRouter()
84-
// 监听当前路由
85-
watch(
86-
() => router.currentRoute.value,
87-
(route: RouteLocationNormalizedLoaded) => {
88-
if (route.name === 'SSOLogin') isSSO.value = true
89-
},
90-
{ immediate: true }
91-
)
9277
</script>
9378

9479
<style lang="scss" scoped>

src/views/Login/components/LoginForm.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ const doSocialLogin = async (type: number) => {
279279
watch(
280280
() => currentRoute.value,
281281
(route: RouteLocationNormalizedLoaded) => {
282-
if (route.name === 'SSOLogin') setLoginState(LoginStateEnum.SSO)
283282
redirect.value = route?.query?.redirect as string
284283
},
285284
{

src/views/Login/components/SSOLogin.vue

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<!-- 表单 -->
3-
<div class="form-cont">
3+
<div v-show="getShow" class="form-cont">
4+
<!-- <LoginFormTitle style="width: 100%" />-->
45
<el-tabs class="form" style="float: none" value="uname">
56
<el-tab-pane :label="'三方授权(' + client.name + ')'" name="uname" />
67
</el-tabs>
@@ -12,8 +13,8 @@
1213
<el-checkbox-group v-model="loginForm.scopes">
1314
<el-checkbox
1415
v-for="scope in params.scopes"
15-
:label="scope"
1616
:key="scope"
17+
:label="scope"
1718
style="display: block; margin-bottom: -10px"
1819
>{{ formatScope(scope) }}
1920
</el-checkbox>
@@ -24,8 +25,8 @@
2425
<el-button
2526
:loading="loading"
2627
size="small"
27-
type="primary"
2828
style="width: 60%"
29+
type="primary"
2930
@click.prevent="handleAuthorize(true)"
3031
>
3132
<span v-if="!loading">同意授权</span>
@@ -40,19 +41,15 @@
4041
</div>
4142
</template>
4243
<script lang="ts" name="SSOLogin" setup>
43-
import { authorize, getAuthorize } from '@/api/login'
44+
// import LoginFormTitle from './LoginFormTitle.vue' // TODO 艿艿你看看要不要这个表头
45+
import { authorize, getAuthorize, paramsType, scopesType } from '@/api/login'
46+
import { LoginStateEnum, useLoginState } from './useLogin'
47+
import type { RouteLocationNormalizedLoaded } from 'vue-router'
4448
4549
const { t } = useI18n()
4650
const ssoForm = ref() // 表单Ref
47-
48-
type scopesType = string[]
49-
interface paramsType {
50-
responseType: string
51-
clientId: string
52-
redirectUri: string
53-
state: string
54-
scopes: scopesType
55-
}
51+
const { getLoginState, setLoginState } = useLoginState()
52+
const getShow = computed(() => unref(getLoginState) === LoginStateEnum.SSO)
5653
const loginForm = reactive<{ scopes: scopesType }>({
5754
scopes: [] // 已选中的 scope 数组
5855
})
@@ -116,6 +113,7 @@ const doAuthorize = (autoApprove, checkedScopes, uncheckedScopes) => {
116113
const formatScope = (scope) => {
117114
// 格式化 scope 授权范围,方便用户理解。
118115
// 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
116+
// TODO 这个之做了中文部分
119117
return t(`login.sso.${scope}`)
120118
}
121119
const route = useRoute()
@@ -146,7 +144,6 @@ const init = () => {
146144
147145
// 获取授权页的基本信息
148146
getAuthorize(params.clientId).then((res) => {
149-
console.log(res)
150147
client.value = res.client
151148
// 解析 scope
152149
let scopes
@@ -173,5 +170,18 @@ const init = () => {
173170
}
174171
})
175172
}
173+
// =======SSO======
174+
const { currentRoute } = useRouter()
175+
// 监听当前路由
176+
watch(
177+
() => currentRoute.value,
178+
(route: RouteLocationNormalizedLoaded) => {
179+
if (route.name === 'SSOLogin') {
180+
setLoginState(LoginStateEnum.SSO)
181+
init()
182+
}
183+
},
184+
{ immediate: true }
185+
)
176186
init()
177187
</script>

0 commit comments

Comments
 (0)