@moluoxixi/ajax-package 0.0.18 → 0.0.19

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.
@@ -39,6 +39,8 @@ export interface BaseApiConfig extends BaseHttpClientConfig {
39
39
  };
40
40
  /** 是否启用 code === -1 的系统异常弹窗,默认为 true */
41
41
  enableSystemErrorDialog?: boolean;
42
+ /** 系统错误消息提示文本,默认为 '系统错误' */
43
+ systemErrorMessage?: string;
42
44
  }
43
45
  /**
44
46
  * Vue Axios 插件配置选项
package/es/class.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
1
+ import { AxiosResponse } from 'axios';
2
2
  import { BaseApiConfig, ExtendedAxiosRequestConfig, NotificationOptions } from './_types/index.ts';
3
3
  import { default as BaseHttpClient } from './BaseHttpClient.ts';
4
4
  /**
@@ -9,57 +9,65 @@ import { default as BaseHttpClient } from './BaseHttpClient.ts';
9
9
  export default class BaseApi extends BaseHttpClient {
10
10
  protected responseFields: Required<BaseApiConfig['responseFields']>;
11
11
  protected enableSystemErrorDialog: boolean;
12
+ protected systemErrorMessage: string;
12
13
  /**
13
- * 创建 BaseApi 实例
14
- * @param config - API 配置对象
14
+ * 检查是否在浏览器环境(在类初始化时判断)
15
+ * 注意:这是静态属性,所有实例共享
15
16
  */
16
- constructor(config: BaseApiConfig);
17
+ private static readonly hasDocument;
17
18
  /**
18
- * 处理请求配置,子类可重写此方法自定义请求配置
19
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
20
- * @param config - 请求配置对象
21
- * @returns 处理后的请求配置
19
+ * SystemErrorDialog 实例
20
+ * 注意:这是实例属性,每个实例有自己的对话框实例
21
+ * 在构造函数中初始化(如果启用系统错误弹窗)
22
22
  */
