1. Data communication between the small program and the public account unionID

Small programs and public accounts must be the same subject, and must be bound to the same open platform, which is divided into two points:

  • 1. Open platform initiative creation at this time, you need to manually bind the small program and the public number on the open platform, interface binding is not available;

  • 2. The open platform is created by a third-party interface. After the public account and the small program are authorized to manage the third-party open platform, they can be bound by the interface.

2. About the small program ext.json

Json file can be added to the root directory of the applet for debugging. For example, add the following configuration to the file:

{
  "extEnable": true."extAppid": "wx90d87655678517ee"."ext": {
    "id": 100}}Copy the code

Set extEnable to true, the logic of the applet will be executed according to extAppid in ext.json, and the parameters in ext can be obtained in either of the following ways:

/ / way
if(wx.getExtConfig) {
  wx.getExtConfig({
    success: function (res) {
      console.log(res.extConfig)
    }
  })
}
2 / / way
let extConfig = wx.getExtConfigSync? wx.getExtConfigSync(): {}
console.log(extConfig)
Copy the code

In addition, third parties can also specify ext fields when they help authorize applets to submit code. We need to pay attention to the ext_JSON format, because the ext_JSON structure is very loose, so many times we need to assemble this field, so it needs to be similar to the following format:

"{\"extAppid\": \"wx90d87655678517ee\",\"ext\":{\"id\":100}}"
Copy the code

In addition, the ext_json submitted through the interface overwrites the corresponding information in the configuration ext. Json. However, the developer tool cannot obtain the information in the ext_json submitted through the interface, only the information in the ext.

3. Mpvue combined with ext. Json

Currently, the latest version of MPvue does not seem to support the configuration of ext.json in main.js, so there are two ways to use it:

  • 1. Write a plug-in to compile ext.json from SRC in dist (mpvue compiled source directory)
  • 2. Create an ext.json file directly in the dist directory but remember to set the extEnable property to true.

4. Decryption algorithm after user applets authorization (JS version)

import crypto from 'crypto-browserify'

function WXBizDataCrypt (appId, sessionKey) { // Applet appid, user login using the sessionKey code obtains from the interface
  this.appId = appId
  this.sessionKey = sessionKey
}

WXBizDataCrypt.prototype.decryptData = function (encryptedData, iv) {
  // base64 decode
  var sessionKey = new Buffer(this.sessionKey, 'base64')
  encryptedData = new Buffer(encryptedData, 'base64')
  iv = new Buffer(iv, 'base64')

  try {
     / / decryption
    var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv)
    // Set the automatic padding to true and remove the padding
    decipher.setAutoPadding(true)
    var decoded = decipher.update(encryptedData, 'binary'.'utf8')
    decoded += decipher.final('utf8')
    decoded = JSON.parse(decoded)
  } catch (err) {
    console.log(err)
    throw new Error('Illegal Buffer')}if(decoded.watermark.appid ! = =this.appId) {
    throw new Error('Illegal Buffer')}return decoded
}

let DataCrypt = {
  getData (appId, sessionKey, encryptedData, iv) {
    var pc = new WXBizDataCrypt(appId, sessionKey)
    return pc.decryptData(encryptedData, iv)
  }
}

export defaultUse of DataCrypt:import DataCrypt from "path/to/DataCrypt"

const data = DataCrypt.getData(...)

Copy the code