zoukankan      html  css  js  c++  java
  • 接着上一个版本在上一个分离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);
    坚持下去就能成功
  • 相关阅读:
    Android 多渠道打包,上百渠道,秒打签名
    Failed to collect certificates from /data/app/vmdl201020547.tmp/base.apk: META-INF/CERT.SF indicates /data/app/vmdl201020547.tmp/base.apk is signed using APK Signature Scheme v2, but no such signature
    React java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.edaixi.activity/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit
    Effective Java 电子书 apk版本下载
    javascript数组遍历的几种常用方法性能分析对比
    微信小程序开发——列表分页上拉加载封装实现(订单列表为例,订单状态改变后刷新列表滚动位置不变)
    微信小程序开发——点击防重的解决方案
    微信小程序开发——点击按钮退出小程序的实现
    css选择器的优先级
    html页面的CSS、DIV命名规则(仅供参考学习)
  • 原文地址:https://www.cnblogs.com/suoking/p/5341286.html
Copyright © 2011-2022 走看看