t-lj-service 1.0.1

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.
@@ -0,0 +1,497 @@
1
+ import { ElMessage, ElNotification, ElMessageBox, ElLoading } from 'element-plus'
2
+ import { nextTick, ref } from 'vue'
3
+ import { useRouter, type Router } from 'vue-router';
4
+ import * as Crypto from "crypto-js";
5
+ import axios from 'axios';
6
+ import { zip } from './zip';
7
+ import {actions} from "./qiankun/actions";
8
+
9
+
10
+ export namespace HOOK_SPACE {
11
+ export interface VERIFIED {
12
+ status: boolean
13
+ keys: any[]
14
+ }
15
+ }
16
+
17
+ /**
18
+ * 全局 message 方法,用于显示消息提示
19
+ * @param message 消息内容
20
+ * @param type 消息类型,默认为 success
21
+ * @param [options] 消息选项
22
+ */
23
+ export const message = (
24
+ message: string,
25
+ type: 'success' | 'info' | 'warning' | 'error' = 'success',
26
+ options: any = {}
27
+ ) =>
28
+ ElMessage({
29
+ message,
30
+ type,
31
+ ...options
32
+ });
33
+ message.error = (message: string, options: any = {}) => ElMessage({
34
+ message,
35
+ type: 'error',
36
+ ...options
37
+ });
38
+ message.success = (message: string, options: any = {}) => ElMessage({
39
+ message,
40
+ type: 'success',
41
+ ...options
42
+ });
43
+ message.warning = (message: string, options: any = {}) => ElMessage({
44
+ message,
45
+ type: 'warning',
46
+ ...options
47
+ });
48
+ message.info = (message: string, options: any = {}) => ElMessage({
49
+ message,
50
+ type: 'info',
51
+ ...options
52
+ });
53
+ /**
54
+ * 全局 loading
55
+ * @param params 加载文案
56
+ * @param status 开启关闭
57
+ * @param background 背景颜色
58
+ */
59
+ const loadingInstance = ref()
60
+ export const loading = (
61
+ status: boolean = true,
62
+ params: string = '',
63
+ background: string = 'rgba(0, 0, 0, 0.3)'
64
+ ): void => {
65
+ if (status) {
66
+ loadingInstance.value = ElLoading.service({
67
+ lock: true,
68
+ text: params,
69
+ background: background,
70
+ fullscreen: true
71
+ })
72
+ } else {
73
+ if (loadingInstance.value) {
74
+ nextTick(() => {
75
+ loadingInstance.value.close()
76
+ loadingInstance.value = null;
77
+ })
78
+ }
79
+ }
80
+ }
81
+
82
+ /**
83
+ * 全局 confirm 方法,用于显示确认对话框
84
+ * @param message 对话框内容
85
+ * @param title 对话框标题,默认为 '提示'
86
+ * @param [options] 对话框选项
87
+ * @return Promise<boolean>
88
+ */
89
+ export const confirm = (message: string, title: string = '提示', options: any = {}) => {
90
+ if (Object.keys(options).length === 0) {
91
+ options = {
92
+ confirmButtonText: '确定',
93
+ cancelButtonText: '取消',
94
+ type: 'warning',
95
+ center: true,
96
+ confirmButtonClass: 't-confirm-class',
97
+ cancelButtonClass: 't-cancel-class'
98
+ }
99
+ }
100
+ return new Promise((resolve) => {
101
+ ElMessageBox.confirm(message, title, options)
102
+ .then(() => {
103
+ resolve(true)
104
+ })
105
+ .catch(() => {
106
+ resolve(false)
107
+ })
108
+ })
109
+ }
110
+
111
+ /**
112
+ * 全局 notification 方法,用于显示通知
113
+ * @param message 通知内容
114
+ * @param title 通知标题,默认为 '提示'
115
+ * @param [options] 通知选项
116
+ */
117
+ export const notification = (message: string, title: string = '提示', options: any = {}): void => {
118
+ ElNotification({
119
+ title,
120
+ message,
121
+ ...options
122
+ })
123
+ }
124
+
125
+ /**
126
+ * 获取静态资源
127
+ * @param url 静态资源assets内文件路径
128
+ */
129
+ export const getAssetsImage = (url: string): any => {
130
+ return new URL(`../assets/${url}`, import.meta.url).href
131
+ }
132
+
133
+ /**
134
+ * 导航菜单active
135
+ * @param label 主菜单名称
136
+ * @param level 子菜单名称
137
+ */
138
+ // export const asideActive = (label: string, level: string): void => {
139
+ // menuList.some((menu) => {
140
+ // if (menu.label === label) {
141
+ // const children = menu.children || []
142
+ // children.some((child) => {
143
+ // if (child.label === level) {
144
+ // setSession('menu_id', child.id)
145
+ // setSession('active_url', child.path)
146
+ // setSession('menu_parent_id', child.parentId)
147
+ // return true
148
+ // }
149
+ // })
150
+ // return true
151
+ // }
152
+ // })
153
+ // }
154
+
155
+ /**
156
+ * 缓存
157
+ * @param key 缓存名称
158
+ * @param value 缓存数据
159
+ */
160
+ export const setSession = (key: string, value: any): void => {
161
+ sessionStorage.setItem(key, JSON.stringify(value))
162
+ }
163
+ export const getSession = (key: string): any => {
164
+ return JSON.parse(sessionStorage.getItem(key) || JSON.stringify(''));
165
+ }
166
+ export const delSession = (key: string): void => {
167
+ sessionStorage.deleteItem(key)
168
+ }
169
+ export const clearSession = (): void => {
170
+ sessionStorage.clear()
171
+ }
172
+ export const setLocal = (key: string, value: any): void => {
173
+ if(typeof value === 'string') {
174
+ localStorage.setItem(key, value)
175
+ } else {
176
+ localStorage.setItem(key, JSON.stringify(value))
177
+ }
178
+ }
179
+ export const getLocal = (key: string): any => {
180
+ if(typeof localStorage.getItem(key) === 'string') {
181
+ return localStorage.getItem(key);
182
+ } else {
183
+ return JSON.parse(localStorage.getItem(key) || JSON.stringify(''));
184
+ }
185
+ }
186
+ export const delLocal = (key: string): void => {
187
+ localStorage.deleteItem(key)
188
+ }
189
+ export const clearLocal = (): void => {
190
+ localStorage.clear()
191
+ }
192
+
193
+ /**
194
+ * 校验
195
+ * @param info object
196
+ * @param outKeys any[] 不校验字段
197
+ */
198
+ export const verified = (info: any, outKeys: any[] = []): HOOK_SPACE.VERIFIED => {
199
+ let status: boolean = true
200
+ let keys: any[] = []
201
+ let infos = { ...info }
202
+ outKeys.forEach((item: any) => {
203
+ delete infos[item]
204
+ })
205
+ if (infos) {
206
+ Object.keys(infos).forEach((key: any): void => {
207
+ if (infos[key] == '') {
208
+ status = false
209
+ keys.push(key)
210
+ }
211
+ })
212
+ }
213
+ return {
214
+ status,
215
+ keys
216
+ }
217
+ }
218
+
219
+ /**
220
+ * 校验提示信息
221
+ * @param keys any[] 校验key
222
+ * @param info any[] 必填提示信息
223
+ * @param key string 必填字段key
224
+ * @param tips string 提示信息key
225
+ */
226
+ export const verifiedTips = (
227
+ keys: any[],
228
+ info: any[],
229
+ key: string = 'key',
230
+ tips: string = 'tips'
231
+ ): void => {
232
+ info.forEach((_info: any) => {
233
+ const idx = keys.indexOf(_info[key])
234
+ if (idx !== -1) {
235
+ message(_info[tips], 'warning')
236
+ }
237
+ })
238
+ }
239
+
240
+ /**
241
+ * 消息传递
242
+ * @param message string 临时缓存消息/发送消息
243
+ * @param target string 目标地址
244
+ * @param sessionName string 临时缓存名称
245
+ */
246
+ export const postMessage = (
247
+ message: string = window.location.href,
248
+ target: string = '*',
249
+ sessionName: string = 'currentHref'
250
+ ): void => {
251
+ setSession(sessionName, message)
252
+ window.parent.postMessage(message, target)
253
+ }
254
+
255
+ /**
256
+ * 空状态
257
+ * @param params any 参数
258
+ */
259
+ export const emptyStatus = (params: any): boolean => {
260
+ if (
261
+ params?.toString() !== '' &&
262
+ params?.toString() !== 'undefined' &&
263
+ params != undefined &&
264
+ params?.toString() !== 'NaN' &&
265
+ params?.toString() !== 'null'
266
+ ) {
267
+ return true
268
+ }
269
+ return false
270
+ }
271
+
272
+ /**
273
+ * 跳转
274
+ * @param path string 地址
275
+ * @param way string 方式
276
+ * @param query any 参数
277
+ * @param $router any 路由
278
+ * @param token any token
279
+ * @param args any 参数
280
+ */
281
+ export const goTo = (
282
+ path: string = '',
283
+ r: Router = useRouter(),
284
+ query: Record<string, any> = {},
285
+ way: 'push' | 'replace' = 'push' as 'push' | 'replace',
286
+ ) => {
287
+ if (path !== '' && r) {
288
+ r[way]({ path, query });
289
+ }
290
+ // if(path !== '' && r !== null){
291
+ // if (way === 'replace') {
292
+ // r.replace({path, query});
293
+ // } else {
294
+ // r.push({path, query});
295
+ // }
296
+ // }
297
+ }
298
+ export const qiankunGoTo = (path: string = '', {
299
+ token = '',
300
+ args = {}
301
+ }) => {
302
+ if(path !== ''){
303
+ actions.setGlobalState({
304
+ token,
305
+ ...args,
306
+ });
307
+ window.history.replaceState({}, '', path)
308
+ }
309
+ }
310
+ // export const iframeGoTo = (dom: HTMLElement, path: string = '', {
311
+ // token = '',
312
+ // args = {}
313
+ // }) => {
314
+ // if(path !== ''){
315
+ // // dom.src = path;
316
+ // }
317
+
318
+
319
+
320
+ /**
321
+ * 加密
322
+ * @param content any 内容
323
+ * @param key any
324
+ * @param iv any
325
+ */
326
+ export const crypto = (content: any, key: any, iv: any) => {
327
+ const _c: any = Crypto.enc.Utf8.parse(content)
328
+ const _k: any = Crypto.enc.Utf8.parse(key)
329
+ const _i: any = Crypto.enc.Utf8.parse(iv)
330
+ const query: any = Crypto.AES.encrypt(_c, _k, { iv: _i, mode: Crypto.mode.CBC }).toString();
331
+ return query;
332
+ }
333
+ export const decrypt = async (content: string, key: any, iv: any) : Promise<string> => {
334
+ const _k: any = Crypto.enc.Utf8.parse(key);
335
+ const _i: any = Crypto.enc.Utf8.parse(iv);
336
+ const decrypt: any = Crypto.AES.decrypt(content, _k, { iv: _i, mode: Crypto.mode.CBC });
337
+ return Crypto.enc.Utf8.stringify(decrypt);
338
+ }
339
+
340
+ /**
341
+ * 获取query
342
+ * @returns query string
343
+ */
344
+ export const getQuery = (): string => {
345
+ return window.location.search.substring(1) || '';
346
+ }
347
+
348
+
349
+
350
+
351
+ /**
352
+ * 获取随机整数
353
+ * @param min number
354
+ * @param max number
355
+ * @param range string, max:包含最大数;min:包含最小数; both:都包含;neither:都不包含
356
+ */
357
+ export const randomInt = (min: number = 0, max: number = 1, range: 'both' | 'min' | 'max' | 'neither' = 'min'): number => {
358
+ let _max: number = max, _min: number = min;
359
+ if (range === 'min') {
360
+ _max = max - 1;
361
+ } else if (range === 'max') {
362
+ _min = min + 1;
363
+ } else if (range === 'neither') {
364
+ _min = min + 1;
365
+ _max = max - 1;
366
+ }
367
+ if (_min > _max) {
368
+ [_min, _max] = [max, _max];
369
+ }
370
+ return Math.floor(Math.random() * (_max - _min + 1)) + _min;
371
+ }
372
+
373
+
374
+
375
+ export const formats = (url: string): string => {
376
+ const parts = url.split('?')[0].split('.');
377
+ return parts[parts.length - 1];
378
+ }
379
+
380
+ /**
381
+ * 下载
382
+ * @param url string
383
+ * @param name string
384
+ * @param format string
385
+ * @param type string
386
+ */
387
+ export const download = async ({
388
+ url= "",
389
+ name= "",
390
+ format= "",
391
+ type = 'network',
392
+ }:{
393
+ url?: string;
394
+ name?: string;
395
+ format?: string;
396
+ type?: string;
397
+ } = {}): Promise<void> => {
398
+ let _format: string = format;
399
+ if(type === 'network'){
400
+ _format = formats(url);
401
+ }
402
+ const response: any = await axios({
403
+ url,
404
+ method: 'GET',
405
+ responseType: 'blob'
406
+ })
407
+ const blob: Blob = new Blob([response.data], {
408
+ type: response.headers['content-type']
409
+ });
410
+ const blobUrl: string = window.URL.createObjectURL(blob)
411
+ const a: any = document.createElement('a')
412
+ a.href = blobUrl;
413
+ a.download = `${name}.${_format}`;
414
+ a.style.display = 'none'
415
+ document.body.appendChild(a)
416
+ a.click()
417
+ document.body.removeChild(a)
418
+ window.URL.revokeObjectURL(blobUrl)
419
+ }
420
+
421
+
422
+
423
+
424
+ export const padStart = (num: string | number, fixed: number = 2) => String(Number(num)).padStart(fixed, '0');
425
+
426
+ /**
427
+ * 时间格式拆分(毫秒)
428
+ * @param time string
429
+ */
430
+ export const formatTimeArray = (time: string): any => {
431
+ const regex = /^\d{2}:\d{2}:\d{2}.\d{2}$/;
432
+ if (!regex.test(time)) {
433
+ message('请输入有效的时间!', 'error');
434
+ return '';
435
+ }
436
+ const [hours, minutes, second] = time.split(':').map(Number);
437
+ const [seconds, milliseconds] = String(second).split('.').map(Number);
438
+ return {
439
+ hours: padStart(hours),
440
+ minutes: padStart(minutes),
441
+ seconds: padStart(seconds),
442
+ milliseconds: padStart(milliseconds || 0),
443
+ }
444
+ }
445
+ export const formatTime = (seconds: number, show: boolean = false): string => {
446
+ const h = String(Math.floor(seconds / 3600)).padStart(2, '0');
447
+ const m = String(Math.floor(seconds % 3600 / 60)).padStart(2, '0');
448
+ const num = seconds % 60;
449
+ const s = String(Math.floor(num)).padStart(2, '0');
450
+ const mil = num.toFixed(2).split('.')[1];
451
+ if(show){
452
+ return `${h}:${m}:${s}.${mil}`;
453
+ } else {
454
+ return `${h}:${m}:${s}`;
455
+ }
456
+ }
457
+
458
+ /**
459
+ * 导出 hook 方法对象,包含所有 hook 方法
460
+ */
461
+ export { zip }
462
+ export default {
463
+ message,
464
+ confirm,
465
+ getAssetsImage,
466
+ notification,
467
+ // asideActive,
468
+ setSession,
469
+ getSession,
470
+ delSession,
471
+ clearSession,
472
+ setLocal,
473
+ getLocal,
474
+ delLocal,
475
+ clearLocal,
476
+ verified,
477
+ verifiedTips,
478
+ postMessage,
479
+ emptyStatus,
480
+ loading,
481
+ goTo,
482
+ randomInt,
483
+ decrypt,
484
+ qiankunGoTo,
485
+ crypto,
486
+ getQuery,
487
+ download,
488
+ zip,
489
+ padStart,
490
+ formatTime,
491
+ formatTimeArray,
492
+ }
493
+
494
+
495
+
496
+
497
+