获取ticket是需要拼接url,url中需要获得的tocken
1、先判断有无tocken;若有则拼接url;
2、请求url;
a、处理回调数据,注意时间戳和签名处理方式,此两项并未存到ticket中,
b、存储回调数据,存储时将时间戳和签名存到ticket中;注意此两项并未存到数据库,而是在存储时动态生成的时间戳和签名,签名还有一个签名函数;
c、返回存储数据。
function refreshTicket(appid, appsecret, page, ticket, res) { getToken(appid, appsecret, function (token, err) { if (err === null) { if (!_.isEmpty(token.access_token)) { // 获取 jsapi_ticket var ticket_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' + token.access_token + '&type=jsapi'; request.get(ticket_url, function (error, response, body) { var t = ticket === null ? new WeChatTicket() : ticket; var data = JSON.parse(body); if(!data.ticket) res.json({error: response}); else { t.appid = appid; t.ticket = data.ticket; t.noncestr = sha1(new Date()); t.expires_in = data.expires_in; t.at = parseInt(new Date().getTime() / 1000); var timestamp = parseInt(new Date().getTime() / 1000); var signature = wechatSignature(t, page, timestamp); t.save(function (err) { var json = t.toJSON(); json.at = timestamp; json.signature = signature; res.json({ticket: json, error: err}); }); } }); } else res.json({error: err}); } else res.json({error: err}); }); } /* 微信签名实现 */ function wechatSignature(t, page, timestamp) { var string = "jsapi_ticket=" + t.ticket + "&noncestr=" + t.noncestr + "×tamp=" + timestamp + "&url=" + page; return sha1(string); }
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!