@moluoxixi/ajax-package 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ import { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, default as axios } from 'axios';
2
+ import { BaseHttpClientConfig } from './_types/index.ts';
3
+ import { MessageInstance, NotificationInstance } from './_utils/index.ts';
4
+ export default class BaseHttpClient {
5
+ protected baseURL: string;
6
+ protected timeout: number;
7
+ protected onTimeout: () => void;
8
+ protected getToken?: () => string | null;
9
+ protected onLoginRequired?: () => void;
10
+ instance: ReturnType<typeof axios.create>;
11
+ protected messageInstance: MessageInstance;
12
+ protected notificationInstance: NotificationInstance;
13
+ constructor(config: BaseHttpClientConfig);
14
+ processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig<any>;
15
+ processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
16
+ processResponseError(error: AxiosError): Promise<AxiosError>;
17
+ private setupInterceptors;
18
+ protected request<R>(config: AxiosRequestConfig): Promise<AxiosResponse['data']>;
19
+ get<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
20
+ post<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
21
+ delete<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
22
+ put<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
23
+ all<R>(requests: Array<AxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
24
+ uploadFile<R>(url: string, file: File | Blob, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
25
+ }
@@ -4,7 +4,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
4
4
  default: string;
5
5
  };
6
6
  width: {
7
- type: (StringConstructor | NumberConstructor)[];
7
+ type: (NumberConstructor | StringConstructor)[];
8
8
  default: number;
9
9
  };
10
10
  userName: {
@@ -40,7 +40,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
40
40
  default: undefined;
41
41
  };
42
42
  errorCode: {
43
- type: (StringConstructor | NumberConstructor)[];
43
+ type: (NumberConstructor | StringConstructor)[];
44
44
  default: undefined;
45
45
  };
46
46
  modelValue: {
@@ -60,7 +60,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
60
60
  default: string;
61
61
  };
62
62
  width: {
63
- type: (StringConstructor | NumberConstructor)[];
63
+ type: (NumberConstructor | StringConstructor)[];
64
64
  default: number;
65
65
  };
66
66
  userName: {
@@ -96,7 +96,7 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
96
96
  default: undefined;
97
97
  };
98
98
  errorCode: {
99
- type: (StringConstructor | NumberConstructor)[];
99
+ type: (NumberConstructor | StringConstructor)[];
100
100
  default: undefined;
101
101
  };
102
102
  modelValue: {
@@ -104,12 +104,13 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
104
104
  default: boolean;
105
105
  };
106
106
  }>> & Readonly<{
107
- "onUpdate:modelValue"?: ((val: boolean) => any) | undefined;
108
107
  onClose?: (() => any) | undefined;
109
108
  onConfirm?: ((data: any) => any) | undefined;
109
+ "onUpdate:modelValue"?: ((val: boolean) => any) | undefined;
110
110
  onReport?: (() => any) | undefined;
111
111
  }>, {
112
112
  title: string;
113
+ modelValue: boolean;
113
114
  width: string | number;
114
115
  userName: string;
115
116
  userId: string;
@@ -120,6 +121,5 @@ declare const _default: import('vue').DefineComponent<import('vue').ExtractPropT
120
121
  traceId: string;
121
122
  errorMessage: string;
122
123
  errorCode: string | number;
123
- modelValue: boolean;
124
124
  }, {}, {}, {}, string, import('vue').ComponentProvideOptions, true, {}, any>;
125
125
  export default _default;
@@ -1,8 +1,14 @@
1
1
  import { App } from 'vue';
2
2
  import { default as BaseApi } from '../class.ts';
