小程序请求封装

const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';

const baseURL = 'url';//写上自己的请求基础路径

function request(method, url, data, headerParam) {
  return new Promise(function (resolve, reject) {
    const cookie = wx.getStorageSync('cookie') || ''
    let header = {
      'Content-type': headerParam ? headerParam : 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': cookie
    };
    wx.request({
      url: baseURL + url,
      method: method,
      data: data,
      header: header,
      success(res) {
        if (res.header["Set-Cookie"]) {
          wx.setStorageSync('cookie', res.header["Set-Cookie"]) // 从返回数据的响应头中取cookie
        }
        //请求成功
        // 判断状态码---code状态根据后端定义来判断
        if (res.data.code == 1000) {
          resolve(res);
        } else {
          //其他异常
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          let pages = getCurrentPages()
          let nowPage = pages[0].__route__
          if (res.data.msg === '请登录后再访问!') {
            if(nowPage!=='pages/login/login'){
              setTimeout(() => {
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              }, 2000)
            }
          }
          reject(res);
        }
      },
      fail(err) {
        //请求失败
        reject(err)
      }
    })
  })
}

function requestSpecial(method, url, data, headerParam) {
  return new Promise(function (resolve, reject) {
    const cookie = wx.getStorageSync('cookie') || ''
    let header = {
      'Content-type': headerParam ? headerParam : 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': cookie
    };
    wx.request({
      url: baseURL + url,
      method: method,
      data: data,
      header: header,
      responseType: 'arraybuffer',
      success(res) {
        if (res.header["Set-Cookie"]) {
          wx.setStorageSync('cookie', res.header["Set-Cookie"]) // 从返回数据的响应头中取cookie
        }
        resolve(res)
      },
      fail(err) {
        //请求失败
        reject(err)
      }
    })
  })
}

function params(obj) {
  let result = '';
  let item;
  for (item in obj) {
    if (obj[item] && String(obj[item])) {
      result += `&${item}=${obj[item]}`;
    }
  }
  if (result) {
    result = '?' + result.slice(1);
  }
  return result;
}
const API = {
  get: (url, data, headerParam) => request(GET, `${url}${params(data)}`, headerParam),
  getRESTful: (url, data, headerParam) => request(GET, url, data, headerParam),
  getSpecial: (url, data, headerParam) => requestSpecial(GET, `${url}${params(data)}`, headerParam),
  post: (url, data, headerParam) => request(POST, url, data, headerParam),
  put: (url, data, headerParam) => request(PUT, url, data, headerParam),
};
module.exports = {
  API: API
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值