封装一个网络请求的公共方法要如何实现

封装一个网络请求的公共方法要如何实现


HarmonyOS
2024-07-05 18:27:29
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
怎么不算呢
import { http } from '@kit.NetworkKit';
import ResponseResult from '../../viewmodel/ResponseResult';
import { promptAction } from '@kit.ArkUI';


const HTTP_READ_TIMEOUT = 10000
const HTTP_CODE_200 = 200
const SERVER_CODE_ERROR = 500

/**
 * Initiates an HTTP request to a given URL.
 *
 * @param url URL for initiating an HTTP request.
 * @param params Params for initiating an HTTP request.
 */
export function httpRequestGet(url: string): Promise<ResponseResult> {
  let httpRequest = http.createHttp();
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.GET,
    readTimeout: HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': 'application/json'
    },
    connectTimeout: HTTP_READ_TIMEOUT,
    extraData: {}
  });
  let serverData: ResponseResult = new ResponseResult();
  // Processes the data and returns.
  return responseResult.then((value: http.HttpResponse) => {
    if (value.responseCode === HTTP_CODE_200) {
      // Obtains the returned data.
      let result = `${value.result}`;
      let resultJson: ResponseResult = JSON.parse(result);
      return resultJson
    } else {
      serverData.msg = `网络请求失败,请稍后尝试!${value.responseCode}`;
    }
    return serverData;
  }).catch(() => {
    serverData.msg = '网络请求失败,请稍后尝试!';
      return serverData;
  })
}

export function httpRequestPost(url: string, data?: ESObject): Promise<ResponseResult> {
  let httpRequest = http.createHttp();
  let responseResult = httpRequest.request(url, {
    method: http.RequestMethod.POST,
    readTimeout: HTTP_READ_TIMEOUT,
    header: {
      'Content-Type': 'application/json'
    },
    connectTimeout: HTTP_READ_TIMEOUT,
    extraData: data
  });
  let serverData: ResponseResult = new ResponseResult();
  // Processes the data and returns.
  return responseResult.then((value: http.HttpResponse) => {
    if (value.responseCode === HTTP_CODE_200) {
      // Obtains the returned data.
      let result = `${value.result}`;
      let resultJson: ResponseResult = JSON.parse(result);
      serverData.data = resultJson.data;
      serverData.code = resultJson.code;
      serverData.msg = resultJson.msg;

      if (resultJson.code === SERVER_CODE_ERROR) {
        promptAction.showToast({
          message: serverData.msg
        })
      }
    } else {
      serverData.msg = `网络请求失败,请稍后尝试!${value.responseCode}`;
      promptAction.showToast({
        message: serverData.msg
      })
    }
    return serverData;
  }).catch(() => {
    serverData.msg = '网络请求失败,请稍后尝试!';
    promptAction.showToast({
      message: serverData.msg
    })
    return serverData;
  })
}
分享
微博
QQ
微信
回复
2024-07-06 11:15:33
相关问题
如何实现一个监听网络变化方法
648浏览 • 1回复 待解决
公共样式提取到一个公共文件中
307浏览 • 1回复 待解决
HarmonyOS 封装一个公用getContext()
52浏览 • 1回复 待解决
如何封装一个通用commonEvent工具类
1888浏览 • 1回复 待解决
HarmonyOS 如何取消一个HTTP请求
69浏览 • 1回复 待解决
有没有异步封装网络请求
200浏览 • 1回复 待解决
HarmonyOS 公共组件封装
396浏览 • 1回复 待解决