3
- export interface BaseApiConfig {
3
+ export interface BaseHttpClientConfig {
4
4
  baseURL?: string;
5
5
  timeout?: number;
6
+ onTimeout?: () => void;
7
+ getToken?: () => string | null;
8
+ onLoginRequired?: () => void;
9
+ [key: string]: any;
10
+ }
11
+ export interface BaseApiConfig extends BaseHttpClientConfig {
6
12
  responseFields?: {
7
13
  code?: string;
8
14
  message?: string;
@@ -10,11 +16,7 @@ export interface BaseApiConfig {
10
16
  errors?: string;
11
17
  tips?: string;
12
18
  };
13
- onTimeout?: () => void;
14
- getToken?: () => string | null;
15
- onLoginRequired?: () => void;
16
19
  enableSystemErrorDialog?: boolean;
17
- [key: string]: any;
18
20
  }
19
21
  export interface vueAxiosPluginOptionsType {
20
22
  default?: BaseApiConfig;
@@ -1,4 +1,4 @@
1
- export * from './api';
2
- export * from './emits';
3
- export type { SystemErrorDialogEmitsType } from './emits';
4
- export * from './props';
1
+ export * from './api.ts';
2
+ export * from './emits.ts';
3
+ export type { SystemErrorDialogEmitsType } from './emits.ts';
4
+ export * from './props.ts';
@@ -1,3 +1,3 @@
1
- export * from './messageWrapper';
2
- export * from './notificationWrapper';
3
- export * from './systemErrorInfo';
1
+ export * from './messageWrapper.ts';
2
+ export * from './notificationWrapper.ts';
3
+ export * from './systemErrorInfo.ts';
package/es/class.d.ts CHANGED
@@ -1,29 +1,21 @@
1
- import { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, default as axios } from 'axios';
2
- import { BaseApiConfig } from './_types/index';
3
- import { MessageInstance, NotificationInstance } from './_utils/index';
4
- export default class BaseApi {
5
- protected baseURL: string;
6
- protected timeout: number;
1
+ import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2
+ import { BaseApiConfig } from './_types/index.ts';
3
+ import { default as BaseHttpClient } from './BaseHttpClient.ts';
4
+ export default class BaseApi extends BaseHttpClient {
7
5
  protected responseFields: Required<BaseApiConfig['responseFields']>;
8
- protected onTimeout: () => void;
9
- protected getToken?: () => string | null;
10
- protected onLoginRequired?: () => void;
11
6
  protected enableSystemErrorDialog: boolean;
12
- instance: ReturnType<typeof axios.create>;
13
- protected messageInstance: MessageInstance;
14
- protected notificationInstance: NotificationInstance;
15
7
  constructor(config: BaseApiConfig);
16
- processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig<any>;
17
- processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
8
+ processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig;
18
9
  processResponseError(error: AxiosError): Promise<AxiosError>;
19
- private setupInterceptors;
20
- protected request<R>(config: AxiosRequestConfig): Promise<AxiosResponse['data']>;
21
- get<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
22
- post<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
23
- delete<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
24
- put<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
25
- all<R>(requests: Array<AxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
26
- uploadFile<R>(url: string, file: File | Blob, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
10
+ processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
11
+ protected parseResponseFields(data: any): {
12
+ code: any;
13
+ message: any;
14
+ responseData: any;
15
+ };
16
+ protected handleErrorCode(response: AxiosResponse, code: any, message: any, responseData: any): void;
17
+ protected handleErrors(responseData: any): void;
18
+ protected handleTips(responseData: any): void;
27
19
  private showSystemExceptionDialog;
28
- private reportError;
20
+ protected reportError(errorInfo: any): Promise<void>;
29
21
  }
package/es/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { default as BaseApi } from './class';
2
- import { default as VueAxiosPlugin, createHttpService, getHttpService } from './netseriver';
3
- export { BaseApi, createHttpService, getHttpService, VueAxiosPlugin, };
1
+ import { default as BaseHttpClient } from './BaseHttpClient.ts';
2
+ import { default as BaseApi } from './class.ts';
3
+ import { default as VueAxiosPlugin, createHttpService, getHttpService } from './netseriver.ts';
4
+ export { BaseApi, BaseHttpClient, createHttpService, getHttpService, VueAxiosPlugin, };
4
5
  export default getHttpService;
package/es/index.mjs CHANGED
@@ -2,9 +2,9 @@
2
2
  "use strict";
3
3
  try {
4
4
  if (typeof document !== "undefined") {
5
- if (!document.getElementById("874c375f-5c2a-486a-be46-d7e2fd195381")) {
5
+ if (!document.getElementById("c6a35fc5-e19d-40c8-8129-fe6481835a30")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "874c375f-5c2a-486a-be46-d7e2fd195381";
7
+ elementStyle.id = "c6a35fc5-e19d-40c8-8129-fe6481835a30";
8
8
  elementStyle.appendChild(document.createTextNode("._root_11p33_1 .el-dialog__header {\n padding: 0 12px 12px;\n}\n\n._root_11p33_1 .el-dialog__body {\n border-top: 1px solid #e5e7eb;\n border-bottom: 1px solid #e5e7eb;\n padding: 0 12px;\n}\n\n._root_11p33_1 .el-dialog__footer {\n padding: 0 12px;\n}"));
9
9
  document.head.appendChild(elementStyle);
10
10
  }
@@ -2483,22 +2483,6 @@ const {
2483
2483
  getAdapter,
2484
2484
  mergeConfig: mergeConfig$1
2485
2485
  } = axios;
2486
- async function dynamicImports(modulePromise, exportNames) {
2487
- const module = await modulePromise;
2488
- const result = {};
2489
- for (const _name of exportNames) {
2490
- const name = _name || "default";
2491
- if (name === "default") {
2492
- result[name] = module.default ?? module;
2493
- } else {
2494
- if (!(name in module)) {
2495
- throw new Error(`模块中不存在导出 "${name}"`);
2496
- }
2497
- result[name] = module[name];
2498
- }
2499
- }
2500
- return result;
2501
- }
2502
2486
  const configProviderContextKey = Symbol();
2503
2487
  const defaultNamespace = "el";
2504
2488
  const statePrefix = "is-";
@@ -12065,141 +12049,6 @@ notify.closeAll = closeAll;
12065
12049
  notify.updateOffsets = updateOffsets;
12066
12050
  notify._context = null;
12067
12051
  const ElNotification = withInstallFunction(notify, "$notify");
12068
- function createApiDialog(DialogComponent) {
12069
- let container = null;
12070
- let vnode = null;
12071
- let isOpen = false;
12072
- let currentReject = null;
12073
- const DefaultDialog = defineComponent({
12074
- name: "DefaultApiDialog",
12075
- props: {
12076
- modelValue: {
12077
- type: Boolean,
12078
- default: false
12079
- },
12080
- title: {
12081
- type: String,
12082
- default: "对话框"
12083
- },
12084
- width: {
12085
- type: [String, Number],
12086
- default: "50%"
12087
- }
12088
- },
12089
- emits: ["close", "confirm", "update:modelValue"],
12090
- setup(props, { slots, emit }) {
12091
- const handleClose = () => {
12092
- emit("close");
12093
- emit("update:modelValue", false);
12094
- };
12095
- const handleConfirm = (data) => {
12096
- emit("confirm", data);
12097
- emit("update:modelValue", false);
12098
- };
12099
- return () => h(ElDialog, {
12100
- "modelValue": props.modelValue,
12101
- "title": props.title,
12102
- "width": props.width,
12103
- "onUpdate:modelValue": (val) => {
12104
- emit("update:modelValue", val);
12105
- if (!val) {
12106
- handleClose();
12107
- }
12108
- }
12109
- }, {
12110
- default: () => {
12111
- var _a2;
12112
- return (_a2 = slots.default) == null ? void 0 : _a2.call(slots);
12113
- },
12114
- header: slots.header,
12115
- footer: slots.footer || (() => h("div", { class: "dialog-footer" }, [
12116
- h(ElButton, {
12117
- onClick: () => handleClose()
12118
- }, () => "取消"),
12119
- h(ElButton, {
12120
- type: "primary",
12121
- onClick: () => handleConfirm({})
12122
- }, () => "确定")
12123
- ]))
12124
- });
12125
- }
12126
- });
12127
- const FinalDialogComponent = DialogComponent || DefaultDialog;
12128
- function createContainer() {
12129
- const el = document.createElement("div");
12130
- document.body.appendChild(el);
12131
- return el;
12132
- }
12133
- function show(options = {}) {
12134
- if (isOpen) {
12135
- close2();
12136
- }
12137
- container = createContainer();
12138
- const { props = {}, slots = {}, appContext } = options;
12139
- return new Promise((resolve, reject) => {
12140
- currentReject = reject;
12141
- vnode = createVNode(
12142
- FinalDialogComponent,
12143
- {
12144
- ...props,
12145
- modelValue: true,
12146
- onUpdateModelValue: (val) => {
12147
- if (!val) {
12148
- reject(new Error("对话框已关闭"));
12149
- cleanup();
12150
- }
12151
- },
12152
- onClose: () => {
12153
- reject(new Error("对话框已关闭"));
12154
- cleanup();
12155
- },
12156
- onConfirm: (data) => {
12157
- resolve(data);
12158
- cleanup();
12159
- }
12160
- },
12161
- slots
12162
- );
12163
- if (appContext) {
12164
- vnode.appContext = appContext;
12165
- }
12166
- render(vnode, container);
12167
- isOpen = true;
12168
- });
12169
- }
12170
- function close2() {
12171
- if (isOpen && vnode && container) {
12172
- if (vnode.component && vnode.component.exposed && typeof vnode.component.exposed.close === "function") {
12173
- vnode.component.exposed.close();
12174
- }
12175
- if (currentReject) {
12176
- currentReject(new Error("对话框被主动关闭"));
12177
- currentReject = null;
12178
- }
12179
- cleanup();
12180
- }
12181
- }
12182
- function cleanup() {
12183
- if (container) {
12184
- render(null, container);
12185
- container.remove();
12186
- container = null;
12187
- }
12188
- vnode = null;
12189
- isOpen = false;
12190
- }
12191
- if (!DialogComponent) {
12192
- return {
12193
- Dialog: DefaultDialog,
12194
- show,
12195
- close: close2
12196
- };
12197
- }
12198
- return {
12199
- show,
12200
- close: close2
12201
- };
12202
- }
12203
12052
  const hasDocument$2 = typeof document !== "undefined";
12204
12053
  function createMessageWrapper() {
12205
12054
  if (hasDocument$2) {
@@ -12321,8 +12170,6 @@ function extractSystemErrorInfo(response, code, message2) {
12321
12170
  errorMessage: message2
12322
12171
  };
12323
12172
  }
12324
- const hasDocument = typeof document !== "undefined";
12325
- let systemErrorDialogInstance = null;
12326
12173
  function defaultOnLoginRequired() {
12327
12174
  if (typeof window !== "undefined") {
12328
12175
  window.location.href = `/login?redirect=${encodeURIComponent(window.location.href)}`;
@@ -12331,45 +12178,32 @@ function defaultOnLoginRequired() {
12331
12178
  function defaultGetToken() {
12332
12179
  return typeof localStorage !== "undefined" ? localStorage.getItem("token") || "" : "";
12333
12180
  }
12334
- class BaseApi {
12181
+ class BaseHttpClient {
12335
12182
  constructor(config) {
12336
12183
  __publicField(this, "baseURL", "");
12337
12184
  __publicField(this, "timeout", 5e3);
12338
- __publicField(this, "responseFields");
12339
12185
  __publicField(this, "onTimeout");
12340
12186
  __publicField(this, "getToken");
12341
12187
  __publicField(this, "onLoginRequired");
12342
- __publicField(this, "enableSystemErrorDialog");
12343
12188
  __publicField(this, "instance");
12344
12189
  __publicField(this, "messageInstance");
12345
12190
  __publicField(this, "notificationInstance");
12346
12191
  const {
12347
12192
  baseURL = "",
12348
12193
  timeout = 5e3,
12349
- responseFields,
12350
12194
  onTimeout = () => {
12351
12195
  },
12352
12196
  getToken = defaultGetToken,
12353
12197
  onLoginRequired = defaultOnLoginRequired,
12354
- enableSystemErrorDialog = true,
12355
12198
  ...axiosConfig
12356
12199
  } = config;
12357
12200
  this.baseURL = baseURL;
12358
12201
  this.timeout = timeout;
12359
12202
  this.messageInstance = createMessageWrapper();
12360
12203
  this.notificationInstance = createNotificationWrapper();
12361
- this.responseFields = {
12362
- code: "Code",
12363
- message: "Message",
12364
- data: "data",
12365
- errors: "errors",
12366
- tips: "tips",
12367
- ...responseFields
12368
- };
12369
12204
  this.onTimeout = onTimeout;
12370
12205
  this.getToken = getToken;
12371
12206
  this.onLoginRequired = onLoginRequired;
12372
- this.enableSystemErrorDialog = enableSystemErrorDialog;
12373
12207
  this.instance = axios.create({
12374
12208
  baseURL: this.baseURL,
12375
12209
  timeout: this.timeout,
@@ -12381,87 +12215,7 @@ class BaseApi {
12381
12215
  return config;
12382
12216
  }
12383
12217
  processResponseConfig(response) {
12384
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12385
- const data = response.data;
12386
- const getValueByPath = (obj, path) => {
12387
- if (!path)
12388
- return obj;
12389
- const keys = path.split(".");
12390
- let result = obj;
12391
- for (const key of keys) {
12392
- if (result && typeof result === "object" && key in result) {
12393
- result = result[key];
12394
- } else {
12395
- return void 0;
12396
- }
12397
- }
12398
- return result;
12399
- };
12400
- const code = getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
12401
- const message2 = getValueByPath(data, (_b = this.responseFields) == null ? void 0 : _b.message);
12402
- const responseData = getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12403
- if (code === 401) {
12404
- throw new Error("登录失效,请重新登录");
12405
- }
12406
- if (code === -1) {
12407
- if (this.enableSystemErrorDialog) {
12408
- this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
12409
- console.error("显示系统异常对话框失败:", error);
12410
- });
12411
- }
12412
- throw new Error(message2 || "系统异常");
12413
- }
12414
- if (code && code !== 200) {
12415
- (_d = this.messageInstance) == null ? void 0 : _d.error({
12416
- message: message2 || "请求失败",
12417
- duration: 5 * 1e3
12418
- });
12419
- throw new Error(message2 || "请求失败");
12420
- }
12421
- const errorsField = (_e = this.responseFields) == null ? void 0 : _e.errors;
12422
- if (errorsField) {
12423
- const errors = responseData == null ? void 0 : responseData[errorsField];
12424
- if (Array.isArray(errors) && errors.length) {
12425
- const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12426
- if (hasDocument) {
12427
- (_f = this.notificationInstance) == null ? void 0 : _f.call(this, {
12428
- title: "提示",
12429
- message: html,
12430
- type: "error"
12431
- });
12432
- } else {
12433
- const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12434
- (_g = this.notificationInstance) == null ? void 0 : _g.call(this, {
12435
- title: "提示",
12436
- message: errorMessages,
12437
- type: "error"
12438
- });
12439
- }
12440
- throw new Error("请求错误");
12441
- }
12442
- }
12443
- const tipsField = (_h = this.responseFields) == null ? void 0 : _h.tips;
12444
- if (tipsField) {
12445
- const tips = responseData == null ? void 0 : responseData[tipsField];
12446
- if (Array.isArray(tips) && tips.length) {
12447
- const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12448
- if (hasDocument) {
12449
- (_i = this.notificationInstance) == null ? void 0 : _i.call(this, {
12450
- title: "提示",
12451
- message: html,
12452
- type: "warning"
12453
- });
12454
- } else {
12455
- const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12456
- (_j = this.notificationInstance) == null ? void 0 : _j.call(this, {
12457
- title: "提示",
12458
- message: tipMessages,
12459
- type: "warning"
12460
- });
12461
- }
12462
- }
12463
- }
12464
- return responseData;
12218
+ return response.data;
12465
12219
  }
12466
12220
  async processResponseError(error) {
12467
12221
  return error;
@@ -12554,6 +12308,285 @@ class BaseApi {
12554
12308
  }
12555
12309
  });
12556
12310
  }
12311
+ }
12312
+ async function dynamicImports(modulePromise, exportNames) {
12313
+ const module = await modulePromise;
12314
+ const result = {};
12315
+ for (const _name of exportNames) {
12316
+ const name = _name || "default";
12317
+ if (name === "default") {
12318
+ result[name] = module.default ?? module;
12319
+ } else {
12320
+ if (!(name in module)) {
12321
+ throw new Error(`模块中不存在导出 "${name}"`);
12322
+ }
12323
+ result[name] = module[name];
12324
+ }
12325
+ }
12326
+ return result;
12327
+ }
12328
+ function createApiDialog(DialogComponent) {
12329
+ let container = null;
12330
+ let vnode = null;
12331
+ let isOpen = false;
12332
+ let currentReject = null;
12333
+ const DefaultDialog = defineComponent({
12334
+ name: "DefaultApiDialog",
12335
+ props: {
12336
+ modelValue: {
12337
+ type: Boolean,
12338
+ default: false
12339
+ },
12340
+ title: {
12341
+ type: String,
12342
+ default: "对话框"
12343
+ },
12344
+ width: {
12345
+ type: [String, Number],
12346
+ default: "50%"
12347
+ }
12348
+ },
12349
+ emits: ["close", "confirm", "update:modelValue"],
12350
+ setup(props, { slots, emit }) {
12351
+ const handleClose = () => {
12352
+ emit("close");
12353
+ emit("update:modelValue", false);
12354
+ };
12355
+ const handleConfirm = (data) => {
12356
+ emit("confirm", data);
12357
+ emit("update:modelValue", false);
12358
+ };
12359
+ return () => h(ElDialog, {
12360
+ "modelValue": props.modelValue,
12361
+ "title": props.title,
12362
+ "width": props.width,
12363
+ "onUpdate:modelValue": (val) => {
12364
+ emit("update:modelValue", val);
12365
+ if (!val) {
12366
+ handleClose();
12367
+ }
12368
+ }
12369
+ }, {
12370
+ default: () => {
12371
+ var _a2;
12372
+ return (_a2 = slots.default) == null ? void 0 : _a2.call(slots);
12373
+ },
12374
+ header: slots.header,
12375
+ footer: slots.footer || (() => h("div", { class: "dialog-footer" }, [
12376
+ h(ElButton, {
12377
+ onClick: () => handleClose()
12378
+ }, () => "取消"),
12379
+ h(ElButton, {
12380
+ type: "primary",
12381
+ onClick: () => handleConfirm({})
12382
+ }, () => "确定")
12383
+ ]))
12384
+ });
12385
+ }
12386
+ });
12387
+ const FinalDialogComponent = DialogComponent || DefaultDialog;
12388
+ function createContainer() {
12389
+ const el = document.createElement("div");
12390
+ document.body.appendChild(el);
12391
+ return el;
12392
+ }
12393
+ function show(options = {}) {
12394
+ if (isOpen) {
12395
+ close2();
12396
+ }
12397
+ container = createContainer();
12398
+ const { props = {}, slots = {}, appContext } = options;
12399
+ return new Promise((resolve, reject) => {
12400
+ currentReject = reject;
12401
+ vnode = createVNode(
12402
+ FinalDialogComponent,
12403
+ {
12404
+ ...props,
12405
+ modelValue: true,
12406
+ onUpdateModelValue: (val) => {
12407
+ if (!val) {
12408
+ reject(new Error("对话框已关闭"));
12409
+ cleanup();
12410
+ }
12411
+ },
12412
+ onClose: () => {
12413
+ reject(new Error("对话框已关闭"));
12414
+ cleanup();
12415
+ },
12416
+ onConfirm: (data) => {
12417
+ resolve(data);
12418
+ cleanup();
12419
+ }
12420
+ },
12421
+ slots
12422
+ );
12423
+ if (appContext) {
12424
+ vnode.appContext = appContext;
12425
+ }
12426
+ render(vnode, container);
12427
+ isOpen = true;
12428
+ });
12429
+ }
12430
+ function close2() {
12431
+ if (isOpen && vnode && container) {
12432
+ if (vnode.component && vnode.component.exposed && typeof vnode.component.exposed.close === "function") {
12433
+ vnode.component.exposed.close();
12434
+ }
12435
+ if (currentReject) {
12436
+ currentReject(new Error("对话框被主动关闭"));
12437
+ currentReject = null;
12438
+ }
12439
+ cleanup();
12440
+ }
12441
+ }
12442
+ function cleanup() {
12443
+ if (container) {
12444
+ render(null, container);
12445
+ container.remove();
12446
+ container = null;
12447
+ }
12448
+ vnode = null;
12449
+ isOpen = false;
12450
+ }
12451
+ if (!DialogComponent) {
12452
+ return {
12453
+ Dialog: DefaultDialog,
12454
+ show,
12455
+ close: close2
12456
+ };
12457
+ }
12458
+ return {
12459
+ show,
12460
+ close: close2
12461
+ };
12462
+ }
12463
+ const hasDocument = typeof document !== "undefined";
12464
+ let systemErrorDialogInstance = null;
12465
+ class BaseApi extends BaseHttpClient {
12466
+ constructor(config) {
12467
+ const {
12468
+ responseFields,
12469
+ enableSystemErrorDialog = true,
12470
+ ...baseConfig
12471
+ } = config;
12472
+ super(baseConfig);
12473
+ __publicField(this, "responseFields");
12474
+ __publicField(this, "enableSystemErrorDialog");
12475
+ this.responseFields = {
12476
+ code: "Code",
12477
+ message: "Message",
12478
+ data: "data",
12479
+ errors: "errors",
12480
+ tips: "tips",
12481
+ ...responseFields
12482
+ };
12483
+ this.enableSystemErrorDialog = enableSystemErrorDialog;
12484
+ }
12485
+ processRequestConfig(config) {
12486
+ return super.processRequestConfig(config);
12487
+ }
12488
+ async processResponseError(error) {
12489
+ return super.processResponseError(error);
12490
+ }
12491
+ processResponseConfig(response) {
12492
+ const data = response.data;
12493
+ const { code, message: message2, responseData } = this.parseResponseFields(data);
12494
+ this.handleErrorCode(response, code, message2, responseData);
12495
+ this.handleErrors(responseData);
12496
+ this.handleTips(responseData);
12497
+ return responseData;
12498
+ }
12499
+ parseResponseFields(data) {
12500
+ var _a2, _b, _c;
12501
+ const getValueByPath = (obj, path) => {
12502
+ if (!path)
12503
+ return obj;
12504
+ const keys = path.split(".");
12505
+ let result = obj;
12506
+ for (const key of keys) {
12507
+ if (result && typeof result === "object" && key in result) {
12508
+ result = result[key];
12509
+ } else {
12510
+ return void 0;
12511
+ }
12512
+ }
12513
+ return result;
12514
+ };
12515
+ const code = getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
12516
+ const message2 = getValueByPath(data, (_b = this.responseFields) == null ? void 0 : _b.message);
12517
+ const responseData = getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12518
+ return { code, message: message2, responseData };
12519
+ }
12520
+ handleErrorCode(response, code, message2, responseData) {
12521
+ var _a2;
12522
+ if (code === 401) {
12523
+ throw new Error("登录失效,请重新登录");
12524
+ }
12525
+ if (code === -1) {
12526
+ if (this.enableSystemErrorDialog) {
12527
+ this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
12528
+ console.error("显示系统异常对话框失败:", error);
12529
+ });
12530
+ }
12531
+ throw new Error(message2 || "系统异常");
12532
+ }
12533
+ if (code && code !== 200) {
12534
+ (_a2 = this.messageInstance) == null ? void 0 : _a2.error({
12535
+ message: message2 || "请求失败",
12536
+ duration: 5 * 1e3
12537
+ });
12538
+ throw new Error(message2 || "请求失败");
12539
+ }
12540
+ }
12541
+ handleErrors(responseData) {
12542
+ var _a2, _b, _c;
12543
+ const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
12544
+ if (errorsField) {
12545
+ const errors = responseData == null ? void 0 : responseData[errorsField];
12546
+ if (Array.isArray(errors) && errors.length) {
12547
+ const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12548
+ if (hasDocument) {
12549
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12550
+ title: "提示",
12551
+ message: html,
12552
+ type: "error"
12553
+ });
12554
+ } else {
12555
+ const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12556
+ (_c = this.notificationInstance) == null ? void 0 : _c.call(this, {
12557
+ title: "提示",
12558
+ message: errorMessages,
12559
+ type: "error"
12560
+ });
12561
+ }
12562
+ throw new Error("请求错误");
12563
+ }
12564
+ }
12565
+ }
12566
+ handleTips(responseData) {
12567
+ var _a2, _b, _c;
12568
+ const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
12569
+ if (tipsField) {
12570
+ const tips = responseData == null ? void 0 : responseData[tipsField];
12571
+ if (Array.isArray(tips) && tips.length) {
12572
+ const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12573
+ if (hasDocument) {
12574
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12575
+ title: "提示",
12576
+ message: html,
12577
+ type: "warning"
12578
+ });
12579
+ } else {
12580
+ const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12581
+ (_c = this.notificationInstance) == null ? void 0 : _c.call(this, {
12582
+ title: "提示",
12583
+ message: tipMessages,
12584
+ type: "warning"
12585
+ });
12586
+ }
12587
+ }
12588
+ }
12589
+ }
12557
12590
  async showSystemExceptionDialog(response, responseData, code, message2) {
12558
12591
  if (!hasDocument) {
12559
12592
  console.error("系统异常信息:", responseData);
@@ -12905,6 +12938,7 @@ const SystemErrorDialog$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object
12905
12938
  }, Symbol.toStringTag, { value: "Module" }));
12906
12939
  export {
12907
12940
  BaseApi,
12941
+ BaseHttpClient,
12908
12942
  VueAxiosPlugin,
12909
12943
  createHttpService,
12910
12944
  getHttpService as default,
@@ -1,5 +1,5 @@
1
- import { BaseApiConfig, vueAxiosPluginType, vueHttpServiceType } from './_types/index';
2
- import { default as BaseApi } from './class';
1
+ import { BaseApiConfig, vueAxiosPluginType, vueHttpServiceType } from './_types/index.ts';
2
+ import { default as BaseApi } from './class.ts';
3
3
  declare global {
4
4
  interface Window {
5
5
  $http?: vueHttpServiceType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",