import { CommonConstants } from '../constants/CommonConstants' import { JSON, uri } from '@kit.ArkTS' import { LogUtil, PreferencesUtil, StrUtil } from '@pura/harmony-utils' import { http } from '@kit.NetworkKit' class NetAxiosUtil{ baseUrl:string = 'http://www.baidu.com/' async getLyric(title:string,artist:string,isApi2:boolean): Promise{ try { if(StrUtil.isNotEmpty(title)&&title==='全世界最好的你'){ artist = '' } let baseUrl = PreferencesUtil.getStringSync('LRC_API','') if (baseUrl=='') { LogUtil.debug("Heanup 未设置API"); return '' } if(baseUrl.includes(CommonConstants.LRC_API_2)){ isApi2 = true } let requestUrl = baseUrl + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim()); // LogUtil.debug("Heanup requestUrl =" + requestUrl + '; title = '+title+'; artist = '+artist) const httpRequest = http.createHttp(); const options: http.HttpRequestOptions = { method: http.RequestMethod.GET, readTimeout: 3000, connectTimeout: 3000, }; const response: http.HttpResponse = await httpRequest.request(requestUrl, options); if(response.responseCode === 200){ let res = response.result as string; // LogUtil.debug("onecold res 11 =" + res) let fileContent = '' if(isApi2){ fileContent = res }else{ let parsedData: lyricInfo[] = JSON.parse(res) as lyricInfo[] ; fileContent = parsedData[0].lyrics } LogUtil.debug("onecold fileContent 11 =" + fileContent) return fileContent } //查询失败 console.log('onecold getLyric--失败',JSON.stringify(response)) } catch (error) { console.error('onecold getLyric catch--失败'+JSON.stringify(error)); } return 'unknown' // request timeout } async getLyricCover(title: string, artist: string,cover_api?:string): Promise { try { let baseUrl = cover_api LogUtil.debug("onecold getLyricCover baseUrl = "+baseUrl+title+artist); if(cover_api==undefined&&StrUtil.isEmpty(cover_api)){ // LogUtil.debug("onecold getLyricCover baseUrl2 = "+baseUrl); // baseUrl = PreferencesUtil.getStringSync('COVER_API','') baseUrl = '' } LogUtil.debug("onecold getLyricCover baseUrl3 = "+baseUrl); if ( baseUrl=='') { LogUtil.debug("onecold Heanup 未设置API"); return '' } if(baseUrl ===CommonConstants.COVER_API2){ baseUrl = CommonConstants.COVER_API } const cover_url = baseUrl + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim()); const httpRequest = http.createHttp(); const options: http.HttpRequestOptions = { method: http.RequestMethod.GET, readTimeout: 3000, connectTimeout: 3000, }; // LogUtil.debug("onecold Heanup 请求封面URL: " + cover_url); const response: http.HttpResponse = await httpRequest.request(cover_url, options); if (response.responseCode === 200) { const res = response.result as string; // LogUtil.debug("onecold Response data: " + res); const parsedData: CoverInfo = JSON.parse(res) as CoverInfo; const code = parsedData.code; if (code === 200) { const fileContent = parsedData.cover_url; // LogUtil.debug("onecold Cover URL: " + fileContent); return fileContent; } else { LogUtil.debug("onecoldStatus not success: " + code); } } else { console.error("onecold Request failed with response code: " + response.responseCode); } } catch (error) { console.error('onecold Request failed with error: ' + JSON.stringify(error)); } return ''; // Return empty string if request fails } } interface lyricInfo{ code:number album:number artist:string lyrics:string cover_url:string status:string } interface CoverInfo{ code:number cover_url:string status:string } interface DataInfo{ code:number data:string msg:string update:string } export default new NetAxiosUtil();