国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home WeChat Applet WeChat Development NodeJs develops WeChat public account WeChat event interaction example code

NodeJs develops WeChat public account WeChat event interaction example code

Mar 21, 2017 pm 04:00 PM

This article mainly introduces the relevant information about using NodeJs to develop WeChat event interaction examples for WeChat public accounts. Friends in need can refer to it

WeChat public accounts have rules. Once the developer mode is turned on, other All regular functions must be called through interface. For example, the custom menu function must be generated by sending a post request. This chapter will talk about how nodejs interacts with WeChat through the entire process from following to unfollowing. The entrance to these functions is the URL you filled in the test official account (replaced with /login/wechat below).

Event interaction

After scanning the QR code to follow the WeChat official account, WeChat will call your interface /login/wechat , and comes with a piece of xml information. First, you need to obtain some signatures, and check whether they are consistent with the TOKEN you filled in through encryption and sorting. If they are consistent, parse the xml. When node parses xml, the module must be referenced first. Therefore, first introduce the xml parsing module

//xml解析模塊
var XMLJS = require('xml2js');
//解析,將xml解析為json
var parser = new XMLJS.Parser();
//重組,將json重組為xml
var builder = new XMLJS.Builder();

to obtain the xml package sent by WeChat through the monitoring data of req. The following is the xml packet data sent by WeChat to your backend interface (/yourapi mentioned in the previous article) after a new user follows the official account. After parsing, its structure is as follows:

NodeJs develops WeChat public account WeChat event interaction example code

tousername: Recipient [Public WeChat ID here]

fromusername: Sender [User openid here]

createTime: sending time

msgtype: message type [event (response event), text (push message), image (push graphic message), etc.]

event : Message name [Follow here]

eventkey: Customized key, which can be customized when setting up the web page. We will talk about it later

The above is used as a The data packet sent by WeChat to your interface after the user follows it. What is useful to us above is fromusername, which is the openid of the follower. After we obtain the openid of the user when following, we can use the specific interface provided by WeChat (https://api.weixin.qq.com/cgi-bin/ user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN) to obtain the user’s avatar, gender, nickname and other information to build a reliable database for your app.

Code implementation

//微信事件推送的入口
app.post('/yourapi', function(req, res, next) {
//獲取參數(shù)
var query = req.query; 
//簽名
var signature = query.signature; 
//輸出的字符,你填寫的TOKEN 
var echostr = query.echostr; 
//時間戳
var timestamp = query['timestamp']; 
//隨機字符串
var nonce = query.nonce; 
var oriArray = new Array(); 
oriArray[] = nonce; 
oriArray[] = timestamp; 
oriArray[] = appConfig.token;
//排序參數(shù)
oriArray.sort(); 
var original = oriArray[]+oriArray[]+oriArray[]; 
//加密
var scyptoString = sha(original); 
//判斷是否與你填寫TOKEN相等
if (signature == scyptoString) {
//獲取xml數(shù)據(jù)
req.on("data", function(data) {
//將xml解析
parser.parseString(data.toString(), function(err, result) {
var body = result.xml;
var messageType = body.MsgType[];
//用戶點擊菜單響應(yīng)事件
if(messageType === 'event') {
var eventName = body.Event[];
(EventFunction[eventName]||function(){})(body, req, res);
//自動回復(fù)消息
}else if(messageType === 'text') {
EventFunction.responseNews(body, res);
//第一次填寫URL時確認接口是否有效
}else {
res.send(echostr);
}
});
});
} else { 
//認證失敗,非法操作
res.send("Bad Token!"); 
}
});
//微信客戶端各類回調(diào)用接口
var EventFunction = {
//關(guān)注
subscribe: function(result, req, res) {
//存入openid 通過微信的接口獲取用戶的信息同時存入數(shù)據(jù)庫。
},
//注銷
unsubscribe: function(openid, req, res) {
//刪除對應(yīng)id
},
//打開某個網(wǎng)頁
VIEW: function() {
//根據(jù)需求,處理不同的業(yè)務(wù)
},
//自動回復(fù)
responseNews: function(body, res) {
//組裝微信需要的json
var xml = {xml: {
ToUserName: body.FromUserName,
FromUserName: body.ToUserName,
CreateTime: + new Date(),
MsgType: 'text',
Content: '編輯@+您想說的話,我們可以收到'
}};
var reciviMessage = body.Content[]
if(/^\@.*/.test(reciviMessage)) {
xml.xml.Content = '已經(jīng)收到您的建議,會及時處理!'
}<br>//將json轉(zhuǎn)為xml
xml = builder.buildObject(xml);<br>//發(fā)送給微信
res.send(xml);
}
}

Here, it is suitable to adopt the strategy in JS design pattern Mode , write your own business in the subscribe method, and by sending a request with the openid parameter, you can save several pieces of information to the database and establish a session when the user follows the WeChat ID. In this way, when the user opens your web page next time, he does not need to authenticate again. He only needs to compare the openid and query the database.

The above is the detailed content of NodeJs develops WeChat public account WeChat event interaction example code. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276