|
|
@@ -15,7 +15,8 @@ import {
|
|
|
defaultResponseDataGetErrorInfo, defaultResponseDataHandlerCatch,
|
|
|
RequestResponse,
|
|
|
WebFetchImplementer,
|
|
|
- appendGetUrlParams, appendPostParams
|
|
|
+ appendGetUrlParams, appendPostParams,
|
|
|
+ RandomUtils
|
|
|
} from "@imengyu/imengyu-utils";
|
|
|
import type { DataModel, KeyValue, NewDataModel } from "@imengyu/js-request-transform";
|
|
|
import { useAuthStore } from "@/stores/auth";
|
|
|
@@ -234,3 +235,85 @@ export class AppServerRequestModule<T extends DataModel> extends RequestCoreInst
|
|
|
this.config.reportError = reportError;
|
|
|
}
|
|
|
}
|
|
|
+/**
|
|
|
+ * 更新服务请求模块
|
|
|
+ */
|
|
|
+export class MengyuServerRequestModule<T extends DataModel> extends RequestCoreInstance<T> {
|
|
|
+ constructor() {
|
|
|
+ super(WebFetchImplementer);
|
|
|
+ this.config.baseUrl = 'https://update-server1.imengyu.top';
|
|
|
+ this.config.requestInceptor = (url, req) => {
|
|
|
+ if (!req.header)
|
|
|
+ req.header = {};
|
|
|
+ req.header['Authorization'] = JSON.stringify({
|
|
|
+ "apiKey":"MQQDGbn8QfFJ7kStNtkxwifHP4sBTSDd",
|
|
|
+ "apiSecret":"3BNAdR7NXGwfiRmQZkRcRM8PsyHPeBmaay2k2F4TXhGEziXSJ3ceEtH2ApfHsMhR"
|
|
|
+ });
|
|
|
+ url = appendGetUrlParams(url, 'identifier', this.getDeviceUid());
|
|
|
+ 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>> {
|
|
|
+ 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
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (!json.success)
|
|
|
+ throw new RequestApiError(
|
|
|
+ 'businessError',
|
|
|
+ json.message,
|
|
|
+ json.code.toString(),
|
|
|
+ json.code,
|
|
|
+ json,
|
|
|
+ json,
|
|
|
+ response.headers,
|
|
|
+ );
|
|
|
+
|
|
|
+ return new RequestApiResult(
|
|
|
+ resultModelClass ?? instance.config.modelClassCreator,
|
|
|
+ json?.code || response.status,
|
|
|
+ json.message,
|
|
|
+ json.data,
|
|
|
+ json
|
|
|
+ );
|
|
|
+ }
|
|
|
+ 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, '', response.url, reject, instance);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ private static readonly DEVICE_UID_KEY = 'deviceUid';
|
|
|
+ private uid = '';
|
|
|
+
|
|
|
+ getDeviceUid() {
|
|
|
+ if (!this.uid) {
|
|
|
+ this.uid = localStorage.getItem(MengyuServerRequestModule.DEVICE_UID_KEY) ?? '';
|
|
|
+ if (!this.uid) {
|
|
|
+ this.uid = RandomUtils.genNonDuplicateID(20);
|
|
|
+ localStorage.setItem(MengyuServerRequestModule.DEVICE_UID_KEY, this.uid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.uid;
|
|
|
+ }
|
|
|
+}
|