微信小程序封装request请求

微信小程序封装request

1. 首先在微信小程序管理后台中注册域名
2. 方法封装:在utils文件夹下创建uitl.js文件,用于对方法封装;创建api.js文件,用于对第二层封装的Promise;

util.js

const baseUrl = "http://127.0.0.0.1:8888";//请求域名

const $get = async function(url, data, success, fail, power) {
    if (url.indexOf('http') == -1) {
      url =  baseUrl + url
    } 
    var header = {};
    header['content-type'] = "application/x-www-form-urlencoded";
    if (power) {
      var token = wx.getStorageSync('token');
      header['authorization'] = token;
    }
    if (data) {
      data = "?" + qs.stringify(data);
      url += data;
    } 
    wx.request({
      url,
      method: 'GET',
      header,
      success(res) {
        // console.log('原始数据===',res)
        if (res.statusCode == 200) {
          if(res.data.code == 401) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
          success && success(res.data)
        } else {
          fail && fail(res)
        }
      },
      fail(res) {
        fail && fail(res)
      }
    })
}
const $post = function(url, data,success,fail, power, contentType, responseType = false) {
  if (url.indexOf('http') == -1) {
    url =  baseUrl + url
  } 
  var header = {};
  header['content-type'] = "application/x-www-form-urlencoded";
  if (contentType) {
    header['content-type'] = contentType;
  } 
  
  if (power) {
    var token = wx.getStorageSync('token');
    header['authorization'] = token;
  }
  if (data) {
    console.log("data====",data)

  } else {
    data = null;
  }
  wx.request({
    url,
    method: 'POST',
    data,
    header,
    responseType,
    success(res) {
      if (res.statusCode == 200) {
        if(res.data.code == 401) {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        success && success(res.data)
      } else {
        fail && fail(res)
      }
    },
    fail(res) {
      fail && fail(res)
    }
  })
}

api.js

const utils = require('./util.js');

const goods = {
  async get_goods(goods_id) {
    return await new Promise((resolve, reject) => {
      utils.$post("/api/goods/get_goods", {
        goods_id
      }, res => {
        resolve(res)
      }, res => {
        reject(res)
      })
    })
  },
},

调用时,具体哪个页面需要用到,在调用函数:const app = getApp();然后调用api.js里面的函数发起请求。

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.get_good()  

  },

  get_good(){
    app.$api.get_goods().then(res=>{
      console.log(res)
      if(res.code==200){
        console.log(res)
      }else{
       wx.showToast({
         title:res.msg,
         icon:'error'
       })
      }
    }).catch(res=>{
      console.log(res);
      wx.showToast({
        title:res.msg,
        icon:''
    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值