Using Nodejs to develop micro-trust (iii) micro-trust event interaction Examples _node.js

Source: Internet
Author: User
Tags openid xml parser

There is a rule in the micro-credit public number that once the developer mode is turned on, other general functions must be done through the interface call. For example, a custom menu function must be generated by sending a POST request. This chapter discusses how NODEJS interacts with micro-letters by focusing on the whole process of canceling attention. The entrance to these functions is the URL you have filled in the test public number (hereinafter replaced by/login/wechat).

Event interaction

Scanning the code after the micro-trust public number, the micro-letter will call your interface/login/wechat, and with a piece of XML information, first you need to obtain some signatures, by encrypting, sorting than to the token you fill in the same, if the consistency of the XML parsing. Node must first refer to the module when parsing XML. So, first introduce XML parsing module

XML parsing module
var xmljs = require (' Xml2js ');
Parsing, parsing XML into JSON
var parser = new Xmljs. Parser ();
Reorganization, reorganizing JSON into XML

Through the req of the listening data, to obtain the micro-letter sent over the XML packet. Here is the XML packet data sent by a new user following the public number message to your backend interface (/yourapi mentioned in the previous article), and after parsing, his structure is as follows:

Tousername: The addressee "here is the public micro-signal."

Fromusername: Sender "Here is the user OpenID"

Createtime: Send Time

Msgtype: Message type Event (response event), text (push message), image (push text message), etc.

Event: Message name "Here is the concern"

Eventkey: A custom key that you can customize when you set up your Web page

The above is when a user is concerned about the packet sent by the micro-letter to your interface. What's useful for us is fromusername, the OpenID that focuses on people, and we get the specific interface that the user's OpenID can provide via a micro-letter when we are concerned (https://api.weixin.qq.com/cgi-bin/user/ INFO?ACCESS_TOKEN=ACCESS_TOKEN&OPENID=OPENID&LANG=ZH_CN the user's avatar, gender, nickname, etc., to create a reliable database for your app.

Code implementation

Micro-letter Events push the portal app.post ('/yourapi ', function (req, res, next) {//get parameter var query = Req.query; 
Signature var signature = Query.signature; 
Output the characters that you fill out with the token var echostr = query.echostr; 
Timestamp var timestamp = query[' timestamp ']; 
Random string var nonce = query.nonce; 
var oriarray = new Array (); 
Oriarray[] = nonce; 
Oriarray[] = timestamp;
Oriarray[] = Appconfig.token; 
Sort parameters Oriarray.sort (); 
var original = oriarray[]+oriarray[]+oriarray[]; 
Encrypt var scyptostring = sha (original); Judge whether to fill in token equal if (signature = = scyptostring) {//Get XML Data Req.on ("Data", function (data) {//parse XML parser.parsestring ( Data.tostring (), function (err, result) {var body = result.xml; var messagetype = body.
Msgtype[]; User clicks on Menu response event if (MessageType = = ' event ') {var eventName = body.
Event[]; (eventfunction[eventname]| |
function () {}) (body, req, res); Automatic reply message}else if (messagetype = = = ' text ') {eventfunction.responsenews (body, res);//Confirm the interface is valid the first time you fill out the URL}else {res.send (E
CHOSTR);
}
});
}); } else {//authentication failed, illegal operation Res.send ("BAd token! ");});
Micro-trust clients all kinds of callback interface var eventfunction = {//concern subscribe:function (result, req, res) {//deposit OpenID through the micro-letter interface to get the user's information at the same time into the database. },//Logoff unsubscribe:function (OpenID, req, res) {//remove corresponding ID},//Open a page view:function () {////////////////////////////////////// Enews:function (body, res) {//Assembly micro-letter required JSON var XML = {xml: {tousername:body. Fromusername, Fromusername:body.
Tousername, Createtime: + new Date (), Msgtype: ' Text ', Content: ' Edit @+ what you want to say, we can receive '}}; var recivimessage = body. Content[] if (/^\@.*/.test (recivimessage)) {xml.xml.Content = ' has received your proposal, will be processed in time!
'}<br>//json to XML = Builder.buildobject (XML); <br>//sent to micro-res.send (XML); }
}

Here, it is suitable to use the strategy mode in the JS design pattern, write your own business in the Subscribe method, by sending the request with OpenID parameter, you can deposit several data into the database when the user concerns the micro-signal, and establish the session. This will not need to be authenticated again when the user opens your Web page, just compare to OpenID and then query the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.