fix: 解决问题
This commit is contained in:
@@ -23,13 +23,13 @@
|
||||
],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.24.0",
|
||||
"packageManager": "pnpm@10.26.2",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@kevisual/context": "^0.0.4",
|
||||
"@kevisual/query": "^0.0.29",
|
||||
"@kevisual/router": "0.0.33",
|
||||
"@types/node": "^24.10.1",
|
||||
"@kevisual/query": "^0.0.33",
|
||||
"@kevisual/router": "0.0.50",
|
||||
"@types/node": "^25.0.3",
|
||||
"crypto-js": "^4.2.0",
|
||||
"xml2js": "^0.6.2"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { SimpleRouter } from '@kevisual/router/simple';
|
||||
import CryptoJS from 'crypto-js';
|
||||
import xml2js from 'xml2js';
|
||||
import { useContextKey } from '@kevisual/context';
|
||||
import { Redis } from 'ioredis';
|
||||
import http from 'node:http';
|
||||
@@ -8,6 +7,7 @@ import { Wx, WxMsgEvent, parseWxMessage } from './wx/index.ts';
|
||||
import { contextConfig as config } from './modules/config.ts';
|
||||
import { loginByTicket } from './wx/login-by-ticket.ts';
|
||||
import { Queue } from 'bullmq';
|
||||
import { parseXml } from './utils/get-json-from-xml.ts';
|
||||
export const simpleRouter: SimpleRouter = await useContextKey('router');
|
||||
export const redis: Redis = await useContextKey('redis');
|
||||
export const wxmsgQueue = useContextKey<Queue>('wxmsgQueue', () => {
|
||||
@@ -36,40 +36,9 @@ simpleRouter.get('/api/wxmsg', async (req: http.IncomingMessage, res: http.Serve
|
||||
}
|
||||
});
|
||||
|
||||
export const getJsonFromXml = async (req: http.IncomingMessage): Promise<any> => {
|
||||
return await new Promise((resolve) => {
|
||||
// 读取请求数据
|
||||
let data = '';
|
||||
req.setEncoding('utf8');
|
||||
// 监听data事件,接收数据片段
|
||||
req.on('data', (chunk: string) => {
|
||||
data += chunk;
|
||||
});
|
||||
// 当请求结束时处理数据
|
||||
req.on('end', () => {
|
||||
try {
|
||||
// 使用xml2js解析XML
|
||||
xml2js.parseString(data, function (err, result) {
|
||||
if (err) {
|
||||
console.error('XML解析错误:', err);
|
||||
resolve(null);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('处理请求时出错:', error);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
simpleRouter.post('/api/wxmsg', async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
try {
|
||||
const xml = await getJsonFromXml(req);
|
||||
const xml = await parseXml(req);
|
||||
const msgOrigin = xml?.xml;
|
||||
if (!msgOrigin) {
|
||||
console.error('No message received');
|
||||
|
||||
52
wxmsg/src/utils/get-json-from-xml.ts
Normal file
52
wxmsg/src/utils/get-json-from-xml.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import xml2js from 'xml2js';
|
||||
import { isBun } from '../utils/is-engine.ts';
|
||||
import http from 'http';
|
||||
export const xms2jsParser = async (data: string): Promise<any> => {
|
||||
try {
|
||||
// 使用xml2js解析XML
|
||||
const xml = await xml2js.parseStringPromise(data);
|
||||
return xml;
|
||||
} catch (error) {
|
||||
console.error('XML解析错误:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export const parseXml = async (req: http.IncomingMessage): Promise<any> => {
|
||||
if (isBun) {
|
||||
// @ts-ignore
|
||||
const body = req.body;
|
||||
let xmlString = '';
|
||||
if (body) {
|
||||
xmlString = body;
|
||||
}
|
||||
if (!xmlString) {
|
||||
// @ts-ignore
|
||||
xmlString = await req.bun?.request?.text?.();
|
||||
}
|
||||
if (xmlString) {
|
||||
return await xms2jsParser(xmlString)
|
||||
}
|
||||
console.error('没有读取到请求体');
|
||||
return null;
|
||||
}
|
||||
return await new Promise((resolve) => {
|
||||
// 读取请求数据
|
||||
let data = '';
|
||||
req.setEncoding('utf8');
|
||||
// 监听data事件,接收数据片段
|
||||
req.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
// 当请求结束时处理数据
|
||||
req.on('end', () => {
|
||||
try {
|
||||
xms2jsParser(data).then((result) => {
|
||||
resolve(result);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('处理请求时出错:', error);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
19
wxmsg/src/utils/is-engine.ts
Normal file
19
wxmsg/src/utils/is-engine.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
||||
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && typeof document.createElement === 'function';
|
||||
// @ts-ignore
|
||||
export const isDeno = typeof Deno !== 'undefined' && typeof Deno.version === 'object' && typeof Deno.version.deno === 'string';
|
||||
// @ts-ignore
|
||||
export const isBun = typeof Bun !== 'undefined' && typeof Bun.version === 'string';
|
||||
|
||||
export const getEngine = () => {
|
||||
if (isBun) {
|
||||
return 'bun';
|
||||
} else if (isNode) {
|
||||
return 'node';
|
||||
} else if (isBrowser) {
|
||||
return 'browser';
|
||||
} else if (isDeno) {
|
||||
return 'deno';
|
||||
}
|
||||
return 'unknown';
|
||||
};
|
||||
Reference in New Issue
Block a user