@ganwei-web/ganwei-pc-cli 6.2.9 → 6.3.2
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/ganwei-iotcenter-index-6.2.3/configuration/moduleConfiguration.json +2 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/env.d.ts +8 -5
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/index.html +1 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/css/ElementPlusAdapter.css +437 -68
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/css/reset-6-1.css +1 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/css/reset-plus.css +396 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/http/createAxios.js +38 -13
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/js/getLanguage.js +19 -12
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/themes/dark-6-1.css +3 -3
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/public/static/themes/green-6-1.css +12 -11
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/App.vue +11 -6
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/equipAlarmDialog/index.vue +6 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/expirationReminder/index.vue +7 -3
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/index.scss +4 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/userInfo/index.js +35 -3
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/userInfo/index.scss +7 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/indexRightContent/headerRight/userInfo/index.vue +2 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/layouts/Navigation/ContractMenu.vue +11 -37
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/layouts/Navigation/TopNav.vue +3 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/components/layouts/Sidebar/LeftContent/index.vue +7 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/router.js +120 -18
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/date.ts +80 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/dom.ts +99 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/env.ts +20 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/file.ts +74 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/index.ts +26 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/number.ts +83 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/performance.ts +69 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/storage.ts +80 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/string.ts +116 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/xss-filter.ts +260 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/views/jumpIframe/index.vue +45 -28
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/tsconfig.json +1 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/vite.config.ts +10 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-login/public/static/http/createAxios.js +40 -15
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-login/public/static/js/getLanguage.js +10 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-login/src/views/login.vue +55 -53
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-login/src/views/ssoLogin.vue +10 -9
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/.env.development +0 -3
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/.env.production +1 -4
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/.env.test +0 -3
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/.eslintrc.cjs +2 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/env.d.ts +9 -5
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/index.html +1 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/package.json +3 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/public/static/http/createAxios.js +38 -13
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/public/static/js/getLanguage.js +10 -2
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/App.vue +1 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/request/api.ts +0 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/request/models/response/template.ts +0 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/date.ts +79 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/dom.ts +99 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/env.ts +20 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/file.ts +74 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/index.ts +29 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/number.ts +83 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/performance.ts +69 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/signalr.ts +564 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/storage.ts +80 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/string.ts +116 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/xss-filter.ts +260 -0
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/views/template.vue +0 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/tsconfig.json +7 -0
- package/ganwei-iotcenter-index-6.2.3/pnpm-lock.yaml +489 -155
- package/package.json +1 -1
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/mixins/judgePermission.js +0 -60
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/src/utils/setStorage.js +0 -5
- package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/setStorage.js +0 -5
- /package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-index/build/{enteryJson.js → entryJson.js} +0 -0
- /package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/{request/models/request/index.ts → enum/template.ts} +0 -0
- /package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/request/models/{response/index.ts → request/template.ts} +0 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 添加事件监听器
|
|
3
|
+
* @returns 移除监听器的函数
|
|
4
|
+
*/
|
|
5
|
+
export function addEventListener(
|
|
6
|
+
element: HTMLElement | Window,
|
|
7
|
+
event: string,
|
|
8
|
+
handler: EventListener,
|
|
9
|
+
options?: AddEventListenerOptions
|
|
10
|
+
): () => void {
|
|
11
|
+
element.addEventListener(event, handler, options)
|
|
12
|
+
return () => element.removeEventListener(event, handler, options)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 判断元素是否可见
|
|
17
|
+
*/
|
|
18
|
+
export function isVisible(element: HTMLElement): boolean {
|
|
19
|
+
const rect = element.getBoundingClientRect()
|
|
20
|
+
return (
|
|
21
|
+
rect.width > 0 &&
|
|
22
|
+
rect.height > 0 &&
|
|
23
|
+
rect.top < window.innerHeight &&
|
|
24
|
+
rect.bottom > 0 &&
|
|
25
|
+
rect.left < window.innerWidth &&
|
|
26
|
+
rect.right > 0
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 滚动到元素
|
|
32
|
+
*/
|
|
33
|
+
export function scrollToElement(
|
|
34
|
+
element: HTMLElement,
|
|
35
|
+
options?: ScrollIntoViewOptions
|
|
36
|
+
): void {
|
|
37
|
+
element.scrollIntoView({
|
|
38
|
+
behavior: 'smooth',
|
|
39
|
+
block: 'start',
|
|
40
|
+
...options
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 复制文本到剪贴板
|
|
46
|
+
*/
|
|
47
|
+
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
48
|
+
try {
|
|
49
|
+
await navigator.clipboard.writeText(text)
|
|
50
|
+
return true
|
|
51
|
+
} catch {
|
|
52
|
+
// 降级方案
|
|
53
|
+
const textarea = document.createElement('textarea')
|
|
54
|
+
textarea.value = text
|
|
55
|
+
textarea.style.position = 'fixed'
|
|
56
|
+
textarea.style.opacity = '0'
|
|
57
|
+
document.body.appendChild(textarea)
|
|
58
|
+
textarea.select()
|
|
59
|
+
const success = document.execCommand('copy')
|
|
60
|
+
document.body.removeChild(textarea)
|
|
61
|
+
return success
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 全屏切换
|
|
67
|
+
*/
|
|
68
|
+
export function toggleFullscreen(element?: HTMLElement): Promise<void> {
|
|
69
|
+
const target = element || document.documentElement
|
|
70
|
+
|
|
71
|
+
if (!document.fullscreenElement) {
|
|
72
|
+
return target.requestFullscreen()
|
|
73
|
+
}
|
|
74
|
+
return document.exitFullscreen()
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取元素样式
|
|
80
|
+
*/
|
|
81
|
+
export function getStyle(element: HTMLElement, property: string): string {
|
|
82
|
+
return window.getComputedStyle(element).getPropertyValue(property)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 设置 CSS 变量
|
|
87
|
+
*/
|
|
88
|
+
export function setCssVariable(name: string, value: string): void {
|
|
89
|
+
document.documentElement.style.setProperty(name, value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 设置主题
|
|
94
|
+
*/
|
|
95
|
+
export const setTheme = (theme: string): void => {
|
|
96
|
+
localStorage.setItem('theme', theme)
|
|
97
|
+
sessionStorage.setItem('theme', theme)
|
|
98
|
+
window.document.documentElement.setAttribute('data-theme', theme)
|
|
99
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const ENV = {
|
|
2
|
+
|
|
3
|
+
/** 应用标题 */
|
|
4
|
+
APP_TITLE: import.meta.env.VITE_APP_TITLE,
|
|
5
|
+
|
|
6
|
+
/** 应用端口 */
|
|
7
|
+
APP_VERSION: import.meta.env.VITE_APP_PORT,
|
|
8
|
+
|
|
9
|
+
/** proxy代理配置 */
|
|
10
|
+
APP_ENV: import.meta.env.VITE_APP_TARGET_URL,
|
|
11
|
+
|
|
12
|
+
/** 本地地址 */
|
|
13
|
+
API_URL: import.meta.env.VITE_APP_THIS_URL,
|
|
14
|
+
|
|
15
|
+
/** 是否开发环境 */
|
|
16
|
+
IS_DEV: import.meta.env.DEV,
|
|
17
|
+
|
|
18
|
+
/** 是否生产环境 */
|
|
19
|
+
IS_PROD: import.meta.env.PROD
|
|
20
|
+
} as const
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 下载文件
|
|
3
|
+
*/
|
|
4
|
+
export function downloadFile(url: string, filename?: string): void {
|
|
5
|
+
const link = document.createElement('a')
|
|
6
|
+
link.href = url
|
|
7
|
+
link.download = filename || ''
|
|
8
|
+
document.body.appendChild(link)
|
|
9
|
+
link.click()
|
|
10
|
+
document.body.removeChild(link)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 下载 Blob 文件
|
|
15
|
+
*/
|
|
16
|
+
export function downloadBlob(blob: Blob, filename: string): void {
|
|
17
|
+
const url = URL.createObjectURL(blob)
|
|
18
|
+
downloadFile(url, filename)
|
|
19
|
+
URL.revokeObjectURL(url)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 文件转 Base64
|
|
24
|
+
*/
|
|
25
|
+
export function fileToBase64(file: File): Promise<string> {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const reader = new FileReader()
|
|
28
|
+
reader.onload = () => resolve(reader.result as string)
|
|
29
|
+
reader.onerror = reject
|
|
30
|
+
reader.readAsDataURL(file)
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Base64 转 Blob
|
|
36
|
+
*/
|
|
37
|
+
export function base64ToBlob(
|
|
38
|
+
base64: string,
|
|
39
|
+
mimeType = 'application/octet-stream'
|
|
40
|
+
): Blob {
|
|
41
|
+
const byteString = atob(base64.split(',')[1])
|
|
42
|
+
const arrayBuffer = new ArrayBuffer(byteString.length)
|
|
43
|
+
const uint8Array = new Uint8Array(arrayBuffer)
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < byteString.length; i++) {
|
|
46
|
+
uint8Array[i] = byteString.charCodeAt(i)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return new Blob([uint8Array], { type: mimeType })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 获取文件扩展名
|
|
54
|
+
*/
|
|
55
|
+
export function getFileExtension(filename: string): string {
|
|
56
|
+
return filename.slice(((filename.lastIndexOf('.') - 1) >>> 0) + 2)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 验证文件类型
|
|
61
|
+
*/
|
|
62
|
+
export function validateFileType(file: File, allowedTypes: string[]): boolean {
|
|
63
|
+
const extension = getFileExtension(file.name).toLowerCase()
|
|
64
|
+
return allowedTypes.includes(extension) || allowedTypes.includes(file.type)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 验证文件大小
|
|
69
|
+
* @param file - 文件对象
|
|
70
|
+
* @param maxSize - 最大大小(MB)
|
|
71
|
+
*/
|
|
72
|
+
export function validateFileSize(file: File, maxSize: number): boolean {
|
|
73
|
+
return file.size <= maxSize * 1024 * 1024
|
|
74
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// 日期处理
|
|
2
|
+
export * from './date'
|
|
3
|
+
|
|
4
|
+
// 数字处理
|
|
5
|
+
export * from './number'
|
|
6
|
+
|
|
7
|
+
// 字符串处理
|
|
8
|
+
export * from './string'
|
|
9
|
+
|
|
10
|
+
// 文件处理
|
|
11
|
+
export * from './file'
|
|
12
|
+
|
|
13
|
+
// DOM 操作
|
|
14
|
+
export * from './dom'
|
|
15
|
+
|
|
16
|
+
// 本地存储
|
|
17
|
+
export * from './storage'
|
|
18
|
+
|
|
19
|
+
// 性能优化
|
|
20
|
+
export * from './performance'
|
|
21
|
+
|
|
22
|
+
// 环境变量
|
|
23
|
+
export * from './env'
|
|
24
|
+
|
|
25
|
+
// XSS 过滤
|
|
26
|
+
export * from './xss-filter'
|
|
27
|
+
|
|
28
|
+
// SignalR 实时通信
|
|
29
|
+
export * from './signalr'
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 格式化数字(千分位)
|
|
3
|
+
* @param num - 数字或字符串
|
|
4
|
+
* @param decimals - 小数位数,默认 0
|
|
5
|
+
* @returns 格式化后的数字字符串
|
|
6
|
+
*/
|
|
7
|
+
export function formatNumber(num: number | string, decimals = 0): string {
|
|
8
|
+
const number = Number(num)
|
|
9
|
+
if (isNaN(number)) return '0'
|
|
10
|
+
|
|
11
|
+
return number.toLocaleString('zh-CN', {
|
|
12
|
+
minimumFractionDigits: decimals,
|
|
13
|
+
maximumFractionDigits: decimals
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 格式化金额
|
|
19
|
+
* @param amount - 金额
|
|
20
|
+
* @param currency - 货币符号,默认 '¥'
|
|
21
|
+
* @param decimals - 小数位数,默认 2
|
|
22
|
+
*/
|
|
23
|
+
export function formatMoney(
|
|
24
|
+
amount: number | string,
|
|
25
|
+
currency = '¥',
|
|
26
|
+
decimals = 2
|
|
27
|
+
): string {
|
|
28
|
+
const number = Number(amount)
|
|
29
|
+
if (isNaN(number)) return `${currency}0.00`
|
|
30
|
+
|
|
31
|
+
return `${currency}${formatNumber(number, decimals)}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 格式化百分比
|
|
36
|
+
* @param value - 值(0-1)
|
|
37
|
+
* @param decimals - 小数位数,默认 2
|
|
38
|
+
*/
|
|
39
|
+
export function formatPercent(
|
|
40
|
+
value: number | string,
|
|
41
|
+
decimals = 2
|
|
42
|
+
): string {
|
|
43
|
+
const number = Number(value)
|
|
44
|
+
if (isNaN(number)) return '0%'
|
|
45
|
+
|
|
46
|
+
return `${(number * 100).toFixed(decimals)}%`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 格式化文件大小
|
|
51
|
+
* @param bytes - 字节数
|
|
52
|
+
* @param decimals - 小数位数,默认 2
|
|
53
|
+
*/
|
|
54
|
+
export function formatFileSize(bytes: number, decimals = 2): string {
|
|
55
|
+
if (bytes === 0) return '0 B'
|
|
56
|
+
|
|
57
|
+
const k = 1024
|
|
58
|
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
59
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
60
|
+
|
|
61
|
+
return `${(bytes / Math.pow(k, i)).toFixed(decimals)} ${sizes[i]}`
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 数字范围限制
|
|
66
|
+
*/
|
|
67
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
68
|
+
return Math.min(Math.max(value, min), max)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 生成随机数
|
|
73
|
+
*/
|
|
74
|
+
export function random(min: number, max: number): number {
|
|
75
|
+
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 生成随机 ID
|
|
80
|
+
*/
|
|
81
|
+
export function generateId(prefix = ''): string {
|
|
82
|
+
return `${prefix}${Date.now()}${Math.random().toString(36).substr(2, 9)}`
|
|
83
|
+
}
|
package/ganwei-iotcenter-index-6.2.3/packages/ganwei-iotcenter-template/src/utils/performance.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 防抖函数
|
|
3
|
+
* @param func - 要防抖的函数
|
|
4
|
+
* @param wait - 等待时间(毫秒)
|
|
5
|
+
* @param immediate - 是否立即执行
|
|
6
|
+
*/
|
|
7
|
+
export function debounce<T extends (...args: any[]) => any>(
|
|
8
|
+
func: T,
|
|
9
|
+
wait: number,
|
|
10
|
+
immediate = false
|
|
11
|
+
): (...args: Parameters<T>) => ReturnType<T> | undefined {
|
|
12
|
+
let timeout: ReturnType<typeof setTimeout> | null = null
|
|
13
|
+
|
|
14
|
+
return function (this: any, ...args: Parameters<T>) {
|
|
15
|
+
const context = this
|
|
16
|
+
|
|
17
|
+
const later = () => {
|
|
18
|
+
timeout = null
|
|
19
|
+
if (!immediate) {
|
|
20
|
+
func.apply(context, args)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const callNow = immediate && !timeout
|
|
25
|
+
|
|
26
|
+
if (timeout) {
|
|
27
|
+
clearTimeout(timeout)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
timeout = setTimeout(later, wait)
|
|
31
|
+
|
|
32
|
+
if (callNow) {
|
|
33
|
+
func.apply(context, args)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 节流函数
|
|
40
|
+
* @param func - 要节流的函数
|
|
41
|
+
* @param wait - 等待时间(毫秒)
|
|
42
|
+
*/
|
|
43
|
+
export function throttle<T extends (...args: any[]) => any>(
|
|
44
|
+
func: T,
|
|
45
|
+
wait: number
|
|
46
|
+
): (...args: Parameters<T>) => ReturnType<T> | undefined {
|
|
47
|
+
let timeout: ReturnType<typeof setTimeout> | null = null
|
|
48
|
+
let previous = 0
|
|
49
|
+
|
|
50
|
+
return function (this: any, ...args: Parameters<T>) {
|
|
51
|
+
const context = this
|
|
52
|
+
const now = Date.now()
|
|
53
|
+
|
|
54
|
+
if (now - previous > wait) {
|
|
55
|
+
if (timeout) {
|
|
56
|
+
clearTimeout(timeout)
|
|
57
|
+
timeout = null
|
|
58
|
+
}
|
|
59
|
+
previous = now
|
|
60
|
+
func.apply(context, args)
|
|
61
|
+
} else if (!timeout) {
|
|
62
|
+
timeout = setTimeout(() => {
|
|
63
|
+
previous = Date.now()
|
|
64
|
+
timeout = null
|
|
65
|
+
func.apply(context, args)
|
|
66
|
+
}, wait)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|