NODE 微信签名
為什么80%的碼農都做不了架構師?>>> ??
/*** Created by liuhu on 2017/6/23.* 微信獲取簽名* 對于簽名需要對token ticket做服務端緩存以防api調用次數超過上限*/ const request = require("request"); const sha1 = require("sha1"); const signature = {info:{appId:"",secret:"",tokenUrl:"https://api.weixin.qq.com/cgi-bin/token?",ticketUrl:"https://api.weixin.qq.com/cgi-bin/ticket/getticket?"},creNonceStr:function() {return Math.random().toString(36).substr(2, 15);},creTimeStamp:function () {return Math.floor(new Date().getTime() / 1000) + '';},getToken : function (callback) {let that = this;request.get(this.info.tokenUrl+'grant_type=client_credential&appid='+this.info.appId+'&secret='+this.info.secret, function(error, response, body) {let token=JSON.parse(body);that.getTicket(token.access_token,callback);});},getTicket:function (o,callback) {let token = o;request.get(this.info.ticketUrl+'access_token='+token+'&type=jsapi',function(error, response, body){let ticket=JSON.parse(body).ticket;let o = {token:token,ticket:ticket};callback(o);})},calcSignature:function (ticket, noncestr, ts, url) {let string = 'jsapi_ticket=' + ticket + '&noncestr=' + noncestr + '×tamp=' + ts + '&url=' + url;//獲得簽名return sha1(string);},inInterface:function(page,callback){let that = this;this.getToken(function (d) {let time = that.creTimeStamp();let non = that.creNonceStr();const signature = that.calcSignature(d.ticket, non, time, page);let resObj = {appId:that.info.appId,timestamp:time,nonceStr:non,signature:signature};//resObj需要返回前端 供wx.config注入callback(resObj);})} };module.exports = signature;轉載于:https://my.oschina.net/530520/blog/1021856
總結
- 上一篇: java五子棋小游戏含免费源码
- 下一篇: [转载]信息安全从业参考