接着上一个版本在上一个分离access-token和ticket的版本

Posted 专注前端开发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接着上一个版本在上一个分离access-token和ticket的版本相关的知识,希望对你有一定的参考价值。

上代码;

本次修改将获取token和ticket分离出来,分别封装在函数中;

每个函数最后一个参数是一个回调参数;

回调函数的参数,是这一步中需要处理的结果;

结果怎么处理,根据传递进去的函数;

 1 var express = require(‘express‘);
 2 var cookieParser = require(‘cookie-parser‘);
 3 var bodyParser = require(‘body-parser‘);
 4 var OAuth = require(‘wechat-oauth‘);
 5 var request = require(‘request‘);
 6 var sha1 = require(‘sha1‘);
 7 var path = require(‘path‘);
 8 var app = express();
 9 app.use(bodyParser.json());
10 app.use(bodyParser.urlencoded({ extended: false }));
11 app.use(cookieParser());
12 app.use(express.static(‘public‘));
13 var port = 18080;
14 var appid = ‘wx75340481908402a8‘;
15 var appsecret = ‘2b6ee0cbeec0114eb539e68ba356329b‘;
16 
17 //首先拼接url
18 var  url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx75340481908402a8&redirect_uri=http%3a%2f%2fwechatapp1.duapp.com%2fcallback&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
19 app.get(‘/test‘,function(req,res){
20     res.redirect(url);
21 });
22 //四步请求打法;
23 //第一步:获得code;
24 app.get(‘/callback‘,function(req,res){
25     var code  = req.query.code;
26     var url = ‘https://api.weixin.qq.com/sns/oauth2/access_token?appid=‘ + appid + ‘&secret=‘ + appsecret + ‘&code=‘ + code + ‘&grant_type=authorization_code‘;
27     //第二步:获得token
28     request.get(url,function(err,response,body) {
29         var json = JSON.parse(body);
30         var refreshUrl = ‘https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=‘ + appid + ‘&grant_type=refresh_token&refresh_token=‘ + json.refresh_token;
31         //第三步:获得refreshtoken和openId;
32         request.get(refreshUrl,function (err,response,refresh) {
33             var json = JSON.parse(refresh);
34             var infoUrl = ‘https://api.weixin.qq.com/sns/userinfo?access_token=‘ + json.access_token + ‘&openid=‘ + json.openid + ‘&lang=zh_CN‘;
35             //第四步:通过上一步刷新得来的refresh和openId请求用户信息;
36             request.get(infoUrl,function(err,response,info) {
37                 var info = JSON.parse(info);
38                 res.send(info);
39             });
40         });
41     });
42 });
43 //分离access_token和jsapi_sdk;
44 //获取token;
45 function getToken(appid,appsecret,cb){
46     var url = ‘https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=‘+appid+‘&secret=‘+appsecret;
47     request.get(url,function(err,response,body) {
48         var token = JSON.parse(body);
49         cb(token);
50     })
51 }
52 //分离ticket
53 
54 function getTicket(page,appid,appsecret,cb) {
55     getToken(appid,appsecret,function(token) {
56         var ticketUrl = ‘https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=‘ + token.access_token + ‘&type=jsapi‘;
57         request.get(ticketUrl, function(err, response, ticket) {
58             var data = JSON.parse(ticket);
59             var timestamp = parseInt(new Date().getTime() / 1000);
60             t.ticket = data.ticket;
61             t.noncestr = sha1(new Date());
62             t.timestamp = timestamp;
63             var string = ‘jsapi_ticket=‘ + t.ticket + ‘&noncestr=‘ + t.noncestr + ‘&timestamp=‘ + timestamp + ‘&url=‘ + page;
64             t.signature = sha1(string);
65             cb(t);
66         });
67     });
68 }
69 
70 var t = {};
71 //1、设置api接口,使前端通过ajax可以获取jsapi-sdk;
72 app.get(‘/wechat/ticket‘,function (req, res) {
73     var page = req.query.page;
74 
75     getTicket(page,appid,appsecret,function(data,err) {
76         res.json(data);
77     })
78 });
79 
80 
81 
82 app.listen(port);

 

以上是关于接着上一个版本在上一个分离access-token和ticket的版本的主要内容,如果未能解决你的问题,请参考以下文章

Swiper 4.0.7 如何在鼠标悬浮在上方时停止自动切换

Mycat 实现 MySQL 主从架构的读写分离

微信获得access-token

koa-获取微信access-token

如何自动地为a链接加上access-token的header

nodejs开发微信1——微信access-token和tickets的数据模型