NetAxiosUtil.ets 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. baseUrl = ''
  58. }
  59. LogUtil.debug("onecold getLyricCover baseUrl3 = "+baseUrl);
  60. if ( baseUrl=='') {
  61. LogUtil.debug("onecold Heanup 未设置API");
  62. return ''
  63. }
  64. if(baseUrl ===CommonConstants.COVER_API2){
  65. baseUrl = CommonConstants.COVER_API
  66. }
  67. const cover_url = baseUrl
  68. + '?title=' + encodeURIComponent(title.trim()) + '&artist=' + encodeURIComponent(artist.trim());
  69. const httpRequest = http.createHttp();
  70. const options: http.HttpRequestOptions = {
  71. method: http.RequestMethod.GET,
  72. readTimeout: 3000,
  73. connectTimeout: 3000,
  74. };
  75. // LogUtil.debug("onecold Heanup 请求封面URL: " + cover_url);
  76. const response: http.HttpResponse = await httpRequest.request(cover_url, options);
  77. if (response.responseCode === 200) {
  78. const res = response.result as string;
  79. // LogUtil.debug("onecold Response data: " + res);
  80. const parsedData: CoverInfo = JSON.parse(res) as CoverInfo;
  81. const code = parsedData.code;
  82. if (code === 200) {
  83. const fileContent = parsedData.cover_url;
  84. // LogUtil.debug("onecold Cover URL: " + fileContent);
  85. return fileContent;
  86. } else {
  87. LogUtil.debug("onecoldStatus not success: " + code);
  88. }
  89. } else {
  90. console.error("onecold Request failed with response code: " + response.responseCode);
  91. }
  92. } catch (error) {
  93. console.error('onecold Request failed with error: ' + JSON.stringify(error));
  94. }
  95. return ''; // Return empty string if request fails
  96. }
  97. }
  98. interface lyricInfo{
  99. code:number
  100. album:number
  101. artist:string
  102. lyrics:string
  103. cover_url:string
  104. status:string
  105. }
  106. interface CoverInfo{
  107. code:number
  108. cover_url:string
  109. status:string
  110. }
  111. interface DataInfo{
  112. code:number
  113. data:string
  114. msg:string
  115. update:string
  116. }
  117. export default new NetAxiosUtil();