Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-02-24 03:57:07 +08:00
parent 2759ac42aa
commit 9107aa3d7e
8 changed files with 326 additions and 1062 deletions

View File

@@ -4,8 +4,8 @@ import { createMessage } from './pages/kv-message';
export { loginEmitter } from './pages/kv-login'
export { checkPluginLogin, clearCode } from './modules/login-handle';
export { checkPluginLogin, clearCode, redirectHome } from './modules/login-handle';
export { createMessage }
export { queryLogin } from './modules/query.ts';
export { queryLogin } from './modules/query.ts';

View File

@@ -10,22 +10,28 @@ type LoginOpts = {
data: any,
el: HTMLElement
}
/**
* 登录成功后重定向到首页
*/
export const redirectHome = () => {
const href = window.location.href;
const url = new URL(href);
const redirect = url.searchParams.get('redirect');
if (redirect) {
const href = decodeURIComponent(redirect);
window.open(href, '_self');
setTimeout(() => {
const href = decodeURIComponent(redirect);
window.open(href, '_self');
}, 2000);
return;
}
}
/**
* 登录成功后重定向到首页
*/
export const logginSuccess = () => {
// 从url上清除 code 参数, 清除 state 参数
emit({ type: 'login-success', data: {} });
setTimeout(() => {
clearCode();
}, 1500);
}, 2000);
}
export const loginHandle = async (opts: LoginOpts) => {
const { loginMethod, data, el } = opts
@@ -59,7 +65,7 @@ const loginByWeb = async (data: {}) => {
window.open(res.url, '_blank');
const status = await login.pollLoginStatus(res);
if (status) {
redirectHome()
logginSuccess()
} else {
message.error('网页登录失败,请重试')
}
@@ -86,9 +92,10 @@ const loginByPassword = async (data: { username: string, password: string }) =>
}
}
if (!needLogin) {
redirectHome()
logginSuccess()
return;
}
localStorage.removeItem('token')
const res = await query.login({
username: data.username,
password: data.password
@@ -96,7 +103,7 @@ const loginByPassword = async (data: { username: string, password: string }) =>
if (res.code === 200) {
console.log('登录成功')
message.success('登录成功')
redirectHome()
logginSuccess()
} else {
message.error(`登录失败: ${res.message}`)
}
@@ -136,7 +143,7 @@ export const checkWechat = async () => {
const res = await query.loginByWechat({ code });
if (res.code === 200) {
message.success('登录成功');
redirectHome();
logginSuccess();
} else {
message.error(res.message || '登录失败');
clearCode();
@@ -277,7 +284,7 @@ export const checkMpQrCodeLogin = (ticket: string) => {
if (res.code === 200) {
message.success('登录成功');
clearTimeout(timer);
redirectHome();
logginSuccess();
} else {
// message.error(res.message || '登录失败');
if (res.code === 401) {

View File

@@ -6,6 +6,10 @@ export const queryBase = new Query({})
export const queryLogin = new QueryLoginBrowser({
query: queryBase,
isBrowser: true,
onLoad: () => {
console.log('加载完成');
}
})
export const query = queryLogin;

View File

@@ -44,10 +44,14 @@ type LoginMethods = 'password' | 'web' | 'phone' | 'wechat' | 'wechat-mp' | 'wec
const getLoginMethodByDomain = (): LoginMethod[] => {
let domain = window.location.host
let methods: LoginMethods[] = []
const has51 = domain.includes('localhost') && (domain.endsWith('51515') || domain.endsWith('51015'));
const isCnb = domain.includes('cnb.run');
const has51 = domain.includes('localhost') || domain.endsWith('51515');
if (has51) {
domain = 'localhost'
}
if (isCnb) {
domain = 'cnb.run'
}
switch (domain) {
case 'kevisual.xiongxiao.me':
methods = ['password', 'wechat-mp']
@@ -58,6 +62,9 @@ const getLoginMethodByDomain = (): LoginMethod[] => {
case 'localhost':
methods = ['password', 'web']
break
case 'cnb.run':
methods = ['password', 'web']
break;
default:
methods = ['password', 'web', 'phone', 'wechat', 'wechat-mp', 'wechat-mp-ticket']
break;