JuHua-app/apis/http.interceptor.js
2024-12-09 14:16:57 +08:00

129 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// common/http.interceptor.js
import projectConfig from '@/env.config.js';
const codeMessage = {
404: '您所请求的资源无法找到',
500: '服务器内部错误,无法完成请求',
};
const install = (Vue, vm) => {
// 这个配置是一次配置,全局通用的,具体参数见 https://www.uviewui.com/js/http.html
uni.$u.http.setConfig((config) => {
// 域名设置
config.baseURL = projectConfig.baseUrl;
// 全局header
config.header = {};
//
config.method = '';
// 设置为json返回后会对数据进行一次JSON.parse()
config.dataType = 'json';
//
config.responseType = 'text';
// 注如果局部custom与全局custom有同名属性则后面的属性会覆盖前面的属性相当于Object.assign(全局,局部)
config.custom = {
// 请求接口展示Loading
ShowLoading: true,
// Loading中是否遮罩
LoadingMask: true,
// Loading文本
LoadingText: '正在加载',
}; // 全局自定义参数默认值
// #ifdef H5 || APP-PLUS || MP-ALIPAY || MP-WEIXIN
config.timeout = 60000;
// #endif
// #ifdef APP-PLUS
// 验证 ssl 证书 仅5+App安卓端支持HBuilderX 2.3.3+
config.sslVerify = true;
// #endif
// #ifdef H5
// 跨域请求时是否携带凭证cookies仅H5支持HBuilderX 2.6.15+
config.withCredentials = false;
// #endif
// #ifdef APP-PLUS
// DNS解析时优先使用ipv4 仅 App-Android 支持 (HBuilderX 2.8.0+)
config.firstIpv4 = false;
// #endif
// 局部优先级高于全局返回当前请求的task,options。请勿在此处修改options。非必填
// getTask: (task, options) => {
// 相当于设置了请求超时时间500ms
// setTimeout(() => {
// task.abort()
// }, 500)
// },
// 全局自定义验证器。参数为statusCode 且必存在,不用判断空情况。
config.validateStatus = (statusCode) => { // statusCode 必存在。此处示例为全局默认配置
return statusCode >= 200 && statusCode < 300
};
return config;
});
// 请求拦截部分,如配置,每次请求前都会执行
uni.$u.http.interceptors.request.use((config) => {
config.header.language = uni.getLocale('lang');
if (config.custom.ShowLoading) {
uni.showLoading({
title: config.custom.LoadingText || '正在加载',
mask: config.custom.LoadingMask || false
});
}
// 引用token
// 方式一存放在vuex的token假设使用了uView封装的vuex方式
// 见https://uviewui.com/components/globalVariable.html
// config.header.token = vm.token;
// 方式二如果没有使用uView封装的vuex方法那么需要使用$store.state获取
if (config.url != '/captchaImage' && config.url != '/login' && config.url != '/iot/tool/register') {
config.header.Authorization = 'Bearer ' + vm.$store.state.vuex_token;
}
// 方式三如果token放在了globalData通过getApp().globalData获取
// config.header.token = getApp().globalData.username;
// 方式四如果token放在了Storage本地存储中拦截是每次请求都执行的
// 所以哪怕您重新登录修改了Storage下一次的请求将会是最新值
// const token = uni.getStorageSync('token');
// config.header.token = token;
// config.header.Token = 'xxxxxx';
// 可以对某个url进行特别处理此url参数为this.$u.get(url)中的url值
// if (config.url == '/pages/login') config.header.noToken = true;
// 最后需要将config进行return
return config;
// 如果return一个false值则会取消本次请求
// if(config.url == '/user/rest') return false; // 取消某次请求
})
// 响应拦截,如配置,每次请求结束都会执行本方法
uni.$u.http.interceptors.response.use((res) => {
if (res.config.custom.ShowLoading) {
uni.hideLoading();
}
// if 状态码是否正常
if (res.statusCode === 200) {
let result = res.data;
// if 与后台规定的成功码是否正常
if (result.code === 200 || result.code === 500 || result.code === 450) {
return result
} else if (result.code == 401) {
// 本地缓存存储token
uni.setStorageSync('token', '');
uni.reLaunch({
url: '/pages/tabBar/home/index'
});
return result
} else {
console.log(result);
vm.$u.toast(result.msg);
}
} else {
vm.$u.toast(res.data.msg);
}
return false;
});
}
export default {
install
}