feat: enhance login handling and message creation

- Updated main.ts to export createMessage and queryLogin.
- Modified checkPluginLogin in login-handle.ts to return structured results.
- Refactored query.ts to adjust imports and export queryLogin.
- Cleaned up kv-login.ts by removing unused imports and improving method handling.
- Adjusted Vite configuration for deployment path changes.
- Simplified web.html by removing unnecessary import maps and scripts.
- Created web.ts to handle login success events and user redirection logic.
This commit is contained in:
2026-02-03 18:33:35 +08:00
parent 6f2be438c9
commit a7829ba5e6
10 changed files with 849 additions and 74 deletions

View File

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

View File

@@ -168,7 +168,14 @@ export const checkMpWechat = async () => {
closePage();
}
}
export const checkPluginLogin = async () => {
type CheckPluginLoginResult = {
code: number
data: {
redirectUrl: string
}
}
export const checkPluginLogin = async (): Promise<CheckPluginLoginResult> => {
const userCheck = 'user-check';
const url = new URL(location.href);
const redirect = url.searchParams.get('redirect');
@@ -185,11 +192,16 @@ export const checkPluginLogin = async () => {
setTimeout(() => {
window.open(newRedirectUrl.toString(), '_blank');
}, 2000);
return;
return { code: 200, data: { redirectUrl } }
}
// 刷新token失败登陆页自己跳转
}
console.log('checkKey', checkKey, redirectUrl);
return {
code: 400,
data: {
redirectUrl: ''
}
}
}
const isWechat = () => {
const ua = navigator.userAgent.toLowerCase();

View File

@@ -1,10 +1,11 @@
import { Query } from '@kevisual/query'
import { QueryLoginBrowser } from '@kevisual/query-login';
import { QueryLoginBrowser } from '@kevisual/api/query-login';
export const queryBase = new Query()
export const queryBase = new Query({})
export const query = new QueryLoginBrowser({
export const queryLogin = new QueryLoginBrowser({
query: queryBase,
})
export const query = queryLogin;

View File

@@ -1,6 +1,6 @@
import { render, html } from 'lit-html'
import { unsafeHTML } from 'lit-html/directives/unsafe-html.js'
import { loginHandle, checkWechat, getQrCode, checkMpQrCodeLogin, redirectHome } from '../modules/login-handle.ts'
import { loginHandle, checkWechat, getQrCode, checkMpQrCodeLogin } from '../modules/login-handle.ts'
import { setWxerwma } from '../modules/wx/ws-login.ts';
import { useCreateLoginQRCode } from '../modules/wx-mp/qr.ts';
import { eventEmitter } from '../modules/mitt.ts';
@@ -27,9 +27,9 @@ const icons: any = {
wxOpenSvg
}
const DefaultLoginMethods: LoginMethod[] = [
{ id: 'wechat', name: '微信登录', icon: 'wxmpSvg', appid: "wx9378885c8390e09b" },
{ id: 'password', name: '密码登录', icon: 'pwd' },
{ id: 'web', name: '网页登录', icon: 'web' },
{ id: 'wechat', name: '微信登录', icon: 'wxmpSvg', appid: "wx9378885c8390e09b" },
{ id: 'wechat-mp', name: '微信公众号', icon: 'wxOpenSvg', appid: WX_MP_APP_ID },
{ id: 'wechat-mp-ticket', name: '微信公众号', icon: 'wxOpenSvg' },
{ id: 'phone', name: '手机号登录', icon: 'phone' }
@@ -63,10 +63,13 @@ const getLoginMethodByDomain = (): LoginMethod[] => {
const getLoginMethod = (methods: LoginMethods[]): LoginMethod[] => {
return DefaultLoginMethods.filter(method => methods.includes(method.id))
}
export const findLoginMethod = (methodId: LoginMethods): LoginMethod | undefined => {
return DefaultLoginMethods.find(method => method.id === methodId);
}
class KvLogin extends HTMLElement {
private selectedMethod: LoginMethods = 'password'
private loginMethods: LoginMethod[] = getLoginMethodByDomain();
private loginMethods: LoginMethod[] = [];
setLoginMethods(methods: LoginMethod[]) {
this.loginMethods = methods
this.render()
@@ -77,23 +80,37 @@ class KvLogin extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' })
this.render()
this.bindEvents()
checkWechat()
const method = this.getAttribute('method');
let id = this.id;
if (!id) {
id = `${Math.random().toString(36).substring(2, 9)}`;
this.id = id;
}
let methodSetFlag = false;
if (method) {
const methods = method ? method.split(',') as LoginMethods[] : [];
if (methods.length > 0) {
const loginMethods = methods.filter(m => LoginMethods.includes(m));
let loginMethods: LoginMethod[] = [];
for (const m of methods) {
const finded = findLoginMethod(m);
if (finded?.id) {
loginMethods.push(finded);
}
}
if (loginMethods.length > 0) {
this.loginMethods = getLoginMethod(loginMethods)
this.selectedMethod = loginMethods[0]
return;
methodSetFlag = true;
this.loginMethods = loginMethods;
this.selectedMethod = loginMethods[0].id;
}
}
}
if (!methodSetFlag) {
this.loginMethods = getLoginMethodByDomain();
this.selectedMethod = this.loginMethods[0].id;
}
this.render()
}
#clearTimer: any = null;
private selectLoginMethod(methodId: LoginMethods) {
@@ -253,6 +270,14 @@ class KvLogin extends HTMLElement {
}
private renderWechatForm() {
let weixinLogin = document.querySelector('#weixinLogin');
if (!weixinLogin) {
weixinLogin = document.createElement('div');
weixinLogin.id = 'weixinLogin';
const id = this.id;
const host = document.querySelector(`#${id}`)!;
host.appendChild(weixinLogin);
}
return html`
<div class="wechat-login">
<slot></slot>