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

5 years ago
//引入
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: '努力加载中~',
5 years ago
loadingTime: 5000,
5 years ago
// 设置自定义头部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
5 years ago
config.header["Device-Type"] = "wxapp"
5 years ago
// 拦截请求组参数并处理格式
// console.log(config.data,Vue.prototype.dealSendData)
// config.data = Vue.prototype.dealSendData(config.data)
5 years ago
console.log("请求参数为========",config.data)
5 years ago
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
}