这篇文章主要介绍了微信小程序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 ? params.timestamp : nowtime }; params = object.assign(params, defaultparams); let requestparams = signature(params) isshowloading && wx.showloading && wx.showloading({ title: '加 中...' }) return new promise((resolve, reject) => { let header; obj.header = obj.header || {}; console.log('获取ptkey') console.log(plugin.getjdliststorage(['guid', 'pt_key', 'pt_token', 'pt_pin'])) let [guid = '', key = '', token = '', pin = ''] = plugin.getjdliststorage(['guid', 'pt_key', 'pt_token', 'pt_pin']), _cookie = `guid=${guid}; pt_pin=${encodeuricomponent(pin)}; pt_key=${key}; pt_token=${token};${wx.getstoragesync("accesstoken_v1")}`, { cookie } = obj.header; console.log(cookie) obj.header.cookie = cookie ? `${cookie};${_cookie}` : _cookie; if (!isempty(gettoken())) { obj.header.authorization = 'bearer ' gettoken() } console.log(obj.header) wx.request({ url: getapp().globaldata.host path, //获取app.js 全局ip data: requestparams, method: 'post', header: obj.header, success: function(res) { console.log(res) isshowloading && wx.hideloading && wx.hideloading() // 缺少登录失效的处理方法 if (res.statuscode == 200 && res.data.code == 200) { if (isempty(res.data.data) || res.data.data == true) { resolve('true') return } if (res.data.data) { resolve(res.data.data); } else if (res.data.data == 0){ resolve(res.data.data); } else { resolve('true') } } else if (res.statuscode == 200 && res.data.code == 401) { //token过期 login(); resolve(false); return; } else { //需要再判断code码 // errorshow(res.data.msg, goback, type); errorshow(res.data.msg); resolve(false); // reject(res.data.res); } }, fail: function(err) { console.log(err) // 因为hide会让showtoast隐藏 isshowloading && wx.hideloading && wx.hideloading() errorshow('网络请求失败'); reject(new error('network request failed')) // throw new error('network request failed') }, complete: function() {} }) }) } catch (err) { console.log(err) errorshow('网络请求失败'); return false; } }
2.signature 签名
//生成签名 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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,