-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathconfig.js
More file actions
415 lines (360 loc) · 10.9 KB
/
config.js
File metadata and controls
415 lines (360 loc) · 10.9 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
/**
* 配置管理服务
* 负责管理应用配置,包括配置地址、站点数据等
* 支持localStorage持久化存储
*/
import axios from 'axios'
/**
* 配置管理类
*/
class ConfigService {
constructor() {
this.configUrl = null
this.configData = null
this.lastFetchTime = null
this.cacheExpiry = 5 * 60 * 1000 // 5分钟缓存
this.liveConfigUrl = null // 直播配置地址
this.loadConfigFromStorage()
}
/**
* 设置配置地址
* @param {string} url - 配置地址URL
* @returns {Promise<boolean>} 设置是否成功
*/
async setConfigUrl(url) {
try {
if (!url || typeof url !== 'string') {
throw new Error('配置地址不能为空')
}
// 验证URL格式
const urlPattern = /^https?:\/\/.+/
if (!urlPattern.test(url)) {
throw new Error('配置地址格式不正确,请输入有效的HTTP/HTTPS地址')
}
// 测试配置地址是否可访问
const isValid = await this.validateConfigUrl(url)
if (!isValid) {
throw new Error('配置地址无法访问或数据格式不正确')
}
this.configUrl = url
this.saveConfigToStorage()
// 自动设置直播配置地址(如果未设置)
await this.autoSetLiveConfigUrl()
console.log('配置地址设置成功:', url)
return true
} catch (error) {
console.error('设置配置地址失败:', error)
throw error
}
}
/**
* 设置直播配置地址
* @param {string} url - 直播配置地址URL
* @returns {Promise<boolean>} 设置是否成功
*/
async setLiveConfigUrl(url) {
try {
if (!url || typeof url !== 'string') {
throw new Error('直播配置地址不能为空')
}
// 验证URL格式
const urlPattern = /^https?:\/\/.+/
if (!urlPattern.test(url)) {
throw new Error('直播配置地址格式不正确,请输入有效的HTTP/HTTPS地址')
}
this.liveConfigUrl = url
this.saveConfigToStorage()
console.log('直播配置地址设置成功:', url)
return true
} catch (error) {
console.error('设置直播配置地址失败:', error)
throw error
}
}
/**
* 获取直播配置地址
* @returns {string|null} 直播配置地址
*/
getLiveConfigUrl() {
return this.liveConfigUrl
}
/**
* 自动设置直播配置地址(从点播配置中提取lives链接)
* @returns {Promise<boolean>} 是否成功设置
*/
async autoSetLiveConfigUrl() {
try {
// 如果已经设置了直播配置地址,则不自动设置
if (this.liveConfigUrl) {
return false
}
// 获取点播配置数据
const configData = await this.getConfigData()
if (!configData || !configData.lives || !Array.isArray(configData.lives) || configData.lives.length === 0) {
console.log('点播配置中未找到lives链接')
return false
}
// 取lives数组中第一个对象的url字段
const firstLiveConfig = configData.lives[0]
if (!firstLiveConfig || !firstLiveConfig.url) {
console.log('lives配置中未找到有效的url')
return false
}
// 设置直播配置地址
this.liveConfigUrl = firstLiveConfig.url
this.saveConfigToStorage()
console.log('自动设置直播配置地址成功:', firstLiveConfig.url)
return true
} catch (error) {
console.error('自动设置直播配置地址失败:', error)
return false
}
}
/**
* 重置直播配置地址(重置为当前点播配置中的默认lives链接)
* @returns {Promise<boolean>} 是否成功重置
*/
async resetLiveConfigUrl() {
try {
// 获取点播配置数据
const configData = await this.getConfigData()
if (!configData || !configData.lives || !Array.isArray(configData.lives) || configData.lives.length === 0) {
console.log('点播配置中未找到lives链接,无法重置')
return false
}
// 取lives数组中第一个对象的url字段
const firstLiveConfig = configData.lives[0]
if (!firstLiveConfig || !firstLiveConfig.url) {
console.log('lives配置中未找到有效的url,无法重置')
return false
}
// 重置直播配置地址
this.liveConfigUrl = firstLiveConfig.url
this.saveConfigToStorage()
console.log('直播配置地址重置成功:', firstLiveConfig.url)
return true
} catch (error) {
console.error('重置直播配置地址失败:', error)
return false
}
}
/**
* 获取当前配置地址
* @returns {string|null} 当前配置地址
*/
getConfigUrl() {
return this.configUrl
}
/**
* 验证配置地址是否有效
* @param {string} url - 配置地址
* @returns {Promise<boolean>} 是否有效
*/
async validateConfigUrl(url) {
try {
const response = await axios.get(url, {
timeout: 10000,
headers: {
'Accept': 'application/json'
}
})
if (!response.data) {
return false
}
// 验证数据格式是否包含必要字段
const data = response.data
if (!data.sites || !Array.isArray(data.sites)) {
return false
}
// 验证sites数组中的数据格式
if (data.sites.length > 0) {
const firstSite = data.sites[0]
if (!firstSite.key || !firstSite.name || !firstSite.api) {
return false
}
}
return true
} catch (error) {
console.error('验证配置地址失败:', error)
return false
}
}
/**
* 获取配置数据
* @param {boolean} forceRefresh - 是否强制刷新
* @returns {Promise<object>} 配置数据
*/
async getConfigData(forceRefresh = false) {
try {
if (!this.configUrl) {
throw new Error('未设置配置地址')
}
// 检查缓存是否有效
const now = Date.now()
const isCacheValid = this.configData &&
this.lastFetchTime &&
(now - this.lastFetchTime) < this.cacheExpiry
if (!forceRefresh && isCacheValid) {
console.log('使用缓存的配置数据')
return this.configData
}
console.log('从配置地址获取数据:', this.configUrl)
const response = await axios.get(this.configUrl, {
timeout: 15000,
headers: {
'Accept': 'application/json'
}
})
if (!response.data) {
throw new Error('配置数据为空')
}
this.configData = response.data
this.lastFetchTime = now
this.saveConfigToStorage()
console.log('配置数据获取成功,站点数量:', this.configData.sites?.length || 0)
return this.configData
} catch (error) {
console.error('获取配置数据失败:', error)
throw error
}
}
/**
* 获取站点列表
* @param {boolean} forceRefresh - 是否强制刷新
* @returns {Promise<Array>} 站点列表
*/
async getSites(forceRefresh = false) {
try {
const configData = await this.getConfigData(forceRefresh)
return configData.sites || []
} catch (error) {
console.error('获取站点列表失败:', error)
throw error
}
}
/**
* 获取推荐配置
* @returns {Promise<Array>} 推荐配置列表
*/
async getRecommendConfig() {
try {
const configData = await this.getConfigData()
return configData.recommend || []
} catch (error) {
console.error('获取推荐配置失败:', error)
return []
}
}
/**
* 获取其他配置信息
* @returns {Promise<object>} 其他配置信息
*/
async getOtherConfig() {
try {
const configData = await this.getConfigData()
const { sites, ...otherConfig } = configData
return otherConfig
} catch (error) {
console.error('获取其他配置失败:', error)
return {}
}
}
/**
* 清除配置缓存
*/
clearCache() {
this.configData = null
this.lastFetchTime = null
console.log('配置缓存已清除')
}
/**
* 重置配置
*/
resetConfig() {
this.configUrl = null
this.configData = null
this.lastFetchTime = null
this.liveConfigUrl = null
this.saveConfigToStorage()
console.log('配置已重置')
}
/**
* 从本地存储加载配置
*/
loadConfigFromStorage() {
try {
const configUrl = localStorage.getItem('drplayer_config_url')
const configData = localStorage.getItem('drplayer_config_data')
const lastFetchTime = localStorage.getItem('drplayer_config_fetch_time')
const liveConfigUrl = localStorage.getItem('drplayer_live_config_url')
if (configUrl) {
this.configUrl = configUrl
}
if (configData) {
this.configData = JSON.parse(configData)
}
if (lastFetchTime) {
this.lastFetchTime = parseInt(lastFetchTime)
}
if (liveConfigUrl) {
this.liveConfigUrl = liveConfigUrl
}
console.log('从本地存储加载配置成功')
} catch (error) {
console.error('加载配置失败:', error)
}
}
/**
* 保存配置到本地存储
*/
saveConfigToStorage() {
try {
if (this.configUrl) {
localStorage.setItem('drplayer_config_url', this.configUrl)
} else {
localStorage.removeItem('drplayer_config_url')
}
if (this.configData) {
localStorage.setItem('drplayer_config_data', JSON.stringify(this.configData))
} else {
localStorage.removeItem('drplayer_config_data')
}
if (this.lastFetchTime) {
localStorage.setItem('drplayer_config_fetch_time', this.lastFetchTime.toString())
} else {
localStorage.removeItem('drplayer_config_fetch_time')
}
if (this.liveConfigUrl) {
localStorage.setItem('drplayer_live_config_url', this.liveConfigUrl)
} else {
localStorage.removeItem('drplayer_live_config_url')
}
console.log('配置保存到本地存储成功')
} catch (error) {
console.error('保存配置失败:', error)
}
}
/**
* 获取配置状态信息
* @returns {object} 配置状态
*/
getConfigStatus() {
const hasValidData = this.configData && this.configData.sites && Array.isArray(this.configData.sites)
const sitesCount = hasValidData ? this.configData.sites.length : 0
return {
hasConfigUrl: !!this.configUrl,
hasConfigData: !!this.configData,
isValid: hasValidData,
sitesCount: sitesCount,
lastFetchTime: this.lastFetchTime,
cacheAge: this.lastFetchTime ? Date.now() - this.lastFetchTime : null,
isCacheValid: this.configData && this.lastFetchTime &&
(Date.now() - this.lastFetchTime) < this.cacheExpiry,
hasLiveConfigUrl: !!this.liveConfigUrl,
liveConfigUrl: this.liveConfigUrl
}
}
}
// 创建单例实例
const configService = new ConfigService()
export default configService