微信小程序项目request请求优化



这是微信小程序官网API列表里面 wx.request(OBJECT)

官方示例

wx.request({
  url: 'xxxx.php',
  data: {
     x: '' ,
     y: ''
  },
  method:"POST",
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})

参数说明

参数名类型必填默认值说明
urlString开发者服务器接口地址
dataObject/String/ArrayBuffer请求的参数
headerObject设置请求的 header,header 中不能设置 Referer。
methodStringGET有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
dataTypeStringjson如果设为json,会尝试对返回的数据做一次 JSON.parse
responseTypeStringtext设置响应的数据类型。合法值:text、arraybuffer
successFunction收到开发者服务成功返回的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

优雅的封装

我写在了page的同级目录utils下的utils.js

const requestData = {
  /**
   * url = String 请求地址
   * obj = Object 请求参数
   * success = function 成功回调
   * fail = function 成功回调
   */
  get: (url, data, success, fail) => {
    if ((typeof data) == 'function') {
      if (success && (typeof success) == 'function') {
        fail = success;
      }
      success = data;
      data = '';
      console.log(success, data, fail)
    }
    wx.request({
      url: url,
      data: data || '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success(res) {
        if (success) {
          success(res);
        }
      },
      fail(res) {
        if (fail) {
          fail(res);
        }
        wx.hideLoading();
        wx.showToast({
          title: '请求超时',
          icon: 'loading',
          duration: 2000
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  post: (url, data, success, fail) => {
    if ((typeof data) == 'function') {
      if (success && (typeof success) == 'function') {
        fail = success;
      }
      success = data;
      data = '';
      console.log(success, data, fail)
    }
    wx.request({
      url: baseUrl + url,
      data: data || '',
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success(res) {
        if (success) {
          success(res);
        }
      },
      fail(res) {
        if (fail) {
          fail(res);
        }
      }
    })
  },
  del: (url, data, success, fail) => {
    if ((typeof data) == 'function') {
      if (success && (typeof success) == 'function') {
        fail = success;
      }
      success = data;
      data = '';
      console.log(success, data, fail)
    }
    wx.request({
      url: baseUrl + url,
      data: data || '',
      header: {},
      method: 'DELETE',
      dataType: 'json',
      responseType: 'text',
      success(res) {
        if (success) {
          success(res);
        }
      },
      fail(res) {
        if (fail) {
          fail(res);
        }
      }
    })
  },
  put: (url, data, success, fail) => {
    if ((typeof data) == 'function') {
      if (success && (typeof success) == 'function') {
        fail = success;
      }
      success = data;
      data = '';
      console.log(success, data, fail)
    }
    wx.request({
      url: baseUrl + url,
      data: data || '',
      header: {},
      method: 'PUT',
      dataType: 'json',
      responseType: 'text',
      success(res) {
        if (success) {
          success(res);
        }
      },
      fail(res) {
        if (fail) {
          fail(res);
        }
      }
    })
  }
}


module.exports = {
  requestData: requestData
}

封装之后的使用

在js的代码示例

// 一定记得先引入
const {requestData} = require('../../utils/util');

requestData.get('你的请求URL', res => {
  if (res.statusCode == 200) {
    console.log(res)
    this.setData({
      hotList: res.data.body
    });
  } else {
    console.log(res.errMsg)
  }
}, err => {
  console.log(err)
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值