Socket
Socket
Sign inDemoInstall

@huangapple/node-wxapi

Package Overview
Dependencies
11
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @huangapple/node-wxapi

> WXAPI的 ts版本实现


Version published
Maintainers
1
Created

Readme

Source

项目名称

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
}

FAQs

Last updated on 29 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc