@moluoxixi/ajax-package 0.0.33 → 0.0.34

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.
@@ -100,17 +100,46 @@ export interface NotificationOptions {
100
100
  [key: string]: any;
101
101
  }
102
102
  /**
103
- * 扩展 AxiosRequestConfig,支持自定义通知配置
103
+ * 消息配置选项
104
104
  */
105
- export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
105
+ export interface MessageOptions {
106
+ /**
107
+ * 消息类型
108
+ */
109
+ type?: 'success' | 'error' | 'warning' | 'info';
110
+ /**
111
+ * 消息内容
112
+ */
113
+ message?: string;
114
+ /**
115
+ * 消息持续时间(毫秒)
116
+ */
117
+ duration?: number;
118
+ /**
119
+ * 是否显示关闭按钮
120
+ */
121
+ showClose?: boolean;
122
+ /**
123
+ * 自定义类名
124
+ */
125
+ customClass?: string;
126
+ /**
127
+ * 其他消息选项
128
+ */
129
+ [key: string]: any;
130
+ }
131
+ /**
132
+ * 消息相关配置选项
133
+ */
134
+ export interface MessageConfigs {
106
135
  /**
107
136
  * 错误通知配置选项,用于覆盖默认的错误通知参数
108
137
  */
109
138
  errorNotificationOptions?: NotificationOptions;
110
139
  /**
111
- * 提示通知配置选项,用于覆盖默认的提示通知参数
140
+ * 提示消息配置选项,用于覆盖默认的提示消息参数
112
141
  */
113
- tipsNotificationOptions?: NotificationOptions;
142
+ tipsMessageOptions?: MessageOptions;
114
143
  /**
115
144
  * 是否使用自定义消息处理
116
145
  * 当为 true 时,handleBusinessError、handleErrorArray、handleTips 都不会执行
@@ -118,3 +147,12 @@ export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
118
147
  */
119
148
  isCustomMessage?: boolean;
120
149
  }
150
+ /**
151
+ * 扩展 AxiosRequestConfig,支持自定义通知配置
152
+ */
153
+ export interface ExtendedAxiosRequestConfig extends AxiosRequestConfig {
154
+ /**
155
+ * 消息相关配置选项,集中管理消息、通知和自定义处理配置
156
+ */
157
+ messageConfigs?: MessageConfigs;
158
+ }
package/es/class.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AxiosResponse } from 'axios';
2
- import { BaseApiConfig, ExtendedAxiosRequestConfig, NotificationOptions } from './_types/index.ts';
2
+ import { BaseApiConfig, ExtendedAxiosRequestConfig, MessageOptions, NotificationOptions } from './_types/index.ts';
3
3
  import { default as BaseHttpClient } from './BaseHttpClient.ts';
4
4
  /**
5
5
  * BaseApi 类
@@ -128,20 +128,13 @@ export default class BaseApi extends BaseHttpClient {
128
128
  */
129
129
  protected handleErrorArray(httpData: any, response: AxiosResponse): void;
130
130
  /**
131
- * 显示通知的通用方法
132
- * @param items - 通知项数组
133
- * @param type - 通知类型
134
- * @param color - HTML 颜色
135
- * @param notificationOptions - 通知配置选项
136
- */
137
- private showNotification;
138
- /**
139
- * 显示错误数组通知
140
- * 子类可重写此方法来自定义错误数组通知显示方式
131
+ * 显示错误信息(默认实现)
132
+ * 使用 notification 显示错误信息
133
+ * 子类可重写此方法来自定义错误信息显示方式
141
134
  * @param errors - 错误数组
142
135
  * @param notificationOptions - 通知配置选项
143
136
  */
