微信小程序的使用

网络请求封装

class Req {
  baseUrl;
  header;
  instence;
  timeout;
  constructor(config) {
    this.baseUrl = config.baseUrl;
    this.header = config.header;
    this.timeout = config.timeout;
  }
  request(config) {
    return new Promise((resove, reject) => {
      this.header.Authorization = wx.getStorageSync("token");
      wx.showLoading();
      wx.request({
        url: this.baseUrl + config.url,
        data: config.data,
        header: config.header || this.header,
        method: config.method,
        timeout: this.timeout,
        success: (res) => {
          // 返回了错误信息
          if (res.data.err_code != 200) {
            // 未登录
            if (res.data.err_code == 401) {
              wx.showToast({
                title: "请先登录",
                icon: "error",
              });
            } else if (res.data.err_code == 403) {
              //没有权限
              wx.showToast({
                title: "没有权限",
                icon: "error",
              });
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "error",
              });
            }
          }
          resove(res.data);
        },
        fail: (err) => {
          wx.showToast({
            title: "接口请求失败",
            icon: "error",
          });
          reject(err.errMsg);
        },
        complete() {
          wx.hideLoading();
        },
      });
    });
  }
}

const request = new Req({
  baseUrl: "http://192.168.31.100:209/api",
  timeout: 10000,
  header: {},
});
// config包含url,data,method,header?
const useRequest = (config) => {
  return request.request({
    ...config,
  });
};
const useGet = (url = "", data = {}) => {
  return request.request({
    url,
    data,
    method: "GET",
  });
};
const usePost = (url = "", data = {}) => {
  return request.request({
    url,
    data,
    method: "POST",
  });
};
export { useRequest, useGet, usePost };

onlaunch和onload的异步问题 

app.js中的函数和变量可以使用getApp().name访问到
在onload需要获取数据的地方,判断可不可以访问到数据,如果不可以,getApp().callback=(data)=>{}。
在onlaunch中异步获取数据后,判断this.callback&&this.callback(data),this.callback=null
或者可以使用Promise

// app.js
onLaunch () {
    wx.login({
        success: result => {
            wx.request({

                method: ''
                url: '' // 示例
                data: {...},
                success: res => {
                    this.globalData.openId = res.data.openId
                     // 回调
                    if (this.callback) callback(res.data)

                    this.callback=null
                },
                fail: err => {
                    // 需要的话请求失败处也可以使用回调
                }
            })
        }
    })
}
// page.js
onLoad () {
    const app = getApp()
    if (app.globalData.openId) {
        // do something
    } else {
        app. callback = (data) => {
            // do something
        }
    }
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值