美文网首页
小程序简单封装request请求

小程序简单封装request请求

作者: 默默无闻的小人物 | 来源:发表于2021-09-27 16:49 被阅读0次

首先新建一个config.js文件,一些简单的配置写在这里,代码如下

export const apiHttp = 'https://api.hengge.vip/api';
//export const apiHttp = 'http://localhost:9700/api';
export const getQrcodeByOrderNo =(orderNo)=> apiHttp+'/order/is_use?itemNum='+orderNo;

request.js内容如下

import {
  apiHttp
} from './../config/config'

function fun(url, method, data, options) {
  let oldOptions = {
    header: {},
    isLoading: true
  }
  if(options&&typeof options==='object'){
    oldOptions= Object.assign(oldOptions,options);
  }
  data = data || {};
  let isLogin = url.indexOf('/user/login') > -1;
  let token = wx.getStorageSync("token"); // 从缓存午拿该信息
  if (token) {
    if (!oldOptions.header || !oldOptions.header["Authorization"] && !isLogin) {
      oldOptions.header["Authorization"] = token;
    }
  }
  if (oldOptions.isLoading) {
    wx.showNavigationBarLoading();
  }
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: apiHttp + url,
      header:oldOptions.header,
      data: data,
      method: method,
      success: function (res) {
        if (typeof res.data === "object") {
          if (res.data.code) {
            if (res.data.code === 401) {
              wx.showToast({
                title: "Token过期,请重新登录",
                icon: "none"
              });
              wx.removeStorageSync('token')
              setTimeout(function () {
                wx.redirectTo({
                  url: "/pages/login/login" // 导航到登录页面
                });
              }, 1000);
              reject("请重新登录");
            } else if (res.data.code === 500) {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              });
              reject(res.data.msg);
            }
          }
        }
        resolve(res);
      },
      fail: reject,
      complete: function () {
        if (oldOptions.isLoading) {
          wx.hideNavigationBarLoading();
        }
      }
    });
  });
  return promise;
}

function upload(url, name, filePath,options) {
  let oldOptions = {
    header: {},
    isLoading: true
  }
  if(options&&typeof options==='object'){
    oldOptions= Object.assign(oldOptions,options);
  }
  let token = wx.getStorageSync("token"); //从缓存午拿该信息
  if (token) {
    if (!oldOptions.header || !oldOptions.header["Authorization"]) {
      oldOptions.header["Authorization"] = token; //添加到请求头午
    }
  }
  if (oldOptions.isLoading) {
    wx.showNavigationBarLoading();
  }
  let promise = new Promise(function (resolve, reject) {
    wx.uploadFile({
      url: apiHttp + url,
      filePath: filePath,
      name: name,
      header: oldOptions.header,
      success: function (res) {
        resolve(res);
      },
      fail: reject,
      complete: function () {
        if (oldOptions.isLoading) {
          wx.hideNavigationBarLoading();
        }
      }
    });
  });
  return promise;
}
module.exports = {
  "get": function (url, data, options) {
    return fun(url, "GET", data, options);
  },
  "post": function (url, data, options) {
    return fun(url, "POST", data, options);
  },
  upload: function (url, name, filePath,options) {
    return upload(url, name, filePath,options);
  }
};

相关文章

网友评论

      本文标题:小程序简单封装request请求

      本文链接:https://www.haomeiwen.com/subject/zyfknltx.html