@moluoxixi/ajax-package 0.0.11 → 0.0.13

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,31 @@
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: (messageInstance: MessageInstance) => void;
8
+ protected getToken?: () => string | null;
9
+ protected onLoginRequired?: (messageInstance: MessageInstance) => 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
+ protected handleHttpStatus(response: AxiosResponse): void;
17
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
18
+ processResponseError(error: AxiosError): Promise<AxiosError>;
19
+ protected handleAuthenticationError(error: AxiosError): void;
20
+ protected handleTimeoutError(error: AxiosError): void;
21
+ protected handleNetworkError(error: AxiosError): void;
22
+ private setupInterceptors;
23
+ protected request<R>(config: AxiosRequestConfig): Promise<AxiosResponse['data']>;
24
+ get<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
25
+ post<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
26
+ delete<R>(url: string, params?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
27
+ put<R>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
28
+ all<R>(requests: Array<AxiosRequestConfig | Promise<AxiosResponse<R>>>): Promise<AxiosResponse['data'][]>;
29
+ uploadFile<R>(url: string, file: File | Blob, config?: AxiosRequestConfig): Promise<AxiosResponse['data']>;
30
+ downloadFile(blob: Blob, filename?: string): void;
31
+ }
@@ -104,9 +104,9 @@ 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;
@@ -1,8 +1,15 @@
1
1
  import { App } from 'vue';
2
+ import { MessageInstance } from '../_utils/index.ts';
2
3
  import { default as BaseApi } from '../class.ts';
