See that the full article is not available online, so take notes yourself

1. Wechat applet requests the back end to obtain the session_key and store it in storageSync()

Wx.login () gets the code, carries the code request back end, which calls Auth.code2session, and uses the code for session_key information.

Then store it locally to the wechat applet or store it locally to the server, which I have here for convenience.

2. Wechat applet obtains the number of steps

Wx.getwerundata ({success: res) => {// Get the number of steps including encryptedData and iv this.decryptedData(res.encryptedData, res.iv); }})Copy the code

3. Request the backend to parse encrypted data

decryptedData(encryptedData, iv) { let SessionKey = wx.getStorageSync("session_key") let data = { EncryptedData: encryptedData, Iv: Iv, SessionKey,} TokenRequest ({url, data, header, method}). Then (res=>{if(res.data.code==0) {this.setData({step: res.data.data.step, }) } }) },Copy the code

4. Back-end decoding

type WeRunCryptDataInput struct { EncryptedData string `json:"encryptedData" note:""` Iv string `json:"iv" note:""` SessionKey string 'json:" SessionKey "note:""'} type WeRunData struct {TimeStamp, uint64 'JSON :" TimeStamp" note:" time "' Step uint64 'json:" Step "note:" Step"'} type WeRunDataList struct {StepInfoList []*WeRunData 'json:" StepInfoList "'} func (s *Data) DecryptWeRunData(argument WeRunCryptDataInput) (WeRunData, business.error) {// Decode aesKey, err := base64.StdEncoding.DecodeString(argument.SessionKey) if err ! = nil { return nil, business.NewError(errors.InternalError, err) } cipherText, err := base64.StdEncoding.DecodeString(argument.EncryptedData) if err ! = nil { return nil, business.NewError(errors.InternalError, err) } ivBytes, err := base64.StdEncoding.DecodeString(argument.Iv) if err ! = nil { return nil, business.NewError(errors.InternalError, err) } block, err := aes.NewCipher(aesKey) if err ! = nil { return nil, business.NewError(errors.InternalError, err) } mode := cipher.NewCBCDecrypter(block, ivBytes) mode.CryptBlocks(cipherText, cipherText) cipherText, err = pkcs7Unpad(cipherText, block.BlockSize()) if err ! = nil { return nil, business.NewError(errors.InternalError, err) } result := &WeRunDataList{} err = json.Unmarshal(cipherText, &result) if err ! = nil {return nil, business.newerror (errors.internalError, err)} // do something else, Return result.StepInfoList[30], nil } // pkcs7Unpad returns slice of the original data without padding func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) { if blockSize <= 0 { return nil, fmt.Errorf("invalid block size") } if len(data)%blockSize ! = 0 || len(data) == 0 { return nil, fmt.Errorf("invalid PKCS7 data") } c := data[len(data)-1] n := int(c) if n == 0 || n > len(data) { return nil, fmt.Errorf("invalid padding on input") } for i := 0; i < n; i++ { if data[len(data)-n+i] ! = c { return nil, fmt.Errorf("invalid padding on input") } } return data[:len(data)-n], nil }Copy the code