215 lines
5.8 KiB
JavaScript
215 lines
5.8 KiB
JavaScript
import { request } from '@gtff/tdesign-gt-vue';
|
||
import { MessagePlugin } from 'tdesign-vue';
|
||
import { mhLogout, getRedirectUri } from '@/pages/index/api/login';
|
||
import { LoadingPlugin, DialogPlugin } from '@gt4/common-front';
|
||
|
||
const SingleLoading = {
|
||
load: null,
|
||
count: 0,
|
||
startLoading() {
|
||
if (!this.load) {
|
||
console.log('loading start', this.count);
|
||
this.load = LoadingPlugin(true);
|
||
}
|
||
// eslint-disable-next-line no-plusplus
|
||
this.count++;
|
||
},
|
||
endLoading(stopNow) {
|
||
// eslint-disable-next-line no-plusplus
|
||
this.count--;
|
||
if (this.load && (this.count === 0 || stopNow)) {
|
||
console.log('loading end');
|
||
this.count = 0;
|
||
this.load = LoadingPlugin(false);
|
||
this.load = null;
|
||
}
|
||
},
|
||
};
|
||
|
||
/**
|
||
* 基于request的实例进行拦截器的request自定义示例
|
||
* 传参为onFulfilled、onRejected两个处理函数
|
||
* 你可以在这里对预设配置对象进行进一步的加工,
|
||
* 比如请求要带的header参数配置、请求Timeout的重设等
|
||
*/
|
||
request.interceptors.request.use(
|
||
/**
|
||
* onFulfilled钩子函数处理
|
||
* @param conf {Object} 实例中request的配置函数
|
||
* @returns {Object} request配置函数
|
||
*/
|
||
(conf) => {
|
||
const newConf = conf;
|
||
if (newConf.loading) {
|
||
SingleLoading.startLoading();
|
||
}
|
||
const { url } = newConf;
|
||
if (newConf.method === 'get' && newConf.params) {
|
||
// 先加时间戳(如果 URL 还没参数)
|
||
if (url.indexOf('?') === -1) {
|
||
newConf.url = `${newConf.url}?t=${new Date().getTime()}&`;
|
||
}
|
||
// 再追加 params
|
||
let paramsUrl = `${newConf.url}`;
|
||
for (const propName of Object.keys(newConf.params)) {
|
||
const value = newConf.params[propName];
|
||
const part = `${encodeURIComponent(propName)}=`;
|
||
if (value !== null && typeof value !== 'undefined') {
|
||
if (typeof value === 'object') {
|
||
for (const key of Object.keys(value)) {
|
||
const params = `${propName}[${key}]`;
|
||
const subPart = `${encodeURIComponent(params)}=`;
|
||
paramsUrl += `${subPart + encodeURIComponent(value[key])}&`;
|
||
}
|
||
} else {
|
||
paramsUrl += `${part + encodeURIComponent(value)}&`;
|
||
}
|
||
}
|
||
}
|
||
paramsUrl = paramsUrl.slice(0, -1);
|
||
newConf.params = {};
|
||
newConf.url = paramsUrl;
|
||
} else {
|
||
// 无 params 时,直接加时间戳
|
||
if (url.indexOf('?') !== -1) {
|
||
newConf.url = `${url}&t=${new Date().getTime()}`;
|
||
} else {
|
||
newConf.url = `${url}?t=${new Date().getTime()}`;
|
||
}
|
||
}
|
||
return newConf;
|
||
},
|
||
/**
|
||
* onRejected钩子函数处理
|
||
* @param err {ErrorEvent} 前一个拦截器返回的request错误事件
|
||
* @returns {Promise<never>} 必须reject对应的错误事件
|
||
*/
|
||
(err) => {
|
||
console.log('====>> req err', err);
|
||
return Promise.reject(err);
|
||
},
|
||
);
|
||
|
||
/**
|
||
* 基于request的实例进行拦截器的response自定义示例,
|
||
* 传参为onFulfilled、onRejected两个处理函数
|
||
* 你可以在这里对预设返回的对象进行进一步的加工,比如key值的大小驼峰转换
|
||
*/
|
||
request.interceptors.response.use(
|
||
/**
|
||
* onFulfilled钩子函数处理
|
||
* @param res {Object} 为前一个拦截器返回的结果
|
||
* @returns {Object} 必须要return`结果对象`
|
||
*/
|
||
async (res) => {
|
||
// 未设置状态码则默认成功状态
|
||
|
||
if (res.reqConfig?.loading || res.config?.loading) {
|
||
SingleLoading.endLoading();
|
||
}
|
||
const { code, type } = res.data;
|
||
// 获取错误信息
|
||
const msg = res.data.msg || '系统未知错误,请反馈给管理员';
|
||
if (code === 401) {
|
||
// 未认证
|
||
// return handleAuthorized();
|
||
window.location.href = `/view/mhzc/login`;
|
||
}
|
||
if (code !== 1) {
|
||
MessagePlugin.error({
|
||
content: msg,
|
||
duration: 2000,
|
||
});
|
||
return Promise.reject(code);
|
||
}
|
||
return res.data;
|
||
},
|
||
/**
|
||
* onRejected钩子函数处理
|
||
* @param err {ErrorEvent} 前一个拦截器返回的错误结果
|
||
* @returns {Promise<never>}
|
||
*/
|
||
(err) => {
|
||
if (err.reqConfig?.loading || err.config?.loading || SingleLoading.load !== null) {
|
||
SingleLoading.endLoading(true);
|
||
}
|
||
let { message } = err;
|
||
if (message === 'Network Error') {
|
||
message = '后端接口连接异常';
|
||
} else if (message.includes('timeout')) {
|
||
message = '系统接口请求超时';
|
||
} else if (message.includes('Request failed with status code')) {
|
||
message = `系统接口${message.substr(message.length - 3)}异常`;
|
||
}
|
||
MessagePlugin.error({
|
||
content: message,
|
||
duration: 5 * 1000,
|
||
});
|
||
return Promise.reject(err);
|
||
},
|
||
);
|
||
|
||
function handleAuthorized() {
|
||
MessagePlugin.error({
|
||
content: '无效的会话,或者会话已过期,请重新登录。',
|
||
duration: 2000,
|
||
}).then(() => {
|
||
const { href } = window.location;
|
||
window.location.href = href.substring(0, href.indexOf('/znsb/view')).concat('/view/mhzc/login');
|
||
|
||
});
|
||
return Promise.reject('无效的会话,或者会话已过期,请重新登录。');
|
||
}
|
||
|
||
/**
|
||
* 新版Api统一请求入口
|
||
* @param options
|
||
* @param custom
|
||
*/
|
||
const fetch = (options, custom) => {
|
||
return request(
|
||
{
|
||
baseURL: window.STATIC_ENV_CONFIG.API_PREFIX,
|
||
...options,
|
||
},
|
||
custom,
|
||
);
|
||
};
|
||
const headers = {
|
||
"TOKEN-TYPE": 'YW',
|
||
};
|
||
const fetchSso = (options, custom) => {
|
||
return request(
|
||
{
|
||
headers: headers,
|
||
baseURL: '/',
|
||
...options,
|
||
},
|
||
custom,
|
||
);
|
||
};
|
||
const headers1 = {
|
||
"TOKEN-TYPE": 'STAR',
|
||
};
|
||
const fetchSso1 = (options, custom) => {
|
||
return request(
|
||
{
|
||
headers: headers1,
|
||
baseURL: '/',
|
||
...options,
|
||
},
|
||
custom,
|
||
);
|
||
};
|
||
const fetchNoPrefix = (options, custom) => {
|
||
return request(
|
||
{
|
||
baseURL: '/',
|
||
...options,
|
||
},
|
||
custom,
|
||
);
|
||
};
|
||
|
||
export { fetch, fetchSso, fetchNoPrefix, fetchSso1 };
|