Files
AlgerMusicPlayer/src/renderer/api/parseFromCustomApi.ts

108 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-09-09 22:05:48 +08:00
import axios from 'axios';
import { get } from 'lodash';
2025-09-09 22:05:48 +08:00
import { useSettingsStore } from '@/store';
import type { ParsedMusicResult } from './gdmusic';
2025-09-09 22:05:48 +08:00
/**
* API JSON插件的结构
*/
interface CustomApiPlugin {
name: string;
apiUrl: string;
method?: 'GET' | 'POST';
params: Record<string, string>;
qualityMapping?: Record<string, string>;
responseUrlPath: string;
2025-09-09 22:05:48 +08:00
}
/**
* API JSON配置中解析音乐URL
*/
export const parseFromCustomApi = async (
id: number,
_songData: any,
quality: string = 'higher',
timeout: number = 10000
2025-09-09 22:05:48 +08:00
): Promise<ParsedMusicResult | null> => {
const settingsStore = useSettingsStore();
const pluginString = settingsStore.setData.customApiPlugin;
2025-09-09 22:05:48 +08:00
if (!pluginString) {
return null;
}
2025-09-09 22:05:48 +08:00
let plugin: CustomApiPlugin;
try {
plugin = JSON.parse(pluginString);
if (!plugin.apiUrl || !plugin.params || !plugin.responseUrlPath) {
console.error('自定义APIJSON配置文件格式不正确。');
return null;
2025-09-09 22:05:48 +08:00
}
} catch (error) {
console.error('自定义API解析JSON配置文件失败。', error);
return null;
}
2025-09-09 22:05:48 +08:00
console.log(`自定义API正在使用插件 [${plugin.name}] 进行解析...`);
2025-09-09 22:05:48 +08:00
try {
// 1. 准备请求参数,替换占位符
const finalParams: Record<string, string> = {};
for (const [key, value] of Object.entries(plugin.params)) {
if (value === '{songId}') {
finalParams[key] = String(id);
} else if (value === '{quality}') {
// 使用 qualityMapping (如果存在) 进行音质翻译否则直接使用原quality
finalParams[key] = plugin.qualityMapping?.[quality] ?? quality;
} else {
// 固定值参数
finalParams[key] = value;
}
}
2025-09-09 22:05:48 +08:00
// 2. 判断请求方法默认为GET
const method = plugin.method?.toUpperCase() === 'POST' ? 'POST' : 'GET';
let response;
2025-09-09 22:05:48 +08:00
// 3. 根据方法发送不同的请求
if (method === 'POST') {
console.log('自定义API发送 POST 请求到:', plugin.apiUrl, '参数:', finalParams);
response = await axios.post(plugin.apiUrl, finalParams, { timeout });
} else {
// 默认为 GET
const finalUrl = `${plugin.apiUrl}?${new URLSearchParams(finalParams).toString()}`;
console.log('自定义API发送 GET 请求到:', finalUrl);
response = await axios.get(finalUrl, { timeout });
}
2025-09-09 22:05:48 +08:00
// 4. 使用 lodash.get 安全地从响应数据中提取URL
const musicUrl = get(response.data, plugin.responseUrlPath);
2025-09-09 22:05:48 +08:00
if (musicUrl && typeof musicUrl === 'string') {
console.log('自定义API成功获取URL');
// 5. 组装成应用所需的标准格式并返回
return {
data: {
data: {
url: musicUrl,
br: parseInt(quality) * 1000,
size: 0,
md5: '',
platform: plugin.name.toLowerCase().replace(/\s/g, ''),
gain: 0
},
params: { id, type: 'song' }
2025-09-09 22:05:48 +08:00
}
};
} else {
console.error('自定义API根据路径未能从响应中找到URL:', plugin.responseUrlPath);
return null;
2025-09-09 22:05:48 +08:00
}
} catch (error) {
console.error(`自定义API [${plugin.name}] 执行失败:`, error);
return null;
}
};