微信小程序-MD5加密

签名串:按照接口中定义的参数名按首字母(首字母相同看第二个字母,依此类推)顺序进行排列,将所有参数值(除了 hmac)按照上面的排序通过key=value&方式连接起来,加密方式为MD5。

第一步,下载md5.js,放置于utils文件夹下。

第二步,utils文件夹下,新建sortJson.js,用于顺序key=value并以&连接。

function jsonSort(jsonObj) {
  let arr = [];
  for (var key in jsonObj) {
    arr.push(key)
  }
  arr.sort();
  let str = '';
  for (var i in arr) {
    str += arr[i] + "=" + jsonObj[arr[i]] + "&"
  }
  return str.substr(0, str.length - 1)
}

exports.jsonSort = jsonSort;

第三步,所需页面引入并使用。

const { jsonSort } = require("../../utils/sortJson.js");
const { hexMD5 } = require('../../utils/md5.js');
const MD5_KEY = '';  // 此MD5_KEY为java提供,固定值。

// 此处请求为封装请求,请查看微信小程序请求封装
https://blog.csdn.net/qq_46003166/article/details/104661384

  getPhone(){
    //发起网络请求
    let url = app.globalData.url + '/phoneNumber';
    let reqInfo = {
      requestId: Math.round(new Date().getTime() / 1000),
      distributorId: '111',
      organizationCode: '111',
      signType: 'MD5',
      iv: this.decodePhone[0],
      encryptedData: this.decodePhone[1],
      sessionKey: wx.getStorageSync('temp')[1],
      timeStamp: Math.round(new Date().getTime() / 1000),
    };
    // 此处为使用方式,具体根据实际要求使用
    reqInfo.hmac = hexMD5(jsonSort(reqInfo) + MD5_KEY).toUpperCase()
    let data = Object.defineProperties(reqInfo, {
      hmac: {  //定义属性hmac
        value: reqInfo.hmac,
        writable: true,  //可写
      }
    });
    app.wxRequest('POST', url, data, (res) => {
      console.log(res.data)
    }, (err) => {
      console.log(err.errMsg)
      wx.showToast({
        title: '提示内容',
        icon: 'none',
        duration: 1500
      })
    })
  },


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值