starmusic-pc
1/**
2 * 音源切换(迁移自 lx-music-mobile/src/core/apiSource.ts)
3 */
4import { updateSetting, getSetting } from './setting.js'
5import event from './event.js'
6import { loadUserApi, destroyUserApi, getLxGlobal } from './user-api/api-executor.js'
7
8/**
9 * 设置音源
10 * @param {string} apiId 内置源 id('' 或 'kw')或自定义源 id(user_api_xxx)
11 */
12export const setApiSource = (apiId) => {
13 // 切换自定义源
14 if (/^user_api/.test(apiId)) {
15 loadUserApi(apiId).catch((err) => {
16 console.log('[setApiSource] load failed', err)
17 // 加载失败回退到内置源(基于当前设置判断,避免快照过期)
18 if (getSetting()['common.apiSource'] === apiId) {
19 setApiSource('')
20 }
21 })
22 } else {
23 // 内置源:销毁自定义源
24 getLxGlobal().qualityList = {}
25 destroyUserApi()
26 }
27
28 if (apiId != getSetting()['common.apiSource']) {
29 updateSetting({ 'common.apiSource': apiId })
30 event.emit('apiSourceUpdated', apiId)
31 }
32}
33
34/** 初始化音源:根据设置恢复 */
35export const initApiSource = () => {
36 const setting = getSetting()
37 const apiId = setting['common.apiSource']
38 if (apiId && /^user_api/.test(apiId)) {
39 setApiSource(apiId)
40 }
41}