UNPKG

@tnwx/wxmp

Version:

TNWX 微信系开发脚手架之极速开发微信公众号

211 lines 10.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const xml2js_1 = require("xml2js"); const accesstoken_1 = require("@tnwx/accesstoken"); const commons_1 = require("@tnwx/commons"); const kits_1 = require("@tnwx/kits"); class WeChat { /** * JSSDK签名 * @param nonce_str * @param timestamp * @param url * @param accessToken api_authorizer_token * @param jsapi_ticket */ static jssdkSignature(nonce_str, timestamp, url, accessToken, jsapi_ticket) { return __awaiter(this, void 0, void 0, function* () { if (!jsapi_ticket) { let jsTicket = yield commons_1.JsTicketApi.getTicket(commons_1.JsApiType.JSAPI, accessToken); if (jsTicket) { jsapi_ticket = jsTicket.getTicket; if (accesstoken_1.ApiConfigKit.isDevMode()) { console.debug('jsapi_ticket:', jsapi_ticket); } } } let str = 'jsapi_ticket=' + jsapi_ticket + '&noncestr=' + nonce_str + '&timestamp=' + timestamp + '&url=' + url; return kits_1.Kits.sha1(str); }); } /** * 验证成为开发者 * @param signature * @param timestamp * @param nonce * @param echostr */ static checkSignature(signature, timestamp, nonce, echostr) { //将 token、timestamp、nonce 三个参数进行字典序排序,并拼接成一个字符串 let tempStr = [accesstoken_1.ApiConfigKit.getToken, timestamp, nonce].sort().join(''); //创建加密类型 const hashCode = crypto.createHash('sha1'); //对传入的字符串进行加密 let tempSignature = hashCode.update(tempStr, 'utf8').digest('hex'); //校验签名 if (tempSignature === signature) { return echostr; } else { return '签名异常'; } } /** * 处理消息 * @param msgAdapter * @param msgXml * @param msgSignature * @param timestamp * @param nonce */ static handleMsg(msgAdapter, msgXml, msgSignature, timestamp, nonce) { //实例微信消息加解密 let cryptoKit; return new Promise(function (resolve, reject) { xml2js_1.parseString(msgXml, { explicitArray: false }, function (err, result) { return __awaiter(this, void 0, void 0, function* () { if (err) { reject(`xml 数据解析错误:${err}`); console.debug(err); return; } result = result.xml; let isEncryptMessage = false; //判断消息加解密方式 if (accesstoken_1.ApiConfigKit.getApiConfig.getEncryptMessage && accesstoken_1.ApiConfigKit.getApiConfig.getEncodingAesKey) { isEncryptMessage = true; cryptoKit = new commons_1.CryptoKit(accesstoken_1.ApiConfigKit.getApiConfig, msgSignature || '', timestamp || '', nonce || ''); //对加密数据解密 result = cryptoKit.decryptMsg(result.Encrypt); } if (accesstoken_1.ApiConfigKit.isDevMode()) { console.debug('接收消息 isEncryptMessage=', isEncryptMessage); console.debug(result); console.debug('------------------------\n'); } let inMsg = commons_1.InMsgParser.parse(result); let responseMsg; let outMsg; // 处理接收的消息 if (inMsg instanceof commons_1.InTextMsg) { outMsg = yield msgAdapter.processInTextMsg(inMsg); } else if (inMsg instanceof commons_1.InImageMsg) { outMsg = yield msgAdapter.processInImageMsg(inMsg); } else if (inMsg instanceof commons_1.InLinkMsg) { outMsg = yield msgAdapter.processInLinkMsg(inMsg); } else if (inMsg instanceof commons_1.InLocationMsg) { outMsg = yield msgAdapter.processInLocationMsg(inMsg); } else if (inMsg instanceof commons_1.InShortVideoMsg) { outMsg = yield msgAdapter.processInShortVideoMsg(inMsg); } else if (inMsg instanceof commons_1.InVideoMsg) { outMsg = yield msgAdapter.processInVideoMsg(inMsg); } else if (inMsg instanceof commons_1.InVoiceMsg) { outMsg = yield msgAdapter.processInVoiceMsg(inMsg); } else if (inMsg instanceof commons_1.InVoiceMsg) { outMsg = yield msgAdapter.processInVoiceMsg(inMsg); } else if (inMsg instanceof commons_1.InSpeechRecognitionResults) { outMsg = yield msgAdapter.processInSpeechRecognitionResults(inMsg); } else if (inMsg instanceof commons_1.InFollowEvent) { outMsg = yield msgAdapter.processInFollowEvent(inMsg); } else if (inMsg instanceof commons_1.InLocationEvent) { outMsg = yield msgAdapter.processInLocationEvent(inMsg); } else if (inMsg instanceof commons_1.InMenuEvent) { outMsg = yield msgAdapter.processInMenuEvent(inMsg); } else if (inMsg instanceof commons_1.InQrCodeEvent) { outMsg = yield msgAdapter.processInQrCodeEvent(inMsg); } else if (inMsg instanceof commons_1.InTemplateMsgEvent) { outMsg = yield msgAdapter.processInTemplateMsgEvent(inMsg); } else if (inMsg instanceof commons_1.InShakearoundUserShakeEvent) { outMsg = yield msgAdapter.processInShakearoundUserShakeEvent(inMsg); } else if (inMsg instanceof commons_1.InComponentVerifyTicket) { isEncryptMessage = false; outMsg = yield msgAdapter.processInComponentVerifyTicket(inMsg); } else if (inMsg instanceof commons_1.InAuthMpEvent) { isEncryptMessage = false; outMsg = yield msgAdapter.processInAuthMpEvent(inMsg); } else if (inMsg instanceof commons_1.InMassEvent) { outMsg = yield msgAdapter.processInMassEvent(inMsg); } else if (inMsg instanceof commons_1.InWxVerifyDispatchEvent) { outMsg = yield msgAdapter.processInWxVerifyDispatchEvent(inMsg); } else if (inMsg instanceof commons_1.InNotDefinedMsg) { if (accesstoken_1.ApiConfigKit.isDevMode()) { console.debug('未能识别的消息类型。 消息 xml 内容为:\n'); console.debug(result); } outMsg = yield msgAdapter.processIsNotDefinedMsg(inMsg); } // 处理发送的消息 if (outMsg instanceof commons_1.OutTextMsg) { let outTextMsg = outMsg; if (outTextMsg.getContent.trim()) { responseMsg = outTextMsg.toXml(); } else { responseMsg = 'success'; } } else if (outMsg instanceof commons_1.OutImageMsg) { responseMsg = outMsg.toXml(); } else if (outMsg instanceof commons_1.OutMusicMsg) { responseMsg = outMsg.toXml(); } else if (outMsg instanceof commons_1.OutNewsMsg) { responseMsg = outMsg.toXml(); } else if (outMsg instanceof commons_1.OutVideoMsg) { responseMsg = outMsg.toXml(); } else if (outMsg instanceof commons_1.OutVoiceMsg) { responseMsg = outMsg.toXml(); } else if (outMsg instanceof commons_1.OutCustomMsg) { responseMsg = outMsg.toXml(); } else if (typeof outMsg === 'string') { responseMsg = outMsg; } //判断消息加解密方式,如果未加密则使用明文,对明文消息进行加密 responseMsg = isEncryptMessage ? cryptoKit.encryptMsg(responseMsg) : responseMsg; if (accesstoken_1.ApiConfigKit.isDevMode()) { console.debug(`发送消息:\n ${responseMsg}`); console.debug('--------------------------\n'); } //返回给微信服务器 resolve(responseMsg); }); }); }); } } exports.WeChat = WeChat; //# sourceMappingURL=WeChat.js.map