144
- protected showErrorArrayNotification(errors: Array<{
137
+ protected showErrors(errors: Array<{
145
138
  code: string;
146
139
  message: string;
147
140
  }>, notificationOptions?: NotificationOptions): void;
@@ -153,15 +146,16 @@ export default class BaseApi extends BaseHttpClient {
153
146
  */
154
147
  protected handleTips(httpData: any, response: AxiosResponse): void;
155
148
  /**
156
- * 显示提示信息通知
157
- * 子类可重写此方法来自定义提示信息通知显示方式
149
+ * 显示提示信息(默认实现)
150
+ * 根据 messageOptions.type 指定消息类型显示,提取所有 message 并连接
151
+ * 子类可重写此方法来自定义提示信息显示方式
158
152
  * @param tips - 提示信息数组
159
- * @param notificationOptions - 通知配置选项
153
+ * @param messageOptions - 消息配置选项
160
154
  */
161
- protected showTipsNotification(tips: Array<{
155
+ protected showTips(tips: Array<{
162
156
  code: string;
163
157
  message: string;
164
- }>, notificationOptions?: NotificationOptions): void;
158
+ }>, messageOptions?: MessageOptions): void;
165
159
  /**
166
160
  * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
167
161
  * 显式声明以确保类型一致性,子类可重写此方法
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("c537d76d-5cec-4507-8fe7-372af7d6473d")) {
5
+ if (!document.getElementById("b2882460-4b80-4dc1-b28b-fc865ae04458")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "c537d76d-5cec-4507-8fe7-372af7d6473d";
7
+ elementStyle.id = "b2882460-4b80-4dc1-b28b-fc865ae04458";
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
  }
@@ -12767,7 +12767,8 @@ const _BaseApi = class _BaseApi extends BaseHttpClient {
12767
12767
  const parsedFields = this.parseResponseFields(httpData);
12768
12768
  const { code, message: message2, responseData } = parsedFields;
12769
12769
  const config = response.config;
12770
- const isCustomMessage = (config == null ? void 0 : config.isCustomMessage) ?? false;
12770
+ const messageConfigs = config == null ? void 0 : config.messageConfigs;
12771
+ const isCustomMessage = (messageConfigs == null ? void 0 : messageConfigs.isCustomMessage) ?? false;
12771
12772
  this.handleSystemError(response, code, message2, responseData);
12772
12773
  if (!isCustomMessage) {
12773
12774
  this.handleBusinessError(code, message2);
@@ -12930,33 +12931,33 @@ const _BaseApi = class _BaseApi extends BaseHttpClient {
12930
12931
  * @param response - Axios 响应对象
12931
12932
  */
12932
12933
  handleErrorArray(httpData, response) {
12933
- var _a2;
12934
+ var _a2, _b;
12934
12935
  const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
12935
12936
  if (errorsField) {
12936
12937
  const errors = this.getValueByPath(httpData, errorsField);
12937
12938
  if (Array.isArray(errors) && errors.length) {
12938
12939
  const config = response.config;
12939
- const notificationOptions = config == null ? void 0 : config.errorNotificationOptions;
12940
- this.showErrorArrayNotification(errors, notificationOptions);
12940
+ const notificationOptions = (_b = config == null ? void 0 : config.messageConfigs) == null ? void 0 : _b.errorNotificationOptions;
12941
+ this.showErrors(errors, notificationOptions);
12941
12942
  throw new Error("请求错误");
12942
12943
  }
12943
12944
  }
12944
12945
  }
12945
12946
  /**
12946
- * 显示通知的通用方法
12947
- * @param items - 通知项数组
12948
- * @param type - 通知类型
12949
- * @param color - HTML 颜色
12947
+ * 显示错误信息(默认实现)
12948
+ * 使用 notification 显示错误信息
12949
+ * 子类可重写此方法来自定义错误信息显示方式
12950
+ * @param errors - 错误数组
12950
12951
  * @param notificationOptions - 通知配置选项
12951
12952
  */
12952
- showNotification(items, type, color, notificationOptions) {
12953
+ showErrors(errors, notificationOptions) {
12953
12954
  var _a2, _b;
12954
12955
  const defaultOptions = {
12955
12956
  title: "提示",
12956
- type
12957
+ type: "error"
12957
12958
  };
12958
12959
  if (this.hasDocument) {
12959
- const html = items.map((item) => `<div style="font-size: 14px;color:${color}">${item.code}:${item.message}</div>`).join("");
12960
+ const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12960
12961
  const finalOptions = {
12961
12962
  ...defaultOptions,
12962
12963
  ...notificationOptions,
@@ -12965,7 +12966,7 @@ const _BaseApi = class _BaseApi extends BaseHttpClient {
12965
12966
  };
12966
12967
  (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12967
12968
  } else {
12968
- const messages = items.map((item) => `${item.code}:${item.message}`).join("\n");
12969
+ const messages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12969
12970
  const finalOptions = {
12970
12971
  ...defaultOptions,
12971
12972
  ...notificationOptions,
@@ -12974,15 +12975,6 @@ const _BaseApi = class _BaseApi extends BaseHttpClient {
12974
12975
  (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12975
12976
  }
12976
12977
  }
12977
- /**
12978
- * 显示错误数组通知
12979
- * 子类可重写此方法来自定义错误数组通知显示方式
12980
- * @param errors - 错误数组
12981
- * @param notificationOptions - 通知配置选项
12982
- */
12983
- showErrorArrayNotification(errors, notificationOptions) {
12984
- this.showNotification(errors, "error", "red", notificationOptions);
12985
- }
12986
12978
  /**
12987
12979
  * 处理提示信息 tips(如果有配置)
12988
12980
  * 子类可重写此方法来自定义提示信息处理逻辑
@@ -12990,25 +12982,55 @@ const _BaseApi = class _BaseApi extends BaseHttpClient {
12990
12982
  * @param response - Axios 响应对象
12991
12983
  */
12992
12984
  handleTips(httpData, response) {
12993
- var _a2;
12985
+ var _a2, _b;
12994
12986
  const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
12995
12987
  if (tipsField) {
12996
12988
  const tips = this.getValueByPath(httpData, tipsField);
12997
12989
  if (Array.isArray(tips) && tips.length) {
12998
12990
  const config = response.config;
12999
- const notificationOptions = config == null ? void 0 : config.tipsNotificationOptions;
13000
- this.showTipsNotification(tips, notificationOptions);
12991
+ const messageOptions = (_b = config == null ? void 0 : config.messageConfigs) == null ? void 0 : _b.tipsMessageOptions;
12992
+ this.showTips(tips, messageOptions);
13001
12993
  }
13002
12994
  }
13003
12995
  }
13004
12996
  /**
13005
- * 显示提示信息通知
13006
- * 子类可重写此方法来自定义提示信息通知显示方式
12997
+ * 显示提示信息(默认实现)
12998
+ * 根据 messageOptions.type 指定消息类型显示,提取所有 message 并连接
12999
+ * 子类可重写此方法来自定义提示信息显示方式
13007
13000
  * @param tips - 提示信息数组
13008
- * @param notificationOptions - 通知配置选项
13001
+ * @param messageOptions - 消息配置选项
13009
13002
  */
13010
- showTipsNotification(tips, notificationOptions) {
13011
- this.showNotification(tips, "warning", "#E6A23C", notificationOptions);
13003
+ showTips(tips, messageOptions) {
13004
+ const tipsMessage = tips.map((item) => item.message).join("");
13005
+ const defaultOptions = {
13006
+ type: "success",
13007
+ duration: 3e3,
13008
+ showClose: true,
13009
+ message: tipsMessage
13010
+ };
13011
+ const finalOptions = {
13012
+ ...defaultOptions,
13013
+ ...messageOptions,
13014
+ message: (messageOptions == null ? void 0 : messageOptions.message) ?? tipsMessage
13015
+ };
13016
+ const messageType = finalOptions.type || "success";
13017
+ const messageInstance = this.messageInstance;
13018
+ if (!messageInstance)
13019
+ return;
13020
+ switch (messageType) {
13021
+ case "success":
13022
+ messageInstance.success(finalOptions);
13023
+ break;
13024
+ case "error":
13025
+ messageInstance.error(finalOptions);
13026
+ break;
13027
+ case "warning":
13028
+ messageInstance.warning(finalOptions);
13029
+ break;
13030
+ case "info":
13031
+ messageInstance.info(finalOptions);
13032
+ break;
13033
+ }
13012
13034
  }
13013
13035
  /**
13014
13036
  * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",