You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wxapp/utils/http.interceptor.js

87 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//引入
const Encrypt = require('./cryptojs.js').Encrypt;
const Decrypt = require('./cryptojs.js').Decrypt;
///解密 Encrypt(待加密数据) Decrypt(待解密数据)
// Encrypt(data)
// Decrypt(data)
const install = (Vue, vm) => {
Vue.prototype.$u.http.setConfig({
baseUrl: Vue.prototype.BASE_URL,
loadingText: '努力加载中~',
loadingTime: 5000,
// 设置自定义头部content-type
header: {
'content-type': 'application/json'
// 后台接口是从app转移的,参数加密过的所以改成form-data格式传参
// 'content-type': 'multipart/form-data;boundary=XXX'
}
// ......
});
// 小程序直接设置content-type为''multipart/form-data;'无效故需要手动封装form-dataXXX为边界值
//content-type为form-data 需要处理传参格式
// Vue.prototype.dealSendData = (obj = {})=> {
// let result = ''
// for (let name of Object.keys(obj)) {
// let value = obj[name];
// result +=
// '\r\n--XXX' +
// '\r\nContent-Disposition: form-data; name=\"' + name + '\"' +
// '\r\n' +
// '\r\n' + value
// }
// return result + '\r\n--XXX--'
// }
// 请求拦截部分,如配置,每次请求前都会执行
Vue.prototype.$u.http.interceptor.request = (config) => {
// if(!uni.getStorageSync('auth')){
// console.log("未登录")
// }
const id = uni.getStorageSync('auth').id
const token = uni.getStorageSync('auth').token
config.header["X-Recook-ID"] = id
config.header["X-Recook-Token"] = token
config.header["Device-Type"] = "wxapp"
// 拦截请求组参数并处理格式
// console.log(config.data,Vue.prototype.dealSendData)
// config.data = Vue.prototype.dealSendData(config.data)
console.log("请求参数为========",config.data)
config.data = Encrypt(config.data)
}
// 响应拦截
Vue.prototype.$u.http.interceptor.response = (res) => {
// console.log(res)
if(res.code=="FAIL"){
vm.$u.toast(res.msg);
return false
}
if (res.data.msg == "游客无法使用该功能,请登录"||res.data.msg == "登录失效") {
console.log("响应拦截")
vm.$u.toast(res.data.msg);
setTimeout(() => {
vm.$u.route('/pages/login/login')
}, 1500)
return false;
}else{
return res;
}
}
}
export default {
install
}