首页 > 代码库 > node微信接入

node微信接入

1.本地微信环境搭建(frp/路由器做端口映射)

--服务器frp配置并启动:注意打开相应端口(如:7000和8888端口)

# frps.ini
[common]
bind_port = 7000
vhost_http_port = 8888

--客户端frp配置:

# frpc.ini
[common]
server_addr = 39.108.139.60
server_port = 7000

[web]
type = http
local_port = 8888
custom_domains = maoriaty.top

--nginx配置:注意微信只接入80或443端口

location /weixin {
		proxy_pass http://localhost:8888/;
		#proxy_set_header Host $host;
		#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header HOST $host:$server_port;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		#proxy_set_header X-Forwarded-Proto $schema;
	}

--创建.bat文件批处理启动本地服务器和链接:

cd /d "E:\JabinDOC\frp_0.13.0_windows_amd64"
start ./frpc -c ./frpc.ini
cd /d "E:\JabinDOC\Demo\node"
start node app.js

2.微信接入

--interface.js接入文件

var express = require("express"),
crypto = require(crypto),
config = require(./config) //默认按文件名引入,如相同则引入顺序前面的

var app = express();

//微信接入
app.get(/,function(req,res){
    var signature = req.query.signature,
    timestamp = req.query.timestamp,
    nonce = req.query.nonce,
    echostr = req.query.echostr;

    var array = [config.token,timestamp,nonce];
    var str = array.sort().join(‘‘);
    var mystr = crypto.createHash(sha1).update(str,utf8).digest(hex);
    if(mystr == signature){
        res.send(echostr);
    }else{
        res.send(err);
    }
    console.log(mystr);
    console.log(signature);
})

var server = app.listen(8888,function(){
    var port = server.address().port;
    console.log(App start,port %s.,port);    
});

--config.json文件

{
    "AppID": "wx5944aaf9020bbcd9",
    "AppSecret": "f8ec451ccd58f2162bc0eb7d9023ab42",
    "token": "maoriaty"
}

--启动interface.js然后微信后台接入即可

node微信接入