|
|
@@ -6,9 +6,9 @@
|
|
|
|
|
|
import ApiCofig from "@/common/config/ApiCofig";
|
|
|
import { isDev } from "../common/config/AppCofig";
|
|
|
-import { appendGetUrlParams } from "@imengyu/imengyu-utils";
|
|
|
+import { appendGetUrlParams, defaultResponseDataHandlerCatch, RandomUtils, RequestApiError, RequestApiResult, type RequestApiInfoStruct, type RequestCoreInstance, type RequestOptions, type RequestResponse } from "@imengyu/imengyu-utils";
|
|
|
import { BaseAppServerRequestModule } from "./BaseAppServerRequestModule";
|
|
|
-import type { DataModel } from "@imengyu/js-request-transform";
|
|
|
+import type { DataModel, NewDataModel } from "@imengyu/js-request-transform";
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -31,4 +31,89 @@ export class MapServerRequestModule<T extends DataModel> extends BaseAppServerRe
|
|
|
return { newUrl: url, newReq: req };
|
|
|
};
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 更新服务请求模块
|
|
|
+ */
|
|
|
+export class UpdateServerRequestModule<T extends DataModel> extends BaseAppServerRequestModule<T> {
|
|
|
+ constructor() {
|
|
|
+ super("https://update-server1.imengyu.top");
|
|
|
+ this.config.requestInterceptor = (url, req) => {
|
|
|
+ if (!req.headers)
|
|
|
+ req.headers = {};
|
|
|
+ req.headers['Authorization'] = JSON.stringify({
|
|
|
+ "apiKey":"MQQDGbn8QfFJ7kStNtkxwifHP4sBTSDd",
|
|
|
+ "apiSecret":"3BNAdR7NXGwfiRmQZkRcRM8PsyHPeBmaay2k2F4TXhGEziXSJ3ceEtH2ApfHsMhR"
|
|
|
+ });
|
|
|
+ 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.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);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static readonly DEVICE_UID_KEY = 'deviceUid';
|
|
|
+ private uid = '';
|
|
|
+
|
|
|
+ getDeviceUid() {
|
|
|
+ if (!this.uid) {
|
|
|
+ this.uid = uni.getStorageSync(UpdateServerRequestModule.DEVICE_UID_KEY);
|
|
|
+ if (!this.uid) {
|
|
|
+ this.uid = RandomUtils.genNonDuplicateID(20);
|
|
|
+ uni.setStorageSync(UpdateServerRequestModule.DEVICE_UID_KEY, this.uid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.uid;
|
|
|
+ }
|
|
|
}
|