yxk_h5_master/request/js/config.js

191 lines
5.2 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.

import Interceptor from "./core/interceptor";
import Request from "./index";
import TokenApi from ".././api/token";
export const globalInterceptor = {
request: new Interceptor(),
response: new Interceptor(),
};
/**
* 全局配置
* 只能配置 静态数据
* `content-type` 默认为 application/json
* header 中`content-type`设置特殊参数 或 配置其他会导致触发 跨域 问题出现跨域会直接进入响应拦截器的catch函数中
*/
export const config = {
baseURL: "/gyhl",
// baseURL: 'http://36.137.58.63:32/gyhl',
imgURL:
window.location.href.split("//")[0] +
"//" +
window.location.href.split("//")[1].split("/")[0] +
"/",
// imgURL: "http://36.137.58.63:32/",
dataType: "json",
responseType: "text",
method: "POST",
header: {
"content-type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded'
},
};
// 调试时解决跨域问题(只有H5会有跨域问题App与小程序调试时可以直接用直连地址)
// //#ifdef H5
// config.baseURL = process.env.NODE_ENV === 'development' ? "/gyhl" : "http://wxxinsoft.gnway.org:11927/gyhl";
// //#endif
/**
* 全局 请求拦截器
* 例如: 配置token
*
* `return config` 继续发送请求
* `return false` 会停止发送请求不会进入错误数据拦截也不会进入请求对象中的catch函数中
* `return Promise.reject('xxxxx')` 停止发送请求, 会错误数据拦截也会进入catch函数中
*
* @param {Object} config 发送请求的配置数据
*/
globalInterceptor.request.use(
(config) => {
config.header.Authorization = getToken();
if (config.paramsBody) {
config.data = config.paramsBody;
}
return config;
},
(err) => {
console.error("is global fail request interceptor: ", err);
// return false;
return Promise.reject(err);
}
);
// 支持添加多个请求、响应拦截器
// globalInterceptor.request.use(config => {
// console.log('is global request interceptor 2');
// return config;
// }, err => {
// console.error('global request: ', err);
// return false;
// });
/**
* 全局 响应拦截器
* 例如: 根据状态码选择性拦截、过滤转换数据
*
* `return res` 继续返回数据
* `return false` 停止返回数据不会进入错误数据拦截也不会进入catch函数中
* `return Promise.reject('xxxxx')` 返回错误信息, 会错误数据拦截也会进入catch函数中
*
* @param {Object} res 请求返回的数据
* @param {Object} config 发送请求的配置数据
* @return {Object|Boolean|Promise<reject>}
*/
globalInterceptor.response.use(
(res, config) => {
if (res.data && res.data.code == 98) {
// const automatic_logon = uni.getStorageSync('automatic_logon');
// const loginInfo = uni.getStorageSync('admin_info');
// if(automatic_logon){
// //调用接口获取最新的token
// uni.request({
// url: config.baseURL+'/apis/user/login', //仅为示例,并非真实接口地址。
// data: loginInfo,
// method:'POST',
// success: (res) => {
// console.log(res.data);
// // uni.setStorageSync('userInfo', res.data.userInfo);
// }
// });
// this.$http.request({
// url: '/apis/user/login',
// params: loginInfo,
// }).then(res=>{
// if(res.data.code == 0){
// uni.setStorageSync('userInfo', res.data.userInfo);
// }else{
// uni.showToast({
// icon: 'none',
// title: res.data.msg,
// });
// }
// }).catch(err=>{
// uni.showToast({
// icon: 'none',
// title: '重新登陆失败',
// });
// });
// }else{
//令牌到期,需要重新登陆
uni.showToast({
title: res.data.msg,
duration: 1000,
icon: "none",
});
setTimeout(function () {
uni.navigateTo({
url: "/pages/login/login",
});
}, 1000);
// }
} else {
return res;
}
// 回传数据中没有携带 code
// if (!(res.data && res.data.code)) {
// return res;
// }
// return Promise.reject('is error')
},
(err, config) => {
uni.showToast({
title: "连接服务器失败",
duration: 1000,
icon: "none",
});
const { errMsg, data } = err;
return Promise.reject({
errMsg,
data,
config,
});
}
);
// 重新请求更新获取 token
async function getApiToken(uid) {
const res = await TokenApi.getMockToken(uid);
const { token } = res.data;
return token;
}
// 获取 localStorage 中的 token
function getToken() {
try {
const value = uni.getStorageSync("userInfo");
if (value) {
return value.token;
}
} catch (e) {
uni.showToast({
title: "请先登陆",
duration: 2000,
icon: "none",
});
setTimeout(function () {
uni.navigateTo({
url: "/pages/login/login",
});
}, 2000);
}
}
// 保存 token 到 localStorage
function saveToken(token) {
uni.setStorageSync("token", token);
}