网络编程 
首页 > 网络编程 > 浏览文章

微信小程序request请求封装,验签代码实例

(编辑:jimmy 日期: 2024/10/26 浏览:3 次 )

这篇文章主要介绍了微信小程序request请求封装,验签代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.公共文件util添加

request请求

//简单封装请求
function request(params, path, isShowLoading = true, goBack = false, type = 'none', obj={}) {
 try {
  console.log(path)
  let NowTime = Date.parse(new Date()) / 1000;
  let defaultParams = {
   platform: app.globalData.platform,
   appVersion: app.globalData.version,
   deviceId: uuId(),
   timestamp: params.timestamp "accessToken_v1")}`, {
     cookie
    } = obj.header;
   console.log(cookie)
   obj.header.cookie = cookie "htmlcode">
//生成签名
function signature(params) {
 let keyArr = []
 for (let key in params) {
  keyArr.push(key)
 }
 let arr = keyArr.sort();
 let result = "";
 arr.map((item, index) => {
  if (params[item] == null || params[item] == undefined) {
   delete params[item];
   arr.splice(index, 1);
  }
 })
 console.log("arr")
 console.log(arr)
 arr.map((item, index) => {
  if (Array.isArray(params[item]) == true) {
   let res = ''
   let arr = params[item]
   arr.map(obj => {
    console.log(typeof(obj))
    if (typeof(obj) != 'object') {
     res += obj
    } else {
     for (let key in obj) {
      if (Array.isArray(obj[key])) {
       let val = '';
       obj[key].map(item => {
        val += item
       })
       res += key + val
      } else {
       res += key + obj[key]
      }
     }
    }
   })
   result += '' + item + '' + res
  } else {
   result += '' + item + '' + params[item]
  }
 })
 console.log("result")
 console.log(result)

 let pos;
 if (isEmpty(getToken())) {
  pos = result
 } else {
  pos = result + getToken()
 }
 console.log(pos)
 console.log("signature")
 let signature = sha256.hmac.create('pos_secret').update(pos).hex().toLowerCase(); //需要引入sha256.min.js
 console.log(signature)
 let obj = {
  'signature': signature
 }
 return Object.assign(params, obj);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:微信小程序日历插件代码实例
下一篇:vue中使用极验验证码的方法(附demo)
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap