@moluoxixi/ajax-package 0.0.17 → 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.
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("75b05bee-ad6e-4470-b902-2118b507f825")) {
5
+ if (!document.getElementById("42dc4169-50f1-494b-a774-1a3f602f5896")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "75b05bee-ad6e-4470-b902-2118b507f825";
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") => {
@@ -12183,6 +12183,10 @@ function defaultGetToken() {
12183
12183
  return typeof localStorage !== "undefined" ? localStorage.getItem("token") || "" : "";
12184
12184
  }
12185
12185
  class BaseHttpClient {
12186
+ /**
12187
+ * 创建 BaseHttpClient 实例
12188
+ * @param config - HTTP 客户端配置对象
12189
+ */
12186
12190
  constructor(config) {
12187
12191
  __publicField(this, "baseURL", "");
12188
12192
  __publicField(this, "timeout", 5e3);
@@ -12212,42 +12216,86 @@ class BaseHttpClient {
12212
12216
  baseURL: this.baseURL,
12213
12217
  timeout: this.timeout,
12214
12218
  ...axiosConfig
12219
+ // 将所有剩余参数传给axios.create
12215
12220
  });
12216
12221
  this.setupInterceptors();
12217
12222
  }
12223
+ /**
12224
+ * 处理请求配置,子类可重写此方法自定义请求配置
12225
+ * @param config - 请求配置对象
12226
+ * @returns 处理后的请求配置
12227
+ */
12218
12228
  processRequestConfig(config) {
12219
12229
  return config;
12220
12230
  }
12231
+ /**
12232
+ * 处理响应配置,子类可重写此方法自定义响应处理
12233
+ * 按照标准 HTTP 结构处理响应
12234
+ * @param response - Axios 响应对象
12235
+ * @returns 解析后的响应数据
12236
+ */
12221
12237
  processResponseConfig(response) {
12222
12238
  this.handleHttpStatus(response);
12223
12239
  return this.handleSuccessResponse(response);
12224
12240
  }
12241
+ /**
12242
+ * 处理 HTTP 状态码
12243
+ * 子类可重写此方法来自定义 HTTP 状态码处理逻辑
12244
+ * @param response - Axios 响应对象
12245
+ */
12225
12246
  handleHttpStatus(response) {
12226
12247
  var _a2;
12227
12248
  if (response.status !== 200) {
12228
12249
  throw new Error(((_a2 = response.data) == null ? void 0 : _a2.message) || `HTTP Error: ${response.status}`);
12229
12250
  }
12230
12251
  }
12252
+ /**
12253
+ * 处理成功响应
12254
+ * 子类可重写此方法来自定义成功响应的处理逻辑
12255
+ * @param response - Axios 响应对象
12256
+ * @returns 解析后的响应数据
12257
+ */
12231
12258
  handleSuccessResponse(response) {
12232
12259
  return response.data;
12233
12260
  }
12261
+ /**
12262
+ * 处理响应错误,子类可重写此方法自定义错误处理
12263
+ * 按照标准 HTTP 错误结构处理错误
12264
+ * @param error - Axios 错误对象
12265
+ * @returns 处理后的错误对象
12266
+ */
12234
12267
  async processResponseError(error) {
12235
12268
  this.handleAuthenticationError(error);
12236
12269
  this.handleTimeoutError(error);
12237
12270
  this.handleNetworkError(error);
12238
12271
  return error;
12239
12272
  }
12273
+ /**
12274
+ * 处理认证错误(401 - 未授权/登录失效)
12275
+ * 子类可重写此方法来自定义认证错误处理逻辑
12276
+ * @param error - Axios 错误对象
12277
+ */
12240
12278
  handleAuthenticationError(error) {
12241
12279
  var _a2, _b;
12242
12280
  if (((_a2 = error.response) == null ? void 0 : _a2.status) === 401) {
12243
12281
  (_b = this.onLoginRequired) == null ? void 0 : _b.call(this, this.messageInstance);
12244
12282
  }
12245
12283
  }
12284
+ /**
12285
+ * 处理超时错误
12286
+ * 子类可重写此方法来自定义超时错误处理逻辑
12287
+ * @param error - Axios 错误对象
12288
+ */
12246
12289
  handleTimeoutError(error) {
12247
12290
  if (error.code === "ECONNABORTED" && error.message.includes("timeout")) {
12248
12291
  this.onTimeout(this.messageInstance);
12249
12292
  }
12250
12293
  }
12294
+ /**
12295
+ * 处理网络错误(其他错误)
12296
+ * 子类可重写此方法来自定义网络错误处理逻辑
12297
+ * @param error - Axios 错误对象
12298
+ */
12251
12299
  handleNetworkError(error) {
12252
12300
  var _a2, _b, _c;
12253
12301
  if (((_a2 = error.response) == null ? void 0 : _a2.status) !== 401 && error.code !== "ECONNABORTED") {
@@ -12258,6 +12306,11 @@ class BaseHttpClient {
12258
12306
  });
12259
12307
  }
12260
12308
  }
12309
+ /**
12310
+ * 设置请求和响应拦截器
12311
+ * 请求拦截器:自动添加 Token
12312
+ * 响应拦截器:处理成功响应和错误响应(401、超时等)
12313
+ */
12261
12314
  setupInterceptors() {
12262
12315
  this.instance.interceptors.request.use(
12263
12316
  (config) => {
@@ -12284,21 +12337,59 @@ class BaseHttpClient {
12284
12337
  }
12285
12338
  );
12286
12339
  }
12340
+ /**
12341
+ * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
12342
+ * @param config - Axios 请求配置对象
12343
+ * @returns 解析后的响应数据
12344
+ */
12287
12345
  async request(config) {
12288
12346
  return this.instance.request(config);
12289
12347
  }
12348
+ /**
12349
+ * 发送 GET 请求
12350
+ * @param url - 请求 URL 路径
12351
+ * @param params - 查询参数对象
12352
+ * @param config - 额外的请求配置
12353
+ * @returns 解析后的响应数据
12354
+ */
12290
12355
  async get(url, params, config) {
12291
12356
  return this.request({ ...config, url, method: "get", params });
12292
12357
  }
12358
+ /**
12359
+ * 发送 POST 请求
12360
+ * @param url - 请求 URL 路径
12361
+ * @param data - 请求体数据
12362
+ * @param config - 额外的请求配置
12363
+ * @returns 解析后的响应数据
12364
+ */
12293
12365
  async post(url, data, config) {
12294
12366
  return this.request({ ...config, url, method: "post", data });
12295
12367
  }
12368
+ /**
12369
+ * 发送 DELETE 请求
12370
+ * @param url - 请求 URL 路径
12371
+ * @param params - 查询参数对象
12372
+ * @param config - 额外的请求配置
12373
+ * @returns 解析后的响应数据
12374
+ */
12296
12375
  async delete(url, params, config) {
12297
12376
  return this.request({ ...config, url, method: "delete", params });
12298
12377
  }
12378
+ /**
12379
+ * 发送 PUT 请求
12380
+ * @param url - 请求 URL 路径
12381
+ * @param data - 请求体数据
12382
+ * @param config - 额外的请求配置
12383
+ * @returns 解析后的响应数据
12384
+ */
12299
12385
  async put(url, data, config) {
12300
12386
  return this.request({ ...config, url, method: "put", data });
12301
12387
  }
12388
+ /**
12389
+ * 批量请求,并发发送多个请求
12390
+ * @param requests - 请求配置数组或已发起的请求 Promise 数组
12391
+ * @returns 所有请求的响应数据数组
12392
+ */
12302
12393
  async all(requests) {
12303
12394
  if (!requests.length)
12304
12395
  return [];
@@ -12311,6 +12402,13 @@ class BaseHttpClient {
12311
12402
  const promises = requests.map((config) => this.request(config));
12312
12403
  return await Promise.all(promises);
12313
12404
  }
12405
+ /**
12406
+ * 文件上传,将文件包装为 FormData 发送
12407
+ * @param url - 上传地址
12408
+ * @param file - 文件对象
12409
+ * @param config - 额外的请求配置
12410
+ * @returns 解析后的响应数据
12411
+ */
12314
12412
  async uploadFile(url, file, config) {
12315
12413
  const formData = new FormData();
12316
12414
  formData.append("file", file);
@@ -12322,6 +12420,11 @@ class BaseHttpClient {
12322
12420
  }
12323
12421
  });
12324
12422
  }
12423
+ /**
12424
+ * 下载文件,将 Blob 对象下载到本地
12425
+ * @param blob - Blob 对象
12426
+ * @param filename - 文件名,如果不提供则使用时间戳
12427
+ */
12325
12428
  downloadFile(blob, filename) {
12326
12429
  if (typeof window === "undefined") {
12327
12430
  console.warn("downloadFile: 非浏览器环境,无法下载文件");
@@ -12437,10 +12540,12 @@ function createApiDialog(DialogComponent) {
12437
12540
  cleanup();
12438
12541
  }
12439
12542
  },
12543
+ // 监听关闭事件
12440
12544
  onClose: () => {
12441
12545
  reject(new Error("对话框已关闭"));
12442
12546
  cleanup();
12443
12547
  },
12548
+ // 监听确认事件(如有)
12444
12549
  onConfirm: (data) => {
12445
12550
  resolve(data);
12446
12551
  cleanup();
@@ -12488,18 +12593,33 @@ function createApiDialog(DialogComponent) {
12488
12593
  close: close2
12489
12594
  };
12490
12595
  }
12491
- const hasDocument = typeof document !== "undefined";
12492
- let systemErrorDialogInstance = null;
12493
- class BaseApi extends BaseHttpClient {
12596
+ const _BaseApi = class _BaseApi extends BaseHttpClient {
12597
+ /**
12598
+ * 创建 BaseApi 实例
12599
+ * @param config - API 配置对象
12600
+ */
12494
12601
  constructor(config) {
12495
12602
  const {
12496
12603
  responseFields,
12497
12604
  enableSystemErrorDialog = true,
12605
+ systemErrorMessage = "系统错误",
12498
12606
  ...baseConfig
12499
12607
  } = config;
12500
12608
  super(baseConfig);
12501
12609
  __publicField(this, "responseFields");
12502
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);
12503
12623
  this.responseFields = {
12504
12624
  code: "Code",
12505
12625
  message: "Message",
@@ -12509,28 +12629,112 @@ class BaseApi extends BaseHttpClient {
12509
12629
  ...responseFields
12510
12630
  };
12511
12631
  this.enableSystemErrorDialog = enableSystemErrorDialog;
12632
+ this.systemErrorMessage = systemErrorMessage;
12633
+ if (this.enableSystemErrorDialog && this.hasDocument) {
12634
+ this.systemErrorDialogInitPromise = this.initSystemErrorDialog();
12635
+ }
12512
12636
  }
12513
- processRequestConfig(config) {
12514
- return super.processRequestConfig(config);
12515
- }
12516
- processResponseConfig(response) {
12517
- return super.processResponseConfig(response);
12518
- }
12519
- async processResponseError(error) {
12520
- return super.processResponseError(error);
12637
+ /**
12638
+ * 获取是否在浏览器环境(实例 getter)
12639
+ * @returns 是否在浏览器环境
12640
+ */
12641
+ get hasDocument() {
12642
+ return _BaseApi.hasDocument;
12521
12643
  }
12522
- handleHttpStatus(response) {
12523
- return super.handleHttpStatus(response);
12644
+ /**
12645
+ * 获取系统错误信息存储(实例 getter)
12646
+ * @returns 系统错误信息存储 Map
12647
+ */
12648
+ get systemErrorInfoMap() {
12649
+ return _BaseApi.systemErrorInfoMap;
12524
12650
  }
12525
- handleAuthenticationError(error) {
12526
- super.handleAuthenticationError(error);
12651
+ /**
12652
+ * 生成唯一错误ID(实例方法)
12653
+ * @returns 唯一错误ID
12654
+ */
12655
+ generateErrorId() {
12656
+ return `system_error_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
12527
12657
  }
12528
- handleTimeoutError(error) {
12529
- return super.handleTimeoutError(error);
12658
+ /**
12659
+ * 打开系统错误详细弹窗(实例方法)
12660
+ * @param errorId - 错误ID
12661
+ */
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
+ }
12530
12715
  }
12531
- handleNetworkError(error) {
12532
- return super.handleNetworkError(error);
12716
+ /**
12717
+ * 初始化 SystemErrorDialog 实例
12718
+ * 在构造函数中调用,提前加载对话框组件
12719
+ * @returns Promise,初始化完成后 resolve
12720
+ */
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
+ }
12533
12729
  }
12730
+ /**
12731
+ * 处理成功响应
12732
+ * 重写父类方法,在标准 HTTP 成功响应基础上,处理业务特定的响应结构
12733
+ * 支持嵌套路径解析,自动处理业务层的登录失效、系统异常等错误
12734
+ * 注意:HTTP 层的错误(如 HTTP 401、超时等)由父类 BaseHttpClient 处理
12735
+ * @param response - Axios 响应对象
12736
+ * @returns 解析后的响应数据
12737
+ */
12534
12738
  handleSuccessResponse(response) {
12535
12739
  const httpData = super.handleSuccessResponse(response);
12536
12740
  const parsedFields = this.parseResponseFields(httpData);
@@ -12539,12 +12743,18 @@ class BaseApi extends BaseHttpClient {
12539
12743
  const isCustomMessage = (config == null ? void 0 : config.isCustomMessage) ?? false;
12540
12744
  this.handleSystemError(response, code, message2, responseData);
12541
12745
  if (!isCustomMessage) {
12542
- this.handleBusinessError(code, message2, response);
12746
+ this.handleBusinessError(code, message2);
12543
12747
  this.handleErrorArray(httpData, response);
12544
12748
  this.handleTips(httpData, response);
12545
12749
  }
12546
12750
  return responseData;
12547
12751
  }
12752
+ /**
12753
+ * 支持路径解析的辅助函数
12754
+ * @param obj - 要解析的对象
12755
+ * @param path - 路径字符串,支持点号分隔的嵌套路径,如 'data.errors'
12756
+ * @returns 解析后的值,如果路径不存在则返回 undefined
12757
+ */
12548
12758
  getValueByPath(obj, path) {
12549
12759
  if (!path)
12550
12760
  return obj;
@@ -12559,6 +12769,12 @@ class BaseApi extends BaseHttpClient {
12559
12769
  }
12560
12770
  return result;
12561
12771
  }
12772
+ /**
12773
+ * 解析响应字段,支持嵌套路径解析
12774
+ * 子类可重写此方法来自定义字段解析逻辑
12775
+ * @param data - 响应数据对象
12776
+ * @returns 解析后的字段值对象
12777
+ */
12562
12778
  parseResponseFields(data) {
12563
12779
  var _a2, _b, _c;
12564
12780
  const code = this.getValueByPath(data, (_a2 = this.responseFields) == null ? void 0 : _a2.code);
@@ -12566,17 +12782,95 @@ class BaseApi extends BaseHttpClient {
12566
12782
  const responseData = this.getValueByPath(data, (_c = this.responseFields) == null ? void 0 : _c.data);
12567
12783
  return { code, message: message2, responseData };
12568
12784
  }
12785
+ /**
12786
+ * 处理系统异常错误(-1 - 系统异常)
12787
+ * 子类可重写此方法来自定义系统异常处理逻辑
12788
+ * @param response - Axios 响应对象
12789
+ * @param code - 响应状态码
12790
+ * @param message - 错误消息
12791
+ * @param responseData - 响应数据
12792
+ */
12569
12793
  handleSystemError(response, code, message2, responseData) {
12570
12794
  if (code === -1) {
12571
12795
  if (this.enableSystemErrorDialog) {
12572
- this.showSystemExceptionDialog(response, responseData, code, message2).catch((error) => {
12573
- console.error("显示系统异常对话框失败:", error);
12574
- });
12796
+ this.showSystemErrorMessage(response, responseData, code, message2);
12575
12797
  }
12576
12798
  throw new Error(message2 || "系统异常");
12577
12799
  }
12578
12800
  }
12579
- handleBusinessError(code, message2, response) {
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
+ }
12867
+ /**
12868
+ * 处理业务错误(其他非200错误码)
12869
+ * 子类可重写此方法来自定义业务错误处理逻辑
12870
+ * @param code - 响应状态码
12871
+ * @param message - 错误消息
12872
+ */
12873
+ handleBusinessError(code, message2) {
12580
12874
  var _a2;
12581
12875
  if (code && code !== 200) {
12582
12876
  (_a2 = this.messageInstance) == null ? void 0 : _a2.error({
@@ -12586,6 +12880,12 @@ class BaseApi extends BaseHttpClient {
12586
12880
  throw new Error(message2 || "请求失败");
12587
12881
  }
12588
12882
  }
12883
+ /**
12884
+ * 处理错误数组 errors(如果有配置)
12885
+ * 子类可重写此方法来自定义错误数组处理逻辑
12886
+ * @param httpData - HTTP 响应数据
12887
+ * @param response - Axios 响应对象
12888
+ */
12589
12889
  handleErrorArray(httpData, response) {
12590
12890
  var _a2;
12591
12891
  const errorsField = (_a2 = this.responseFields) == null ? void 0 : _a2.errors;
@@ -12599,14 +12899,21 @@ class BaseApi extends BaseHttpClient {
12599
12899
  }
12600
12900
  }
12601
12901
  }
12602
- showErrorArrayNotification(errors, notificationOptions) {
12902
+ /**
12903
+ * 显示通知的通用方法
12904
+ * @param items - 通知项数组
12905
+ * @param type - 通知类型
12906
+ * @param color - HTML 颜色
12907
+ * @param notificationOptions - 通知配置选项
12908
+ */
12909
+ showNotification(items, type, color, notificationOptions) {
12603
12910
  var _a2, _b;
12604
- const html = errors.map((item) => `<div style="font-size: 14px;color:red">${item.code}:${item.message}</div>`).join("");
12605
12911
  const defaultOptions = {
12606
12912
  title: "提示",
12607
- type: "error"
12913
+ type
12608
12914
  };
12609
- 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("");
12610
12917
  const finalOptions = {
12611
12918
  ...defaultOptions,
12612
12919
  ...notificationOptions,
@@ -12614,15 +12921,30 @@ class BaseApi extends BaseHttpClient {
12614
12921
  };
12615
12922
  (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12616
12923
  } else {
12617
- const errorMessages = errors.map((item) => `${item.code}:${item.message}`).join("\n");
12924
+ const messages = items.map((item) => `${item.code}:${item.message}`).join("\n");
12618
12925
  const finalOptions = {
12619
12926
  ...defaultOptions,
12620
12927
  ...notificationOptions,
12621
- message: errorMessages
12928
+ message: messages
12622
12929
  };
12623
12930
  (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12624
12931
  }
12625
12932
  }
12933
+ /**
12934
+ * 显示错误数组通知
12935
+ * 子类可重写此方法来自定义错误数组通知显示方式
12936
+ * @param errors - 错误数组
12937
+ * @param notificationOptions - 通知配置选项
12938
+ */
12939
+ showErrorArrayNotification(errors, notificationOptions) {
12940
+ this.showNotification(errors, "error", "red", notificationOptions);
12941
+ }
12942
+ /**
12943
+ * 处理提示信息 tips(如果有配置)
12944
+ * 子类可重写此方法来自定义提示信息处理逻辑
12945
+ * @param httpData - HTTP 响应数据
12946
+ * @param response - Axios 响应对象
12947
+ */
12626
12948
  handleTips(httpData, response) {
12627
12949
  var _a2;
12628
12950
  const tipsField = (_a2 = this.responseFields) == null ? void 0 : _a2.tips;
@@ -12635,114 +12957,119 @@ class BaseApi extends BaseHttpClient {
12635
12957
  }
12636
12958
  }
12637
12959
  }
12960
+ /**
12961
+ * 显示提示信息通知
12962
+ * 子类可重写此方法来自定义提示信息通知显示方式
12963
+ * @param tips - 提示信息数组
12964
+ * @param notificationOptions - 通知配置选项
12965
+ */
12638
12966
  showTipsNotification(tips, notificationOptions) {
12639
- var _a2, _b;
12640
- const html = tips.map((item) => `<div style="font-size: 14px;color:#E6A23C">${item.code}:${item.message}</div>`).join("");
12641
- const defaultOptions = {
12642
- title: "提示",
12643
- type: "warning"
12644
- };
12645
- if (hasDocument) {
12646
- const finalOptions = {
12647
- ...defaultOptions,
12648
- ...notificationOptions,
12649
- message: html
12650
- };
12651
- (_a2 = this.notificationInstance) == null ? void 0 : _a2.call(this, finalOptions);
12652
- } else {
12653
- const tipMessages = tips.map((item) => `${item.code}:${item.message}`).join("\n");
12654
- const finalOptions = {
12655
- ...defaultOptions,
12656
- ...notificationOptions,
12657
- message: tipMessages
12658
- };
12659
- (_b = this.notificationInstance) == null ? void 0 : _b.call(this, finalOptions);
12660
- }
12661
- }
12662
- async showSystemExceptionDialog(response, responseData, code, message2) {
12663
- if (!hasDocument) {
12664
- console.error("系统异常信息:", responseData);
12665
- return;
12666
- }
12667
- try {
12668
- if (!systemErrorDialogInstance) {
12669
- try {
12670
- const { default: SystemErrorDialog2 } = await dynamicImports(Promise.resolve().then(() => SystemErrorDialog$1), ["default"]);
12671
- systemErrorDialogInstance = createApiDialog(SystemErrorDialog2);
12672
- } catch (error) {
12673
- console.warn("Failed to load SystemErrorDialog:", error);
12674
- console.error("系统异常信息:", responseData);
12675
- return;
12676
- }
12677
- }
12678
- const errorInfo = extractSystemErrorInfo(response, code, message2);
12679
- systemErrorDialogInstance.show({
12680
- props: {
12681
- title: "系统异常信息",
12682
- width: 600,
12683
- ...errorInfo
12684
- }
12685
- }).then((result) => {
12686
- if (result == null ? void 0 : result.reported) {
12687
- /* @__PURE__ */ console.log("系统异常已上报:", result);
12688
- this.reportError(result.errorInfo);
12689
- } else {
12690
- /* @__PURE__ */ console.log("系统异常对话框已确认");
12691
- }
12692
- }).catch((e) => {
12693
- /* @__PURE__ */ console.log("系统异常对话框已关闭", e);
12694
- });
12695
- } catch (error) {
12696
- console.error("显示系统异常对话框失败:", error);
12697
- console.error("系统异常信息:", responseData);
12698
- }
12699
- }
12700
- async reportError(errorInfo) {
12701
- var _a2, _b;
12702
- try {
12703
- /* @__PURE__ */ console.log("🚀 开始上报错误信息:", errorInfo);
12704
- /* @__PURE__ */ console.log("✅ 错误信息上报成功");
12705
- (_a2 = this.messageInstance) == null ? void 0 : _a2.success({
12706
- message: "错误信息已成功上报",
12707
- duration: 3 * 1e3
12708
- });
12709
- } catch (error) {
12710
- console.error("❌ 错误信息上报失败:", error);
12711
- (_b = this.messageInstance) == null ? void 0 : _b.error({
12712
- message: "错误信息上报失败,请稍后重试",
12713
- duration: 5 * 1e3
12714
- });
12715
- }
12967
+ this.showNotification(tips, "warning", "#E6A23C", notificationOptions);
12716
12968
  }
12969
+ /**
12970
+ * 发送 HTTP 请求,所有 HTTP 方法最终都调用此方法
12971
+ * 显式声明以确保类型一致性,子类可重写此方法
12972
+ * @param config - Axios 请求配置对象
12973
+ * @returns 解析后的响应数据
12974
+ */
12717
12975
  async request(config) {
12718
12976
  return super.request(config);
12719
12977
  }
12978
+ /**
12979
+ * 发送 GET 请求
12980
+ * 显式声明以确保类型一致性,子类可重写此方法
12981
+ * @param url - 请求 URL 路径
12982
+ * @param params - 查询参数对象
12983
+ * @param config - 额外的请求配置
12984
+ * @returns 解析后的响应数据
12985
+ */
12720
12986
  async get(url, params, config) {
12721
12987
  return super.get(url, params, config);
12722
12988
  }
12989
+ /**
12990
+ * 发送 POST 请求
12991
+ * 显式声明以确保类型一致性,子类可重写此方法
12992
+ * @param url - 请求 URL 路径
12993
+ * @param data - 请求体数据
12994
+ * @param config - 额外的请求配置
12995
+ * @returns 解析后的响应数据
12996
+ */
12723
12997
  async post(url, data, config) {
12724
12998
  return super.post(url, data, config);
12725
12999
  }
13000
+ /**
13001
+ * 发送 DELETE 请求
13002
+ * 显式声明以确保类型一致性,子类可重写此方法
13003
+ * @param url - 请求 URL 路径
13004
+ * @param params - 查询参数对象
13005
+ * @param config - 额外的请求配置
13006
+ * @returns 解析后的响应数据
13007
+ */
12726
13008
  async delete(url, params, config) {
12727
13009
  return super.delete(url, params, config);
12728
13010
  }
13011
+ /**
13012
+ * 发送 PUT 请求
13013
+ * 显式声明以确保类型一致性,子类可重写此方法
13014
+ * @param url - 请求 URL 路径
13015
+ * @param data - 请求体数据
13016
+ * @param config - 额外的请求配置
13017
+ * @returns 解析后的响应数据
13018
+ */
12729
13019
  async put(url, data, config) {
12730
13020
  return super.put(url, data, config);
12731
13021
  }
13022
+ /**
13023
+ * 批量请求,并发发送多个请求
13024
+ * 显式声明以确保类型一致性,子类可重写此方法
13025
+ * @param requests - 请求配置数组或已发起的请求 Promise 数组
13026
+ * @returns 所有请求的响应数据数组
13027
+ */
12732
13028
  async all(requests) {
12733
13029
  return super.all(requests);
12734
13030
  }
13031
+ /**
13032
+ * 文件上传,将文件包装为 FormData 发送
13033
+ * 显式声明以确保类型一致性,子类可重写此方法
13034
+ * @param url - 上传地址
13035
+ * @param file - 文件对象
13036
+ * @param config - 额外的请求配置
13037
+ * @returns 解析后的响应数据
13038
+ */
12735
13039
  async uploadFile(url, file, config) {
12736
13040
  return super.uploadFile(url, file, config);
12737
13041
  }
13042
+ /**
13043
+ * 下载文件,将 Blob 对象下载到本地
13044
+ * 显式声明以确保类型一致性,子类可重写此方法
13045
+ * @param blob - Blob 对象
13046
+ * @param filename - 文件名,如果不提供则使用时间戳
13047
+ */
12738
13048
  downloadFile(blob, filename) {
12739
13049
  return super.downloadFile(blob, filename);
12740
13050
  }
12741
- }
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;
12742
13064
  function createHttpService(options = {}) {
12743
13065
  return new BaseApi(options);
12744
13066
  }
12745
13067
  const VueAxiosPlugin = {
13068
+ /**
13069
+ * 安装插件
13070
+ * @param app - Vue 应用实例
13071
+ * @param options - 插件配置选项
13072
+ */
12746
13073
  install(app, options = {}) {
12747
13074
  const httpService = createHttpService(options.default ?? {});
12748
13075
  app.config.globalProperties.$http = httpService;
@@ -12923,10 +13250,12 @@ const SystemErrorDialog = defineComponent({
12923
13250
  h("span", { style: { fontWeight: "bold", fontSize: "16px" } }, props.title || "系统异常信息")
12924
13251
  ]),
12925
13252
  default: () => h("div", { style: { padding: 0, maxHeight: "500px", overflowY: "auto" } }, [
13253
+ // 第一块:无法完成您的请求
12926
13254
  h("div", { style: { padding: "20px", borderBottom: "1px solid #ebeef5" } }, [
12927
13255
  h("h3", { style: { margin: "0 0 12px 0", fontSize: "16px", fontWeight: "bold", color: "#303133" } }, "无法完成您的请求"),
12928
13256
  h("p", { style: { margin: 0, color: "#606266", lineHeight: 1.5 } }, "系统在处理您的请求时遇到了问题,可能是由于服务暂时不可用。")
12929
13257
  ]),
13258
+ // 第二块:技术摘要(可展开)
12930
13259
  h("div", { style: { borderBottom: "1px solid #ebeef5" } }, [
12931
13260
  h(
12932
13261
  "div",
@@ -12988,6 +13317,7 @@ const SystemErrorDialog = defineComponent({
12988
13317
  )
12989
13318
  ) : null
12990
13319
  ]),
13320
+ // SkyWalking 按钮
12991
13321
  h("div", { style: { padding: "16px 20px", borderBottom: "1px solid #ebeef5" } }, [
12992
13322
  h(
12993
13323
  ElButton,
@@ -12999,6 +13329,7 @@ const SystemErrorDialog = defineComponent({
12999
13329
  { default: () => "📊 在SkyWalking中查看详情" }
13000
13330
  )
13001
13331
  ]),
13332
+ // 黑色错误信息区域
13002
13333
  h("div", { style: { backgroundColor: "#2c3e50", color: "#fff", padding: "16px 20px", fontFamily: 'Monaco, Consolas, "Courier New", monospace', fontSize: "12px", lineHeight: 1.5, maxHeight: "200px", overflowY: "auto" } }, [
13003
13334
  h("div", { style: { marginBottom: "8px", color: "#ecf0f1" } }, `Trace ID: ${props.traceId || "a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8"}`),
13004
13335
  h("div", { style: { color: "#e74c3c", fontWeight: "bold", whiteSpace: "pre-wrap" } }, `Error: ${props.errorMessage || "Connection timeout after 5000ms"}`)