当前位置:   article > 正文

【HarmonyOS】鸿蒙开发之文件操作与网络请求-HTTP网络请求——第5.1章_harmonyos http code 0

harmonyos http code 0

HTTP网络请求封装

network/request.ets

import { configInterface } from './type'
import http from '@ohos.net.http'
import { getToken } from '../utils/storage'

//网络请求封装
export const request = (config:configInterface)=>{
  let httpRequest:http.HttpRequest = http.createHttp()
  let method:http.RequestMethod = config.method.toLowerCase()=='get'?http.RequestMethod.GET:http.RequestMethod.POST
  let header = {}
  let Token = getToken()
  if(config.headers){
    header={
      ...config.headers,
      'X-CSRF-TOKEN': `VueCms_xg ${Token}`,
      'Authorization': `Bearer vuecms.cn`,
    }
  }else if(!config.headers){
    config.headers={
      "Content-Type": "application/json"
    }
  }
  console.log('http://localhost:3000' + config.url,"32333333333");
  let response = httpRequest.request(
    // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
    // config.url,
    "http://localhost:3000"+config.url,
    {
      method, // 可选,默认为http.RequestMethod.GET
      // 开发者根据自身业务需要添加header字段
      header,
      extraData:{...config.data},
      expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
    });
  return response.then((res:any)=>{
    let resultData:any = {}
    // 取消订阅HTTP响应头事件
    httpRequest.off('headersReceive');
    // 当该请求使用完毕时,调用destroy方法主动销毁
    httpRequest.destroy();

    let result:any = JSON.parse(res.result);
    console.log(result.code);
    if (result.code === 403) {
      console.log("登录状态已过期,您可以继续留在该页面,或者重新登录");
      // model.handleMsgBox('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
      //   confirmButtonText: '重新登录',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   userStore.LogOut().then(() => {
      //     location.href = "/login";
      //   })
      // })
    }
    resultData = {
      code:result.code,
      data:result.data,
      message:result.message,
    }
    return Promise.resolve(resultData)
  }).catch(err=>{
    console.log(JSON.stringify(err),"errrrrr111rrr");
    // 取消订阅HTTP响应头事件
    httpRequest.off('headersReceive');
    // 当该请求使用完毕时,调用destroy方法主动销毁。
    httpRequest.destroy();
    return new Promise((resolve,reject)=>{
      let res = {
        code:err.code,
        data:"",
        message:err.message,
      }
      reject(res)
    })
  })
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

network/type/index.ts 网络请求文件的typescript文件

export type methodsData = "post" | "get"
interface downloadParamsInterface{
  url: string
  params?: any
  filename: string
  isPost?: boolean
}
export interface configInterface{
  url: string
  data?: any
  method?: methodsData
  headers?: any
  downloadData?:downloadParamsInterface
}
export interface responseInterface {
  data:any
  message:string
  code:Number
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

实战项目使用

登录页的网络请求文件
ets/network/login/index.ts
在这里插入图片描述

登录页使用
ets/pages/login/index.ets
在这里插入图片描述

注意:

  1. 浏览器暂不支持网络请求,只能在模拟器或真机进行
  2. 请求需要申请ohos.permission.INTERNET 权限
    在这里插入图片描述
  3. 网络请求限定并发个数为100,超过这一限制的后续请求会失败。
  4. 默认支持https,如果要支持http,需要在module.json5里添加network标签
    在这里插入图片描述

踩坑不易,还希望各位大佬支持一下 \textcolor{gray}{踩坑不易,还希望各位大佬支持一下} 踩坑不易,还希望各位大佬支持一下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/225587
推荐阅读
相关标签