mgacn-commonsdk 1.2.0 → 1.2.3

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.
@@ -1,563 +0,0 @@
1
- import U from "axios";
2
- const M = "__MGACN_COMMONSDK_SYSTEM_ID__", D = "__MGACN_COMMONSDK_USER_ID__";
3
- function F(t) {
4
- globalThis[M] = t == null ? "" : String(t);
5
- }
6
- function S() {
7
- return globalThis[M] || "";
8
- }
9
- function G(t) {
10
- globalThis[D] = t == null ? "" : String(t);
11
- }
12
- function H() {
13
- return globalThis[D] || "";
14
- }
15
- function K(t = {}) {
16
- const {
17
- baseURL: e = "",
18
- timeout: n = 6e4,
19
- tokenKey: r,
20
- systemId: i,
21
- userId: s,
22
- loginUrl: l = "/login"
23
- } = t;
24
- i != null && i !== "" && F(i), s != null && s !== "" && G(s);
25
- const a = U.create({ baseURL: e, timeout: n });
26
- let f = !1;
27
- function c() {
28
- typeof localStorage < "u" && localStorage.clear(), !f && (f = !0, setTimeout(() => {
29
- typeof window < "u" && window.location.replace(l);
30
- }, 1500));
31
- }
32
- return a.interceptors.request.use(
33
- (o) => {
34
- if (o.headers["Content-Type"] = "application/json;charset=UTF-8", typeof localStorage < "u" && r) {
35
- const m = localStorage.getItem(r);
36
- m && (o.headers.Authorization = m);
37
- }
38
- const u = S();
39
- return u && (o.headers.systemId = u), o;
40
- },
41
- (o) => Promise.reject(o)
42
- ), a.interceptors.response.use(
43
- (o) => {
44
- if (o.status !== 200)
45
- return Promise.reject(new Error(`请求发生错误,状态码 ${o.status}`));
46
- const { data: u, config: m } = o;
47
- if (m.responseType === "arraybuffer" || m.responseType === "blob")
48
- return u;
49
- if (u && u.code === 0) return u.data;
50
- const y = Number(u == null ? void 0 : u.code);
51
- if (y === 401 || y === 403)
52
- return c(), Promise.reject(new Error("登录失效,请重新登录"));
53
- const N = A(u) || "请求失败", g = new Error(N);
54
- return g.code = y, g.data = u == null ? void 0 : u.data, g.biz = u, Promise.reject(g);
55
- },
56
- (o) => {
57
- var N, g;
58
- const u = (N = o == null ? void 0 : o.response) == null ? void 0 : N.status;
59
- if (u === 401 || u === 403)
60
- return c(), Promise.reject(new Error("登录失效,请重新登录"));
61
- const m = (g = o == null ? void 0 : o.response) == null ? void 0 : g.data, y = A(m) || (o == null ? void 0 : o.message) || "请求失败";
62
- return Promise.reject(new Error(String(y)));
63
- }
64
- ), a;
65
- }
66
- function A(t) {
67
- if (t == null) return "";
68
- if (typeof t == "string") {
69
- const n = t.trim();
70
- return n ? /^<!DOCTYPE html>/i.test(n) || /^<html[\s>]/i.test(n) ? "接口地址无效或未配置代理" : n.length > 200 ? `${n.slice(0, 200)}…` : n : "";
71
- }
72
- if (typeof t != "object") return String(t);
73
- const e = [t.message, t.msg, t.data, t.error];
74
- for (const n of e)
75
- if (!(n == null || n === "") && (typeof n == "string" || typeof n == "number")) {
76
- const r = String(n).trim();
77
- return /^<!DOCTYPE html>/i.test(r) || /^<html[\s>]/i.test(r) ? "接口地址无效或未配置代理" : r.length > 200 ? `${r.slice(0, 200)}…` : r;
78
- }
79
- return "";
80
- }
81
- function dt(t = {}) {
82
- const e = K(t);
83
- return {
84
- GET(n, r = {}) {
85
- return e({ method: "get", url: n, ...r });
86
- },
87
- POST(n, r, i = {}) {
88
- return e({ method: "post", url: n, data: r, ...i });
89
- },
90
- DELETE(n, r = {}) {
91
- return e({ method: "delete", url: n, ...r });
92
- }
93
- };
94
- }
95
- const P = "__MGACN_COMMONSDK_REQUEST__";
96
- function gt(t) {
97
- globalThis[P] = t;
98
- }
99
- function L() {
100
- const t = globalThis[P];
101
- if (!t)
102
- throw new Error("[mgacn-commonsdk] 请先 app.use(MgacnCommonSdk, { tokenKey, baseURL })");
103
- return t;
104
- }
105
- function $(t, e, n = "application/vnd.ms-excel") {
106
- if (t == null || typeof window > "u") return;
107
- const r = t instanceof Blob ? t : new Blob([t], { type: n }), i = window.URL.createObjectURL(r), s = document.createElement("a");
108
- s.href = i, s.download = e || "download", s.style.display = "none", document.body.appendChild(s), s.click(), document.body.removeChild(s), window.URL.revokeObjectURL(i);
109
- }
110
- function b(t) {
111
- return String(t).padStart(2, "0");
112
- }
113
- function B(t = /* @__PURE__ */ new Date()) {
114
- return `${t.getFullYear()}${b(t.getMonth() + 1)}${b(t.getDate())}${b(t.getHours())}${b(t.getMinutes())}${b(t.getSeconds())}`;
115
- }
116
- async function q(t) {
117
- if (!(t instanceof Blob) || !t.type || !/json/i.test(t.type)) return;
118
- const e = await t.text();
119
- let n = "导出失败";
120
- try {
121
- const r = JSON.parse(e);
122
- n = (r == null ? void 0 : r.message) || (r == null ? void 0 : r.msg) || n;
123
- } catch {
124
- }
125
- throw new Error(n);
126
- }
127
- async function z(t = {}) {
128
- const {
129
- url: e,
130
- data: n,
131
- params: r,
132
- method: i = "POST",
133
- filename: s,
134
- ext: l = "xlsx"
135
- } = t;
136
- if (!e) throw new Error("导出地址不能为空");
137
- if (!s) throw new Error("导出文件名不能为空");
138
- const a = L(), f = String(i).toUpperCase();
139
- let c;
140
- f === "GET" ? c = await a.GET(e, { params: r, responseType: "blob" }) : c = await a.POST(e, n, { responseType: "blob" }), await q(c);
141
- const u = /\.[a-z0-9]+$/i.test(s) ? s : `${s}_${B()}.${l}`;
142
- return $(c, u), u;
143
- }
144
- let d = null, p = null;
145
- function k(t, e) {
146
- if (!t || e == null || e === "") return !1;
147
- if (typeof e == "object")
148
- return e.id != null && e.id !== "" && String(t.id) === String(e.id) || e.platformName != null && e.platformName !== "" && String(t.platformName || "") === String(e.platformName).trim();
149
- const n = String(e).trim();
150
- return t.id != null && String(t.id) === n || String(t.platformName || "") === n;
151
- }
152
- function Y(t) {
153
- d = Array.isArray(t) ? t : [];
154
- }
155
- function Q() {
156
- d = null, p = null;
157
- }
158
- function X(t) {
159
- if (!Array.isArray(d) || !d.length) return "";
160
- const e = d.find((n) => k(n, t));
161
- return (e == null ? void 0 : e.platformLogo) || "";
162
- }
163
- async function J() {
164
- return d ?? (p || (p = (async () => {
165
- try {
166
- const t = await at().listPlatformInfoAll();
167
- return Y(t), d;
168
- } finally {
169
- p = null;
170
- }
171
- })(), p));
172
- }
173
- function I(t) {
174
- const e = String(t || "").trim().toUpperCase();
175
- if (/^\d{15}$/.test(e)) {
176
- const c = +`19${e.slice(6, 8)}`, o = Number(e.slice(8, 10)), u = Number(e.slice(10, 12)), m = new Date(c, o - 1, u);
177
- return m.getFullYear() === c && m.getMonth() + 1 === o && m.getDate() === u;
178
- }
179
- if (!/^\d{17}[\dX]$/.test(e)) return !1;
180
- const n = Number(e.slice(6, 10)), r = Number(e.slice(10, 12)), i = Number(e.slice(12, 14)), s = new Date(n, r - 1, i);
181
- if (s.getFullYear() !== n || s.getMonth() + 1 !== r || s.getDate() !== i) return !1;
182
- const l = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = "10X98765432";
183
- let f = 0;
184
- for (let c = 0; c < 17; c++) f += Number(e[c]) * l[c];
185
- return a[f % 11] === e[17];
186
- }
187
- function O(t) {
188
- const e = String(t || "").trim().replace(/\s+/g, "");
189
- return e ? /^1\d{10}$/.test(e) || /^\+[1-9]\d{6,14}$/.test(e) : !1;
190
- }
191
- function C(t) {
192
- const e = String(t || "").trim();
193
- return e ? /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e) : !1;
194
- }
195
- function _(t) {
196
- return t == null || t === "";
197
- }
198
- function Z(t, e, n) {
199
- if (_(e)) {
200
- n();
201
- return;
202
- }
203
- if (!I(e)) {
204
- n(new Error("请输入正确的身份证号(15/18位)"));
205
- return;
206
- }
207
- n();
208
- }
209
- function V(t, e, n) {
210
- if (_(e)) {
211
- n();
212
- return;
213
- }
214
- if (!O(e)) {
215
- n(new Error("请输入正确的国内或海外手机号"));
216
- return;
217
- }
218
- n();
219
- }
220
- function W(t, e, n) {
221
- if (_(e)) {
222
- n();
223
- return;
224
- }
225
- if (!C(e)) {
226
- n(new Error("请输入正确的邮箱"));
227
- return;
228
- }
229
- n();
230
- }
231
- function v(t) {
232
- return !!(t == null || typeof t == "string" && t.trim() === "" || Array.isArray(t) && t.length === 0 || typeof t == "object" && !Array.isArray(t) && Object.keys(t).length === 0);
233
- }
234
- function T(t) {
235
- if (t === null || typeof t != "object") return t;
236
- if (t instanceof Date) return new Date(t.getTime());
237
- if (Array.isArray(t)) return t.map((n) => T(n));
238
- const e = {};
239
- return Object.keys(t).forEach((n) => {
240
- e[n] = T(t[n]);
241
- }), e;
242
- }
243
- function tt(t, e = 300) {
244
- let n = null;
245
- return function(...i) {
246
- n && clearTimeout(n), n = setTimeout(() => {
247
- t.apply(this, i);
248
- }, e);
249
- };
250
- }
251
- function et(t, e = 300) {
252
- let n = 0;
253
- return function(...i) {
254
- const s = Date.now();
255
- s - n >= e && (n = s, t.apply(this, i));
256
- };
257
- }
258
- function x(t) {
259
- if (t == null || t === "") return null;
260
- if (t instanceof Date)
261
- return Number.isNaN(t.getTime()) ? null : t;
262
- if (typeof t == "number") {
263
- const i = new Date(t);
264
- return Number.isNaN(i.getTime()) ? null : i;
265
- }
266
- const e = String(t).trim();
267
- if (!e) return null;
268
- if (/^\d{10,13}$/.test(e)) {
269
- const i = e.length === 10 ? Number(e) * 1e3 : Number(e), s = new Date(i);
270
- return Number.isNaN(s.getTime()) ? null : s;
271
- }
272
- if (/[Zz]$|[+-]\d{2}:?\d{2}$/.test(e)) {
273
- const i = new Date(e);
274
- return Number.isNaN(i.getTime()) ? null : i;
275
- }
276
- const n = e.match(
277
- /^(\d{4})[-/.年](\d{1,2})[-/.月](\d{1,2})日?(?:[\sT]+(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?(?:\.(\d{1,3}))?)?/
278
- );
279
- if (n) {
280
- const [, i, s, l, a = "0", f = "0", c = "0", o = "0"] = n, u = new Date(
281
- Number(i),
282
- Number(s) - 1,
283
- Number(l),
284
- Number(a),
285
- Number(f),
286
- Number(c),
287
- Number(String(o).padEnd(3, "0"))
288
- );
289
- return Number.isNaN(u.getTime()) ? null : u;
290
- }
291
- const r = new Date(e);
292
- return Number.isNaN(r.getTime()) ? null : r;
293
- }
294
- function w(t, e = "YYYY-MM-DD HH:mm:ss") {
295
- const n = x(t);
296
- if (!n) return "";
297
- const r = n.getFullYear(), i = n.getMonth() + 1, s = n.getDate(), l = n.getHours(), a = n.getMinutes(), f = n.getSeconds(), c = {
298
- YYYY: String(r),
299
- MM: String(i).padStart(2, "0"),
300
- DD: String(s).padStart(2, "0"),
301
- HH: String(l).padStart(2, "0"),
302
- mm: String(a).padStart(2, "0"),
303
- ss: String(f).padStart(2, "0"),
304
- M: String(i),
305
- D: String(s),
306
- H: String(l),
307
- m: String(a),
308
- s: String(f)
309
- };
310
- return e.replace(/YYYY|MM|DD|HH|mm|ss|M|D|H|m|s/g, (o) => c[o]);
311
- }
312
- function nt() {
313
- const t = /* @__PURE__ */ new Date();
314
- t.setHours(23, 59, 59, 0);
315
- const e = /* @__PURE__ */ new Date();
316
- return e.setDate(e.getDate() - 30), e.setHours(0, 0, 0, 0), {
317
- start: w(e, "YYYY-MM-DD HH:mm:ss"),
318
- end: w(t, "YYYY-MM-DD HH:mm:ss")
319
- };
320
- }
321
- function h(t) {
322
- const e = {};
323
- return Object.keys(t || {}).forEach((n) => {
324
- const r = t[n];
325
- r == null || r === "" || Array.isArray(r) && r.length === 0 || (e[n] = r);
326
- }), e;
327
- }
328
- function rt(t) {
329
- return t == null || t === "" ? "-" : t;
330
- }
331
- function E(t, e = 2) {
332
- if (t == null || t === "") return "-";
333
- const n = Number(t);
334
- if (Number.isNaN(n)) return t;
335
- const r = n.toFixed(e), [i, s] = r.split("."), l = i.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
336
- if (s == null) return l;
337
- const a = s.replace(/0+$/, "");
338
- return a ? `${l}.${a}` : l;
339
- }
340
- function it(t) {
341
- if (t == null || t === "") return "-";
342
- const e = Number(t);
343
- return Number.isNaN(e) ? "-" : Math.abs(e) > 1e4 ? `${E(e / 1e4, 2)}w` : String(E(e, 0));
344
- }
345
- function st(t, e = 4, n = "toDecimal") {
346
- if (n === "toPercent" || n === "toPercentText") {
347
- if (t == null || t === "") return n === "toPercentText" ? "-" : t;
348
- const i = Number(t);
349
- if (!Number.isFinite(i) || Number.isNaN(i)) return n === "toPercentText" ? "-" : t;
350
- if (i === 0) return n === "toPercentText" ? "0%" : 0;
351
- const s = Number(String(Number((i * 100).toFixed(2))).replace(/(?:\.0*|(\.\d+?)0+)$/, "$1"));
352
- return n === "toPercentText" ? `${s}%` : s;
353
- }
354
- if (t === "" || t == null) return;
355
- const r = Number(t);
356
- if (Number.isFinite(r))
357
- return Number((r / 100).toFixed(e));
358
- }
359
- function ot(t) {
360
- if (t === "" || t == null) return "";
361
- const e = Number(t);
362
- return Number.isFinite(e) ? e : "";
363
- }
364
- function ut(t, e = 2, n = 9999999999999) {
365
- if (t === 0 || t === "0") return 0;
366
- if (t == null || t === "") return "";
367
- let r = Number(t);
368
- if (!Number.isFinite(r)) return "";
369
- const i = Number(n), s = Number.isFinite(i) ? Math.abs(i) : 9999999999999;
370
- return Math.abs(r) >= s && (r = r < 0 ? -s : s), Number.isInteger(r) ? r : Number(r.toFixed(e));
371
- }
372
- function ct(t = "id") {
373
- return `${t}_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
374
- }
375
- const pt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
376
- __proto__: null,
377
- cleanNumber: ut,
378
- cleanParams: h,
379
- clearPlatformCache: Q,
380
- debounce: tt,
381
- deepClone: T,
382
- downloadBlob: $,
383
- exportFile: z,
384
- findPlatformLogo: X,
385
- formatDate: w,
386
- formatEmpty: rt,
387
- formatFansCount: it,
388
- formatNum: E,
389
- getRecentMonthRange: nt,
390
- isEmail: C,
391
- isEmpty: v,
392
- isMainlandIdCard: I,
393
- isMobile: O,
394
- parseDate: x,
395
- preloadPlatformList: J,
396
- setPlatformCache: Y,
397
- throttle: et,
398
- toNumberOrEmpty: ot,
399
- toRebateRatio: st,
400
- uniqueId: ct,
401
- validateEmail: W,
402
- validateIdCardNo: Z,
403
- validateMobile: V
404
- }, Symbol.toStringTag, { value: "Module" }));
405
- function yt(t) {
406
- if (!t)
407
- throw new Error("[mgacn-commonsdk] createApi 需要传入 request 实例");
408
- return {
409
- // ---------- 注销登录 ----------
410
- logout(e) {
411
- return t.GET(`/yhAdmin/login/exit/${e}`);
412
- },
413
- // ---------- 用户 ----------
414
- getUserInfo(e) {
415
- return t.GET(`/yhAdmin/user/queryUser/${e}`);
416
- },
417
- /**
418
- * 用户下拉选择列表
419
- * POST /yhAdmin/user/listForSelect
420
- * @param {object} [params]
421
- * @param {0|1} [params.status] 0 正常/有效,1 禁用/锁定;不传查全部
422
- * @param {0|1} [params.isDelete] 0 否,1 是;不传默认查未删除
423
- * @param {string} [params.realName] 真实姓名,模糊
424
- * @param {string} [params.username] 登录账号/工号,模糊
425
- * @param {string} [params.mobile] 手机号,模糊
426
- * @param {string} [params.openId] 第三方 openId,模糊
427
- * @param {string} [params.createTimeStart]
428
- * @param {string} [params.createTimeEnd]
429
- * @param {string} [params.updateTimeStart]
430
- * @param {string} [params.updateTimeEnd]
431
- * @returns {Promise<Array>} 用户列表(兼容 result / list / 直接数组)
432
- */
433
- async listUserForSelect(e = {}) {
434
- const n = await t.POST("/yhAdmin/user/listForSelect", h(e));
435
- return Array.isArray(n) ? n : Array.isArray(n == null ? void 0 : n.result) ? n.result : Array.isArray(n == null ? void 0 : n.list) ? n.list : [];
436
- },
437
- /**
438
- * 按角色名称查询用户列表(下拉)
439
- * POST /yhAdmin/user/queryUserByRoleName
440
- * systemId 由组件库自动填充,宿主勿传
441
- * @param {object} params
442
- * @param {string} params.roleName 角色名称
443
- */
444
- async queryByRoleName(e = {}) {
445
- const { systemId: n, ...r } = e && typeof e == "object" ? e : {}, i = await t.POST(
446
- "/yhAdmin/user/queryUserByRoleName",
447
- h({
448
- ...r,
449
- systemId: S()
450
- })
451
- );
452
- return Array.isArray(i) ? i : Array.isArray(i == null ? void 0 : i.result) ? i.result : Array.isArray(i == null ? void 0 : i.list) ? i.list : [];
453
- },
454
- /**
455
- * 媒介供应商全量列表(下拉)
456
- * GET /yhAccount/mediaSupplier/listAll
457
- */
458
- listMediaSupplierAll() {
459
- return t.GET("/yhAccount/mediaSupplier/listAll");
460
- },
461
- /**
462
- * 账号库平台配置全量列表(下拉)
463
- * GET /yhAccount/platformInfo/listAll
464
- */
465
- listPlatformInfoAll() {
466
- return t.GET("/yhAccount/platformInfo/listAll");
467
- },
468
- /**
469
- * 标签配置全量列表
470
- * GET /yhShared/tagConfig/queryAllTagConfigs
471
- * @param {object} [params]
472
- * @param {string} [params.businessKey]
473
- */
474
- queryAllTagConfigs(e = {}) {
475
- return t.GET("/yhShared/tagConfig/queryAllTagConfigs", {
476
- params: h(e)
477
- });
478
- },
479
- /**
480
- * 写入埋点数据
481
- * POST /yhAdmin/trace/addTraceData
482
- * userId / projectId(系统 id) / clientType 由组件库自动填充,宿主勿传
483
- * @param {object} params 宿主业务参数
484
- * @param {string} params.traceId 事件 ID
485
- * @param {string} [params.route] 页面路由
486
- * @param {string} [params.wxUserId] 微信用户 ID
487
- */
488
- addTraceData(e = {}) {
489
- const { userId: n, projectId: r, clientType: i, ...s } = e && typeof e == "object" ? e : {};
490
- return t.POST(
491
- "/yhAdmin/trace/addTraceData",
492
- h({
493
- ...s,
494
- userId: H(),
495
- projectId: S(),
496
- clientType: 1
497
- })
498
- );
499
- },
500
- // ---------- OSS ----------
501
- getOssSts(e) {
502
- return t.GET("/yhAdmin/oss/getStsToken", { params: { dir: e } });
503
- }
504
- };
505
- }
506
- const R = "__MGACN_COMMONSDK_API__";
507
- function bt(t) {
508
- globalThis[R] = t;
509
- }
510
- function at() {
511
- const t = globalThis[R];
512
- if (!t)
513
- throw new Error("[mgacn-commonsdk] 请先 app.use(MgacnCommonSdk, { tokenKey, baseURL })");
514
- return t;
515
- }
516
- const j = "__MGACN_COMMONSDK_PERMISSION_GETTER__";
517
- function ht(t) {
518
- globalThis[j] = typeof t == "function" ? t : null;
519
- }
520
- function lt() {
521
- return globalThis[j] || null;
522
- }
523
- function ft(t, e, n = "and") {
524
- const r = (Array.isArray(e) ? e : [e]).map((s) => String(s ?? "").trim()).filter(Boolean);
525
- if (!r.length) return !0;
526
- const i = new Set((t || []).map(String));
527
- return n === "or" ? r.some((s) => i.has(s)) : r.every((s) => i.has(s));
528
- }
529
- function Nt(t, e = "and") {
530
- const n = lt();
531
- return typeof n != "function" ? !0 : ft(n(), t, e);
532
- }
533
- const St = (t, e) => {
534
- const n = t.__vccOpts || t;
535
- for (const [r, i] of e)
536
- n[r] = i;
537
- return n;
538
- };
539
- export {
540
- St as _,
541
- F as a,
542
- L as b,
543
- ft as c,
544
- T as d,
545
- h as e,
546
- tt as f,
547
- at as g,
548
- E as h,
549
- rt as i,
550
- Nt as j,
551
- ht as k,
552
- dt as l,
553
- yt as m,
554
- gt as n,
555
- bt as o,
556
- K as p,
557
- lt as q,
558
- S as r,
559
- G as s,
560
- st as t,
561
- H as u,
562
- pt as v
563
- };