Guest User

Untitled

a guest
Aug 30th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //WeChat JS Library https://res.wx.qq.com/open/js/jweixin-1.0.0.js
  2.  
  3. //REFERENCES
  4. //WeChat official documentation on sharing API http://open.wechat.com/cgi-bin/newreadtemplate?t=overseas_open/docs/oa/web/js-sdk#sharing-apis
  5. //PHP version of sig gen https://github.com/31ten/wechat-qrcode-scanner/blob/master/jssdk.php
  6. //SHA1 Code http://coursesweb.net/javascript/sha1-encrypt-data_cs
  7.  
  8. //APP ID GENERATION
  9. //Generated off sandbox from http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
  10. //Should sign up here http://open.wechat.com/cgi-bin/newreadtemplate?t=overseas_open/section_detail&show=web
  11.  
  12. //For us to configure WeChat, we must do the following
  13. //Step 1. Acquire appID and appSecret
  14. //Step 2. Acquire WeChat Access Token using the appID and appSecret with the getWeChatAccessToken function
  15. //Step 3. Acquire JSApiTicket using the access token with the getJSApiTicket function
  16. //Step 4. Generate a signature which is a SHA1 encoded string including JSAPITicket, url, a nonce nonsense string and a timestamp
  17. //Step 5. Pass the appID, timestamp, nonce, generated signature and requested api list through wx.config
  18. //Step 6. Call the weChat mobile share when desired
  19.  
  20. define([
  21.     'jquery',
  22.     'jweixin',
  23.     'configurator-env',
  24.     'configurator-sharing'
  25. ], function($, wx) {
  26.     var that = Configurator;
  27.     if (!that.enableWechat) {
  28.         return;
  29.     } else {
  30.         that.WeChatConfigs = {
  31.             "appID": "wx320d0e111fd5feba",
  32.             "appSecret": "26c9b6f25b7b3ac097c0e6795c5ed7fd",
  33.             "timestamp": Math.floor(Date.now() / 1000),
  34.             "nonce": "",
  35.             "signature": "",
  36.             "jsApiTicket": ""
  37.         };
  38.  
  39.         that.WeChat = {
  40.  
  41.             /* ==================================================
  42.              FUNCTION: initiateWeChat
  43.              Arguments: none
  44.  
  45.              Initiates the WeChat functionality by generating a signature
  46.              ===================================================== */
  47.             "initiateWeChat": function() {
  48.                 that.WeChatConfigs.nonce = that.WeChat.genNonce(18);
  49.                 that.WeChat.getWeChatAccessToken();
  50.             },
  51.  
  52.             /* ==================================================
  53.              FUNCTION: getWeChatAccessToken
  54.              Arguments: none
  55.  
  56.              Generates Access token via app id and appsecret
  57.              ===================================================== */
  58.             "getWeChatAccessToken": function() {
  59.                 var verificationEndpoint = "https://api.wechat.com/cgi-bin/token?grant_type=client_credential&appid="+ WeChatConfigs.appID + "&secret=" + WeChatConfigs.appSecret;
  60.                 $.ajax({
  61.                     type: "POST",
  62.                     url: verificationEndpoint,
  63.                     data: {},
  64.                     dataType: "json"
  65.                 })
  66.                     .done(function(data) {
  67.                         that.WeChat.getJSApiTicket(data.access_token);
  68.                     });
  69.             },
  70.  
  71.             /* ==================================================
  72.              FUNCTION: genJSApiTicket
  73.              Arguments: accesstoken
  74.  
  75.              Callback from genWeChatAccessToken which generates the JSAPI Ticket
  76.              required by the WeChat signature
  77.              ===================================================== */
  78.             "getJSApiTicket": function(accessToken) {
  79.                 var jsApiTicketEndpoint = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + accessToken;
  80.                 $.ajax({
  81.                     type: "POST",
  82.                     url: jsApiTicketEndpoint ,
  83.                     data: {},
  84.                     dataType: "json"
  85.                 })
  86.                 .done(function(data) {
  87.                     that.WeChatConfigs.jsApiTicket = data;
  88.                     that.WeChat.weChatSignature();
  89.                     that.WeChat.configWeChat();
  90.                 });
  91.             },
  92.  
  93.             /* ==================================================
  94.              FUNCTION: weChatSignature
  95.              Arguments: Object
  96.  
  97.              Reads generated jsapi_ticket and creates the SHA1 encoded signature
  98.              ===================================================== */
  99.             "weChatSignature": function() {
  100.                 var signature = {
  101.                     "jsapi_ticket": that.WeChatConfigs.jsApiTicket,
  102.                     "noncestr": that.WeChatConfigs.nonce,
  103.                     "timestamp": that.WeChatConfigs.timestamp,
  104.                     "url": window.location.href
  105.                 };
  106.                 signature = JSON.stringify(signature);
  107.                 signature = that.SHA1(signature);
  108.                 that.WeChat.signature = signature;
  109.             },
  110.  
  111.             /* ==================================================
  112.              FUNCTION: configWeChat
  113.              Arguments: Object
  114.  
  115.              Configures WeChat SDK so that we have can gain access to their APIs
  116.              ===================================================== */
  117.             "configWeChat": function() {
  118.                 wx.config({
  119.                     debug: true,
  120.                     appId: that.WeChatConfigs.appID, //Our appID
  121.                     timestamp: that.WeChatConfigs.timestamp, //Timestamp in seconds
  122.                     nonceStr: that.WeChatConfigs.nonce, //Our generated nonsense string
  123.                     signature: that.WeChatConfigs.signature, //a SHA1 encoded string including JSAPITicket, url, a nonce nonsense string and a timestamp
  124.                     jsApiList: [
  125.                         'onMenuShareTimeline'
  126.                     ]
  127.                 });
  128.             },
  129.  
  130.             /* ==================================================
  131.              FUNCTION: weChatMobileShare
  132.              Arguments: title, link and image
  133.  
  134.              Utilizes WeChat API to share to a user's Moments Page
  135.              ===================================================== */
  136.             "weChatMobileShare": function(title,link,img) {
  137.                 wx.onMenuShareTimeline({
  138.                     title: title, // Sharing title
  139.                     link: link, // Sharing link
  140.                     imgUrl: img, // Sharing image URL
  141.                     success: function () {
  142.                         console.log("success");
  143.                     },
  144.                     cancel: function () {
  145.                         console.log("failure");
  146.                     }
  147.                 });
  148.             },
  149.  
  150.             /* ==================================================
  151.              FUNCTION: genNonce
  152.              Arguments: length
  153.  
  154.              Generates a nonsense string with predetermined length
  155.              ===================================================== */
  156.             "genNonce": function(length) {
  157.                 var nonce = "";
  158.                 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  159.                 for(var i = 0; i < length; i++) {
  160.                     nonce += possible.charAt(Math.floor(Math.random() * possible.length));
  161.                 }
  162.                 return nonce;
  163.             }
  164.         };
  165.  
  166.         /* ==================================================
  167.          FUNCTION: SHA1
  168.          Arguments: msg
  169.  
  170.          Encodes a message with SHA1
  171.          ===================================================== */
  172.         that.SHA1 = function(msg) {
  173.             function rotate_left(n,s) {
  174.                 var t4 = ( n<<s ) | (n>>>(32-s));
  175.                 return t4;
  176.             }
  177.             function lsb_hex(val) {
  178.                 var str="";
  179.                 var i;
  180.                 var vh;
  181.                 var vl;
  182.                 for( i=0; i<=6; i+=2 ) {
  183.                     vh = (val>>>(i*4+4))&0x0f;
  184.                     vl = (val>>>(i*4))&0x0f;
  185.                     str += vh.toString(16) + vl.toString(16);
  186.                 }
  187.                 return str;
  188.             }
  189.             function cvt_hex(val) {
  190.                 var str="";
  191.                 var i;
  192.                 var v;
  193.                 for( i=7; i>=0; i-- ) {
  194.                     v = (val>>>(i*4))&0x0f;
  195.                     str += v.toString(16);
  196.                 }
  197.                 return str;
  198.             }
  199.             function Utf8Encode(string) {
  200.                 string = string.replace(/\r\n/g,"\n");
  201.                 var utftext = "";
  202.                 for (var n = 0; n < string.length; n++) {
  203.                     var c = string.charCodeAt(n);
  204.                     if (c < 128) {
  205.                         utftext += String.fromCharCode(c);
  206.                     }
  207.                     else if((c > 127) && (c < 2048)) {
  208.                         utftext += String.fromCharCode((c >> 6) | 192);
  209.                         utftext += String.fromCharCode((c & 63) | 128);
  210.                     }
  211.                     else {
  212.                         utftext += String.fromCharCode((c >> 12) | 224);
  213.                         utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  214.                         utftext += String.fromCharCode((c & 63) | 128);
  215.                     }
  216.                 }
  217.                 return utftext;
  218.             }
  219.             var blockstart;
  220.             var i, j;
  221.             var W = new Array(80);
  222.             var H0 = 0x67452301;
  223.             var H1 = 0xEFCDAB89;
  224.             var H2 = 0x98BADCFE;
  225.             var H3 = 0x10325476;
  226.             var H4 = 0xC3D2E1F0;
  227.             var A, B, C, D, E;
  228.             var temp;
  229.             msg = Utf8Encode(msg);
  230.             var msg_len = msg.length;
  231.             var word_array = [];
  232.             for( i=0; i<msg_len-3; i+=4 ) {
  233.                 j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
  234.                     msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
  235.                 word_array.push( j );
  236.             }
  237.             switch( msg_len % 4 ) {
  238.                 case 0:
  239.                     i = 0x080000000;
  240.                     break;
  241.                 case 1:
  242.                     i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
  243.                     break;
  244.                 case 2:
  245.                     i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
  246.                     break;
  247.                 case 3:
  248.                     i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8  | 0x80;
  249.                     break;
  250.             }
  251.             word_array.push( i );
  252.             while( (word_array.length % 16) != 14 ) word_array.push( 0 );
  253.             word_array.push( msg_len>>>29 );
  254.             word_array.push( (msg_len<<3)&0x0ffffffff );
  255.             for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {
  256.                 for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
  257.                 for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
  258.                 A = H0;
  259.                 B = H1;
  260.                 C = H2;
  261.                 D = H3;
  262.                 E = H4;
  263.                 for( i= 0; i<=19; i++ ) {
  264.                     temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  265.                     E = D;
  266.                     D = C;
  267.                     C = rotate_left(B,30);
  268.                     B = A;
  269.                     A = temp;
  270.                 }
  271.                 for( i=20; i<=39; i++ ) {
  272.                     temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  273.                     E = D;
  274.                     D = C;
  275.                     C = rotate_left(B,30);
  276.                     B = A;
  277.                     A = temp;
  278.                 }
  279.                 for( i=40; i<=59; i++ ) {
  280.                     temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  281.                     E = D;
  282.                     D = C;
  283.                     C = rotate_left(B,30);
  284.                     B = A;
  285.                     A = temp;
  286.                 }
  287.                 for( i=60; i<=79; i++ ) {
  288.                     temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  289.                     E = D;
  290.                     D = C;
  291.                     C = rotate_left(B,30);
  292.                     B = A;
  293.                     A = temp;
  294.                 }
  295.                 H0 = (H0 + A) & 0x0ffffffff;
  296.                 H1 = (H1 + B) & 0x0ffffffff;
  297.                 H2 = (H2 + C) & 0x0ffffffff;
  298.                 H3 = (H3 + D) & 0x0ffffffff;
  299.                 H4 = (H4 + E) & 0x0ffffffff;
  300.             }
  301.             temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  302.  
  303.             return temp.toLowerCase();
  304.         };
  305.     }
  306. });
Advertisement
Add Comment
Please, Sign In to add comment