3
- export interface BaseApiConfig {
4
+ export interface BaseHttpClientConfig {
4
5
  baseURL?: string;
5
6
  timeout?: number;
7
+ onTimeout?: (messageInstance: MessageInstance) => void;
8
+ getToken?: () => string | null;
9
+ onLoginRequired?: (messageInstance: MessageInstance) => void;
10
+ [key: string]: any;
11
+ }
12
+ export interface BaseApiConfig extends BaseHttpClientConfig {
6
13
  responseFields?: {
7
14
  code?: string;
8
15
  message?: string;
@@ -10,11 +17,7 @@ export interface BaseApiConfig {
10
17
  errors?: string;
11
18
  tips?: string;
12
19
  };
13
- onTimeout?: () => void;
14
- getToken?: () => string | null;
15
- onLoginRequired?: () => void;
16
20
  enableSystemErrorDialog?: boolean;
17
- [key: string]: any;
18
21
  }
19
22
  export interface vueAxiosPluginOptionsType {
20
23
  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,34 @@
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
+ protected handleHttpStatus(response: AxiosResponse): void;
11
+ protected handleAuthenticationError(error: AxiosError): void;
12
+ protected handleTimeoutError(error: AxiosError): void;
13
+ protected handleNetworkError(error: AxiosError): void;
14
+ protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
15
+ protected parseResponseFields(data: any): {
16
+ code: any;
17
+ message: any;
18
+ responseData: any;
19
+ };
20
+ protected handleSystemError(response: AxiosResponse, code: any, message: any, responseData: any): void;
21
+ protected handleBusinessError(code: any, message: any): void;
22
+ protected handleErrorArray(responseData: any): void;
23
+ protected showErrorArrayNotification(errors: Array<{
24
+ code: string;
25
+ message: string;
26
+ }>): void;
27
+ protected handleTips(responseData: any): void;
28
+ protected showTipsNotification(tips: Array<{
29
+ code: string;
30
+ message: string;
31
+ }>): void;
27
32
  private showSystemExceptionDialog;
28
- private reportError;
33
+ protected reportError(errorInfo: any): Promise<void>;
29
34
  }
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("065a78da-34dc-4a45-874d-4fb480012cd9")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "874c375f-5c2a-486a-be46-d7e2fd195381";
7
+ elementStyle.id = "065a78da-34dc-4a45-874d-4fb480012cd9";
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,9 +12170,11 @@ function extractSystemErrorInfo(response, code, message2) {
12321
12170
  errorMessage: message2
12322
12171
  };
12323
12172
  }
12324
- const hasDocument = typeof document !== "undefined";
12325
- let systemErrorDialogInstance = null;
12326
- function defaultOnLoginRequired() {
12173
+ function defaultOnLoginRequired(messageInstance) {
12174
+ messageInstance == null ? void 0 : messageInstance.error({
12175
+ message: "登录已过期,请重新登录",
12176
+ duration: 5 * 1e3
12177
+ });
12327
12178
  if (typeof window !== "undefined") {
12328
12179
  window.location.href = `/login?redirect=${encodeURIComponent(window.location.href)}`;
12329
12180
  }
@@ -12331,45 +12182,32 @@ function defaultOnLoginRequired() {
12331
12182
  function defaultGetToken() {
12332
12183
  return typeof localStorage !== "undefined" ? localStorage.getItem("token") || "" : "";
12333
12184
  }
12334
- class BaseApi {
12185
+ class BaseHttpClient {
12335
12186
  constructor(config) {
12336
12187
  __publicField(this, "baseURL", "");
12337
12188
  __publicField(this, "timeout", 5e3);
12338
- __publicField(this, "responseFields");
12339
12189
  __publicField(this, "onTimeout");
12340
12190
  __publicField(this, "getToken");
12341
12191
  __publicField(this, "onLoginRequired");
12342
- __publicField(this, "enableSystemErrorDialog");
12343
12192
  __publicField(this, "instance");
12344
12193
  __publicField(this, "messageInstance");
12345
12194
  __publicField(this, "notificationInstance");
12346
12195
  const {
12347
12196
  baseURL = "",
12348
12197
  timeout = 5e3,
12349
- responseFields,
12350
12198
  onTimeout = () => {
12351
12199
  },
12352
12200
  getToken = defaultGetToken,
12353
12201
  onLoginRequired = defaultOnLoginRequired,
12354
- enableSystemErrorDialog = true,
12355
12202
  ...axiosConfig
12356
12203
  } = config;
12357
12204
  this.baseURL = baseURL;
12358
12205
  this.timeout = timeout;
12359
12206
  this.messageInstance = createMessageWrapper();
12360
12207
  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
12208
  this.onTimeout = onTimeout;
12370
12209
  this.getToken = getToken;
12371
12210
  this.onLoginRequired = onLoginRequired;
12372
- this.enableSystemErrorDialog = enableSystemErrorDialog;
12373
12211
  this.instance = axios.create({
12374
12212
  baseURL: this.baseURL,
12375
12213
  timeout: this.timeout,
@@ -12381,90 +12219,44 @@ class BaseApi {
12381
12219
  return config;
12382
12220
  }
12383
12221
  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("登录失效,请重新登录");
12222
+ this.handleHttpStatus(response);
12223
+ return this.handleSuccessResponse(response);
12224
+ }
12225
+ handleHttpStatus(response) {
12226
+ var _a2;
12227
+ if (response.status !== 200) {
12228
+ throw new Error(((_a2 = response.data) == null ? void 0 : _a2.message) || `HTTP Error: ${response.status}`);
12405
12229
  }
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 || "系统异常");
12230
+ }
12231
+ handleSuccessResponse(response) {
12232
+ return response.data;
12233
+ }
12234
+ async processResponseError(error) {
12235
+ this.handleAuthenticationError(error);
12236
+ this.handleTimeoutError(error);
12237
+ this.handleNetworkError(error);
12238
+ return error;
12239
+ }
12240
+ handleAuthenticationError(error) {
12241
+ var _a2, _b;
12242
+ if (((_a2 = error.response) == null ? void 0 : _a2.status) === 401) {
12243
+ (_b = this.onLoginRequired) == null ? void 0 : _b.call(this, this.messageInstance);
12413
12244
  }
12414
- if (code && code !== 200) {
12415
- (_d = this.messageInstance) == null ? void 0 : _d.error({
12416
- message: message2 || "请求失败",
12245
+ }
12246
+ handleTimeoutError(error) {
12247
+ if (error.code === "ECONNABORTED" && error.message.includes("timeout")) {
12248
+ this.onTimeout(this.messageInstance);
12249
+ }
12250
+ }
12251
+ handleNetworkError(error) {
12252
+ var _a2, _b, _c;
12253
+ if (((_a2 = error.response) == null ? void 0 : _a2.status) !== 401 && error.code !== "ECONNABORTED") {
12254
+ const fallbackError = error;
12255
+ (_c = this.messageInstance) == null ? void 0 : _c.error({
12256
+ message: ((_b = fallbackError.response) == null ? void 0 : _b.data) || fallbackError.message || "网络错误",
12417
12257
  duration: 5 * 1e3
12418
12258
  });
12419
- throw new Error(message2 || "请求失败");
12420
12259
  }
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;
12465
- }
12466
- async processResponseError(error) {
12467
- return error;
12468
12260
  }
12469
12261
  setupInterceptors() {
12470
12262
  this.instance.interceptors.request.use(
@@ -12484,34 +12276,10 @@ class BaseApi {
12484
12276
  );
12485
12277
  this.instance.interceptors.response.use(
12486
12278
  (res) => {
12487
- var _a2;
12488
- if (res.status !== 200) {
12489
- return Promise.reject(new Error(((_a2 = res.data) == null ? void 0 : _a2.message) || "Error"));
12490
- }
12491
12279
  return this.processResponseConfig(res);
12492
12280
  },
12493
12281
  async (error) => {
12494
- var _a2, _b, _c, _d, _e, _f;
12495
12282
  await this.processResponseError(error);
12496
- if (((_a2 = error.response) == null ? void 0 : _a2.status) === 401) {
12497
- (_b = this.onLoginRequired) == null ? void 0 : _b.call(this);
12498
- (_c = this.messageInstance) == null ? void 0 : _c.error({
12499
- message: "登录已过期,请重新登录",
12500
- duration: 5 * 1e3
12501
- });
12502
- } else if (error.code === "ECONNABORTED" && error.message.includes("timeout")) {
12503
- this.onTimeout();
12504
- (_d = this.messageInstance) == null ? void 0 : _d.error({
12505
- message: "请求超时,请检查网络连接或稍后重试",
12506
- duration: 5 * 1e3
12507
- });
12508
- } else {
12509
- const fallbackError = error;
12510
- (_f = this.messageInstance) == null ? void 0 : _f.error({
12511
- message: ((_e = fallbackError.response) == null ? void 0 : _e.data) || fallbackError.message || "网络错误",
12512
- duration: 5 * 1e3
12513
- });
12514
- }
12515
12283
  return Promise.reject(error);
12516
12284
  }
12517
12285
  );
@@ -12554,6 +12322,320 @@ class BaseApi {
12554
12322
  }
12555
12323
  });
12556
12324
  }
12325
+ downloadFile(blob, filename) {
12326
+ if (typeof window === "undefined") {
12327
+ console.warn("downloadFile: 非浏览器环境,无法下载文件");
12328
+ return;
12329
+ }
12330
+ const url = window.URL.createObjectURL(blob);
12331
+ const link = document.createElement("a");
12332
+ link.href = url;
12333
+ link.download = filename || `download-${Date.now()}`;
12334
+ document.body.appendChild(link);
12335
+ link.click();
12336
+ document.body.removeChild(link);
12337
+ window.URL.revokeObjectURL(url);
12338
+ }
12339
+ }
12340
+ async function dynamicImports(modulePromise, exportNames) {
12341
+ const module = await modulePromise;
12342
+ const result = {};
12343
+ for (const _name of exportNames) {
12344
+ const name = _name || "default";
12345
+ if (name === "default") {
12346
+ result[name] = module.default ?? module;
12347
+ } else {
12348
+ if (!(name in module)) {
12349
+ throw new Error(`模块中不存在导出 "${name}"`);
12350
+ }
12351
+ result[name] = module[name];
12352
+ }
12353
+ }
12354
+ return result;
12355
+ }
12356
+ function createApiDialog(DialogComponent) {
12357
+ let container = null;
12358
+ let vnode = null;
12359
+ let isOpen = false;
12360
+ let currentReject = null;
12361
+ const DefaultDialog = defineComponent({
12362
+ name: "DefaultApiDialog",
12363
+ props: {
12364
+ modelValue: {
12365
+ type: Boolean,
12366
+ default: false
12367
+ },
12368
+ title: {
12369
+ type: String,
12370
+ default: "对话框"
12371
+ },
12372
+ width: {
12373
+ type: [String, Number],
12374
+ default: "50%"
12375
+ }
12376
+ },
12377
+ emits: ["close", "confirm", "update:modelValue"],
12378
+ setup(props, { slots, emit }) {
12379
+ const handleClose = () => {
12380
+ emit("close");
12381
+ emit("update:modelValue", false);
12382
+ };
12383
+ const handleConfirm = (data) => {
12384
+ emit("confirm", data);
12385
+ emit("update:modelValue", false);
12386
+ };
12387
+ return () => h(ElDialog, {
12388
+ "modelValue": props.modelValue,
12389
+ "title": props.title,
12390
+ "width": props.width,
12391
+ "onUpdate:modelValue": (val) => {
12392
+ emit("update:modelValue", val);
12393
+ if (!val) {
12394
+ handleClose();
12395
+ }
12396
+ }
12397
+ }, {
12398
+ default: () => {
12399
+ var _a2;
12400
+ return (_a2 = slots.default) == null ? void 0 : _a2.call(slots);
12401
+ },
12402
+ header: slots.header,
12403
+ footer: slots.footer || (() => h("div", { class: "dialog-footer" }, [
12404
+ h(ElButton, {
12405
+ onClick: () => handleClose()
12406
+ }, () => "取消"),
12407
+ h(ElButton, {
12408
+ type: "primary",
12409
+ onClick: () => handleConfirm({})
12410
+ }, () => "确定")
12411
+ ]))
12412
+ });
12413
+ }
12414
+ });
12415
+ const FinalDialogComponent = DialogComponent || DefaultDialog;
12416
+ function createContainer() {
12417
+ const el = document.createElement("div");
12418
+ document.body.appendChild(el);
12419
+ return el;
12420
+ }
12421
+ function show(options = {}) {
12422
+ if (isOpen) {
12423
+ close2();
12424
+ }
12425
+ container = createContainer();
12426
+ const { props = {}, slots = {}, appContext } = options;
12427
+ return new Promise((resolve, reject) => {
12428
+ currentReject = reject;
12429
+ vnode = createVNode(
12430
+ FinalDialogComponent,
12431
+ {
12432
+ ...props,
12433
+ modelValue: true,
12434
+ onUpdateModelValue: (val) => {
12435
+ if (!val) {
12436
+ reject(new Error("对话框已关闭"));
12437
+ cleanup();
12438
+ }
12439
+ },
12440
+ onClose: () => {
12441
+ reject(new Error("对话框已关闭"));
12442
+ cleanup();
12443
+ },
12444
+ onConfirm: (data) => {
12445
+ resolve(data);
12446
+ cleanup();
12447
+ }
12448
+ },
12449
+ slots
12450
+ );
12451
+ if (appContext) {
12452
+ vnode.appContext = appContext;
12453
+ }
12454
+ render(vnode, container);
12455
+ isOpen = true;
12456
+ });
12457
+ }
12458
+ function close2() {
12459
+ if (isOpen && vnode && container) {
12460
+ if (vnode.component && vnode.component.exposed && typeof vnode.component.exposed.close === "function") {
12461
+ vnode.component.exposed.close();
12462
+ }
12463
+ if (currentReject) {
12464
+ currentReject(new Error("对话框被主动关闭"));
12465
+ currentReject = null;
12466
+ }
12467
+ cleanup();
12468
+ }
12469
+ }
12470
+ function cleanup() {
12471
+ if (container) {
12472
+ render(null, container);
12473
+ container.remove();
12474
+ container = null;
12475
+ }
12476
+ vnode = null;
12477
+ isOpen = false;
12478
+ }
12479
+ if (!DialogComponent) {
12480
+ return {
12481
+ Dialog: DefaultDialog,
12482
+ show,
12483
+ close: close2
12484
+ };
12485
+ }
12486
+ return {
12487
+ show,
12488
+ close: close2
12489
+ };
12490
+ }
12491
+ const hasDocument = typeof document !== "undefined";
12492
+ let systemErrorDialogInstance = null;
12493
+ class BaseApi extends BaseHttpClient {
12494
+ constructor(config) {
12495
+ const {
12496
+ responseFields,
12497
+ enableSystemErrorDialog = true,
12498
+ ...baseConfig
12499
+ } = config;
12500
+ super(baseConfig);
12501
+ __publicField(this, "responseFields");
12502
+ __publicField(this, "enableSystemErrorDialog");
12503
+ this.responseFields = {
12504
+ code: "Code",
12505
+ message: "Message",
12506
+ data: "data",
12507
+ errors: "errors",
12508
+ tips: "tips",
12509
+ ...responseFields
12510
+ };
12511
+ this.enableSystemErrorDialog = enableSystemErrorDialog;
12512
+ }
12513
+ processRequestConfig(config) {
12514
+ return super.processRequestConfig(config);
12515
+ }
12516
+ async processResponseError(error) {
12517
+ return super.processResponseError(error);
12518
+ }
12519
+ handleHttpStatus(response) {
12520
+ return super.handleHttpStatus(response);
12521
+ }
12522
+ handleAuthenticationError(error) {
12523
+ super.handleAuthenticationError(error);
12524
+ }
12525
+ handleTimeoutError(error) {
12526
+ return super.handleTimeoutError(error);
12527
+ }
12528
+ handleNetworkError(error) {
12529
+ return super.handleNetworkError(error);
12530
+ }
12531
+ handleSuccessResponse(response) {
12532
+ const httpData = super.handleSuccessResponse(response);
12533
+ const parsedFields = this.parseResponseFields(httpData);
12534
+ const { code, message: message2, responseData } = parsedFields;
12535
+ this.handleSystemError(response, code, message2, responseData);
12536
+ this.handleBusinessError(code, message2);
12537
+ this.handleErrorArray(responseData);
12538
+ this.handleTips(responseData);
12539
+ return responseData;
12540
+ }
12541
+ parseResponseFields(data) {
12542
+ var _a2, _b, _c;
12543
+ const getValueByPath = (obj, path) => {
12544
+ if (!path)
12545
+ return obj;
12546
+ const keys = path.split(".");
12547
+ let result = obj;
12548
+ for (const key of keys) {
12549
+ if (result && typeof result === "object" && key in result) {
12550
+ result = result[key];
12551
+ } else {
12552
+ return void 0;
12553
+ }
12554
+ }
12555
+ return result;
12556
+ };
12557
+ const code = getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
12558
+ const message2 = getValueByPath(data, (_b = this.responseFields) == null ? void 0 : _b.message);
12559
+ const responseData = getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12560
+ return { code, message: message2, responseData };
12561
+ }
12562
+ handleSystemError(response, code, message2, responseData) {
12563
+ if (code === -1) {
12564
+ if (this.enableSystemErrorDialog) {
12565
+ this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
12566
+ console.error("显示系统异常对话框失败:", error);
12567
+ });
12568
+ }
12569
+ throw new Error(message2 || "系统异常");
12570
+ }
12571
+ }
12572
+ handleBusinessError(code, message2) {
12573
+ var _a2;
12574
+ if (code && code !== 200) {
12575
+ (_a2 = this.messageInstance) == null ? void 0 : _a2.error({
12576
+ message: message2 || "请求失败",
12577
+ duration: 5 * 1e3
12578
+ });
12579
+ throw new Error(message2 || "请求失败");
12580
+ }
12581
+ }
12582
+ handleErrorArray(responseData) {
12583
+ var _a2;
12584
+ const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
12585
+ if (errorsField) {
12586
+ const errors = responseData == null ? void 0 : responseData[errorsField];
12587
+ if (Array.isArray(errors) && errors.length) {
12588
+ this.showErrorArrayNotification(errors);
12589
+ throw new Error("请求错误");
12590
+ }
12591
+ }
12592
+ }
12593
+ showErrorArrayNotification(errors) {
12594
+ var _a2, _b;
12595
+ const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12596
+ if (hasDocument) {
12597
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12598
+ title: "提示",
12599
+ message: html,
12600
+ type: "error"
12601
+ });
12602
+ } else {
12603
+ const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12604
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12605
+ title: "提示",
12606
+ message: errorMessages,
12607
+ type: "error"
12608
+ });
12609
+ }
12610
+ }
12611
+ handleTips(responseData) {
12612
+ var _a2;
12613
+ const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
12614
+ if (tipsField) {
12615
+ const tips = responseData == null ? void 0 : responseData[tipsField];
12616
+ if (Array.isArray(tips) && tips.length) {
12617
+ this.showTipsNotification(tips);
12618
+ }
12619
+ }
12620
+ }
12621
+ showTipsNotification(tips) {
12622
+ var _a2, _b;
12623
+ const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12624
+ if (hasDocument) {
12625
+ (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, {
12626
+ title: "提示",
12627
+ message: html,
12628
+ type: "warning"
12629
+ });
12630
+ } else {
12631
+ const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12632
+ (_b = this.notificationInstance) == null ? void 0 : _b.call(this, {
12633
+ title: "提示",
12634
+ message: tipMessages,
12635
+ type: "warning"
12636
+ });
12637
+ }
12638
+ }
12557
12639
  async showSystemExceptionDialog(response, responseData, code, message2) {
12558
12640
  if (!hasDocument) {
12559
12641
  console.error("系统异常信息:", responseData);
@@ -12905,6 +12987,7 @@ const SystemErrorDialog$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object
12905
12987
  }, Symbol.toStringTag, { value: "Module" }));
12906
12988
  export {
12907
12989
  BaseApi,
12990
+ BaseHttpClient,
12908
12991
  VueAxiosPlugin,
12909
12992
  createHttpService,
12910
12993
  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.13",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",