fix: fix bugs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/kv-login",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"description": "",
|
||||
"main": "src/main.ts",
|
||||
"scripts": {
|
||||
@@ -8,7 +8,7 @@
|
||||
"build": "vite build --config vite-lib.config.ts",
|
||||
"build:test": "vite build",
|
||||
"prepub": "rm -rf ./dist && pnpm run build:test",
|
||||
"pub": "ev deploy ./dist -k kv-login-test -v 0.0.1 -u -y yes"
|
||||
"pub": "ev deploy ./dist -k kv-login-test -v 0.0.2 -u -y yes"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createMessage } from '../pages/kv-message.ts';
|
||||
import { WX_MP_APP_ID } from '../pages/kv-login.ts';
|
||||
export const message = createMessage();
|
||||
type LoginOpts = {
|
||||
loginMethod: 'password' | 'phone' | 'wechat' | 'wechat-mp',
|
||||
loginMethod: 'password' | 'phone' | 'wechat' | 'wechat-mp' | 'wechat-mp-ticket',
|
||||
data: any,
|
||||
el: HTMLElement
|
||||
}
|
||||
@@ -38,7 +38,11 @@ export const loginHandle = async (opts: LoginOpts) => {
|
||||
console.warn('未知的登录方式:', loginMethod)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用用户名和密码登录
|
||||
* @param data
|
||||
* @returns
|
||||
*/
|
||||
const loginByPassword = async (data: { username: string, password: string }) => {
|
||||
console.log('使用用户名密码登录:', data)
|
||||
let needLogin = true; // 这里可以根据实际情况决定是否需要登录, 只能判断密码登录和手机号登录
|
||||
@@ -185,4 +189,49 @@ const checkMpWechatInWx = async () => {
|
||||
|
||||
setTimeout(() => {
|
||||
checkMpWechat();
|
||||
}, 100);
|
||||
}, 100);
|
||||
|
||||
export const getQrCode = async () => {
|
||||
const res = await query.post({
|
||||
path: 'wx',
|
||||
key: 'get-qrcode-ticket'
|
||||
})
|
||||
if (res.code !== 200) {
|
||||
message.error('获取二维码失败');
|
||||
return null;
|
||||
}
|
||||
return res?.data as { ticket: string, url: string }
|
||||
}
|
||||
|
||||
export const checkMpQrCodeLogin = (ticket: string) => {
|
||||
let run = true;
|
||||
const fetchLoginStatus = async () => {
|
||||
const res = await query.post({
|
||||
path: 'wx',
|
||||
key: 'check-qrcode-login',
|
||||
payload: { ticket }
|
||||
})
|
||||
if (res.code === 200) {
|
||||
message.success('登录成功');
|
||||
clearTimeout(timer);
|
||||
redirectHome();
|
||||
} else {
|
||||
// message.error(res.message || '登录失败');
|
||||
if (res.code === 401) {
|
||||
console.log('等待扫码登录...');
|
||||
} else {
|
||||
console.log('扫码登录状态:', res);
|
||||
}
|
||||
if (run) {
|
||||
setTimeout(fetchLoginStatus, 2000);
|
||||
}
|
||||
}
|
||||
}
|
||||
const timer = setTimeout(fetchLoginStatus, 2000);
|
||||
const close = () => {
|
||||
console.log('停止检测扫码登录状态');
|
||||
clearTimeout(timer);
|
||||
run = false
|
||||
}
|
||||
return close;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { render, html } from 'lit-html'
|
||||
import { loginHandle, checkWechat } 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';
|
||||
export const WX_MP_APP_ID = "wxff97d569b1db16b6";
|
||||
@@ -13,9 +13,10 @@ const DefaultLoginMethods: LoginMethod[] = [
|
||||
{ id: 'password', name: '密码登录', icon: '🔒' },
|
||||
{ id: 'wechat', name: '微信登录', icon: '💬', appid: "wx9378885c8390e09b" },
|
||||
{ id: 'wechat-mp', name: '微信公众号登录', icon: '💬', appid: WX_MP_APP_ID },
|
||||
{ id: 'wechat-mp-ticket', name: '微信公众号登录', icon: '💬' },
|
||||
{ id: 'phone', name: '手机号登录', icon: '📱' }
|
||||
]
|
||||
type LoginMethods = 'password' | 'phone' | 'wechat' | 'wechat-mp'
|
||||
type LoginMethods = 'password' | 'phone' | 'wechat' | 'wechat-mp' | 'wechat-mp-ticket';
|
||||
const getLoginMethodByDomain = (): LoginMethod[] => {
|
||||
const domain = window.location.hostname
|
||||
let methods: LoginMethods[] = []
|
||||
@@ -24,10 +25,10 @@ const getLoginMethodByDomain = (): LoginMethod[] => {
|
||||
methods = ['password', 'wechat-mp']
|
||||
break;
|
||||
case 'kevisual.cn':
|
||||
methods = ['password', 'wechat']
|
||||
methods = ['password', 'wechat', 'wechat-mp-ticket']
|
||||
break;
|
||||
default:
|
||||
methods = ['password', 'phone', 'wechat', 'wechat-mp']
|
||||
methods = ['password', 'phone', 'wechat', 'wechat-mp', 'wechat-mp-ticket']
|
||||
break;
|
||||
}
|
||||
return DefaultLoginMethods.filter(method => methods.includes(method.id))
|
||||
@@ -224,6 +225,31 @@ class KvLogin extends HTMLElement {
|
||||
</div>
|
||||
`
|
||||
}
|
||||
private renderWechatMpTicketForm() {
|
||||
const that = this;
|
||||
setTimeout(async () => {
|
||||
const data = await getQrCode();
|
||||
if (!data) return;
|
||||
const imgEl = that.shadowRoot!.querySelector('.qrcode') as HTMLImageElement;
|
||||
if (data.url) {
|
||||
imgEl.src = data.url;
|
||||
// TODO: 轮询检测登录状态
|
||||
const clear = checkMpQrCodeLogin(data.ticket)
|
||||
// 当切换登录方式时,停止轮询
|
||||
that.#clearTimer = clear
|
||||
}
|
||||
}, 0)
|
||||
return html`
|
||||
<div class="wechat-login">
|
||||
<div class="qr-container">
|
||||
<div class="qr-placeholder">
|
||||
<img class="qrcode" width="300" height="300" data-appid="" data-size="200" data-ticket=""></img>
|
||||
<p class="qr-desc">请使用微信扫描二维码登录</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
}
|
||||
|
||||
private sendVerificationCode() {
|
||||
console.log('发送验证码')
|
||||
@@ -248,6 +274,8 @@ class KvLogin extends HTMLElement {
|
||||
return this.renderWechatForm()
|
||||
case 'wechat-mp':
|
||||
return this.renderWechatMpForm()
|
||||
case 'wechat-mp-ticket':
|
||||
return this.renderWechatMpTicketForm()
|
||||
default:
|
||||
return this.renderPasswordForm()
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
"@kevisual/query": "0.0.29",
|
||||
"@kevisual/query-login": "^0.0.6",
|
||||
"@kevisual/system-lib": "^0.0.22",
|
||||
"@kevisual/system-ui": "^0.0.3",
|
||||
"clsx": "^2.1.1",
|
||||
"dayjs": "^1.11.19",
|
||||
"lodash-es": "^4.17.21",
|
||||
|
||||
Reference in New Issue
Block a user