小程序网络请求封装(三)

function myRequest(url, param, callback) {
  wx.showLoading();
  wx.request({
    url: "https://gzguguan.com/XiAn/HrApp/public/api/" + url, //仅为示例,并非真实的接口地址
    data: param,
    header: {
      'content-type': 'application/json', // 默认值
      'token': wx.getStorageSync("token"),
    },

    success: function success(res) {
      wx.hideLoading();
      if (res.data.code == 250) {
        wx.showModal({
          content: '需要授权登录',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../../pages/login/login'
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.data.code == 255) {
        this.doLogin();
      } else {
        callback(res.data);
      }
    }
  });
}
//将方法暴露
module.exports = {
  method: myRequest,
}
原文地址:https://www.cnblogs.com/xiaozhou223/p/12694229.html