|
@@ -16,7 +16,9 @@ import {
|
|
|
RequestResponse,
|
|
RequestResponse,
|
|
|
WebFetchImplementer,
|
|
WebFetchImplementer,
|
|
|
appendGetUrlParams, appendPostParams,
|
|
appendGetUrlParams, appendPostParams,
|
|
|
- RandomUtils
|
|
|
|
|
|
|
+ RandomUtils,
|
|
|
|
|
+ type RequestApiInfoStruct,
|
|
|
|
|
+ formatError
|
|
|
} from "@imengyu/imengyu-utils";
|
|
} from "@imengyu/imengyu-utils";
|
|
|
import type { DataModel, KeyValue, NewDataModel } from "@imengyu/js-request-transform";
|
|
import type { DataModel, KeyValue, NewDataModel } from "@imengyu/js-request-transform";
|
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
@@ -39,14 +41,14 @@ function matchNotReportMessage(str: string) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//请求拦截器
|
|
//请求拦截器
|
|
|
-function requestInceptor(url: string, req: RequestOptions) {
|
|
|
|
|
|
|
+function requestInterceptor(url: string, req: RequestOptions) {
|
|
|
//获取store中的token,追加到头;
|
|
//获取store中的token,追加到头;
|
|
|
const autoStore = useAuthStore();
|
|
const autoStore = useAuthStore();
|
|
|
- if (!req.header)
|
|
|
|
|
- req.header = {};
|
|
|
|
|
- if (StringUtils.isNullOrEmpty((req.header as KeyValue).token as string)) {
|
|
|
|
|
- req.header['token'] = autoStore.token;
|
|
|
|
|
- req.header['__token__'] = autoStore.token;
|
|
|
|
|
|
|
+ if (!req.headers)
|
|
|
|
|
+ req.headers = {};
|
|
|
|
|
+ if (StringUtils.isNullOrEmpty(req.headers.token as string)) {
|
|
|
|
|
+ req.headers['token'] = autoStore.token;
|
|
|
|
|
+ req.headers['__token__'] = autoStore.token;
|
|
|
}
|
|
}
|
|
|
if (req.method == 'GET') {
|
|
if (req.method == 'GET') {
|
|
|
//追加GET参数
|
|
//追加GET参数
|
|
@@ -59,7 +61,9 @@ function requestInceptor(url: string, req: RequestOptions) {
|
|
|
return { newUrl: url, newReq: req };
|
|
return { newUrl: url, newReq: req };
|
|
|
}
|
|
}
|
|
|
//响应数据处理函数
|
|
//响应数据处理函数
|
|
|
-function responseDataHandler<T extends DataModel>(response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel|undefined, instance: RequestCoreInstance<T>, apiName: string | undefined): Promise<RequestApiResult<T>> {
|
|
|
|
|
|
|
+function responseDataHandler<T extends DataModel>(
|
|
|
|
|
+ response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel | undefined, instance: RequestCoreInstance<T>, apiInfo: RequestApiInfoStruct
|
|
|
|
|
+): Promise<RequestApiResult<T>> {
|
|
|
return new Promise<RequestApiResult<T>>((resolve, reject) => {
|
|
return new Promise<RequestApiResult<T>>((resolve, reject) => {
|
|
|
const method = req.method || 'GET';
|
|
const method = req.method || 'GET';
|
|
|
response.json().then((json) => {
|
|
response.json().then((json) => {
|
|
@@ -72,9 +76,8 @@ function responseDataHandler<T extends DataModel>(response: RequestResponse, req
|
|
|
response.status,
|
|
response.status,
|
|
|
null,
|
|
null,
|
|
|
null,
|
|
null,
|
|
|
- req,
|
|
|
|
|
- apiName,
|
|
|
|
|
- response.url
|
|
|
|
|
|
|
+ apiInfo.apiRawReq?.headers,
|
|
|
|
|
+ apiInfo,
|
|
|
));
|
|
));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -153,19 +156,18 @@ function responseDataHandler<T extends DataModel>(response: RequestResponse, req
|
|
|
response.status,
|
|
response.status,
|
|
|
null,
|
|
null,
|
|
|
null,
|
|
null,
|
|
|
- req,
|
|
|
|
|
- apiName,
|
|
|
|
|
- response.url
|
|
|
|
|
|
|
+ apiInfo.apiRawReq?.headers,
|
|
|
|
|
+ apiInfo,
|
|
|
));
|
|
));
|
|
|
}
|
|
}
|
|
|
}).catch((err) => {
|
|
}).catch((err) => {
|
|
|
//错误统一处理
|
|
//错误统一处理
|
|
|
- defaultResponseDataHandlerCatch(method, req, response, null, err, apiName, response.url, reject, instance);
|
|
|
|
|
|
|
+ defaultResponseDataHandlerCatch(method, req, response, null, err, apiInfo, response.url, reject, instance);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
//错误报告处理
|
|
//错误报告处理
|
|
|
-function responseErrReoprtInceptor<T extends DataModel>(instance: RequestCoreInstance<T>, response: RequestApiError) {
|
|
|
|
|
|
|
+function responseErrorReportInterceptor<T extends DataModel>(instance: RequestCoreInstance<T>, response: RequestApiError) {
|
|
|
return (
|
|
return (
|
|
|
(response.errorType !== 'businessError' && response.errorType !== 'networkError') ||
|
|
(response.errorType !== 'businessError' && response.errorType !== 'networkError') ||
|
|
|
notReportErrorCode.indexOf(response.code) >= 0 ||
|
|
notReportErrorCode.indexOf(response.code) >= 0 ||
|
|
@@ -181,7 +183,7 @@ export function reportError<T extends DataModel>(instance: RequestCoreInstance<T
|
|
|
console.log(response.apiName);
|
|
console.log(response.apiName);
|
|
|
console.log(response.errorMessage);
|
|
console.log(response.errorMessage);
|
|
|
console.log(response.apiUrl);
|
|
console.log(response.apiUrl);
|
|
|
- console.log(response.rawRequest);
|
|
|
|
|
|
|
+ console.log(response.apiRawReq);
|
|
|
console.log(response.rawData);
|
|
console.log(response.rawData);
|
|
|
console.log(response.code);
|
|
console.log(response.code);
|
|
|
console.log(response.errorCodeMessage);
|
|
console.log(response.errorCodeMessage);
|
|
@@ -200,7 +202,7 @@ export function reportError<T extends DataModel>(instance: RequestCoreInstance<T
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-function responseErrorHandler<T extends DataModel>(err: Error, instance: RequestCoreInstance<T>, apiName: string | undefined) : RequestApiError {
|
|
|
|
|
|
|
+function responseErrorHandler<T extends DataModel>(err: unknown, instance: RequestCoreInstance<T>, apiInfo: RequestApiInfoStruct) : RequestApiError {
|
|
|
if (err instanceof TypeError) {
|
|
if (err instanceof TypeError) {
|
|
|
let errorMessage = '';
|
|
let errorMessage = '';
|
|
|
if (err.message.indexOf('Failed to fetch') >= 0)
|
|
if (err.message.indexOf('Failed to fetch') >= 0)
|
|
@@ -215,9 +217,9 @@ function responseErrorHandler<T extends DataModel>(err: Error, instance: Request
|
|
|
errorMessage = '无效URL';
|
|
errorMessage = '无效URL';
|
|
|
else
|
|
else
|
|
|
errorMessage = err.message;
|
|
errorMessage = err.message;
|
|
|
- return new RequestApiError('networkError', errorMessage, '', 0, null, null, null, apiName, '');
|
|
|
|
|
|
|
+ return new RequestApiError('networkError', errorMessage, '', 0, null, null, undefined, apiInfo);
|
|
|
}
|
|
}
|
|
|
- return new RequestApiError('networkError', err.message, '', 0, null, null, null, apiName, '');
|
|
|
|
|
|
|
+ return new RequestApiError('networkError', formatError(err), '', 0, null, null, undefined, apiInfo);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -228,10 +230,10 @@ export class AppServerRequestModule<T extends DataModel> extends RequestCoreInst
|
|
|
super(WebFetchImplementer);
|
|
super(WebFetchImplementer);
|
|
|
this.config.baseUrl = ApiCofig.serverProd;
|
|
this.config.baseUrl = ApiCofig.serverProd;
|
|
|
this.config.errCodes = []; //
|
|
this.config.errCodes = []; //
|
|
|
- this.config.requestInceptor = requestInceptor;
|
|
|
|
|
|
|
+ this.config.requestInterceptor = requestInterceptor;
|
|
|
this.config.responseDataHandler = responseDataHandler;
|
|
this.config.responseDataHandler = responseDataHandler;
|
|
|
this.config.responseErrorHandler = responseErrorHandler;
|
|
this.config.responseErrorHandler = responseErrorHandler;
|
|
|
- this.config.responseErrReoprtInceptor = responseErrReoprtInceptor;
|
|
|
|
|
|
|
+ this.config.responseErrorReportInterceptor = responseErrorReportInterceptor;
|
|
|
this.config.reportError = reportError;
|
|
this.config.reportError = reportError;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -242,17 +244,17 @@ export class MengyuServerRequestModule<T extends DataModel> extends RequestCoreI
|
|
|
constructor() {
|
|
constructor() {
|
|
|
super(WebFetchImplementer);
|
|
super(WebFetchImplementer);
|
|
|
this.config.baseUrl = 'https://update-server1.imengyu.top';
|
|
this.config.baseUrl = 'https://update-server1.imengyu.top';
|
|
|
- this.config.requestInceptor = (url, req) => {
|
|
|
|
|
- if (!req.header)
|
|
|
|
|
- req.header = {};
|
|
|
|
|
- req.header['Authorization'] = JSON.stringify({
|
|
|
|
|
|
|
+ this.config.requestInterceptor = (url, req) => {
|
|
|
|
|
+ if (!req.headers)
|
|
|
|
|
+ req.headers = {};
|
|
|
|
|
+ req.headers['Authorization'] = JSON.stringify({
|
|
|
"apiKey":"MQQDGbn8QfFJ7kStNtkxwifHP4sBTSDd",
|
|
"apiKey":"MQQDGbn8QfFJ7kStNtkxwifHP4sBTSDd",
|
|
|
"apiSecret":"3BNAdR7NXGwfiRmQZkRcRM8PsyHPeBmaay2k2F4TXhGEziXSJ3ceEtH2ApfHsMhR"
|
|
"apiSecret":"3BNAdR7NXGwfiRmQZkRcRM8PsyHPeBmaay2k2F4TXhGEziXSJ3ceEtH2ApfHsMhR"
|
|
|
});
|
|
});
|
|
|
url = appendGetUrlParams(url, 'identifier', this.getDeviceUid());
|
|
url = appendGetUrlParams(url, 'identifier', this.getDeviceUid());
|
|
|
return { newUrl: url, newReq: req };
|
|
return { newUrl: url, newReq: req };
|
|
|
};
|
|
};
|
|
|
- this.config.responseDataHandler = async function responseDataHandler<T extends DataModel>(response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel | undefined, instance: RequestCoreInstance<T>): Promise<RequestApiResult<T>> {
|
|
|
|
|
|
|
+ this.config.responseDataHandler = async function responseDataHandler<T extends DataModel>(response: RequestResponse, req: RequestOptions, resultModelClass: NewDataModel | undefined, instance: RequestCoreInstance<T>, apiInfo: RequestApiInfoStruct): Promise<RequestApiResult<T>> {
|
|
|
const method = req.method || 'GET';
|
|
const method = req.method || 'GET';
|
|
|
try {
|
|
try {
|
|
|
const json = await response.json();
|
|
const json = await response.json();
|
|
@@ -297,7 +299,7 @@ export class MengyuServerRequestModule<T extends DataModel> extends RequestCoreI
|
|
|
}
|
|
}
|
|
|
//错误统一处理
|
|
//错误统一处理
|
|
|
return new Promise<RequestApiResult<T>>((resolve, reject) => {
|
|
return new Promise<RequestApiResult<T>>((resolve, reject) => {
|
|
|
- defaultResponseDataHandlerCatch(method, req, response, null, err as any, '', response.url, reject, instance);
|
|
|
|
|
|
|
+ defaultResponseDataHandlerCatch(method, req, response, null, err as any, apiInfo, response.url, reject, instance);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|