@ecoding/helper.request.hook
v0.0.22
Published
dev request.hook
Downloads
25
Readme
import axios from 'axios'; import { getCookie } from '@ecoding/helper.cookie'; import { Loading, Toast, http } from '@ecoding/components.antd'; import R from '@ecoding/helper.request.hook'; import { getAppCode } from "@sciecomm/portal.global"; import { localLocale } from '@/locales'; export const ignoreTokenInterFace: string[] = [];
const originPrefix = { "localhost:8081": "http://192.168.2.29:8302", // 本地纯前端环境 'local': 'http://api.sciecomm.cn', //本地环境bff 'test': 'http://192.168.2.139:8302', //测试环境 'prod': 'http://api.sciecomm.cn', //线上环境 };
const request = new R(axios);
request.setFinallyHook(() => {
Loading.hide();
});
request.setResTranslateHook((out, res) => {
if (res.code == -10001) { // 登录失效
setTimeout(() => {
// window.location.href = /user/login?redirect=${encodeURIComponent(window.location.href)}
;
}, 1500);
}
return {
success: res.successed,
code: res.code,
msg: res.message,
message: res.message,
data: res.model,
};
});
request.setReqHook(async (options) => {
const url = options.url;
const host = location.host;
let domain;
if (/^/api?//.test(url)) {
domain = originPrefix[host] || originPrefix[window.PRELOAD_STATE?.ctxEnv || process.env.NODE_ENV];
} else {
domain = "";
}
if (!ignoreTokenInterFace.includes(url)) {
const token = getCookie('accessToken');
if (token) {
options.headers = Object.assign(options.headers || {}, {
accessToken: token,
appCode: getAppCode(url)
});
} else {
// Toast.error({
// title: '登录信息失效,请重新登录'
// });
// setTimeout(() => {
// // location.href = "/user/login";
// }, 1500);
}
}
options.headers = Object.assign(options.headers || {}, {
language: localLocale
});
options.url = ${domain}${url}
;
// options.params = Object.assign(options.params || {}, { locale: localLocale });
return Promise.resolve(options);
});
request.setErrorUiHook((err) => {
Toast.error({
mask: true,
duration: 20000,
title: String(err.message || err.msg)
});
});
http.setOutRequest(request);
export const r = request;
export const useGet = request.getUseGet();
export const usePost = request.getUsePost();
/*
import React from "react"; import axios from "axios"; import { Loading, Toast, http } from "@ecoding/components.antd"; import { message } from "antd"; import { i18nInstance } from "@ecoding/helper.i18n"; import R from "@ecoding/helper.request.hook"; import { getTokenWithFresh } from "@/utils/user"; import { ignoreTokenInterFace } from "@/constants/project";
const originPrefix = { // "localhost:8080": "http://192.168.1.117:22335", // 本地地址 "localhost:8080": "http://192.168.2.110:22335", // 测试地址 // "localhost:8080": "http://60.12.241.126:22335", //测试环境 公网地址 "192.168.1.160:8080": "http://192.168.1.117:22335", // 本地地址 // "192.168.1.160:8080": "http://60.12.241.126:22335", // 本地地址 development: "http://192.168.2.110:22335", // 本地开发环境 "192.168.2.191:7003": "http://www.yaofanbao.com:22335", //临时测试环境 // "60.12.241.126:7003": "http://www.yaofanbao.com:22335", //公网临时测试环境 "192.168.2.231:7003": "http://192.168.2.110:22335", //测试环境 "60.12.241.126:7003": "http://60.12.241.126:22335", //测试环境 公网地址 "www.opshields.com": "//www.opshields.com:22335" //生产环境 };
const request = new R(axios); request.setFinallyHook(() => { Loading.hide(); }); request.setResTranslateHook((out, res) => { return { success: res.successed, code: res.code === 1 || res.code === 0 ? 0 : res.code, msg: res.message, message: res.message, data: res.model }; }); request.setReqHook(async (options) => { const url = options.url; if (url.indexOf("/api/") !== -1) { const host = location.host; request.setDomain(originPrefix[host] || originPrefix[window.PRELOAD_STATE?.ctxEnv || process.env.NODE_ENV]); // request.setDomain(""); } else { request.setDomain(""); }
if (ignoreTokenInterFace.indexOf(url) == -1) {
const token = await getTokenWithFresh(url);
if (token) {
options.headers = Object.assign(options.headers || {}, { Authorization: `Bearer${token}` });
} else {
message.error("登录信息失效,请重新登录");
setTimeout(() => {
location.href = "/user/login";
}, 1500);
}
}
options.params = Object.assign(options.params || {}, { locale: window.__locale });
return Promise.resolve(options);
}); request.setErrorUiHook((err) => { Toast.error({ duration: 20000, title: i18nInstance.$t(String(err.message || err.msg), String(err.message || err.msg)) }); if (err.message === 'sys.no.login.or.expired.login') { setTimeout(() => { location.href = "/user/login"; }, 1500); } }); http.setOutRequest(request); export const r = request; export const useGet = request.getUseGet(); export const usePost = request.getUsePost();
*/