-
Notifications
You must be signed in to change notification settings - Fork 822
Expand file tree
/
Copy pathutils.ts
More file actions
284 lines (256 loc) · 7.98 KB
/
Copy pathutils.ts
File metadata and controls
284 lines (256 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import dayjs from 'dayjs'
import { useCache } from '@/utils/useCache'
import colorFunctions from 'less/lib/less/functions/color.js'
import colorTree from 'less/lib/less/tree/color.js'
const { wsCache } = useCache()
const getCheckDate = (timestamp: any) => {
if (!timestamp) return false
const dt = new Date(timestamp)
if (isNaN(dt.getTime())) return false
return dt
}
export const datetimeFormat = (timestamp: any) => {
const dt = getCheckDate(timestamp)
if (!dt) return timestamp
const y = dt.getFullYear()
const m = (dt.getMonth() + 1 + '').padStart(2, '0')
const d = (dt.getDate() + '').padStart(2, '0')
const hh = (dt.getHours() + '').padStart(2, '0')
const mm = (dt.getMinutes() + '').padStart(2, '0')
const ss = (dt.getSeconds() + '').padStart(2, '0')
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
/**
*
* string: only accept ISO 8601, example: '2018-04-04T16:00:00.000Z'
* number: timestamp
* @param time
*/
export function getDate(time?: Date | string | number) {
if (!time) return undefined
if (time instanceof Date) return time
if (typeof time === 'string') {
return dayjs(time).toDate()
}
return new Date(time)
}
export const getBrowserLocale = () => {
const language = navigator.language
if (!language) {
return 'zh-CN'
}
if (language.startsWith('en')) {
return 'en'
}
if (language.toLowerCase().startsWith('zh')) {
const temp = language.toLowerCase().replace('_', '-')
return temp === 'zh' ? 'zh-CN' : temp === 'zh-cn' ? 'zh-CN' : 'tw'
}
return language
}
export const getLocale = () => {
return wsCache.get('user.language') || getBrowserLocale() || 'zh-CN'
}
export const setSize = (size: any) => {
let data = ''
const _size = Number.parseFloat(size)
if (_size < 1 * 1024) {
//如果小于0.1KB转化成B
data = _size.toFixed(2) + 'B'
} else if (_size < 1 * 1024 * 1024) {
//如果小于0.1MB转化成KB
data = (_size / 1024).toFixed(2) + 'KB'
} else if (_size < 1 * 1024 * 1024 * 1024) {
//如果小于0.1GB转化成MB
data = (_size / (1024 * 1024)).toFixed(2) + 'MB'
} else {
//其他转化成GB
data = (_size / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
}
const size_str = data + ''
const len = size_str.indexOf('.')
const dec = size_str.substr(len + 1, 2)
if (dec == '00') {
//当小数点后为00时 去掉小数部分
return size_str.substring(0, len) + size_str.substr(len + 3, 2)
}
return size_str
}
export const isInIframe = () => {
try {
return window.top !== window.self
} catch (error) {
console.error(error)
return true
}
}
export const isBtnShow = (val: string) => {
if (!val || val === '0') {
return true
} else if (val === '1') {
return false
} else {
return !isInIframe()
}
}
export const setTitle = (title?: string) => {
document.title = title || 'SQLBot'
}
function rgbToHex(r: any, g: any, b: any) {
// 确保数值在0-255范围内
r = Math.max(0, Math.min(255, r))
g = Math.max(0, Math.min(255, g))
b = Math.max(0, Math.min(255, b))
// 转换为16进制并补零
const hexR = r.toString(16).padStart(2, '0')
const hexG = g.toString(16).padStart(2, '0')
const hexB = b.toString(16).padStart(2, '0')
return `#${hexR}${hexG}${hexB}`.toUpperCase()
}
function rgbaToHex(r: any, g: any, b: any, a: any) {
// 处理RGB部分
const hexR = Math.max(0, Math.min(255, r)).toString(16).padStart(2, '0')
const hexG = Math.max(0, Math.min(255, g)).toString(16).padStart(2, '0')
const hexB = Math.max(0, Math.min(255, b)).toString(16).padStart(2, '0')
// 处理透明度(可选)
const hexA =
a !== undefined
? Math.round(Math.max(0, Math.min(1, a)) * 255)
.toString(16)
.padStart(2, '0')
: ''
return `#${hexR}${hexG}${hexB}${hexA}`.toUpperCase()
}
export function colorStringToHex(colorStr: any) {
if (colorStr.startsWith('#')) return colorStr
// 提取颜色值
const rgbRegex =
/^(rgb|rgba)\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)$/
const match = colorStr.match(rgbRegex)
if (!match) return null
const r = parseInt(match[2])
const g = parseInt(match[3])
const b = parseInt(match[4])
const a = match[5] ? parseFloat(match[5]) : undefined
return a !== undefined ? rgbaToHex(r, g, b, a) : rgbToHex(r, g, b)
}
export const setCurrentColor = (color: any, element: HTMLElement = document.documentElement) => {
const currentColor = colorStringToHex(color) as any
if (!currentColor) {
return
}
element.style.setProperty('--ed-color-primary', currentColor)
element.style.setProperty('--van-blue', currentColor)
element.style.setProperty(
'--ed-color-primary-light-5',
colorFunctions
.mix(new colorTree('ffffff'), new colorTree(currentColor.substr(1)), { value: 40 })
.toRGB()
)
element.style.setProperty(
'--ed-color-primary-light-3',
colorFunctions
.mix(new colorTree('ffffff'), new colorTree(currentColor.substr(1)), { value: 15 })
.toRGB()
)
element.style.setProperty(
'--ed-color-primary-60',
colorFunctions
.mix(new colorTree('ffffff'), new colorTree(currentColor.substr(1)), { value: 60 })
.toRGB()
)
element.style.setProperty(
'--ed-color-primary-80',
colorFunctions
.mix(new colorTree('ffffff'), new colorTree(currentColor.substr(1)), { value: 80 })
.toRGB()
)
element.style.setProperty(
'--ed-color-primary-15-d',
colorFunctions
.mix(new colorTree('000000'), new colorTree(currentColor.substr(1)), { value: 15 })
.toRGB()
)
element.style.setProperty('--ed-color-primary-1a', `${currentColor}1a`)
element.style.setProperty('--ed-color-primary-14', `${currentColor}14`)
element.style.setProperty('--ed-color-primary-33', `${currentColor}33`)
element.style.setProperty('--ed-color-primary-99', `${currentColor}99`)
element.style.setProperty(
'--ed-color-primary-dark-2',
colorFunctions
.mix(new colorTree('000000'), new colorTree(currentColor.substr(1)), { value: 15 })
.toRGB()
)
}
export const getQueryString = (name: string) => {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
const r = window.location.search.substr(1).match(reg)
if (r != null) {
return unescape(r[2])
}
return null
}
export const getUrlParams = () => {
const urlParams = new URLSearchParams(window.location.search) as any
return Object.fromEntries(urlParams)
}
export const isLarkPlatform = () => {
return !!getQueryString('state') && !!getQueryString('code')
}
export const isPlatformClient = () => {
return !!getQueryString('client') || getQueryString('state')?.includes('client')
}
export const checkPlatform = () => {
const flagArray = ['/casbi', 'oidcbi']
const pathname = window.location.pathname
if (
!flagArray.some((flag) => pathname.includes(flag)) &&
!isLarkPlatform() &&
!isPlatformClient()
) {
return cleanPlatformFlag()
}
return true
}
export const cleanPlatformFlag = () => {
const platformKey = 'out_auth_platform'
wsCache.delete(platformKey)
return false
}
export function isTablet() {
const userAgent = navigator.userAgent
const tabletRegex = /iPad|Silk|Galaxy Tab|PlayBook|BlackBerry|(tablet|ipad|playbook)/i
return tabletRegex.test(userAgent)
}
export function isMobile() {
return (
navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
) && !isTablet()
)
}
export const getSQLBotAddr = (portEnd?: boolean) => {
const addr = location.origin + location.pathname
if (!portEnd || !addr.endsWith('/')) {
return addr
}
return addr.substring(0, addr.length - 1)
}
export const formatArg = (text: string) => {
if (!text) {
return false
}
const mappingArray = ['true', 'false', '1', '0']
const match = mappingArray.some((item: string) => {
return item === text.toLocaleLowerCase()
})
if (!match) {
return text
}
try {
return JSON.parse(text)
} catch (e: any) {
console.warn(e)
return text
}
}