Prior to the start

Step 1: Create an application at the management end of the corresponding enterprise wechat privatization

Step 2: Configure the trusted domain name in the self-built app: open.gov.weixin.qq.com (your own server’s domain name)

After these two steps are configured, you are ready for back-end signing

The node the back-end

Step 1: Obtain the access_token, according to the interface of the official document:

Obtain an Access_Token (valid for 7200 seconds. Developers must cache the access_token globally in their own services.) An Access_token is a globally unique ticket for an application. Access_token needs to be exchanged with CorpID and Secret. Different Secret will return different access_token. Normally, the validity period of the access_token is 7200 seconds. If the access_token is obtained repeatedly within the validity period, the same result is returned. The access_token reserves at least 512 bytes of storage space.

Request description Https request method: GET {{your private address domain}}/cgi-bin/ getToken? corpid=id&corpsecret=secrect

Url: 'your private address domain name '+'/cgi-bin/ getToken? Corpid =' + 'corpsecret=' +' corpsecret=' + ' reject) { request(this.url, function(error, response, body) { error ? reject(error) : resolve({ response: response, body: body }) })Copy the code

Step 2: Get the ticket of Config and AgentConfig respectively. Note that the two requests are different

Jsapi_ticket (valid for 7200 seconds, developers must cache jSAPi_ticket globally in their own services) :

{{your private address domain}}/cgi-bin/get_jsapi_ticket? access_token=ACCESS_TOKEN

Ticket of agentConfig: {{your private address domain}}/cgi-bin/ticket/get? access_token=ACCESS_TOKEN&type=agent_config

Var conifgurlURL =' your private address '+'/cgi-bin/get_jsapi_ticket? Access_token ='+' access_token' var agentConfigUrl= 'your private address '+' /cgi-bin/ticket/get? Access_token = config = new Promise(function(resolve, reject) {request(conifgurl, function(error, response, body) { error ? reject(error) : resolve({ response: response, body: body }) }) }) agentConfig = new Promise(function(resolve, reject) { request(agentconfigUrl, function(error, response, body) { error ? reject(error) : resolve({ response: response, body: Return Promise. All ([config, Var interItem = {} interitem.noncestr = weixin. RandomString (17) var interItem = {} interItem. interItem.timestamp = Date.parse(new Date()) / 1000 let configItem = { "jsapi_ticket": JSON.parse(result[0].body).ticket, "noncestr": interItem.noncestr, "timestamp": Interitem.timestamp, // interitem.requrl Request address of your own window "url": interitem.requrl}; let agentconfigItem = { "jsapi_ticket": JSON.parse(result[1].body).ticket, "noncestr": interItem.noncestr, "timestamp": interItem.timestamp, "url": interItem.reqUrl }; let signstr = ""; for (let item in configItem) { signstr += item + "=" + configItem[item] + "&" } signstr = signstr.substr(0, signstr.length - 1); interItem.signature = weixin.sha1(signstr) let agentsignstr = ""; for (let item in agentconfigItem) { agentsignstr += item + "=" + agentconfigItem[item] + "&" } agentsignstr = agentsignstr.substr(0, agentsignstr.length - 1); Interitem.agentsignature = weixin.sha1(agentSignstr) // Put the result of the request into the corresponding res object and return var res = {noncestr: interItem.noncestr, timestamp : interItem.timestamp, signature : interItem.signature, agentSignature : interItem.agentSignature } return res }) }).catch((error) => { console.log(error) })Copy the code

The related functions used are:

