微信小程序封装请求

本文详细介绍了如何在微信小程序中封装网络请求和文件上传功能,包括设置公共参数、错误处理以及不同HTTP方法的实现。同时,展示了如何使用wx.request进行数据交互,并对上传文件进行了特别处理,确保了请求头和数据的正确性。
摘要由CSDN通过智能技术生成

微信小程序封装请求

请求中可以做一些操作

import {
  BASE_URL,
  STORAGE_USER,
  STORAGE_HEADER
} from '../utils/constant.js' // 需要存的公共数据,都放在这个文件,包括环境


const http = ({
  url = '',
  param,
  ...other
} = {}) => {
  wx.showLoading({
    title: '数据请求中'
  });

  let timeStart = Date.now();
  const data = {
    // 在这里面我们可以放入公共的参数后面加的是引用时加入的参数
    channelNo: 'xcx',
    ...param,
  }
  let common_header = {
    token: wx.getStorageSync(STORAGE_HEADER)
  }
  return new Promise((resolve, reject) => {
    wx.request({
      url: getUrl(url),
      data: data || {},
      header: {
        // "content-type": "application/x-www-form-urlencoded"
        'content-type': 'application/json',
        ...common_header
      },
      ...other,
      complete: (res) => {
        wx.hideLoading();
        if (res.statusCode = 200) {
          if (res.data.code == 200) {
            resolve(res.data)
          } else if (res.data.code == 500) {
            Dialog.alert({
              message: res.data.msg,
              title: '提示',
              confirmButtonText: '确定',
            }).then(() => {
            })
          } else if (res.data.code == 999999) {
            Dialog.alert({
              message: res.data.msg,
              title: '提示',
              confirmButtonText: '确定',
            }).then(() => {
            })
          } else if (res.data.code == 300) {
            wx.reLaunch({
              url: '/pages/login/login',
            })
            setTimeout(() => {
              Dialog.alert({
                message: res.data.msg + ',请重新登录',
                title: '提示',
                confirmButtonText: '确定',
              }).then(() => {
              })
            }, 2000)
          }
        } else if (res.statusCode == 404) {
          Dialog.alert({
            message: '服务异常,请稍后重试',
            title: '提示',
            confirmButtonText: '确定',
          }).then(() => {
          })
        } else {
          reject(res)
        }
      }
    })
  })
}

const uploadFile = function (params = {}) {
  let {
    complete,
    success,
    failure,
    data = {},
    url,
    filePath,
    name
  } = params, noop = function () {};

  if (!filePath) return console.log('filePath不能为空')

  if (!url) return console.log('url不能为空')

  if (!name) return console.log('name不能为空')

  if (!complete || typeof complete != 'function') complete = noop

  if (!success || typeof success != 'function') success = noop

  if (!failure || typeof failure != 'function') failure = noop

  const callback = (res) => {
    complete(res)
  }

  const uploadTask = wx.uploadFile({
    url: BASE_URL + url,
    filePath,
    name,
    header: wx.getStorageSync(STORAGE_HEADER),
    formData: Object.assign({}, data, common_header),
    success: success,
    failure: failure
  })

  uploadTask.onProgressUpdate(callback)
}

const getUrl = (url) => {
  if (url.indexOf('://') == -1) {
    url = BASE_URL + url;
  }
  return url
}

// get方法
const _get = (url, param = {}) => {
  return http({
    url,
    param
  })
}

const _post = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'post'
  })
}

const _put = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'put'
  })
}

const _delete = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'put'
  })
}
module.exports = {
  _get,
  _post,
  _put,
  _delete,
  uploadFile
}

封装的方法有这些,当做收藏,嗯!!!开始下一个,工具类!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值