1.本地微信环境搭建(frp/路由器做端口映射)
--服务器frp配置并启动:./frps -c ./frps.ini & (注意:打开7000端口)
# frps.ini
[common]
bind_port = 7000
vhost_http_port = 8888
--客户端frp配置:start ./frpc -c ./frpc.ini
# frpc.ini [common] server_addr = 39.108.139.60 server_port = 7000 [web] type = http local_port = 8888 #端口任意 custom_domains = maoriaty.top
注意:服务器端的http端口和客户端的http端口无关,所以更换端口只需修改客户端即可
--nginx配置:注意微信只接入80或443端口,所以nginx代理端口必须为80或443
#test local proxy server { listen 80; server_name t.maoriaty.top; expires 300d; gzip on; location / { proxy_pass http://127.0.0.1:8888; proxy_redirect default; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
--创建.bat文件批处理启动本地服务器和链接:
cd /d "E:\JabinDOC\frp_0.13.0_windows_amd64" start ./frpc -c ./frpc.ini
2.微信接入
--app.js接入文件
var express = require('express'); var crypto = require('crypto'); var config = { "AppId": 'wx1af02f9490ff3e98', "AppSecret": '92bd3a03d76da03abba6b7d1c25c23a7', "Token": 'maoriaty' } var app = express(); app.get('/',function(req,res){ var signature = req.query.signature; var timestamp = req.query.timestamp; var nonce = req.query.nonce; var echostr = req.query.echostr; var str = [config.Token,timestamp,nonce].sort().join(''); var sha1 = crypto.createHash('sha1').update(str,'utf-8').digest('hex'); if(sha1 == signature){ res.send(echostr); }else{ res.send('err'); } console.log(signature+'==='+sha1); }) var server = app.listen('8888',function(){ console.log('the port is %s',server.address().port); })
--启动app.js然后微信后台接入即可
--如果不需验证加密,最简单可以如下:
// ====================微信接入==================== // const app = require("express")(), app.get('/',(req,res) => res.send(req.query.echostr;)) let server = app.listen(8888,() => console.log('the port is %s',server.address().port);