23
- processRequestConfig(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig;
23
+ private systemErrorDialogInstance;
24
24
  /**
25
- * 处理响应配置,子类可重写此方法自定义响应处理
26
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
27
- * @param response - Axios 响应对象
28
- * @returns 解析后的响应数据
25
+ * SystemErrorDialog 初始化 Promise
26
+ * 用于跟踪初始化状态,避免重复初始化
29
27
  */
30
- processResponseConfig(response: AxiosResponse): AxiosResponse['data'];
28
+ private systemErrorDialogInitPromise;
31
29
  /**
32
- * 处理响应错误,子类可重写此方法自定义错误处理
33
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
34
- * @param error - Axios 错误对象
35
- * @returns 处理后的错误对象
30
+ * 系统错误信息存储,用于在点击 icon 时打开详细错误弹窗
31
+ * key: 错误ID,value: 错误信息对象
32
+ * 注意:这是静态属性,所有实例共享同一个错误信息存储
36
33
  */
37
- processResponseError(error: AxiosError): Promise<AxiosError>;
34
+ private static systemErrorInfoMap;
38
35
  /**
39
- * 处理 HTTP 状态码
40
- * 重写父类方法,确保子类可以重写此方法
41
- * @param response - Axios 响应对象
36
+ * 获取是否在浏览器环境(实例 getter)
37
+ * @returns 是否在浏览器环境
38
+ */
39
+ protected get hasDocument(): boolean;
40
+ /**
41
+ * 获取系统错误信息存储(实例 getter)
42
+ * @returns 系统错误信息存储 Map
42
43
  */
43
- protected handleHttpStatus(response: AxiosResponse): void;
44
+ protected get systemErrorInfoMap(): Map<string, {
45
+ response: AxiosResponse;
46
+ responseData: AxiosResponse['data'];
47
+ code: number;
48
+ message: string;
49
+ }>;
50
+ /**
51
+ * 生成唯一错误ID(实例方法)
52
+ * @returns 唯一错误ID
53
+ */
54
+ protected generateErrorId(): string;
44
55
  /**
45
- * 处理认证错误(401 - 未授权/登录失效)
46
- * 重写父类方法,处理 HTTP 401 错误
47
- * 子类可重写此方法来自定义 HTTP 认证错误处理逻辑
48
- * @param error - Axios 错误对象
56
+ * 打开系统错误详细弹窗(实例方法)
57
+ * @param errorId - 错误ID
49
58
  */
50
- protected handleAuthenticationError(error: AxiosError): void;
59
+ private openSystemErrorDialog;
51
60
  /**
52
- * 处理超时错误
53
- * 重写父类方法,确保子类可以重写此方法
54
- * @param error - Axios 错误对象
61
+ * 创建 BaseApi 实例
62
+ * @param config - API 配置对象
55
63
  */
56
- protected handleTimeoutError(error: AxiosError): void;
64
+ constructor(config: BaseApiConfig);
57
65
  /**
58
- * 处理网络错误(其他错误)
59
- * 重写父类方法,确保子类可以重写此方法
60
- * @param error - Axios 错误对象
66
+ * 初始化 SystemErrorDialog 实例
67
+ * 在构造函数中调用,提前加载对话框组件
68
+ * @returns Promise,初始化完成后 resolve
61
69
  */
62
- protected handleNetworkError(error: AxiosError): void;
70
+ private initSystemErrorDialog;
63
71
  /**
64
72
  * 处理成功响应
65
73
  * 重写父类方法,在标准 HTTP 成功响应基础上,处理业务特定的响应结构
@@ -71,9 +79,9 @@ export default class BaseApi extends BaseHttpClient {
71
79
  protected handleSuccessResponse(response: AxiosResponse): AxiosResponse['data'];
72
80
  /**
73
81
  * 支持路径解析的辅助函数
74
- * @param obj
75
- * @param path
76
- * @protected
82
+ * @param obj - 要解析的对象
83
+ * @param path - 路径字符串,支持点号分隔的嵌套路径,如 'data.errors'
84
+ * @returns 解析后的值,如果路径不存在则返回 undefined
77
85
  */
78
86
  protected getValueByPath(obj: any, path: string | undefined): any;
79
87
  /**
@@ -96,6 +104,15 @@ export default class BaseApi extends BaseHttpClient {
96
104
  * @param responseData - 响应数据
97
105
  */
98
106
  protected handleSystemError(response: AxiosResponse, code: any, message: any, responseData: any): void;
107
+ /**
108
+ * 显示系统错误消息(带可点击 icon)
109
+ * 使用 vNode 渲染,点击 icon 后打开详细错误弹窗
110
+ * @param response - Axios 响应对象
111
+ * @param responseData - 响应数据
112
+ * @param code - 错误状态码
113
+ * @param message - 错误消息
114
+ */
115
+ private showSystemErrorMessage;
99
116
  /**
100
117
  * 处理业务错误(其他非200错误码)
101
118
  * 子类可重写此方法来自定义业务错误处理逻辑
@@ -106,15 +123,23 @@ export default class BaseApi extends BaseHttpClient {
106
123
  /**
107
124
  * 处理错误数组 errors(如果有配置)
108
125
  * 子类可重写此方法来自定义错误数组处理逻辑
109
- * @param httpData
110
- * @param response
126
+ * @param httpData - HTTP 响应数据
127
+ * @param response - Axios 响应对象
111
128
  */
112
129
  protected handleErrorArray(httpData: any, response: AxiosResponse): void;
130
+ /**
131
+ * 显示通知的通用方法
132
+ * @param items - 通知项数组
133
+ * @param type - 通知类型
134
+ * @param color - HTML 颜色
135
+ * @param notificationOptions - 通知配置选项
136
+ */
137
+ private showNotification;
113
138
  /**
114
139
  * 显示错误数组通知
115
140
  * 子类可重写此方法来自定义错误数组通知显示方式
116
141
  * @param errors - 错误数组
117
- * @param notificationOptions
142
+ * @param notificationOptions - 通知配置选项
118
143
  */
119
144
  protected showErrorArrayNotification(errors: Array<{
120
145
  code: string;
@@ -123,33 +148,20 @@ export default class BaseApi extends BaseHttpClient {
123
148
  /**
124
149
  * 处理提示信息 tips(如果有配置)
125
150
  * 子类可重写此方法来自定义提示信息处理逻辑
126
- * @param httpData
127
- * @param response
151
+ * @param httpData - HTTP 响应数据
152
+ * @param response - Axios 响应对象
128
153
  */
129
154
  protected handleTips(httpData: any, response: AxiosResponse): void;
130
155
  /**
131
156
  * 显示提示信息通知
132
157
  * 子类可重写此方法来自定义提示信息通知显示方式
133
158
  * @param tips - 提示信息数组
134
- * @param notificationOptions
159
+ * @param notificationOptions - 通知配置选项
135
160
  */
136
161
  protected showTipsNotification(tips: Array<{
137
162
  code: string;
138
163
  message: string;
139
164
  }>, notificationOptions?: NotificationOptions): void;
140
- /**
141
- * 显示系统异常对话框,当响应状态码为 -1 时调用
142
- * @param response - Axios 响应对象
143
- * @param responseData - 响应数据
144
- * @param code - 错误状态码
145
- * @param message - 错误消息
146
- */
147
- private showSystemExceptionDialog;
148
- /**
149
- * 上报错误信息到服务器,默认实现仅显示提示,子类可重写实现真实上报
150
- * @param errorInfo - 错误信息对象
151
- */
152
- protected reportError(errorInfo: any): Promise<void>;
153
165
  /**
154
166
  * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
155
167
  * 显式声明以确保类型一致性,子类可重写此方法
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("fff2b744-05a5-42b2-a548-d0942177ace6")) {
5
+ if (!document.getElementById("42dc4169-50f1-494b-a774-1a3f602f5896")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "fff2b744-05a5-42b2-a548-d0942177ace6";
7
+ elementStyle.id = "42dc4169-50f1-494b-a774-1a3f602f5896";
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
  }
@@ -12049,9 +12049,9 @@ notify.closeAll = closeAll;
12049
12049
  notify.updateOffsets = updateOffsets;
12050
12050
  notify._context = null;
12051
12051
  const ElNotification = withInstallFunction(notify, "$notify");
12052
- const hasDocument$2 = typeof document !== "undefined";
12052
+ const hasDocument$1 = typeof document !== "undefined";
12053
12053
  function createMessageWrapper() {
12054
- if (hasDocument$2) {
12054
+ if (hasDocument$1) {
12055
12055
  return ElMessage;
12056
12056
  }
12057
12057
  return {
@@ -12071,9 +12071,9 @@ function createMessageWrapper() {
12071
12071
  }
12072
12072
  };
12073
12073
  }
12074
- const hasDocument$1 = typeof document !== "undefined";
12074
+ const hasDocument = typeof document !== "undefined";
12075
12075
  function createNotificationWrapper() {
12076
- if (hasDocument$1) {
12076
+ if (hasDocument) {
12077
12077
  return ElNotification;
12078
12078
  }
12079
12079
  const consoleNotification = (options, level = "info") => {
@@ -12593,9 +12593,7 @@ function createApiDialog(DialogComponent) {
12593
12593
  close: close2
12594
12594
  };
12595
12595
  }
12596
- const hasDocument = typeof document !== "undefined";
12597
- let systemErrorDialogInstance = null;
12598
- class BaseApi extends BaseHttpClient {
12596
+ const _BaseApi = class _BaseApi extends BaseHttpClient {
12599
12597
  /**
12600
12598
  * 创建 BaseApi 实例
12601
12599
  * @param config - API 配置对象
@@ -12604,11 +12602,24 @@ class BaseApi extends BaseHttpClient {
12604
12602
  const {
12605
12603
  responseFields,
12606
12604
  enableSystemErrorDialog = true,
12605
+ systemErrorMessage = "系统错误",
12607
12606
  ...baseConfig
12608
12607
  } = config;
12609
12608
  super(baseConfig);
12610
12609
  __publicField(this, "responseFields");
12611
12610
  __publicField(this, "enableSystemErrorDialog");
12611
+ __publicField(this, "systemErrorMessage");
12612
+ /**
12613
+ * SystemErrorDialog 实例
12614
+ * 注意:这是实例属性,每个实例有自己的对话框实例
12615
+ * 在构造函数中初始化(如果启用系统错误弹窗)
12616
+ */
12617
+ __publicField(this, "systemErrorDialogInstance", null);
12618
+ /**
12619
+ * SystemErrorDialog 初始化 Promise
12620
+ * 用于跟踪初始化状态,避免重复初始化
12621
+ */
12622
+ __publicField(this, "systemErrorDialogInitPromise", null);
12612
12623
  this.responseFields = {
12613
12624
  code: "Code",
12614
12625
  message: "Message",
@@ -12618,66 +12629,103 @@ class BaseApi extends BaseHttpClient {
12618
12629
  ...responseFields
12619
12630
  };
12620
12631
  this.enableSystemErrorDialog = enableSystemErrorDialog;
12632
+ this.systemErrorMessage = systemErrorMessage;
12633
+ if (this.enableSystemErrorDialog && this.hasDocument) {
12634
+ this.systemErrorDialogInitPromise = this.initSystemErrorDialog();
12635
+ }
12621
12636
  }
12622
12637
  /**
12623
- * 处理请求配置,子类可重写此方法自定义请求配置
12624
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
12625
- * @param config - 请求配置对象
12626
- * @returns 处理后的请求配置
12627
- */
12628
- processRequestConfig(config) {
12629
- return super.processRequestConfig(config);
12630
- }
12631
- /**
12632
- * 处理响应配置,子类可重写此方法自定义响应处理
12633
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
12634
- * @param response - Axios 响应对象
12635
- * @returns 解析后的响应数据
12636
- */
12637
- processResponseConfig(response) {
12638
- return super.processResponseConfig(response);
12639
- }
12640
- /**
12641
- * 处理响应错误,子类可重写此方法自定义错误处理
12642
- * 显式声明以确保类型一致性,避免打包后的类型不兼容问题
12643
- * @param error - Axios 错误对象
12644
- * @returns 处理后的错误对象
12638
+ * 获取是否在浏览器环境(实例 getter)
12639
+ * @returns 是否在浏览器环境
12645
12640
  */
12646
- async processResponseError(error) {
12647
- return super.processResponseError(error);
12641
+ get hasDocument() {
12642
+ return _BaseApi.hasDocument;
12648
12643
  }
12649
12644
  /**
12650
- * 处理 HTTP 状态码
12651
- * 重写父类方法,确保子类可以重写此方法
12652
- * @param response - Axios 响应对象
12645
+ * 获取系统错误信息存储(实例 getter)
12646
+ * @returns 系统错误信息存储 Map
12653
12647
  */
12654
- handleHttpStatus(response) {
12655
- return super.handleHttpStatus(response);
12648
+ get systemErrorInfoMap() {
12649
+ return _BaseApi.systemErrorInfoMap;
12656
12650
  }
12657
12651
  /**
12658
- * 处理认证错误(401 - 未授权/登录失效)
12659
- * 重写父类方法,处理 HTTP 401 错误
12660
- * 子类可重写此方法来自定义 HTTP 认证错误处理逻辑
12661
- * @param error - Axios 错误对象
12652
+ * 生成唯一错误ID(实例方法)
12653
+ * @returns 唯一错误ID
12662
12654
  */
12663
- handleAuthenticationError(error) {
12664
- super.handleAuthenticationError(error);
12655
+ generateErrorId() {
12656
+ return `system_error_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
12665
12657
  }
12666
12658
  /**
12667
- * 处理超时错误
12668
- * 重写父类方法,确保子类可以重写此方法
12669
- * @param error - Axios 错误对象
12659
+ * 打开系统错误详细弹窗(实例方法)
12660
+ * @param errorId - 错误ID
12670
12661
  */
12671
- handleTimeoutError(error) {
12672
- return super.handleTimeoutError(error);
12662
+ async openSystemErrorDialog(errorId) {
12663
+ const errorInfo = this.systemErrorInfoMap.get(errorId);
12664
+ if (!errorInfo) {
12665
+ console.warn("未找到错误信息,ID:", errorId);
12666
+ return;
12667
+ }
12668
+ this.systemErrorInfoMap.delete(errorId);
12669
+ try {
12670
+ if (!this.systemErrorDialogInstance) {
12671
+ if (this.systemErrorDialogInitPromise) {
12672
+ try {
12673
+ await this.systemErrorDialogInitPromise;
12674
+ } catch {
12675
+ console.error("系统异常信息:", errorInfo.responseData);
12676
+ return;
12677
+ }
12678
+ } else {
12679
+ try {
12680
+ await this.initSystemErrorDialog();
12681
+ } catch {
12682
+ console.error("系统异常信息:", errorInfo.responseData);
12683
+ return;
12684
+ }
12685
+ }
12686
+ if (!this.systemErrorDialogInstance) {
12687
+ console.error("系统异常信息:", errorInfo.responseData);
12688
+ return;
12689
+ }
12690
+ }
12691
+ const errorInfoData = extractSystemErrorInfo(
12692
+ errorInfo.response,
12693
+ errorInfo.code,
12694
+ errorInfo.message
12695
+ );
12696
+ this.systemErrorDialogInstance.show({
12697
+ props: {
12698
+ title: "系统异常信息",
12699
+ width: 600,
12700
+ ...errorInfoData
12701
+ }
12702
+ }).then((result) => {
12703
+ if (result == null ? void 0 : result.reported) {
12704
+ /* @__PURE__ */ console.log("系统异常已上报:", result);
12705
+ } else {
12706
+ /* @__PURE__ */ console.log("系统异常对话框已确认");
12707
+ }
12708
+ }).catch((e) => {
12709
+ /* @__PURE__ */ console.log("系统异常对话框已关闭", e);
12710
+ });
12711
+ } catch (error) {
12712
+ console.error("显示系统异常对话框失败:", error);
12713
+ console.error("系统异常信息:", errorInfo.responseData);
12714
+ }
12673
12715
  }
12674
12716
  /**
12675
- * 处理网络错误(其他错误)
12676
- * 重写父类方法,确保子类可以重写此方法
12677
- * @param error - Axios 错误对象
12717
+ * 初始化 SystemErrorDialog 实例
12718
+ * 在构造函数中调用,提前加载对话框组件
12719
+ * @returns Promise,初始化完成后 resolve
12678
12720
  */
12679
- handleNetworkError(error) {
12680
- return super.handleNetworkError(error);
12721
+ async initSystemErrorDialog() {
12722
+ try {
12723
+ const { default: SystemErrorDialog2 } = await dynamicImports(Promise.resolve().then(() => SystemErrorDialog$1), ["default"]);
12724
+ this.systemErrorDialogInstance = createApiDialog(SystemErrorDialog2);
12725
+ } catch (error) {
12726
+ console.warn("Failed to load SystemErrorDialog:", error);
12727
+ throw error;
12728
+ }
12681
12729
  }
12682
12730
  /**
12683
12731
  * 处理成功响应
@@ -12695,7 +12743,7 @@ class BaseApi extends BaseHttpClient {
12695
12743
  const isCustomMessage = (config == null ? void 0 : config.isCustomMessage) ?? false;
12696
12744
  this.handleSystemError(response, code, message2, responseData);
12697
12745
  if (!isCustomMessage) {
12698
- this.handleBusinessError(code, message2, response);
12746
+ this.handleBusinessError(code, message2);
12699
12747
  this.handleErrorArray(httpData, response);
12700
12748
  this.handleTips(httpData, response);
12701
12749
  }
@@ -12703,9 +12751,9 @@ class BaseApi extends BaseHttpClient {
12703
12751
  }
12704
12752
  /**
12705
12753
  * 支持路径解析的辅助函数
12706
- * @param obj
12707
- * @param path
12708
- * @protected
12754
+ * @param obj - 要解析的对象
12755
+ * @param path - 路径字符串,支持点号分隔的嵌套路径,如 'data.errors'
12756
+ * @returns 解析后的值,如果路径不存在则返回 undefined
12709
12757
  */
12710
12758
  getValueByPath(obj, path) {
12711
12759
  if (!path)
@@ -12745,13 +12793,77 @@ class BaseApi extends BaseHttpClient {
12745
12793
  handleSystemError(response, code, message2, responseData) {
12746
12794
  if (code === -1) {
12747
12795
  if (this.enableSystemErrorDialog) {
12748
- this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
12749
- console.error("显示系统异常对话框失败:", error);
12750
- });
12796
+ this.showSystemErrorMessage(response, responseData, code, message2);
12751
12797
  }
12752
12798
  throw new Error(message2 || "系统异常");
12753
12799
  }
12754
12800
  }
12801
+ /**
12802
+ * 显示系统错误消息(带可点击 icon)
12803
+ * 使用 vNode 渲染,点击 icon 后打开详细错误弹窗
12804
+ * @param response - Axios 响应对象
12805
+ * @param responseData - 响应数据
12806
+ * @param code - 错误状态码
12807
+ * @param message - 错误消息
12808
+ */
12809
+ showSystemErrorMessage(response, responseData, code, message2) {
12810
+ var _a2;
12811
+ if (!this.hasDocument) {
12812
+ console.error("系统异常信息:", responseData);
12813
+ return;
12814
+ }
12815
+ const errorId = this.generateErrorId();
12816
+ this.systemErrorInfoMap.set(errorId, {
12817
+ response,
12818
+ responseData,
12819
+ code,
12820
+ message: message2
12821
+ });
12822
+ setTimeout(() => {
12823
+ this.systemErrorInfoMap.delete(errorId);
12824
+ }, 5 * 60 * 1e3);
12825
+ const handleIconClick = () => {
12826
+ this.openSystemErrorDialog(errorId).catch((error) => {
12827
+ console.error("打开系统错误对话框失败:", error);
12828
+ });
12829
+ };
12830
+ const messageVNode = h("div", {
12831
+ style: {
12832
+ display: "flex",
12833
+ alignItems: "center",
12834
+ gap: "8px",
12835
+ maxWidth: "100%"
12836
+ }
12837
+ }, [
12838
+ h("div", {
12839
+ style: {
12840
+ color: "var(--el-message-text-color)",
12841
+ lineHeight: "24px"
12842
+ }
12843
+ }, message2 || this.systemErrorMessage),
12844
+ h("svg", {
12845
+ onClick: handleIconClick,
12846
+ style: {
12847
+ cursor: "pointer",
12848
+ color: "#F56C6C",
12849
+ width: "16px",
12850
+ height: "16px",
12851
+ flexShrink: 0
12852
+ },
12853
+ viewBox: "0 0 1024 1024",
12854
+ fill: "currentColor",
12855
+ xmlns: "http://www.w3.org/2000/svg"
12856
+ }, [
12857
+ h("path", {
12858
+ d: "M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm32 664c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V456c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272zm-32-344a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"
12859
+ })
12860
+ ])
12861
+ ]);
12862
+ (_a2 = this.messageInstance) == null ? void 0 : _a2.error({
12863
+ message: messageVNode,
12864
+ duration: 5 * 1e3
12865
+ });
12866
+ }
12755
12867
  /**
12756
12868
  * 处理业务错误(其他非200错误码)
12757
12869
  * 子类可重写此方法来自定义业务错误处理逻辑
@@ -12771,8 +12883,8 @@ class BaseApi extends BaseHttpClient {
12771
12883
  /**
12772
12884
  * 处理错误数组 errors(如果有配置)
12773
12885
  * 子类可重写此方法来自定义错误数组处理逻辑
12774
- * @param httpData
12775
- * @param response
12886
+ * @param httpData - HTTP 响应数据
12887
+ * @param response - Axios 响应对象
12776
12888
  */
12777
12889
  handleErrorArray(httpData, response) {
12778
12890
  var _a2;
@@ -12788,19 +12900,20 @@ class BaseApi extends BaseHttpClient {
12788
12900
  }
12789
12901
  }
12790
12902
  /**
12791
- * 显示错误数组通知
12792
- * 子类可重写此方法来自定义错误数组通知显示方式
12793
- * @param errors - 错误数组
12794
- * @param notificationOptions
12903
+ * 显示通知的通用方法
12904
+ * @param items - 通知项数组
12905
+ * @param type - 通知类型
12906
+ * @param color - HTML 颜色
12907
+ * @param notificationOptions - 通知配置选项
12795
12908
  */
12796
- showErrorArrayNotification(errors, notificationOptions) {
12909
+ showNotification(items, type, color, notificationOptions) {
12797
12910
  var _a2, _b;
12798
- const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12799
12911
  const defaultOptions = {
12800
12912
  title: "提示",
12801
- type: "error"
12913
+ type
12802
12914
  };
12803
- if (hasDocument) {
12915
+ if (this.hasDocument) {
12916
+ const html = items.map((item) => `<div style="font-size: 14px;color:${color}">${item.code}:${item.message}</div>`).join("");
12804
12917
  const finalOptions = {
12805
12918
  ...defaultOptions,
12806
12919
  ...notificationOptions,
@@ -12808,20 +12921,29 @@ class BaseApi extends BaseHttpClient {
12808
12921
  };
12809
12922
  (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12810
12923
  } else {
12811
- const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12924
+ const messages = items.map((item) => `${item.code}:${item.message}`).join("\n");
12812
12925
  const finalOptions = {
12813
12926
  ...defaultOptions,
12814
12927
  ...notificationOptions,
12815
- message: errorMessages
12928
+ message: messages
12816
12929
  };
12817
12930
  (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12818
12931
  }
12819
12932
  }
12933
+ /**
12934
+ * 显示错误数组通知
12935
+ * 子类可重写此方法来自定义错误数组通知显示方式
12936
+ * @param errors - 错误数组
12937
+ * @param notificationOptions - 通知配置选项
12938
+ */
12939
+ showErrorArrayNotification(errors, notificationOptions) {
12940
+ this.showNotification(errors, "error", "red", notificationOptions);
12941
+ }
12820
12942
  /**
12821
12943
  * 处理提示信息 tips(如果有配置)
12822
12944
  * 子类可重写此方法来自定义提示信息处理逻辑
12823
- * @param httpData
12824
- * @param response
12945
+ * @param httpData - HTTP 响应数据
12946
+ * @param response - Axios 响应对象
12825
12947
  */
12826
12948
  handleTips(httpData, response) {
12827
12949
  var _a2;
@@ -12839,97 +12961,10 @@ class BaseApi extends BaseHttpClient {
12839
12961
  * 显示提示信息通知
12840
12962
  * 子类可重写此方法来自定义提示信息通知显示方式
12841
12963
  * @param tips - 提示信息数组
12842
- * @param notificationOptions
12964
+ * @param notificationOptions - 通知配置选项
12843
12965
  */
12844
12966
  showTipsNotification(tips, notificationOptions) {
12845
- var _a2, _b;
12846
- const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12847
- const defaultOptions = {
12848
- title: "提示",
12849
- type: "warning"
12850
- };
12851
- if (hasDocument) {
12852
- const finalOptions = {
12853
- ...defaultOptions,
12854
- ...notificationOptions,
12855
- message: html
12856
- };
12857
- (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12858
- } else {
12859
- const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12860
- const finalOptions = {
12861
- ...defaultOptions,
12862
- ...notificationOptions,
12863
- message: tipMessages
12864
- };
12865
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12866
- }
12867
- }
12868
- /**
12869
- * 显示系统异常对话框,当响应状态码为 -1 时调用
12870
- * @param response - Axios 响应对象
12871
- * @param responseData - 响应数据
12872
- * @param code - 错误状态码
12873
- * @param message - 错误消息
12874
- */
12875
- async showSystemExceptionDialog(response, responseData, code, message2) {
12876
- if (!hasDocument) {
12877
- console.error("系统异常信息:", responseData);
12878
- return;
12879
- }
12880
- try {
12881
- if (!systemErrorDialogInstance) {
12882
- try {
12883
- const { default: SystemErrorDialog2 } = await dynamicImports(Promise.resolve().then(() => SystemErrorDialog$1), ["default"]);
12884
- systemErrorDialogInstance = createApiDialog(SystemErrorDialog2);
12885
- } catch (error) {
12886
- console.warn("Failed to load SystemErrorDialog:", error);
12887
- console.error("系统异常信息:", responseData);
12888
- return;
12889
- }
12890
- }
12891
- const errorInfo = extractSystemErrorInfo(response, code, message2);
12892
- systemErrorDialogInstance.show({
12893
- props: {
12894
- title: "系统异常信息",
12895
- width: 600,
12896
- ...errorInfo
12897
- }
12898
- }).then((result) => {
12899
- if (result == null ? void 0 : result.reported) {
12900
- /* @__PURE__ */ console.log("系统异常已上报:", result);
12901
- this.reportError(result.errorInfo);
12902
- } else {
12903
- /* @__PURE__ */ console.log("系统异常对话框已确认");
12904
- }
12905
- }).catch((e) => {
12906
- /* @__PURE__ */ console.log("系统异常对话框已关闭", e);
12907
- });
12908
- } catch (error) {
12909
- console.error("显示系统异常对话框失败:", error);
12910
- console.error("系统异常信息:", responseData);
12911
- }
12912
- }
12913
- /**
12914
- * 上报错误信息到服务器,默认实现仅显示提示,子类可重写实现真实上报
12915
- * @param errorInfo - 错误信息对象
12916
- */
12917
- async reportError(errorInfo) {
12918
- var _a2, _b;
12919
- try {
12920
- /* @__PURE__ */ console.log("🚀 开始上报错误信息:", errorInfo);
12921
- /* @__PURE__ */ console.log("✅ 错误信息上报成功");
12922
- (_a2 = this.messageInstance) == null ? void 0 : _a2.success({
12923
- message: "错误信息已成功上报",
12924
- duration: 3 * 1e3
12925
- });
12926
- } catch (error) {
12927
- console.error("❌ 错误信息上报失败:", error);
12928
- (_b = this.messageInstance) == null ? void 0 : _b.error({
12929
- message: "错误信息上报失败,请稍后重试",
12930
- duration: 5 * 1e3
12931
- });
12932
- }
12967
+ this.showNotification(tips, "warning", "#E6A23C", notificationOptions);
12933
12968
  }
12934
12969
  /**
12935
12970
  * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
@@ -13013,7 +13048,19 @@ class BaseApi extends BaseHttpClient {
13013
13048
  downloadFile(blob, filename) {
13014
13049
  return super.downloadFile(blob, filename);
13015
13050
  }
13016
- }
13051
+ };
13052
+ /**
13053
+ * 检查是否在浏览器环境(在类初始化时判断)
13054
+ * 注意:这是静态属性,所有实例共享
13055
+ */
13056
+ __publicField(_BaseApi, "hasDocument", typeof document !== "undefined");
13057
+ /**
13058
+ * 系统错误信息存储,用于在点击 icon 时打开详细错误弹窗
13059
+ * key: 错误ID,value: 错误信息对象
13060
+ * 注意:这是静态属性,所有实例共享同一个错误信息存储
13061
+ */
13062
+ __publicField(_BaseApi, "systemErrorInfoMap", /* @__PURE__ */ new Map());
13063
+ let BaseApi = _BaseApi;
13017
13064
  function createHttpService(options = {}) {
13018
13065
  return new BaseApi(options);
13019
13066
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",
@@ -1,40 +0,0 @@
1
- export default service;
2
- /**
3
- * 创建 axios 实例
4
- * @type {NewApi}
5
- */
6
- declare const service: NewApi;
7
- /**
8
- * 自定义 API 类,继承自 BaseApi
9
- * 实现了原有的 axios 封装功能
10
- */
11
- export class NewApi {
12
- constructor(config?: {});
13
- /**
14
- * 处理请求配置
15
- * 在请求发送之前进行一些处理
16
- * 对应旧代码的请求拦截器逻辑
17
- */
18
- processRequestConfig(config: any): any;
19
- /**
20
- * 处理 HTTP 状态码
21
- */
22
- handleHttpStatus(response: any): void;
23
- /**
24
- * 处理业务错误(重写父类方法,不抛出错误,只显示消息)
25
- */
26
- handleBusinessError(code: any, message: any): void;
27
- /**
28
- * 处理成功响应(完全重写,符合旧代码逻辑)
29
- */
30
- handleSuccessResponse(response: any): any;
31
- /**
32
- * 处理超时错误(重写父类方法)
33
- */
34
- handleTimeoutError(error: any): void;
35
- /**
36
- * 处理网络错误(重写父类方法)
37
- */
38
- handleNetworkError(error: any): void;
39
- }
40
- export function showErrorNotification(title: any, errors: any): void;