NetAxiosUtil.ets 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { CommonConstants } from '../constants/CommonConstants'
  2. import { JSON, uri } from '@kit.ArkTS'
  3. import { LogUtil, PreferencesUtil, StrUtil } from '@pura/harmony-utils'
  4. import { http } from '@kit.NetworkKit'
  5. class NetAxiosUtil{
  6. baseUrl:string = 'http://www.baidu.com/'
  7. async getLyric(title:string,artist:string,isApi2:boolean): Promise<string>{
  8. try {
  9. if(StrUtil.isNotEmpty(title)&&title==='全世界最好的你'){
  10. artist = ''
  11. }
  12. let baseUrl = PreferencesUtil.getStringSync('LRC_API','')
  13. if (baseUrl=='') {
  14. LogUtil.debug("Heanup 未设置API");
  15. return ''
  16. }
  17. if(baseUrl.includes(CommonConstants.LRC_API_2)){
  18. isApi2 = true
  19. }
  20. let requestUrl = baseUrl
  21. + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim());
  22. LogUtil.debug("Heanup requestUrl =" + requestUrl + '; title = '+title+'; artist = '+artist)
  23. const httpRequest = http.createHttp();
  24. const options: http.HttpRequestOptions = {
  25. method: http.RequestMethod.GET,
  26. readTimeout: 3000,
  27. connectTimeout: 3000,
  28. };
  29. const response: http.HttpResponse = await httpRequest.request(requestUrl, options);
  30. if(response.responseCode === 200){
  31. let res = response.result as string;
  32. LogUtil.debug("onecold res 11 =" + res)
  33. let fileContent = ''
  34. if(isApi2){
  35. fileContent = res
  36. }else{
  37. let parsedData: lyricInfo[] = JSON.parse(res) as lyricInfo[] ;
  38. fileContent = parsedData[0].lyrics
  39. }
  40. LogUtil.debug("onecold fileContent 11 =" + fileContent)
  41. return fileContent
  42. }
  43. //查询失败
  44. console.log('onecold getLyric--失败',JSON.stringify(response))
  45. } catch (error) {
  46. console.error('onecold getLyric catch--失败'+JSON.stringify(error));
  47. }
  48. return 'unknown' // request timeout
  49. }
  50. async getLyricCover(title: string, artist: string,cover_api?:string): Promise<string> {
  51. try {
  52. let baseUrl = cover_api
  53. LogUtil.debug("onecold getLyricCover baseUrl = "+baseUrl+title+artist);
  54. if(cover_api===undefined&&StrUtil.isEmpty(cover_api)){
  55. LogUtil.debug("onecold getLyricCover baseUrl2 = "+baseUrl);
  56. baseUrl = PreferencesUtil.getStringSync('COVER_API','')
  57. }
  58. LogUtil.debug("onecold getLyricCover baseUrl3 = "+baseUrl);
  59. if ( baseUrl=='') {
  60. LogUtil.debug("onecold Heanup 未设置API");
  61. return ''
  62. }
  63. const cover_url = baseUrl
  64. + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim());
  65. const httpRequest = http.createHttp();
  66. const options: http.HttpRequestOptions = {
  67. method: http.RequestMethod.GET,
  68. readTimeout: 3000,
  69. connectTimeout: 3000,
  70. };
  71. LogUtil.debug("onecold Heanup 请求封面URL: " + cover_url);
  72. const response: http.HttpResponse = await httpRequest.request(cover_url, options);
  73. if (response.responseCode === 200) {
  74. const res = response.result as string;
  75. // LogUtil.debug("onecold Response data: " + res);
  76. const parsedData: CoverInfo = JSON.parse(res) as CoverInfo;
  77. const code = parsedData.code;
  78. if (code === 200) {
  79. const fileContent = parsedData.cover_url;
  80. // LogUtil.debug("onecold Cover URL: " + fileContent);
  81. return fileContent;
  82. } else {
  83. LogUtil.debug("onecoldStatus not success: " + code);
  84. }
  85. } else {
  86. console.error("onecold Request failed with response code: " + response.responseCode);
  87. }
  88. } catch (error) {
  89. console.error('onecold Request failed with error: ' + JSON.stringify(error));
  90. }
  91. return ''; // Return empty string if request fails
  92. }
  93. }
  94. interface lyricInfo{
  95. code:number
  96. album:number
  97. artist:string
  98. lyrics:string
  99. cover_url:string
  100. status:string
  101. }
  102. interface CoverInfo{
  103. code:number
  104. cover_url:string
  105. status:string
  106. }
  107. interface DataInfo{
  108. code:number
  109. data:string
  110. msg:string
  111. update:string
  112. }
  113. export default new NetAxiosUtil();