feat: support mini app sign

This commit is contained in:
手瓜一十雪 2024-05-24 23:24:10 +08:00
parent f1e8ef1cf6
commit 7640deb798
6 changed files with 47 additions and 25 deletions

View File

@ -3,32 +3,51 @@ import http from 'node:http';
export class RequestUtil { export class RequestUtil {
// 适用于获取服务器下发cookies时获取仅GET // 适用于获取服务器下发cookies时获取仅GET
static async HttpsGetCookies(url: string): Promise<Map<string, string>> { static async HttpsGetCookies(url: string): Promise<{ [key: string]: string }> {
return new Promise<Map<string, string>>((resolve, reject) => { const client = url.startsWith('https') ? https : http;
const protocol = url.startsWith('https://') ? https : http; return new Promise((resolve, reject) => {
protocol.get(url, (res) => { client.get(url, (res) => {
const cookiesHeader = res.headers['set-cookie']; let cookies: { [key: string]: string } = {};
if (!cookiesHeader) { const handleRedirect = (res: http.IncomingMessage) => {
resolve(new Map<string, string>()); //console.log(res.headers.location);
if (res.statusCode === 301 || res.statusCode === 302) {
if (res.headers.location) {
const redirectUrl = new URL(res.headers.location, url);
RequestUtil.HttpsGetCookies(redirectUrl.href).then((redirectCookies) => {
// 合并重定向过程中的cookies
cookies = { ...cookies, ...redirectCookies };
resolve(cookies);
});
} else { } else {
const cookiesMap = new Map<string, string>(); resolve(cookies);
cookiesHeader.forEach((cookieStr) => { }
cookieStr.split(';').forEach((cookiePart) => { } else {
const trimmedPart = cookiePart.trim(); resolve(cookies);
if (trimmedPart.includes('=')) { }
const [key, value] = trimmedPart.split('=').map(part => part.trim()); };
cookiesMap.set(key, decodeURIComponent(value)); // 解码cookie值 res.on('data', () => { }); // Necessary to consume the stream
res.on('end', () => {
handleRedirect(res);
});
if (res.headers['set-cookie']) {
//console.log(res.headers['set-cookie']);
res.headers['set-cookie'].forEach((cookie) => {
const parts = cookie.split(';')[0].split('=');
const key = parts[0];
const value = parts[1];
if (key && value && key.length > 0 && value.length > 0) {
cookies[key] = value;
} }
}); });
});
resolve(cookiesMap);
} }
}).on('error', (error) => { }).on('error', (err) => {
reject(error); reject(err);
}); });
}); });
} }
// 请求和回复都是JSON data传原始内容 自动编码json // 请求和回复都是JSON data传原始内容 自动编码json
static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}, isJsonRet: boolean = true, isArgJson: boolean = true): Promise<T> { static async HttpGetJson<T>(url: string, method: string = 'GET', data?: any, headers: Record<string, string> = {}, isJsonRet: boolean = true, isArgJson: boolean = true): Promise<T> {
const option = new URL(url); const option = new URL(url);

@ -1 +1 @@
Subproject commit 55727918696e7173f2a0830c21a17d92b89ff7eb Subproject commit f2eb0fa596131a39b15cdca71c325314af40265c

View File

@ -3,7 +3,7 @@ import { OB11GroupMember } from '../../types';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../constructor';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { napCatCore, NTQQGroupApi, NTQQUserApi } from '@/core'; import { napCatCore, NTQQGroupApi, NTQQUserApi, SignMiniApp } from '@/core';
import { WebApi } from '@/core/apis/webapi'; import { WebApi } from '@/core/apis/webapi';
import { logDebug } from '@/common/utils/log'; import { logDebug } from '@/common/utils/log';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';

View File

@ -5,6 +5,7 @@ import {
Group, Group,
IdMusicSignPostData, IdMusicSignPostData,
NTQQFileApi, NTQQFileApi,
SendArkElement,
SendMessageElement, SendMessageElement,
SendMsgElementConstructor SendMsgElementConstructor
} from '@/core'; } from '@/core';
@ -132,6 +133,7 @@ const _handlers: {
return SendMsgElementConstructor.video(path, fileName, thumb); return SendMsgElementConstructor.video(path, fileName, thumb);
}, },
[OB11MessageDataType.miniapp]: async ({ data: any }) => (await SendMsgElementConstructor.miniapp()) as SendArkElement,
[OB11MessageDataType.voice]: async (sendMsg, context) => [OB11MessageDataType.voice]: async (sendMsg, context) =>
SendMsgElementConstructor.ptt((await handleOb11FileLikeMessage(sendMsg, context)).path), SendMsgElementConstructor.ptt((await handleOb11FileLikeMessage(sendMsg, context)).path),

View File

@ -58,7 +58,8 @@ export enum OB11MessageDataType {
xml = 'xml', xml = 'xml',
poke = 'poke', poke = 'poke',
dice = 'dice', dice = 'dice',
RPS = 'rps' RPS = 'rps',
miniapp = 'miniapp'//json类
} }
export interface OB11MessageMFace { export interface OB11MessageMFace {