| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /**
- * 说明:
- * * 不同服务器的请求模块。
- */
- import type { DataModel, NewDataModel } from "@imengyu/js-request-transform";
- import { BaseAppServerRequestModule } from "./BaseAppServerRequestModule";
- import { appendGetUrlParams, defaultResponseDataHandlerCatch, RequestApiError, RequestApiResult, RequestCoreInstance, RequestOptions, RequestResponse, type RequestApiInfoStruct } from "@imengyu/imengyu-utils";
- import ApiCofig from "@/common/config/ApiCofig";
- /**
- * 主应用服务请求模块
- */
- export class AppServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
- constructor() {
- super('base-app-server');
- }
- }
- /**
- * 更新服务请求模块
- */
- export class UpdateServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
- constructor() {
- super("https://update-server1.imengyu.top");
- this.config.requestInterceptor = undefined;
- 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';
- try {
- const json = await response.json();
- if (response.ok) {
- if (!json) {
- throw new RequestApiError(
- 'businessError',
- '后端未返回数据',
- '',
- response.status,
- null,
- null,
- response.headers,
- apiInfo
- );
- }
- if (!json.success)
- throw new RequestApiError(
- 'businessError',
- json.message,
- json.code.toString(),
- json.code,
- json,
- json,
- response.headers,
- apiInfo
- );
-
- return new RequestApiResult(
- resultModelClass ?? instance.config.modelClassCreator,
- json?.code || response.status,
- json.message,
- json.data,
- json,
- response.headers,
- apiInfo
- );
- }
- else {
- throw json;
- }
- } catch (err) {
- if (err instanceof RequestApiError) {
- throw response;
- }
- //错误统一处理
- return new Promise<RequestApiResult<T>>((resolve, reject) => {
- defaultResponseDataHandlerCatch(method, req, response, null, err as any, apiInfo, response.url, reject, instance);
- });
- }
- };
- }
- }
- /**
- * 腾讯地图服务请求模块
- */
- export class MapServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
- constructor() {
- super("https://apis.map.qq.com");
- this.config.requestInterceptor = (url, req) => {
- url = appendGetUrlParams(url, 'key', ApiCofig.mapKey);
- return { newUrl: url, newReq: req };
- };
- 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';
- try {
- const json = await response.json();
- if (response.ok) {
- if (!json) {
- throw new RequestApiError(
- 'businessError',
- '后端未返回数据',
- '',
- response.status,
- null,
- null,
- response.headers,
- apiInfo
- );
- }
- if (json.status !== 0)
- throw new RequestApiError(
- 'businessError',
- json.message,
- json.status.toString(),
- json.status,
- json,
- json,
- response.headers,
- apiInfo
- );
-
- return new RequestApiResult(
- resultModelClass ?? instance.config.modelClassCreator,
- json?.status || response.status,
- json.message,
- json.result,
- json,
- response.headers,
- apiInfo
- );
- }
- else {
- throw json;
- }
- } catch (err) {
- if (err instanceof RequestApiError) {
- throw response;
- }
- //错误统一处理
- return new Promise<RequestApiResult<T>>((resolve, reject) => {
- defaultResponseDataHandlerCatch(method, req, response, null, err as any, apiInfo, response.url, reject, instance);
- });
- }
- };
- }
- }
|