on the code;
This modification will obtain tokens and ticket separated, respectively encapsulated in the function;
The last parameter of each function is a callback parameter;
The parameters of the callback function are the results that need to be processed in this step;
How to deal with the result, according to the function passed in;
1 varExpress = require (' Express ');2 varCookieparser = require (' Cookie-parser '));3 varBodyparser = require (' Body-parser '));4 varOAuth = require (' Wechat-oauth ');5 varRequest = require (' request '));6 varSHA1 = require (' SHA1 '));7 varPath = require (' path ');8 varApp =Express ();9 App.use (Bodyparser.json ());TenApp.use (bodyparser.urlencoded ({extended:false })); One App.use (Cookieparser ()); AApp.use (express.static (' public '))); - varPort = 18080; - varAppID = ' wx75340481908402a8 '; the varAppsecret = ' 2b6ee0cbeec0114eb539e68ba356329b '; - - //First splicing URL - varurl = "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 +App.get ('/test ',function(req,res) { - res.redirect (URL); + }); A //Four-step request play; at //First step: Get code; -App.get ('/callback ',function(req,res) { - varCode =Req.query.code; - varurl = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= ' + AppID + ' &secret= ' + appsecret + ' &code= ' + cod E + ' &grant_type=authorization_code '; - //Step Two: Get token -Request.get (URL,function(err,response,body) { in varJSON =Json.parse (body); - varRefreshurl = ' https://api.weixin.qq.com/sns/oauth2/refresh_token?appid= ' + AppID + ' &grant_type=refresh_token &refresh_token= ' +Json.refresh_token; to //Step three: Get Refreshtoken and OpenID; +Request.get (Refreshurl,function(Err,response,refresh) { - varJSON =Json.parse (refresh); the varInfourl = ' https://api.weixin.qq.com/sns/userinfo?access_token= ' + json.access_token + ' &openid= ' + Json.openid + ' & AMP;LANG=ZH_CN '; * //Fourth Step: Refresh and OpenID request user information through the previous step; $Request.get (Infourl,function(err,response,info) {Panax Notoginseng varinfo =Json.parse (info); - Res.send (info); the }); + }); A }); the }); + //separation of Access_token and JSAPI_SDK; - //get tokens; $ functionGetToken (APPID,APPSECRET,CB) { $ varurl = ' https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= ' +appid+ ' &secret= ' +Appsecret; -Request.get (URL,function(err,response,body) { - vartoken =Json.parse (body); the CB (token); - })Wuyi } the //separating ticket - Wu functionGetTicket (PAGE,APPID,APPSECRET,CB) { -GetToken (Appid,appsecret,function(token) { About varTicketurl = ' https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token= ' + Token.access_token + ' &type= Jsapi; $Request.get (Ticketurl,function(Err, response, ticket) { - vardata =Json.parse (ticket); - vartimestamp = parseint (NewDate (). GetTime ()/1000); -T.ticket =Data.ticket; AT.NONCESTR = SHA1 (NewDate ()); +T.timestamp =timestamp; the varstring = ' jsapi_ticket= ' + t.ticket + ' &noncestr= ' + t.noncestr + ' ×tamp= ' + timestamp + ' &url= ' +page; -T.signature =SHA1 (string); $ CB (t); the }); the }); the } the - vart = {}; in //1, set the API interface, so that the front-end through Ajax can get jsapi-sdk; theApp.get ('/wechat/ticket ',function(req, res) { the varpage =Req.query.page; About theGetTicket (Page,appid,appsecret,function(data,err) { the Res.json (data); the }) + }); - the Bayi theApp.listen (port);
Then the previous version was detached from the previous version of Access-token and ticket