在这里我记录一下用node express配置微信开发的一些代码
简单的三个页面
app.use('/', routes);
app.use('/users', users);
app.use('/login', login);
routes
这个页面我做的是 用户同意授权,获取code
var express = require('express');
var router = express.Router();
var fetch = require('node-fetch');
/* GET home page. */
router.get('/', function(req, res, next) {
var appid = 'wx9bf16c912ddec096';
var redirect_uri = "http%3A%2F%2Fwww.frombottomto.top/login";
var scope = 'snsapi_userinfo';//两种方式
var state = 'STATE';//非必须
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+redirect_uri+'&response_type=code&scope='+scope+'&state='+state+'#wechat_redirect';
res.redirect(url);
});
module.exports = router;
login
这个页面获取的是 拉取用户信息
var express = require('express');
var router = express.Router();
var fetch = require('node-fetch');
router.get('/', function(req, res, next) {
var code = req.query.code;
var appid = 'wx9bf16c912ddec096';
var secret = '6841a50bd9b6e99c77d0e052e5e5eb2a';
var url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='+appid+'&secret=6841a50bd9b6e99c77d0e052e5e5eb2a&code='+code+'&grant_type=authorization_code';
//获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
//获取json后get: https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
fetch(url).then(function(res){
return res.json()
}).then(function(json){
var accesstoken = json.access_token;
var openid = json.openid;
var tokenurl = 'https://api.weixin.qq.com/sns/userinfo?access_token='+accesstoken+'&openid='+openid+'&lang=zh_CN ';
return fetch(tokenurl).then(function(res){
return res.json()
})
}).then(function(json){
res.send(json)
})
});
module.exports = router;
users
这个页面获取的是** JS-SDK的 signature**
var express = require('express');
var router = express.Router();
var fetch = require('node-fetch');
var sha1 = require('sha1');
router.get('/', function(req, res, next) {
var appid = 'wx9bf16c912ddec096';
var secret = '6841a50bd9b6e99c77d0e052e5e5eb2a';
var aturl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+appid+'&secret='+secret;
var access_token;
fetch(aturl).then(function(res){
return res.json()
}).then(function(json){
access_token = json.access_token;
var ticketurl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='+access_token+'&type=jsapi'
//https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
var jsticket;
fetch(ticketurl).then(function(res){
return res.json()
}).then(function(json){
jsticket = json.ticket;
console.log(req.originalUrl)
var string1 = 'jsapi_ticket='+jsticket+'&noncestr=Wm3WZYTPz0wzccnW×tamp=1414587457&url=http://www.frombottomto.top'+req.originalUrl;
var signature = sha1(string1);
res.render('users',{signature:signature})
})
})
//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
});
module.exports = router;
另外前端渲染页面,记录的是调取微信api
users.ejs
微信js测试
你好
Welcome to 微信