pref: 允许忽略自签证书校验

pull/229/head
xiaojunnuo 2024-10-22 18:46:29 +08:00
parent 18ee87daff
commit 09847ce074
5 changed files with 30 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent'; import { HttpsProxyAgent } from 'https-proxy-agent';
import nodeHttp from 'http'; import nodeHttp from 'http';
import * as https from 'node:https'; import * as https from 'node:https';
import { merge } from 'lodash-es';
export class HttpError extends Error { export class HttpError extends Error {
status?: number; status?: number;
statusText?: string; statusText?: string;
@ -40,7 +41,7 @@ export class HttpError extends Error {
url = error.config?.baseURL + url; url = error.config?.baseURL + url;
} }
if (url) { if (url) {
this.message = `${this.message} : ${url}`; this.message = `${this.message} : url=${url}`;
} }
this.response = { this.response = {
@ -99,6 +100,11 @@ export function createAxiosService({ logger }: { logger: Logger }) {
delete config.skipSslVerify; delete config.skipSslVerify;
config.httpsAgent = agents.httpsAgent; config.httpsAgent = agents.httpsAgent;
config.httpAgent = agents.httpAgent; config.httpAgent = agents.httpAgent;
// const agent = new https.Agent({
// rejectUnauthorized: false // 允许自签名证书
// });
// config.httpsAgent = agent;
config.proxy = false; //必须 否则还会走一层代理, config.proxy = false; //必须 否则还会走一层代理,
return config; return config;
}, },
@ -161,9 +167,6 @@ export function createAxiosService({ logger }: { logger: Logger }) {
`请求出错status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}` `请求出错status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}`
); );
logger.error('返回数据:', JSON.stringify(error.response?.data)); logger.error('返回数据:', JSON.stringify(error.response?.data));
if (error?.config?.logRes !== false) {
logger.error('返回数据:', JSON.stringify(error.response?.data));
}
if (error.response?.data) { if (error.response?.data) {
error.message = error.response.data.message || error.response.data.msg || error.response.data.error || error.response.data; error.message = error.response.data.message || error.response.data.msg || error.response.data.error || error.response.data;
} }
@ -195,6 +198,7 @@ export function createAgent(opts: nodeHttp.AgentOptions = {}) {
if (httpProxy) { if (httpProxy) {
logger.info('use httpProxy:', httpProxy); logger.info('use httpProxy:', httpProxy);
httpAgent = new HttpProxyAgent(httpProxy, opts as any); httpAgent = new HttpProxyAgent(httpProxy, opts as any);
merge(httpAgent.options, opts);
} else { } else {
httpAgent = new nodeHttp.Agent(opts); httpAgent = new nodeHttp.Agent(opts);
} }
@ -202,6 +206,7 @@ export function createAgent(opts: nodeHttp.AgentOptions = {}) {
if (httpsProxy) { if (httpsProxy) {
logger.info('use httpsProxy:', httpsProxy); logger.info('use httpsProxy:', httpsProxy);
httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any); httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any);
merge(httpsAgent.options, opts);
} else { } else {
httpsAgent = new https.Agent(opts); httpsAgent = new https.Agent(opts);
} }

View File

@ -11,6 +11,7 @@
"debug:force": "vite --force --mode debug", "debug:force": "vite --force --mode debug",
"build": " vite build ", "build": " vite build ",
"dev-build": "echo 1", "dev-build": "echo 1",
"test:unit": "vitest",
"serve": "vite preview", "serve": "vite preview",
"preview": "vite preview", "preview": "vite preview",
"pretty-quick": "pretty-quick", "pretty-quick": "pretty-quick",
@ -114,7 +115,9 @@
"vite-plugin-html": "^3.2.2", "vite-plugin-html": "^3.2.2",
"vite-plugin-windicss": "^1.9.3", "vite-plugin-windicss": "^1.9.3",
"vue-eslint-parser": "^9.4.2", "vue-eslint-parser": "^9.4.2",
"vue-tsc": "^1.8.8" "vue-tsc": "^1.8.8",
"@vue/test-utils": "^2.4.6",
"vitest": "^2.1.2"
}, },
"husky": { "husky": {
"hooks": { "hooks": {

View File

@ -53,7 +53,9 @@ function createService() {
// @ts-ignore // @ts-ignore
response.config.onError(new Error(errorMessage)); response.config.onError(new Error(errorMessage));
} }
errorCreate(`${errorMessage}: ${response.config.url}`); //@ts-ignore
const showErrorNotify = response?.config?.showErrorNotify;
errorCreate(`${errorMessage}: ${response.config.url}`, showErrorNotify);
return dataAxios; return dataAxios;
} }
} }
@ -97,7 +99,7 @@ function createService() {
default: default:
break; break;
} }
errorLog(error); errorLog(error, error?.response?.config?.showErrorNotify);
if (status === 401) { if (status === 401) {
const userStore = useUserStore(); const userStore = useUserStore();
userStore.logout(); userStore.logout();

View File

@ -48,7 +48,7 @@ export function responseError(data = {}, msg = "请求失败", code = 500) {
* @description * @description
* @param {Error} error * @param {Error} error
*/ */
export function errorLog(error: any) { export function errorLog(error: any, notify = true) {
// 打印到控制台 // 打印到控制台
console.error("errorLog", error); console.error("errorLog", error);
let message = error.message; let message = error.message;
@ -58,17 +58,22 @@ export function errorLog(error: any) {
if (message.indexOf("ssl3_get_record:wrong version number") >= 0) { if (message.indexOf("ssl3_get_record:wrong version number") >= 0) {
message = "http协议错误服务端要求http协议请检查是否使用了https请求"; message = "http协议错误服务端要求http协议请检查是否使用了https请求";
} }
// 显示提示 if (notify) {
uiContext.get().notification.error({ message }); // 显示提示
uiContext.get().notification.error({ message });
}
} }
/** /**
* @description * @description
* @param {String} msg * @param {String} msg
*/ */
export function errorCreate(msg: string) { export function errorCreate(msg: string, notify = true) {
const err = new Error(msg); const err = new Error(msg);
console.error("errorCreate", err); console.error("errorCreate", err);
uiContext.get().notification.error({ message: err.message }); if (notify) {
uiContext.get().notification.error({ message: err.message });
}
throw err; throw err;
} }

View File

@ -43,6 +43,7 @@ const attrs = useAttrs();
const optionsRef = ref([]); const optionsRef = ref([]);
const message = ref(""); const message = ref("");
const getOptions = async () => { const getOptions = async () => {
message.value = "";
const res = await doRequest( const res = await doRequest(
{ {
type: props.type, type: props.type,
@ -53,10 +54,10 @@ const getOptions = async () => {
{ {
onError(err: any) { onError(err: any) {
message.value = `获取选项出错:${err.message}`; message.value = `获取选项出错:${err.message}`;
} },
showErrorNotify: false
} }
); );
message.value = "";
return res; return res;
}; };