| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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<string>{
- 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<string> {
- 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();
|