记录:微信小程序对wx.request请求的简单封装

api.js


const host = "https://www.baidu.com/wx/api";
const api = {
  host: host,
  // 登录接口
  apiWxLogin: `${host}/Auth/WxLoginV2`,
  // 首页
  apiHome: `${host}/home/index`,
  //购物车
  apiGetCartCount: `${host}/Cart/CartNum`,
  // ......

  // 获取购物车数量
  getCartCount() {
    this.http({
      api: 'apiGetCartCount',
      loading: false,
      success: res => {
        if (res.CartNum > 0) {
          wx.setTabBarBadge({
            index: 3,
            text: '' + res.CartNum
          });
        } else {
          wx.removeTabBarBadge({
            index: 3
          });
        }

      }
    })
  },
  toast: function (msg) {
    wx.showToast({
      icon: 'none',
      title: msg,
      duration: msg.length > 10 ? 2500 : 1500
    });
  },
  http: function (option) {
    const config = {//默认参数
      api: '',
      data: {},
      success: null,
      error: null,
      notlogin: null,
      loading: true,
    }
    for (let key in option) {
      config[key] = option[key];
    }
    let url = "";
    if (config.api) {
      url = this[config.api] || "";
    }
    if (!url) {
      return;
    }
    if (config.loading) {
      wx.showLoading({
        title: config.loading === true ? "加载中" : config.loading
      });
    }
    wx.request({
      url: url,
      data: config.data,
      method: "POST",
      header: {
        huaSessionId: wx.getStorageSync("wxUserInfoKey"),
        version: version
      },
      success(response) {
        if (config.loading) {
          wx.hideLoading();
        }
        const res = response.data;
        if (res.Status === "0") {
          //正常
          if (res.DataStatus === 200) {
            if (config.success) {
              config.success(res.Datas);
            }
          } else if (res.DataStatus === 1500) {//对状态码为1500进行处理
          		//进行1500相应处理
          } else if (res.DataStatus === 1501) {//对状态码为1501进行处理
            //授权信息已失效,请重新授权
            api.toast("授权信息已失效,请重新授权~");
            wx.redirectTo({
              url: "/pages/login/index"
            });
          } else if (res.DataStatus === 1502) {
            //未登录,已有授权信息,去绑定手机号
            wx.navigateTo({
              url: "/pages/login/loginbind/index"
            });
          } else {//其他错误码
            if (config.error) {
              config.error(res);
            } else {
              const datas = res.Datas == null ? {} : res.Datas;
              api.toast(res.ErrMsg || datas.Message || "网络错误,请稍后再试(1)~");
            }
          }
        } else {
          if (config.error) {
            config.error(res);
          } else {
            const datas = res.Datas == null ? {} : res.Datas;
            api.toast(res.ErrMsg || datas.Message || "网络错误,请稍后再试(2)~");
          }
        }
      },
      fail: function (err) {
        api.toast("网络错误,请稍后再试~");
        if (config.error) {
          config.error(err);
        }
      },
    })
  },
};
module.exports = api;

page.js

//使用示例伪代码
const api = require("../../utils/api.js");
api.http({
  api:"apiHome",
  data:{},
  success:res => {
    console.log("success!")
  },
  error:err => {
    console.log("error!")
  },
  loading:false
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值