@moluoxixi/ajax-package 0.0.36 → 0.0.38

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.
@@ -17,6 +17,10 @@ export default class BaseHttpClient {
17
17
  protected getToken?: () => string | null;
18
18
  protected onLoginRequired?: (messageInstance: MessageInstance) => void;
19
19
  protected appendTo?: HTMLElement | string | null;
20
+ protected appendToFallback: string | null;
21
+ protected containerId: string;
22
+ protected popoverContainerId: string;
23
+ protected messageContainerId: string;
20
24
  instance: ReturnType<typeof axios.create>;
21
25
  protected messageInstance: MessageInstance;
22
26
  protected notificationInstance: NotificationInstance;
@@ -33,24 +37,24 @@ export default class BaseHttpClient {
33
37
  protected get hasDocument(): boolean;
34
38
  /**
35
39
  * 解析 appendTo 配置,返回目标 HTMLElement
36
- * @returns 目标元素,默认为 document.body
40
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
37
41
  */
38
- protected resolveAppendToTarget(): HTMLElement;
42
+ protected resolveAppendToTarget(): HTMLElement | null;
39
43
  /**
40
44
  * 获取或创建 ajaxPackage-container 容器元素
41
45
  * @returns 容器元素,如果不是浏览器环境则返回 null
42
46
  */
43
47
  protected getContainer(): HTMLElement | null;
44
48
  /**
45
- * 获取或创建 ajaxPackage-popover 容器元素(用于 Dialog)
49
+ * 获取或创建 popover 容器元素(用于 Dialog)
46
50
  * @returns 容器元素,如果不是浏览器环境则返回 null
47
51
  */
48
- protected getPopoverContainer(): HTMLElement | null;
52
+ getPopoverContainer(): HTMLElement | null;
49
53
  /**
50
- * 获取或创建 ajaxPackage-message 容器元素(用于 Message 和 Notification)
54
+ * 获取或创建 message 容器元素(用于 Message 和 Notification)
51
55
  * @returns 容器元素,如果不是浏览器环境则返回 null
52
56
  */
53
- protected getMessageContainer(): HTMLElement | null;
57
+ getMessageContainer(): HTMLElement | null;
54
58
  /**
55
59
  * 创建 BaseHttpClient 实例
56
60
  * @param config - HTTP 客户端配置对象
@@ -18,6 +18,8 @@ export interface BaseHttpClientConfig {
18
18
  onLoginRequired?: (messageInstance: MessageInstance) => void;
19
19
  /** container 插入的目标元素,可以是 HTMLElement 或选择器字符串,默认为 document.body */
20
20
  appendTo?: HTMLElement | string | null;
21
+ /** 当 appendTo 配置了但找不到元素时的处理方式:'body' - 使用 document.body(默认),string - 作为选择器查找后备元素,null - 返回 null 不降级 */
22
+ appendToFallback?: string | null;
21
23
  /** 允许其他任意配置项,会直接传递给 axios.create */
22
24
  [key: string]: any;
23
25
  }
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * 创建消息实例的包装函数
3
3
  * @param hasDocument - 是否在浏览器环境
4
- * @param container - 容器元素,如果提供则消息将挂载到该容器中
4
+ * @param container - 容器元素或获取容器的函数,如果提供则消息将挂载到该容器中
5
5
  * @returns 消息函数实例,直接调用即可,支持传入 type: 'success' | 'error' | 'warning' | 'info'
6
6
  */
7
- export declare function createMessageWrapper(hasDocument: boolean, container?: HTMLElement | null): (options: string | {
7
+ export declare function createMessageWrapper(hasDocument: boolean, container?: HTMLElement | null | (() => HTMLElement | null)): (options: string | {
8
8
  message?: string;
9
9
  type?: "success" | "error" | "warning" | "info";
10
10
  [key: string]: any;
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * 创建通知实例的包装函数(用于 errors / tips 展示)
3
3
  * @param hasDocument - 是否在浏览器环境
4
- * @param container - 容器元素,如果提供则通知将挂载到该容器中
4
+ * @param container - 容器元素或获取容器的函数,如果提供则通知将挂载到该容器中
5
5
  * @returns 通知函数实例,直接调用即可,支持传入 type: 'success' | 'error' | 'warning' | 'info'
6
6
  */
7
- export declare function createNotificationWrapper(hasDocument: boolean, container?: HTMLElement | null): ((options: string | {
7
+ export declare function createNotificationWrapper(hasDocument: boolean, container?: HTMLElement | null | (() => HTMLElement | null)): ((options: string | {
8
8
  message?: string;
9
9
  title?: string;
10
10
  type?: "success" | "error" | "warning" | "info";
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("55abf58f-b1b4-4e82-ab0d-ee119c15bd62")) {
5
+ if (!document.getElementById("631710a0-260c-438a-9571-db68806a8879")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "55abf58f-b1b4-4e82-ab0d-ee119c15bd62";
7
+ elementStyle.id = "631710a0-260c-438a-9571-db68806a8879";
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
  }
@@ -12053,7 +12053,8 @@ function createMessageWrapper(hasDocument, container) {
12053
12053
  if (hasDocument) {
12054
12054
  return (options) => {
12055
12055
  const opts = typeof options === "string" ? { message: options } : options;
12056
- const finalOptions = container ? { ...opts, appendTo: container } : opts;
12056
+ const actualContainer = typeof container === "function" ? container() : container;
12057
+ const finalOptions = actualContainer ? { ...opts, appendTo: actualContainer } : opts;
12057
12058
  return ElMessage(finalOptions);
12058
12059
  };
12059
12060
  }
@@ -12078,7 +12079,8 @@ function createNotificationWrapper(hasDocument, container) {
12078
12079
  if (hasDocument) {
12079
12080
  return (options) => {
12080
12081
  const opts = typeof options === "string" ? { message: options } : options;
12081
- const finalOptions = container ? { ...opts, appendTo: container } : opts;
12082
+ const actualContainer = typeof container === "function" ? container() : container;
12083
+ const finalOptions = actualContainer ? { ...opts, appendTo: actualContainer } : opts;
12082
12084
  return ElNotification(finalOptions);
12083
12085
  };
12084
12086
  }
@@ -12206,6 +12208,16 @@ function defaultOnLoginRequired(messageInstance) {
12206
12208
  function defaultGetToken() {
12207
12209
  return typeof localStorage !== "undefined" ? localStorage.getItem("token") || "" : "";
12208
12210
  }
12211
+ function generateUUID() {
12212
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
12213
+ return crypto.randomUUID();
12214
+ }
12215
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
12216
+ const r = Math.random() * 16 | 0;
12217
+ const v = c === "x" ? r : r & 3 | 8;
12218
+ return v.toString(16);
12219
+ });
12220
+ }
12209
12221
  const _BaseHttpClient = class _BaseHttpClient {
12210
12222
  /**
12211
12223
  * 创建 BaseHttpClient 实例
@@ -12218,6 +12230,10 @@ const _BaseHttpClient = class _BaseHttpClient {
12218
12230
  __publicField(this, "getToken");
12219
12231
  __publicField(this, "onLoginRequired");
12220
12232
  __publicField(this, "appendTo");
12233
+ __publicField(this, "appendToFallback", "body");
12234
+ __publicField(this, "containerId", "");
12235
+ __publicField(this, "popoverContainerId", "");
12236
+ __publicField(this, "messageContainerId", "");
12221
12237
  __publicField(this, "instance");
12222
12238
  __publicField(this, "messageInstance");
12223
12239
  __publicField(this, "notificationInstance");
@@ -12230,16 +12246,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12230
12246
  getToken = defaultGetToken,
12231
12247
  onLoginRequired = defaultOnLoginRequired,
12232
12248
  appendTo,
12249
+ appendToFallback = "body",
12233
12250
  addSign,
12234
12251
  ...axiosConfig
12235
12252
  } = config;
12236
12253
  this.baseURL = baseURL;
12237
12254
  this.timeout = timeout;
12238
12255
  this.appendTo = appendTo;
12239
- this.getContainer();
12240
- const messageContainer = this.getMessageContainer();
12241
- this.messageInstance = createMessageWrapper(this.hasDocument, messageContainer);
12242
- this.notificationInstance = createNotificationWrapper(this.hasDocument, messageContainer);
12256
+ this.appendToFallback = appendToFallback;
12257
+ this.messageInstance = createMessageWrapper(this.hasDocument, () => this.getMessageContainer());
12258
+ this.notificationInstance = createNotificationWrapper(this.hasDocument, () => this.getMessageContainer());
12243
12259
  this.onTimeout = onTimeout;
12244
12260
  this.getToken = getToken;
12245
12261
  this.onLoginRequired = onLoginRequired;
@@ -12261,7 +12277,7 @@ const _BaseHttpClient = class _BaseHttpClient {
12261
12277
  }
12262
12278
  /**
12263
12279
  * 解析 appendTo 配置,返回目标 HTMLElement
12264
- * @returns 目标元素,默认为 document.body
12280
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
12265
12281
  */
12266
12282
  resolveAppendToTarget() {
12267
12283
  if (!this.hasDocument) {
@@ -12278,8 +12294,19 @@ const _BaseHttpClient = class _BaseHttpClient {
12278
12294
  if (element) {
12279
12295
  return element;
12280
12296
  }
12281
- console.warn(`appendTo 选择器 "${this.appendTo}" 未找到元素,将使用默认的 document.body`);
12282
- return document.body;
12297
+ if (this.appendToFallback === null) {
12298
+ console.warn(`appendTo 选择器 "${this.appendTo}" 未找到元素,appendToFallback 为 null,返回 null`);
12299
+ return null;
12300
+ } else if (this.appendToFallback === "body") {
12301
+ return document.body;
12302
+ } else if (typeof this.appendToFallback === "string") {
12303
+ const fallbackElement = document.querySelector(this.appendToFallback);
12304
+ if (fallbackElement) {
12305
+ return fallbackElement;
12306
+ }
12307
+ console.warn(`appendTo 选择器 "${this.appendTo}" 和 appendToFallback 选择器 "${this.appendToFallback}" 都未找到元素,返回 null`);
12308
+ return null;
12309
+ }
12283
12310
  }
12284
12311
  return document.body;
12285
12312
  }
@@ -12291,43 +12318,77 @@ const _BaseHttpClient = class _BaseHttpClient {
12291
12318
  if (!this.hasDocument) {
12292
12319
  return null;
12293
12320
  }
12294
- let container = document.getElementById("ajaxPackage-container");
12321
+ if (!this.containerId) {
12322
+ this.containerId = `ajaxPackage-container-${generateUUID()}`;
12323
+ this.popoverContainerId = `ajaxPackage-popover-${generateUUID()}`;
12324
+ this.messageContainerId = `ajaxPackage-message-${generateUUID()}`;
12325
+ }
12326
+ let container = document.getElementById(this.containerId);
12295
12327
  if (!container) {
12296
12328
  container = document.createElement("div");
12297
- container.id = "ajaxPackage-container";
12329
+ container.id = this.containerId;
12298
12330
  const targetElement = this.resolveAppendToTarget();
12299
- targetElement.appendChild(container);
12331
+ if (targetElement) {
12332
+ targetElement.appendChild(container);
12333
+ } else {
12334
+ return null;
12335
+ }
12300
12336
  const popoverContainer = document.createElement("div");
12301
- popoverContainer.id = "ajaxPackage-popover";
12337
+ popoverContainer.id = this.popoverContainerId;
12338
+ popoverContainer.style.position = "relative";
12339
+ popoverContainer.style.zIndex = "999999";
12302
12340
  container.appendChild(popoverContainer);
12303
12341
  const messageContainer = document.createElement("div");
12304
- messageContainer.id = "ajaxPackage-message";
12342
+ messageContainer.id = this.messageContainerId;
12343
+ messageContainer.style.position = "relative";
12305
12344
  messageContainer.style.zIndex = "99999999";
12306
12345
  container.appendChild(messageContainer);
12307
12346
  }
12308
12347
  return container;
12309
12348
  }
12310
12349
  /**
12311
- * 获取或创建 ajaxPackage-popover 容器元素(用于 Dialog)
12350
+ * 获取或创建 popover 容器元素(用于 Dialog)
12312
12351
  * @returns 容器元素,如果不是浏览器环境则返回 null
12313
12352
  */
12314
12353
  getPopoverContainer() {
12315
12354
  if (!this.hasDocument) {
12316
12355
  return null;
12317
12356
  }
12318
- this.getContainer();
12319
- return document.getElementById("ajaxPackage-popover");
12357
+ const container = this.getContainer();
12358
+ if (!container) {
12359
+ return null;
12360
+ }
12361
+ if (!this.popoverContainerId) {
12362
+ return null;
12363
+ }
12364
+ const popoverContainer = document.getElementById(this.popoverContainerId);
12365
+ if (popoverContainer && !popoverContainer.style.position) {
12366
+ popoverContainer.style.position = "relative";
12367
+ popoverContainer.style.zIndex = "999999";
12368
+ }
12369
+ return popoverContainer;
12320
12370
  }
12321
12371
  /**
12322
- * 获取或创建 ajaxPackage-message 容器元素(用于 Message 和 Notification)
12372
+ * 获取或创建 message 容器元素(用于 Message 和 Notification)
12323
12373
  * @returns 容器元素,如果不是浏览器环境则返回 null
12324
12374
  */
12325
12375
  getMessageContainer() {
12326
12376
  if (!this.hasDocument) {
12327
12377
  return null;
12328
12378
  }
12329
- this.getContainer();
12330
- return document.getElementById("ajaxPackage-message");
12379
+ const container = this.getContainer();
12380
+ if (!container) {
12381
+ return null;
12382
+ }
12383
+ if (!this.messageContainerId) {
12384
+ return null;
12385
+ }
12386
+ const messageContainer = document.getElementById(this.messageContainerId);
12387
+ if (messageContainer && !messageContainer.style.position) {
12388
+ messageContainer.style.position = "relative";
12389
+ messageContainer.style.zIndex = "99999999";
12390
+ }
12391
+ return messageContainer;
12331
12392
  }
12332
12393
  /**
12333
12394
  * 处理请求配置,子类可重写此方法自定义请求配置
@@ -13356,14 +13417,10 @@ const SystemErrorDialog = defineComponent({
13356
13417
  emit("update:modelValue", val);
13357
13418
  }
13358
13419
  function getAjaxPackageMessageContainer() {
13359
- if (typeof document === "undefined")
13360
- return null;
13361
- return document.getElementById("ajaxPackage-message");
13420
+ return baseApi.getMessageContainer();
13362
13421
  }
13363
13422
  function getAjaxPackagePopoverContainer() {
13364
- if (typeof document === "undefined")
13365
- return null;
13366
- return document.getElementById("ajaxPackage-popover");
13423
+ return baseApi.getPopoverContainer();
13367
13424
  }
13368
13425
  function showMessage(options) {
13369
13426
  const container = getAjaxPackageMessageContainer();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",