This article mainly shares with you the tutorial on how to develop WeChat public platform using node.js, and how to develop WeChat. Interested friends can refer to it
How to use nodejs to develop the WeChat public platform?
I won’t say much else. First, let’s briefly introduce the basic principles of the WeChat public platform.
The WeChat server is equivalent to a forwarding server. The terminal (mobile phone, Pad, etc.) initiates a request to the WeChat server, and the WeChat server then forwards the request to the custom service (here is our specific implementation). After the service is processed, it is then forwarded to the WeChat server, and the WeChat server replies with a specific response to the terminal; the communication protocol is: HTTP; the data format is: XML.
The specific process is shown in the figure below:
In fact, what we need to do is to respond to HTTP requests. We parse the specific request content according to a specific XML format. After processing, we must also return it according to a specific XML format.
Platform registration
To complete the development of the WeChat public platform, we need to register a WeChat public platform account. The registration steps are as follows:
Open the official website of the WeChat public platform, https://mp.weixin.qq.com/, and click "Register Now".
Then follow the prompts to fill in the basic information, activate the email, select the type, information registration, official account information, and complete the registration.
After the registration is completed, we need to make some basic settings for the official account. Log in to the official account, find [Official Account Settings], and then set the avatar and other information.
nodejs environment construction
We need to find a server on the public Internet so that we can start our nodejs environment. After starting the environment, by setting the access address, we can receive the messages sent by the WeChat server message, and we can also send messages to the WeChat server.
In the public network serverInstallationAfter completing nodejs, we also need to install some modules used by nodejs, such as: express, node-xml, jssha and other modules. It can be installed through the npm command.
We use nodejs to implement sending and receiving messages to the WeChat server, as well as signature authentication with the WeChat server.
In the editing environment on the right, the nodejs environment has been installed for the students. In the following content, we will implement the signature authentication of WeChat server for students.
Create expressFramework
We have installed the express module in the previous course, and have created a file named app.js in our environment on the right document. Now we will complete the express framework in this file. The following code:
var express = require("express"); var path=require('path'); var app = express(); server = require('http').Server(app); app.set('views',dirname); // 設置視圖 app.set('view engine', 'html'); app.engine( '.html', require( 'ejs' ).express ); require('./index')(app); //路由配置文件 server.listen(80,function(){ console.log('App start,port 80.'); });
Then add a file named test.html. Write the following content
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>匯智網</title> </head> <body> <p><%=issuccess%></p> </body> </html>
We will also add a file named index.js to implement our routing. Click the Add File button in the editing environment to add the file, and then we write the following code, in which the GET request is used to verify the legitimacy of the configured URL, and the POST request is used to process WeChat messages. .
module.exports = function(app){ app.get('/',function(req,res){ res.render('test',{issuccess:"success"}) }); app.get('/interface',function(req,res){}); app.post('/interface',function(req,res){}); }
In this way, the express framework we need is completed. Of course, we can also add the public public folder and the middleware we want to use. Save the file, click [Submit to run], then click [Access Test] to give it a try. Note the address to access the test, we will use this address in the next section.
WeChat server configuration
We log in to the WeChat public platform, find the basic configuration under developer mode, and then modify the server configuration. As shown in the figure:
# First, the URL must be filled in the path where we install nodejs to receive and send data on the public network. We can fill in the address of [Access Test] in the previous section and then add the corresponding route.
The Token must be consistent with our custom server-side token. After filling in, you can click submit. Before submitting, we start app.js (click [Submit to run]). In this way, we can verify whether the signature is valid based on our route matching.
After the configuration is completed, the configuration must be enabled.
網址接入
公眾平臺用戶提交信息后,微信服務器將發(fā)送GET請求到填寫的URL上,并且?guī)纤膫€參數:
參數 描述
signature 微信加密簽名
timestamp 時間戳
nonce 隨機數
echostr 隨機字符串
開發(fā)者通過檢驗signature對請求進行校驗(下面有校驗方式)。若確認此次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,否則接入失敗。
signature結合了開發(fā)者填寫的token參數和請求中的timestamp參數、nonce參數。
加密/校驗流程:
1、將token、timestamp、nonce三個參數進行字典序排序;
2、將三個參數字符串拼接成一個字符串進行sha1加密;
3、開發(fā)者獲得加密后的字符串可與signature對比,標識該請求來源于微信。
參數排序
首先我們確認請求是來自微信服務器的get請求,那么就可以在index.js文件中進行添加代碼了。然后在app.get(‘/interface',function(req,res){});的function中進行添加。
先來獲取各個參數的值,如下代碼:
var token="weixin"; var signature = req.query.signature; var timestamp = req.query.timestamp; var echostr = req.query.echostr; var nonce = req.query.nonce;
我們在這里對token進行設置,讓其與微信服務器中設置的token一致。
然后對其中的token、timestamp、nonce進行排序,如下代碼:
var oriArray = new Array(); oriArray[0] = nonce; oriArray[1] = timestamp; oriArray[2] = token; oriArray.sort();
這樣我們就完成了排序。
參數加密
在上節(jié)中我們已經對參數進行了排序,然后我們在這一節(jié)中要將參數組成一個字符串,進行SH-1加密。在加密以前要用到jssha模塊,在我們的文件中要引用該模塊。
var jsSHA = require('jssha');
在上一節(jié)課中我們已經對參數排序完成,并存放在數組中,我們可以通過join方法來生成一個字符串,如下代碼:
var original = oriArray.join('');
最后對該數據進行加密,如下代碼:
var jsSHA = require('jssha'); var shaObj = new jsSHA(original, 'TEXT'); var scyptoString=shaObj.getHash('SHA-1', 'HEX');
好了這樣就生成了我們需要的簽名字符串scyptoString。
簽名對比
我們已經得到了我們想要的簽名字符串scyptoString,然后我們就可以與來自微信服務器的簽名進行對比了,對比通過,則我們就可以接收與發(fā)送消息了。
if(signature == scyptoString){ //驗證成功 } else { //驗證失敗 }
The above is the detailed content of Tutorial on developing WeChat public platform using node.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)