feat: 整合giee一键登录

This commit is contained in:
xlsea 2025-03-14 10:30:34 +08:00
parent 770afc88c5
commit 441227d88e
3 changed files with 68 additions and 4 deletions

View File

@ -46,3 +46,11 @@ export function fetchRefreshToken(refreshToken: string) {
export function fetchCustomBackendError(code: string, msg: string) {
return request({ url: '/auth/error', params: { code, msg } });
}
export function fetchAuthLoginToken(token: string) {
return request<Api.Auth.LoginToken>({
url: '/auth/login/token',
method: 'post',
data: { token }
});
}

View File

@ -4,7 +4,7 @@ import { defineStore } from 'pinia';
import { useLoading } from '@sa/hooks';
import { SetupStoreId } from '@/enum';
import { useRouterPush } from '@/hooks/common/router';
import { fetchGetUserInfo, fetchLogin, fetchVersion } from '@/service/api';
import { fetchAuthLoginToken, fetchGetUserInfo, fetchLogin, fetchVersion } from '@/service/api';
import { localStg } from '@/utils/storage';
import { $t } from '@/locales';
import { roleTypeRecord } from '@/constants/business';
@ -97,6 +97,36 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
endLoading();
}
/**
* Login
*
* @param authToken token
* @param [redirect=true] Whether to redirect after login. Default is `true`
*/
async function loginByAuthToken(authToken: string, redirect = true) {
startLoading();
const { data: loginToken, error } = await fetchAuthLoginToken(authToken);
if (!error) {
const pass = await loginByToken(loginToken);
if (pass) {
await redirectFromLogin(redirect);
window.$notification?.success({
title: $t('page.login.common.loginSuccess'),
content: $t('page.login.common.welcomeBack', { userName: userInfo.userName }),
duration: 4500
});
}
} else {
resetStore();
}
endLoading();
}
async function loginByToken(loginToken: Api.Auth.LoginToken) {
// 1. stored in the localStorage, the later requests need it in headers
localStg.set('token', loginToken.token);
@ -190,6 +220,7 @@ export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
loginLoading,
resetStore,
login,
loginByAuthToken,
getUserInfo,
initUserInfo,
initAppVersion,

View File

@ -1,6 +1,7 @@
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { onMounted, reactive, ref } from 'vue';
import type { FormInst } from 'naive-ui';
import { useRoute } from 'vue-router';
import { md5 } from '@/utils/common';
import { $t } from '@/locales';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
@ -62,7 +63,7 @@ const codeFormRef = ref<FormInst>();
const onSuccess = async () => {
await codeFormRef.value?.validate();
const { code } = codeForm.value;
const codes: string[] = [];
const codes: string[] = import.meta.env.VITE_LOGIN_CODES;
if (!codes.includes(String(code))) {
window.$message?.warning('验证码错误');
return;
@ -73,6 +74,17 @@ const onSuccess = async () => {
const codePopoverStytle = {
padding: 0
};
function handleGiteeLogin() {
window.location.href = `${import.meta.env.VITE_GITEE_REDIRECT_URL}&t=${Date.now()}`;
}
onMounted(async () => {
const route = useRoute();
const token = route.query.token;
if (!token) return;
await authStore.loginByAuthToken(String(token));
});
</script>
<template>
@ -91,7 +103,7 @@ const codePopoverStytle = {
<NSpace vertical :size="24">
<NPopover :show="codeShow" row :style="codePopoverStytle">
<template #trigger>
<NButton type="primary" size="large" round block :loading="authStore.loginLoading" @click="validateCode">
<NButton type="primary" size="large" block :loading="authStore.loginLoading" @click="validateCode">
{{ $t('page.login.common.login') }}
</NButton>
</template>
@ -117,6 +129,19 @@ const codePopoverStytle = {
</NForm>
</NCard>
</NPopover>
<NButton
type="primary"
size="large"
class="h-42px"
block
:loading="authStore.loginLoading"
@click="handleGiteeLogin"
>
<template #icon>
<icon-simple-icons:gitee />
</template>
使用 Gitee 账号 Star 免密登录
</NButton>
</NSpace>
</NForm>
</template>