NodeJS WeChat sharing

The example of this article shared the specific code of NodeJS WeChat sharing for everyone. For your reference, the specific content is as follows

1. Access WeChat JSSDK

Direct reference or download local bank

, JS request Node background signature, random number, timestamp class

Function getWxSignature () {$ .ajax ({url: “/ activitywxshaer”, type: ‘post’, data: {urlhref: location.href}: function (jsons) {var Data = JSON .parse (jsons); wx.config ({Debug: false, // On debug mode, the return value of all APIs called will come out on the client ALERT, to view the incoming parameters, can open, parameters on the PC The information will be played through the log, only when the PC is only printed. AppID: ‘XXXXXXXXX’, // Required, the unique identity of the public number TimeStamp: Data.TimeStamp, // Required, generated the timestamp of the signature Noncestr: Data .noncestr, // mustal, generate signature random string signature: data.signature, // mustal, signature, see Appendix 1 Jsapilist: [‘Onmenusharetimeline’, ‘OnmenushareAppMessage “,’onmenushareqq’, ‘onmenushareweibo’, ‘onmenushareqzone’] // Required, the JS interface list, all JS interface lists see Appendix 2}); ShaRefunc (); // WeChat sharing interface is ready}})
  3, NodeJs Express routing  


/ ** * Share * / router.post (‘/ ActivitywxShaer’, Function (Req, Res, next) {let hrefurl = Req.body.urlhref; wxshare.Prototype.accesstoke (HREFURL, FUNCTION (DATA) {Res. Json (DATA); });});

Fourth, NodeJS signature, WeChat key signature, I used Sha1, I saw a lot of use of SHA- 1, HEX, I don’t know if it is my environment, I can’t use it in the end
   
/ ** * Signature * @Param {*} URL * / Sign (URL) {let _this = this; var ret = {jsapi_ticket: configend.ticket, noncesser:Configend.noncestr, TimeStamp: configend.timestamp, URL: URL}; var string = _this.rawstring (RET); var shaobjs = sha1 (string); return shaobjs;}; four, nodejs background code direct code [JavaScript] View Plain CopyVar URL = Require (‘URL’); Var Request = Require (‘Request’); Var Sha1 = Require (‘Sha1’); Let config = {appid: “”, // WeChat public number ID appsecret: ” “// WeChat public number is}, configend = {appid: ‘, access_token:’ ‘, Ticket:’, timestamp: ‘, // mustal, generate the signature timestamp Noncestr:’, // Required, generate the signature random string signature: ”, // mustal, signature, see Appendix 1}; / ** * WeChat sharing * / class wxshare {/ ** * Request to get access_token method port * @Param {* URL link} hrefurl * @param {* callback request method} Callback * / accessToken (Hrefurl, callback) {// Get access_token let _this =this; var tokenUrl = ‘https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=’ + config.appID + ‘& secret =’ + config.appSecret; request (tokenUrl, function (error , response, body) {if (response.statusCode && response.statusCode === 200) {body = JSON.parse (body); configEnd.access_token = body.access_token; _this.upJsapiTicket (hrefURL, body.access_token, callback) }});}; / ** * Get

jsapi_ticket * @Param {* URL link} hrefurl * @Param {* token} access_ttoken * @Param {* callback request method} callback * / upjsapiticket (HREFURL Access_ttoken, callback {// jsapi_ticket let _this = this; var ticketurl = ‘https://api.weixin.qq.com/cgi-bin/ticket/getticket data_token=’ + access_ttoken + ‘& TyPE = JSAPI ‘; Request (TicketURL, Function (Err, Response, Content) {Content = JSON.PARSE (Content); if (content.errcode == 0) {confiGend.ticket = content.ticket; // ticket configend. TimeStamp = _this.createtimestamp (); // Timestamp configend.noncestr = _this.createnoncestr (); // Random CONFIGEND.SIGNATURE = _THIS.SIGN (HREFURL); // Sign Callback && Callback (configend); // Press Front end JS method}}}; / ** * random string * / cretenoncestr () {return math.random (). Tostring (36) .substr (2, 15);}; / ** * Timestamp * / CreateTimeStamp () {Return Parseint (New Date (). gettime () / 1000) .tostring ();}; / ** * Splic string * @Param {*} args * / rawstring (args) {var Keys = Object Keys (args); keys = keys.Sort () var newargs = {}; keys.Fore (Function (key) {newargs [key.tolowercase ()] = args [key];}; var string = ‘; for (var k in newargs) {string + = ‘&’ + k + ‘=’ + newargs [k];} String = string.substr (1); return string;}; / ** * Signature * @Param {*} url * / sign (URL) {let _this = this; var ret = {jsapi_ticket: configEnd.ticket, nonceStr: configEnd.nonceStr, timestamp: configEnd.timestamp, url: url}; var string = _this.rawString (ret); var shaObjs = sha1 (string) Return Shaobjs;};} Module.Exports = wxshare;

An ordinary Ajax requests the NodeJS background, and the background returns a packaged data.
 The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Cloud.                      
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment