update
This commit is contained in:
103
wxmsg/src/index.ts
Normal file
103
wxmsg/src/index.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
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';
|
||||
import { Wx, parseWxMessage } from './wx/index.ts';
|
||||
import { config } from './modules/config.ts';
|
||||
export const simpleRouter: SimpleRouter = await useContextKey('router');
|
||||
export const redis: Redis = await useContextKey('redis');
|
||||
|
||||
simpleRouter.get('/api/wxmsg', async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
console.log('微信检测服务是否可用');
|
||||
const query = simpleRouter.getSearch(req);
|
||||
const body = await simpleRouter.getBody(req);
|
||||
const {
|
||||
signature, // 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
|
||||
timestamp, // 时间戳
|
||||
nonce, // 随机数
|
||||
echostr, // 随机字符串
|
||||
} = query;
|
||||
const token = 'xiongabc123';
|
||||
let str = [token, timestamp, nonce].sort().join('');
|
||||
let strSha1 = CryptoJS.SHA1(str).toString();
|
||||
// 签名对比,相同则按照微信要求返回echostr参数值
|
||||
if (signature == strSha1) {
|
||||
res.end(echostr);
|
||||
} else {
|
||||
res.end('send fail');
|
||||
}
|
||||
});
|
||||
|
||||
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 msgOrigin = xml?.xml;
|
||||
if (!msgOrigin) {
|
||||
console.error('No message received');
|
||||
res.end('');
|
||||
return
|
||||
}
|
||||
const msg = parseWxMessage(msgOrigin);
|
||||
const { fromusername, msgtype } = msg;
|
||||
res.end('')
|
||||
if (msgtype === 'event') {
|
||||
console.log('Received event message');
|
||||
return
|
||||
}
|
||||
if (fromusername) {
|
||||
// 代理分析返回
|
||||
const wx = new Wx({ appId: config.WX_MP_APP_ID, appSecret: config.WX_MP_APP_SECRET, redis });
|
||||
wx.analyzeUserMsg(msg);
|
||||
}
|
||||
return
|
||||
|
||||
|
||||
// 直接返回
|
||||
// const builder = new xml2js.Builder();
|
||||
// const result = builder.buildObject({
|
||||
// xml: {
|
||||
// ToUserName: fromusername,
|
||||
// FromUserName: tousername,
|
||||
// CreateTime: Date.now(),
|
||||
// MsgType: msgtype,
|
||||
// Content: 'Hello ' + content,
|
||||
// },
|
||||
// });
|
||||
// res.end(result);
|
||||
} catch (e) {
|
||||
console.error('Error processing message:', e);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user