@huangapple/node-wxapi
TypeScript icon, indicating that this package has built-in type declarations

0.0.28 • Public • Published

项目名称

WXAPI的 ts版本实现

运行条件

    //设置appid跟密钥, 然后就都能直接使用了。
WXAPIAPIBaseInstance.setGlobalConfig({ appId: "appid", appSecret: "appSecret" })
let { data } = await WXAPIAccountInstance.getAccountBasicInfo()

被动回复用户消息

//生成回复文本的xml字符串
MessageGen.text({ toUser: "toUser", fromUser: "fromUser", content: "content" })

//这样就会返回如下xml
// <xml>
// <ToUserName><![CDATA[toUser]]></ToUserName>
// <FromUserName><![CDATA[fromUser]]></FromUserName>
// <CreateTime>1681565510</CreateTime>
//
// <MsgType><![CDATA[text]]></MsgType>
// <Content><![CDATA[content]]></Content>
//
// </xml>


//还有image, voice, video, music, news等
MessageGen.image({ toUser: "toUser", fromUser: "fromUser", mediaId: "mediaId" })
MessageGen.voice({ toUser: "toUser", fromUser: "fromUser", mediaId: "mediaId" })
MessageGen.video({
  toUser: "toUser",
  fromUser: "fromUser",
  mediaId: "mediaId",
  title: "title",
  description: "description"
})
MessageGen.music({
  toUser: "toUser",
  fromUser: "fromUser",
  thumbMediaId: "thumbMediaId",
  hqMusicUrl: "hqMusicUrl",
  musicUrl: "musicUrl",
  description: "description",
  title: "title"
})
MessageGen.news({
  toUser: "toUser",
  fromUser: "fromUser",
  articleCount: 2,
  articles: [
    { title: "title1", url: "url1", picUrl: "picUrl1", description: "description1" },
    { title: "title2", url: "url2", picUrl: "picUrl2", description: "description2" }
  ]
})

微信基础消息

签名校验

    let payload = {
  signature: "9c904127b5278e868d97bd04b451668804e51693",
  timestamp: 1681552030,
  nonce: "1937606791"
}

//返回签名校验是否成功
let ok = WXUtil.checkSignature(payload, "1grTZZqtWaLDhjR61xGrpg50R0");

消息XML转json

    let xml = `<xml><ToUserName><![CDATA[gh_fd35603a3406]]></ToUserName>
<FromUserName><![CDATA[oAkeW05qqiR8NOzzA1Und3ufh-Tg]]></FromUserName>
<CreateTime>1681563392</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[小家庭]]></Content>
<MsgId>24074200986519996</MsgId>
</xml>
`

let message: MessagePayload = WXUtil.xml2json(xml)
console.log(JSON.stringify(message, null, 4))
//打印以下结构信息
let temp = {
  "ToUserName": "gh_fd35603a3406",
  "FromUserName": "oAkeW05qqiR8NOzzA1Und3ufh-Tg",
  "CreateTime": 1681563392,
  "MsgType": "text",
  "Content": "小家庭",
  "MsgId": 24074200986519996
}

Readme

Keywords

none

Package Sidebar

Install

npm i @huangapple/node-wxapi

Weekly Downloads

0

Version

0.0.28

License

MIT

Unpacked Size

332 kB

Total Files

84

Last publish

Collaborators

  • huangapple