小程序常用的函数封装

 // 读取本地储存
  getUserInfo: function () {
    return new Promise((resolve, reject) => {
      wx.getStorage({
        key: 'user',
        success: function (res) {
          resolve(res.data)
        },
        fail: function (res) {
          reject('err')
        }
      })
    })
  },

获取本地缓存

app.getUserInfo().then((res) => { //获取本地储存
that.setData({
userInfo: {
id: res.id,
token: res.token,
},
uid: res.id
})
 
 //getUrlImg 解决带域名或不带域名的图片地址
  getUrlImg: function ($string) {
    if (!$string) {
      return this.imgUrl() + '/uploadfiles/UserFace/no-image.jpg';
    }
    if ($string.indexOf('https://') < 0) { // 不是http开头的
      return this.imgUrl() + $string;
    }
    return $string;
  },

  imgUrl: function () {
    return "这里放置url"
  },


  // 封装post请求
  post: function (url, data) {
    return new Promise((resolve, reject) => {
      //网络请求
      wx.request({
        url: url,
        data: data,
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) { //服务器返回数据
        console.log(data)
          if (res.statusCode == 200) {
            if (res.data.code == "NO_LOGIN") {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
              })
              wx.removeStorage({
                key: 'user',
                success: function (res) { }
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: "/pages/login/login",
                })
              }, 1000) //延迟时间 这里是1秒
            } else {
              resolve(res);
            }
          } else { //返回错误提示信息
            reject(res.data);
          }
        },
        error: function (e) {
          reject('网络出错');
        }
      })
    });
  },
  // 封装get请求
  get: function (url, data) {
    return new Promise((resolve, reject) => {
      //网络请求
      wx.request({
        url: url,
        data: data,
        method: 'GET',
        header: {
          'content-type': 'application/json',
        },
        success: function (res) { //服务器返回数据
          if (res.statusCode == 200) {
            if (res.data.code == "NO_LOGIN") {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
              })
              wx.removeStorage({
                key: 'user',
                success: function (res) { }
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: "/pages/login/login",
                })
              }, 1000) //延迟时间 这里是1秒
            } else {
              resolve(res);
            }
          } else { //返回错误提示信息
            reject(res.data);
          }
        },
        complete: function () {
          reject('网络出错');
        },
        error: function (e) {
          reject('网络出错');
        }
      })
    });
  },

方便快捷,平时用来进行token 校验的请求。

使用:
  app.post(that.data.url + 'api.php?ac=user_my', data).then((res) => {
  console.log(res)
  }
 

几个常用的封装函数。每次都要重写很麻烦。存着

原文地址:https://www.cnblogs.com/wxhhts/p/10951905.html