use tauri::State; use tauri_helper::auto_collect_command; use crate::models::METING_API_URL; use crate::{ models::ApiResponse, services::http::{ApiEndpoint, HttpClient}, }; // 代理请求并下载封面图 #[tauri::command] #[auto_collect_command] pub async fn music_cover( http: State<'_, HttpClient>, url: String, artist: String, ) -> Result, String> { let params: Vec<(String, String)> = vec![("url".to_string(), url), ("artist".to_string(), artist)]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/cover", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 探测音频大小与码率 #[tauri::command] #[auto_collect_command] pub async fn music_inspect( http: State<'_, HttpClient>, id: String, source: String, duration: u32, ) -> Result, String> { let params = vec![ ("id".to_string(), id), ("source".to_string(), source), ("duration".to_string(), duration.to_string()), ]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/inspect", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 获取 JSON 格式歌词 #[tauri::command] #[auto_collect_command] pub async fn music_lyric( http: State<'_, HttpClient>, id: String, source: String, ) -> Result, String> { let params = vec![("id".to_string(), id), ("source".to_string(), source)]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/lyric", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 下载 LRC 歌词文件 #[tauri::command] #[auto_collect_command] pub async fn music_lrc_file( http: State<'_, HttpClient>, id: String, source: String, name: String, artist: String, ) -> Result, String> { let params = vec![ ("id".to_string(), id), ("source".to_string(), source), ("name".to_string(), name), ("artist".to_string(), artist), ]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/lyric/file", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 综合搜索与链接解析 #[tauri::command] #[auto_collect_command] pub async fn music_search( http: State<'_, HttpClient>, keyword: String, type_: String, sources: Vec, ) -> Result, String> { let mut params = vec![("q".to_string(), keyword), ("type".to_string(), type_)]; // 追加动态 sources 参数 for source in sources { params.push(("source".to_string(), source)); } let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/search", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 串流代理与下载音频 #[tauri::command] #[auto_collect_command] pub async fn music_stream( http: State<'_, HttpClient>, id: String, source: String, name: String, artist: String, ) -> Result, String> { let params = vec![ ("id".to_string(), id), ("source".to_string(), source), ("name".to_string(), name), ("artist".to_string(), artist), ]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/stream", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 智能切换可用的平替音源 pub async fn music_switch( http: State<'_, HttpClient>, name: String, artist: String, source: String, target: String, duration: u32, ) -> Result, String> { let params = vec![ ("name".to_string(), name), ("artist".to_string(), artist), ("source".to_string(), source), ("target".to_string(), target), ("duration".to_string(), duration.to_string()), ]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/switch", ¶ms) .await .map_err(|e| e.to_string()); Ok(ApiResponse::success(data?)) } // 获取音频裸直链 #[tauri::command] #[auto_collect_command] pub async fn music_url( http: State<'_, HttpClient>, id: String, source: String, ) -> Result, String> { let params = vec![("id".to_string(), id), ("source".to_string(), source)]; let data = http .get_at(ApiEndpoint::GoMusic, "/api/v1/music/url", ¶ms) .await .map_err(|e| e.to_string())?; Ok(ApiResponse::success(data)) } // #[tauri::command] // #[auto_collect_command] // pub async fn music_url( // id: String, // server: String, // type_: String, // ) -> Result, String> { // let client = reqwest::Client::new(); // let params = vec![ // ("id".to_string(), id), // ("server".to_string(), server), // ("type".to_string(), type_), // ]; // let resp = client // .get(format!("{}/api", METING_API_URL)) // .query(¶ms) // .send() // .await // .map_err(|e| e.to_string())?; // let data = resp.json().await.map_err(|e| e.to_string())?; // Ok(ApiResponse::success(data)) // }