This commit is contained in:
2025-12-28 15:45:35 +08:00
parent 2c74755c0d
commit 875195f6a2
3 changed files with 83 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/api",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"main": "mod.ts",
"scripts": {
@@ -18,11 +18,12 @@
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT",
"packageManager": "pnpm@10.26.1",
"packageManager": "pnpm@10.26.2",
"type": "module",
"devDependencies": {
"@kevisual/cache": "^0.0.4",
"@kevisual/query": "^0.0.33",
"@kevisual/router": "^0.0.41",
"@kevisual/router": "^0.0.51",
"@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.21",
"@types/bun": "^1.3.5",
@@ -31,6 +32,7 @@
"fast-glob": "^3.3.3"
},
"dependencies": {
"@kevisual/load": "^0.0.6",
"es-toolkit": "^1.43.0",
"nanoid": "^5.1.6"
}

32
pnpm-lock.yaml generated
View File

@@ -8,9 +8,9 @@ importers:
.:
dependencies:
dotenv:
specifier: ^17.2.3
version: 17.2.3
'@kevisual/load':
specifier: ^0.0.6
version: 0.0.6
es-toolkit:
specifier: ^1.43.0
version: 1.43.0
@@ -18,6 +18,9 @@ importers:
specifier: ^5.1.6
version: 5.1.6
devDependencies:
'@kevisual/cache':
specifier: ^0.0.4
version: 0.0.4
'@kevisual/query':
specifier: ^0.0.33
version: 0.0.33
@@ -36,6 +39,9 @@ importers:
'@types/node':
specifier: ^25.0.3
version: 25.0.3
dotenv:
specifier: ^17.2.3
version: 17.2.3
fast-glob:
specifier: ^3.3.3
version: 3.3.3
@@ -62,6 +68,9 @@ importers:
packages:
'@kevisual/cache@0.0.4':
resolution: {integrity: sha512-NlyriJ9fC27TgQhWYbEH9hG84R2k0lIofOxo/+nVHN6a6LJSLnVbpDIysRcnH8MI52n/XHfWwLSjeDDL3D1/cQ==}
'@kevisual/load@0.0.6':
resolution: {integrity: sha512-+3YTFehRcZ1haGel5DKYMUwmi5i6f2psyaPZlfkKU/cOXgkpwoG9/BEqPCnPjicKqqnksEpixVRkyHJ+5bjLVA==}
@@ -316,6 +325,9 @@ packages:
humanize-ms@1.2.1:
resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==}
idb-keyval@6.2.2:
resolution: {integrity: sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg==}
inherits@2.0.4:
resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==}
@@ -331,6 +343,10 @@ packages:
resolution: {integrity: sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==}
engines: {node: '>=0.12.0'}
lru-cache@11.2.4:
resolution: {integrity: sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg==}
engines: {node: 20 || >=22}
math-intrinsics@1.1.0:
resolution: {integrity: sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==}
engines: {node: '>= 0.4'}
@@ -501,6 +517,12 @@ packages:
snapshots:
'@kevisual/cache@0.0.4':
dependencies:
idb-keyval: 6.2.2
lru-cache: 11.2.4
nanoid: 5.1.6
'@kevisual/load@0.0.6':
dependencies:
eventemitter3: 5.0.1
@@ -824,6 +846,8 @@ snapshots:
dependencies:
ms: 2.1.3
idb-keyval@6.2.2: {}
inherits@2.0.4: {}
is-extglob@2.1.1: {}
@@ -834,6 +858,8 @@ snapshots:
is-number@7.0.0: {}
lru-cache@11.2.4: {}
math-intrinsics@1.1.0: {}
merge2@1.4.1: {}

View File

@@ -3,7 +3,7 @@ import type { Result, DataOpts } from '@kevisual/query/query';
import { setBaseResponse } from '@kevisual/query/query';
import { LoginCacheStore, CacheStore } from './login-cache.ts';
import { Cache } from './login-cache.ts';
import { BaseLoad } from '@kevisual/load';
export type QueryLoginOpts = {
query?: Query;
isBrowser?: boolean;
@@ -418,6 +418,12 @@ export class QueryLogin extends BaseQuery {
}
/**
* 使用web登录,创建url地址, 需要MD5和jsonwebtoken
*
*
import MD5 from 'crypto-js/md5.js';
import jsonwebtoken from 'jsonwebtoken';
*/
loginWithWeb(baseURL: string, { MD5, jsonwebtoken }: { MD5: any; jsonwebtoken: any }) {
const randomId = Math.random().toString(36).substring(2, 15);
@@ -431,4 +437,46 @@ export class QueryLogin extends BaseQuery {
const url = `${baseURL}/api/router?path=user&key=webLogin&p&loginToken=${token}&sign=${sign}&randomId=${randomId}`;
return { url, token, tokenSecret };
}
/**
*轮询登录状态
*
*
*
const res = queryLogin.loginWithWeb(baseURL, { MD5, jsonwebtoken });
await pollLoginStatus(res.token, { tokenSecret: res.tokenSecret });
* 轮询登录状态
*/
async pollLoginStatus(data: { token: string; tokenSecret: string }) {
const token = data.token;
const load = new BaseLoad();
load.load(
async () => {
const res = await this.checkLoginStatus(token);
if (res.code === 500) {
load.cancel('check-login-status');
}
return res;
},
{
key: 'check-login-status',
isReRun: true,
checkSuccess: (data) => {
return data?.code === 200;
},
},
);
const res = await load.hasLoaded('check-login-status', {
timeout: 60 * 3 * 1000, // 3分钟超时
});
if (res.code === 200 && res.data?.code === 200) {
try {
console.log('网页登录成功');
return;
} catch (error) {
console.log('登录失败', error);
return;
}
}
console.log('登录失败', res);
}
}