fix: 解决问题

This commit is contained in:
2025-12-28 10:52:04 +08:00
parent 868979a423
commit 9f51d27398
18 changed files with 57061 additions and 201 deletions

View File

@@ -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');