-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathaidFn.js
More file actions
510 lines (447 loc) · 15.4 KB
/
aidFn.js
File metadata and controls
510 lines (447 loc) · 15.4 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import { parse, stringify } from 'qs'
import html2canvas from 'html2canvas'
export const getEnv = () => {
let env
if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
env = 'NODE'
}
if (typeof XMLHttpRequest !== 'undefined') {
env = 'BROWSER'
}
return env
}
export const isArray = (val) => typeof val === 'object' && Object.prototype.toString.call(val) === '[object Array]'
export const isURLSearchParams = (val) => typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams
export const isDate = (val) => typeof val === 'object' && Object.prototype.toString.call(val) === '[object Date]'
export const isObject = (val) => val !== null && typeof val === 'object'
export const getParamObject = (val) => {
if (isURLSearchParams(val)) {
return parse(val.toString(), { strictNullHandling: true })
}
if (typeof val === 'string') {
return [val]
}
return val
}
export const reqStringify = (val) => stringify(val, { arrayFormat: 'repeat', strictNullHandling: true })
export const getType = (obj) => {
const typeString = Object.prototype.toString.call(obj)
const match = /\[object (.*?)\]/.exec(typeString)
return match ? match[1] : null
}
export const hidePhone = (phone) => phone?.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
// asyncAction(action)(callback)
export const asyncAction = (action) => {
const wait = Promise.resolve(action)
return (cb) => {
wait.then(() => setTimeout(() => cb()))
}
}
export const getImgsUrl = (html) => {
const imgReg = /<img.*?(?:>|\/>)/gi
const srcReg = /src=['"]?([^'"]*)['"]?/i
const arr = html.match(imgReg)
if (!arr) return null
const urlArr = arr.reduce((prev, next) => {
const src = next.match(srcReg)
return src[1] ? [...prev, src[1]] : prev
}, [])
return urlArr
}
export const customizeTimer = {
intervalTimer: null,
timeoutTimer: null,
setTimeout (cb, interval) {
const { now } = Date
const stime = now()
let etime = stime
const loop = () => {
this.timeoutTimer = requestAnimationFrame(loop)
etime = now()
if (etime - stime >= interval) {
cb()
cancelAnimationFrame(this.timeoutTimer)
}
}
this.timeoutTimer = requestAnimationFrame(loop)
return this.timeoutTimer
},
clearTimeout () {
cancelAnimationFrame(this.timeoutTimer)
},
setInterval (cb, interval) {
const { now } = Date
let stime = now()
let etime = stime
const loop = () => {
this.intervalTimer = requestAnimationFrame(loop)
etime = now()
if (etime - stime >= interval) {
stime = now()
etime = stime
cb()
}
}
this.intervalTimer = requestAnimationFrame(loop)
return this.intervalTimer
},
clearInterval () {
cancelAnimationFrame(this.intervalTimer)
}
}
export const isDecimal = (value) => {
const reg = /(?:^[1-9](\d+)?(?:\.\d{1,2})?$)|(?:^(?:0)$)|(?:^\d\.\d(?:\d)?$)/
return reg.test(value)
}
export const limitDecimal = (val) => val.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
export const passwordStrength = (pass) => {
const reg = /^(?=.*[A-Z])(?=.*[\d])(?=.*[a-z]).{8,}$/
return reg.test(pass)
}
/*
** 判断用户是否离开当前页面
*/
export const checkIsLocalPage = () => {
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
return false
}
if (document.visibilityState === 'visible') {
return true
}
window.addEventListener(
'pagehide',
(event) => {
if (event.persisted) {
/* the page isn't being discarded, so it can be reused later */
}
},
false
)
})
}
// Generate Random Hex
export const randomHex = () =>
`#${Math.floor(Math.random() * 0xffffff)
.toString(16)
.padEnd(6, '0')}`
export const rgbToHex = (r, g, b) => `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`
export const fibonacci = (n) => {
const seq = [0, 1]
for (let i = 2; i < n; i++) {
seq.push(seq[i - 1] + seq[i - 2])
}
return seq.slice(0, n)
}
export const lerp = (a, b, t) => a + (b - a) * t
export const formatFileSize = (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${(bytes / Math.pow(k, i)).toFixed(decimals)} ${sizes[i]}`
}
export const formatOrdinal = (num) => {
const suffixes = ['th', 'st', 'nd', 'rd']
const v = num % 100
return `${num}${suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]}`
}
export const formatPercentage = (num, decimals = 2) => `${(num * 100).toFixed(decimals)}%`
export const formatRomanNumeral = (num) => {
const romanMap = [
{ value: 1000, numeral: 'M' },
{ value: 900, numeral: 'CM' },
{ value: 500, numeral: 'D' },
{ value: 400, numeral: 'CD' },
{ value: 100, numeral: 'C' },
{ value: 90, numeral: 'XC' },
{ value: 50, numeral: 'L' },
{ value: 40, numeral: 'XL' },
{ value: 10, numeral: 'X' },
{ value: 9, numeral: 'IX' },
{ value: 5, numeral: 'V' },
{ value: 4, numeral: 'IV' },
{ value: 1, numeral: 'I' }
]
let result = ''
for (const { value, numeral } of romanMap) {
while (num >= value) {
result += numeral
num -= value
}
}
return result
}
export const formatScientific = (num, decimalPlaces = 2) => toExponential(decimalPlaces)
export const capitalizeWords = (str) => str.replace(/\b\w/g, (match) => match.toUpperCase())
export const isPalindrome = (str) => {
const normalized = str.toLowerCase().replace(/[^a-z0-9]/g, '')
return normalized === normalized.split('').reverse().join('')
}
export const toCamelCase = (str) => str.replace(/\W+(.)/g, (match, chr) => chr.toUpperCase())
export const toKebabCase = (str) => {
return str
.replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g, '')
.replace(/([a-z])([A-Z])/g, (m, a, b) => `${a}_${b.toLowerCase()}`)
.replace(/[^A-Za-z0-9]+|_+/g, '-')
.toLowerCase()
}
export const truncate = (str, num) => (str.length > num ? str.slice(0, num) + '...' : str)
export const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
// Clear All Cookies
export const clearCookies = document.cookie
.split(';')
.forEach(
(cookie) =>
(document.cookie = cookie.replace(/^ +/, '').replace(/[=].*/, `=;expires=${new Date(0).toUTCString()};path=/`))
)
// Find the number of days between two days
export const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
// Capitalize a String
export const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1)
// Check if the array is empty
export const isNotEmpty = (arr) => Array.isArray(arr) && arr.length > 0
// Detect Dark Mode
export const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
export const fetchSomething = () =>
new Promise((resolve) => {
setTimeout(() => {
resolve('')
}, 1000)
})
export const toFixed = (number, m) => {
if (typeof number !== 'number') {
throw new Error('number不是数字')
}
let result = Math.round(10 ** m * number) / 10 ** m
result = String(result)
if (result.indexOf('.') === -1) {
if (m !== 0) {
result += '.'
result += new Array(m + 1).join('0')
}
} else {
const arr = result.split('.')
if (arr[1].length < m) {
arr[1] += new Array(m - arr[1].length + 1).join('0')
}
result = arr.join('.')
}
return result
}
export const toFixedBug = (n, fixed) => ~~(10 ** fixed * n) / 10 ** fixed
export const promiseWithTimeout = (promise, timeout) => {
const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve('Time Out!'), timeout))
return Promise.race([timeoutPromise, promise])
}
export const shuffleArr = (arr) => arr.sort(() => 0.5 - Math.random())
export const sleep = (time) => new Promise((resolve) => setTimeout(() => resolve(), time))
const handleNotification = async (message) => {
setShowNotification(true)
await sleep(3000)
setShowNotification(false)
}
export const ThousandNum = (num) => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
export const RandomId = (len) => Math.random().toString(36).substring(3, len)
export const RoundNum = (num, decimal) => Math.round(num * 10 ** decimal) / 10 ** decimal
export const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
export const isEmptyArray = (arr) => Array.isArray(arr) && !arr.length
export const randomItem = (arr) => arr[Math.floor(Math.random() * arr.length)]
export const asyncTo = (promise) => promise.then((data) => [null, data]).catch((err) => [err])
export const hasFocus = (element) => element === document.activeElement
export const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
export const randomString = () => Math.random().toString(36).slice(2)
export const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
export const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`
export const pause = (millions) => new Promise((resolve) => setTimeout(resolve, millions))
export const camelizeCamelCase = (str) =>
str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => (index === 0 ? letter.toLowerCase() : letter.toUpperCase()))
.replace(/\s+/g, '')
export const copyTextToClipboard = async (textToCopy) => {
try {
if (navigator?.clipboard?.writeText) {
await navigator.clipboard.writeText(textToCopy)
console.log('已成功复制到剪贴板')
}
} catch (err) {
console.error(`复制到剪贴板失败:${err.message}`)
}
}
export const getRandomId = () => {
let text = ''
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text
}
// https://github.com/Azure/fetch-event-source
// https://github.com/mpetazzoni/sse.js
// https://nodejs.org/api/http.html#httprequesturl-options-callback
export const oneApiChat = (chatList, token, signal) =>
fetch('https://api.zhizengzeng.com/v1/chat/completions', {
method: 'POST',
signal,
headers: {
Authorization: token,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: chatList,
stream: true
})
})
export const getCurrentDate = () => {
const date = new Date()
const day = date.getDate()
const month = date.getMonth() + 1
const year = date.getFullYear()
return `${year}-${month}-${day}`
}
export const exportJsonData = (data) => {
const date = getCurrentDate()
const jsonString = JSON.stringify(JSON.parse(data), null, 2)
const blob = new Blob([jsonString], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = `chat-store_${date}.json`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
export const saveHtmlToPng = async (eleHtml, successFun, errorFun) => {
try {
const ele = eleHtml ?? document.getElementById('image-wrapper')
const canvas = await html2canvas(ele, {
useCORS: true
})
const imgUrl = canvas.toDataURL('image/png')
const tempLink = document.createElement('a')
tempLink.style.display = 'none'
tempLink.href = imgUrl
tempLink.setAttribute('download', 'chat-shot.png')
if (typeof tempLink.download === 'undefined') tempLink.setAttribute('target', '_blank')
document.body.appendChild(tempLink)
tempLink.click()
document.body.removeChild(tempLink)
window.URL.revokeObjectURL(imgUrl)
if (successFun) successFun()
Promise.resolve()
} catch (error) {
if (errorFun) errorFun(error.message)
}
}
export const trimTopic = (topic) => topic.replace(/[,。!?”“"、,.!?]*$/, '')
// onClick={() => importFromFile()}
// readFromFile().then((content) => { JSON.parse(content)})
export const readFromFile = () =>
new Promise((res, rej) => {
const fileInput = document.createElement('input')
fileInput.type = 'file'
fileInput.accept = 'application/json'
fileInput.onchange = (event) => {
const file = event.target.files[0]
const fileReader = new FileReader()
fileReader.onload = (e) => {
res(e.target.result)
}
fileReader.onerror = (e) => rej(new Error(e))
fileReader.readAsText(file)
}
fileInput.click()
})
export const prettyObject = (msg) => {
let obj = ''
if (typeof msg !== 'string') {
obj = JSON.stringify(msg, null, ' ')
}
if (obj === '{}') {
return obj.toString()
}
if (obj.startsWith('```json')) {
return obj
}
return ['```json', obj, '```'].join('\n')
}
export const getFileType = (data, fileName) => {
// 根据文件扩展名判断类型
const extension = fileName.split('.').pop().toLowerCase()
switch (extension) {
case 'txt':
return 'text/plain'
case 'json':
return 'application/json'
case 'doc':
return 'application/msword'
case 'docx':
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
case 'xls':
return 'application/vnd.ms-excel'
case 'xlsx':
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
case 'ppt':
return 'application/vnd.ms-powerpoint'
case 'pptx':
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
case 'pdf':
return 'application/pdf'
case 'jpg':
case 'jpeg':
return 'image/jpeg'
case 'png':
return 'image/png'
case 'gif':
return 'image/gif'
case 'zip':
return 'application/zip'
case 'rar':
return 'application/x-rar-compressed'
// 可以继续添加其他类型...
default:
// 如果无法根据扩展名判断,则尝试根据数据内容判断或返回默认类型
if (typeof data === 'string') {
try {
JSON.parse(data)
return 'application/json'
} catch (event) {
console.log('event', event)
return 'text/plain'
}
} else if (data instanceof Uint8Array || data instanceof ArrayBuffer) {
return 'application/octet-stream' // 默认二进制流
} else {
return 'application/octet-stream' // 默认类型,无法确定时使用
}
}
}
// 导出
export const exportFile = (res, fileName) => {
if (res?.headers['content-type']?.indexOf('application/json') !== -1) {
return
}
const disposition = res.headers?.['content-disposition'] || `attachment;filename=${fileName || 'file'}.xlsx`
const disName =
decodeURI(disposition.split('=')[1].replace(/'/g, '').replace(/UTF-8/g, '').replace(/utf-8/g, '')) || ''
const fileType = getFileType(res.data, fileName || disName)
const blob = new Blob([res.data], { type: fileType })
const objectUrl = URL.createObjectURL(blob)
const downloadElement = document.createElement('a')
document.body.appendChild(downloadElement)
downloadElement.style = 'display: none'
downloadElement.href = objectUrl
downloadElement.download = disName
downloadElement.click()
document.body.removeChild(downloadElement)
}
export const getDirection = (ev, obj) => {
const { width: w, height: h, left, top } = obj.getBoundingClientRect()
const x = ev.clientX - left - (w / 2) * (w > h ? h / w : 1)
const y = ev.clientY - top - (h / 2) * (h > w ? w / h : 1)
const d = Math.round(Math.atan2(y, x) / 1.57079633 + 5) % 4
return d
}