/ / signature auxiliary function const utils = the require (' @ tencent/mail. Utils') module. Exports = {/ / generate random string randomString (len) {len = len | | 32; var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /**** removes confusing characters oOLl,9gq,Vv,Uu,I1****/ var maxPos = $chars.length; var pwd = ''; for (let i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; }, encodeUTF8(s) { var i, r = [], c, x; for (i = 0; i < s.length; i++) if ((c = s.charCodeAt(i)) < 0x80) r.push(c); else if (c < 0x800) r.push(0xC0 + (c >> 6 &amp; 0x1F), 0x80 + (c &amp; 0x3F)); Else {if ((x = c ^ 0xD800) >> 10 == 0) // Convert 4-byte UTF-16 to Unicode C = (x << 10) + (s.charcodeat (++ I) ^ 0xDC00) + 0x10000, r.push(0xF0 + (c >> 18 &amp; 0x7), 0x80 + (c >> 12 &amp; 0x3F)); else r.push(0xE0 + (c >> 12 &amp; 0xF)); r.push(0x80 + (c >> 6 &amp; 0x3F), 0x80 + (c &amp; 0x3F)); }; return r; Sha1 (s) {var data = new Uint8Array(this.encodeutf8 (s)) var I, j, t; var l = ((data.length + 8) >>> 6 << 4) + 16, s = new Uint8Array(l << 2); s.set(new Uint8Array(data.buffer)), s = new Uint32Array(s.buffer); for (t = new DataView(s.buffer), i = 0; i < l; i++) s[i] = t.getUint32(i << 2); s[data.length >> 2] |= 0x80 << (24 - (data.length &amp; 3) * 8); s[l - 1] = data.length << 3; var w = [], f = [ function() { return m[1] &amp; m[2] | ~m[1] &amp; m[3]; }, function() { return m[1] ^ m[2] ^ m[3];  }, function() { return m[1] &amp; m[2] | m[1] &amp; m[3] | m[2] &amp; m[3]; }, function() { return m[1] ^ m[2] ^ m[3];  } ], rol = function(n, c) { return n << c | n >>> (32 - c); }, k = [1518500249, 1859775393, -1894007588, -899497514], m = [1732584193, -271733879, null, null, -1009589776]; m[2] = ~m[0], m[3] = ~m[1]; for (i = 0; i < s.length; i += 16) { var o = m.slice(0); for (j = 0; j < 80; j++) w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1), t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0, m[1] = rol(m[1], 30), m.pop(), m.unshift(t); for (j = 0; j < 5; j++) m[j] = m[j] + o[j] | 0; }; t = new DataView(new Uint32Array(m).buffer); for (var i = 0; i < 5; i++) m[i] = t.getUint32(i << 2); var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer), function(e) { return (e < 16 ? "0" : "") + e.toString(16); }).join(""); return hex; }}Copy the code

VUE front-end processing

Config:

Wx. config({beta: true, // must be written this way, otherwise wx.invoke jsAPI will have a problem: True, // If debug mode is enabled, the return value of all API calls will be alert on the client side. If you want to view the parameters passed in, you can open it on the PC side, and the parameter information will be printed in log, and only printed on the PC side. AppId: resData[0]. Corpid, // Mandatory, corpid timestamp: This. Res., timestamp, / / required to generate the signature of the timestamp nonceStr: this. Res., nonceStr, / / required, randomly generated signature string of signature: This.res.signature, // Required, signature, see appendix -js-SDK using permission signature algorithm jsApiList: ["getNetworkType","onHistoryBack","scanQRCode","chooseImage","getLocalImgData","agentConfig","onMenuShareAppMessage","on MenuShareWechat", "checkJsApi","openChatWithMsg","shareAppMessage","shareWechatMessage","onMenuShareTimeline","previewImage","checkIsSuppo rtSoterAuthentication", "uploadImage","downloadImage","previewFile","chooseVideo","uploadVideo","downloadVideo","startRecord","stopRecord","play Voice","pauseVoice", "stopVoice","startRecordVoiceBuffer","stopRecordVoiceBuffer","translateVoice","uploadVoice","downloadVoice","onVoiceReco rdEnd","onVoicePlayEnd", "onRecordBufferReceived","startWifi","stopWifi","connectWifi","getWifiList","onGetWifiList","onWifiConnected","getConnec tedWifi", "openBluetoothAdapter","closeBluetoothAdapter","getBluetoothAdapterState","onBluetoothAdapterStateChange","startBluetoot hDevicesDiscovery", "stopBluetoothDevicesDiscovery","getBluetoothDevices","onBluetoothDeviceFound","getConnectedBluetoothDevices","createBLE Connection", "closeBLEConnection","onBLEConnectionStateChange","getBLEDeviceServices","getBLEDeviceCharacteristics","readBLECharacter isticValue", "writeBLECharacteristicValue","notifyBLECharacteristicValueChange","onBLECharacteristicValueChange","startBeaconDiscover y","stopBeaconDiscovery", "onBeaconUpdate","getBeacons","onBeaconServiceChange","setClipboardData","getClipboardData","onNetworkStatusChange","ope nLocation", "getLocation","startAutoLBS","stopAutoLBS","onLocationChange","hideOptionMenu","showOptionMenu","closeWindow","hideMenuI tems","showMenuItems", "hideAllNonBaseMenuItem","showAllNonBaseMenuItem","onUserCaptureScreen","openUrl","showWatermark","hideWatermark","check IsSoterEnrolledInDevice", "StartSoterAuthentication ","bioassayAuthentication","launch3rdApp","getInstallState","request3rdApp"," OCR "] A list of JS interfaces that need to be used, any interface to be called needs to be passed in});Copy the code

Configuration related to agentConfig:

Wx. invoke("agentConfig", {beta: true, // this must be done otherwise the JsAPI in the form of wx.invoke will have a problem: True, // If debug mode is enabled, the return value of all API calls will be alert on the client side. If you want to view the parameters passed in, you can open it on the PC side, and the parameter information will be printed in log, and only printed on the PC side. Agentid: resData[0]. Agentid: resData[0]. Agentid: resData[0]. This.res. timestamp, // Required, generated signature timestamp, int type, such as 1539100800 nonceStr: this.res. nonceStr, // Required, generated signature random string signature: This. Res. AgentSignature, / / will fill, sign, see appendix 5 jsApiList: ["openUserProfile","getStepCount","addCalendarEvent","getAllPhoneContacts","selectEnterpriseContact"] // Mandatory, the required JS interface list, See Appendix 2 for a list of all JS interfaces.Copy the code