-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathindex.js
More file actions
282 lines (242 loc) · 6.03 KB
/
index.js
File metadata and controls
282 lines (242 loc) · 6.03 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
/**
* API工具函数
* 提供常用的数据处理、验证和转换功能
*/
/**
* Base64编码
* @param {string} str - 需要编码的字符串
* @returns {string} Base64编码结果
*/
export const base64Encode = (str) => {
try {
return btoa(unescape(encodeURIComponent(str)))
} catch (error) {
console.error('Base64编码失败:', error)
return str
}
}
/**
* Base64解码
* @param {string} str - 需要解码的Base64字符串
* @returns {string} 解码结果
*/
export const base64Decode = (str) => {
try {
return decodeURIComponent(escape(atob(str)))
} catch (error) {
console.error('Base64解码失败:', error)
return str
}
}
/**
* 将筛选条件对象转换为Base64编码的JSON字符串
* @param {object} filters - 筛选条件对象
* @returns {string} Base64编码的JSON字符串
*/
export const encodeFilters = (filters) => {
if (!filters || typeof filters !== 'object') {
return ''
}
try {
const jsonStr = JSON.stringify(filters)
return base64Encode(jsonStr)
} catch (error) {
console.error('筛选条件编码失败:', error)
return ''
}
}
/**
* 将Base64编码的JSON字符串解码为筛选条件对象
* @param {string} encodedFilters - Base64编码的JSON字符串
* @returns {object} 筛选条件对象
*/
export const decodeFilters = (encodedFilters) => {
if (!encodedFilters || typeof encodedFilters !== 'string') {
return {}
}
try {
const jsonStr = base64Decode(encodedFilters)
return JSON.parse(jsonStr)
} catch (error) {
console.error('筛选条件解码失败:', error)
return {}
}
}
/**
* 构建查询参数字符串
* @param {object} params - 参数对象
* @returns {string} 查询参数字符串
*/
export const buildQueryString = (params) => {
if (!params || typeof params !== 'object') {
return ''
}
const searchParams = new URLSearchParams()
Object.keys(params).forEach(key => {
const value = params[key]
if (value !== null && value !== undefined && value !== '') {
searchParams.append(key, String(value))
}
})
return searchParams.toString()
}
/**
* 解析查询参数字符串
* @param {string} queryString - 查询参数字符串
* @returns {object} 参数对象
*/
export const parseQueryString = (queryString) => {
if (!queryString || typeof queryString !== 'string') {
return {}
}
const params = {}
const searchParams = new URLSearchParams(queryString)
for (const [key, value] of searchParams) {
params[key] = value
}
return params
}
/**
* 验证模块名称
* @param {string} module - 模块名称
* @returns {boolean} 是否有效
*/
export const validateModule = (module) => {
if (!module || typeof module !== 'string') {
return false
}
// 模块名称不能为空,且长度不能超过100个字符
// 支持字母、数字、中文、下划线、连字符、方括号、圆括号等常见字符
if (module.trim().length === 0 || module.length > 100) {
return false
}
// 不允许包含特殊控制字符
const invalidChars = /[\x00-\x1F\x7F]/
return !invalidChars.test(module)
}
/**
* 验证视频ID
* @param {string} videoId - 视频ID
* @returns {boolean} 是否有效
*/
export const validateVideoId = (videoId) => {
if (!videoId || typeof videoId !== 'string') {
return false
}
const trimmedId = videoId.trim()
// 视频ID不能为空
if (trimmedId.length === 0) {
return false
}
// no_data 被认为是无效的视频ID
if (trimmedId === 'no_data') {
return false
}
// 视频ID长度限制在1024字符内
return videoId.length <= 1024
}
/**
* 验证URL格式
* @param {string} url - URL字符串
* @returns {boolean} 是否有效
*/
export const validateUrl = (url) => {
if (!url || typeof url !== 'string') {
return false
}
try {
new URL(url)
return true
} catch {
return false
}
}
/**
* 格式化错误信息
* @param {Error|string} error - 错误对象或错误信息
* @returns {string} 格式化后的错误信息
*/
export const formatError = (error) => {
if (!error) {
return '未知错误'
}
if (typeof error === 'string') {
return error
}
if (error instanceof Error) {
return error.message || '请求失败'
}
if (error.response && error.response.data) {
const { data } = error.response
return data.msg || data.message || '服务器错误'
}
return '网络请求失败'
}
/**
* 深度合并对象
* @param {object} target - 目标对象
* @param {object} source - 源对象
* @returns {object} 合并后的对象
*/
export const deepMerge = (target, source) => {
if (!target || typeof target !== 'object') {
return source
}
if (!source || typeof source !== 'object') {
return target
}
const result = { ...target }
Object.keys(source).forEach(key => {
if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
result[key] = deepMerge(result[key], source[key])
} else {
result[key] = source[key]
}
})
return result
}
/**
* 防抖函数
* @param {Function} func - 需要防抖的函数
* @param {number} delay - 延迟时间(毫秒)
* @returns {Function} 防抖后的函数
*/
export const debounce = (func, delay) => {
let timeoutId
return function (...args) {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => func.apply(this, args), delay)
}
}
/**
* 节流函数
* @param {Function} func - 需要节流的函数
* @param {number} delay - 延迟时间(毫秒)
* @returns {Function} 节流后的函数
*/
export const throttle = (func, delay) => {
let lastCall = 0
return function (...args) {
const now = Date.now()
if (now - lastCall >= delay) {
lastCall = now
return func.apply(this, args)
}
}
}
// 默认导出所有工具函数
export default {
base64Encode,
base64Decode,
encodeFilters,
decodeFilters,
buildQueryString,
parseQueryString,
validateModule,
validateVideoId,
validateUrl,
formatError,
deepMerge,
debounce,
throttle
}