request.js 3.29 KB
"use strict";
const common_vendor = require("./vendor.js");
const config = require("../config.js");
const common_login = require("./login.js");
const excludeUrls = ["/system/wx/getUserInfo"];
function getToken() {
  try {
    const token = common_vendor.index.getStorageSync("token");
    if (token) {
      return token;
    } else {
      return "";
    }
  } catch (e) {
    console.log(e);
  }
}
function getHeaders() {
  let token = getToken();
  let header = {
    "Authorization": token,
    "Content-Type": "application/json"
    //根据自己的数据类型
    // "Content-Type":"application/x-www-form-urlencoded",
  };
  return header;
}
let request = function(req) {
  req.method = req.method.toUpperCase();
  if (!["GET", "POST", "PUT", "DELETE"].includes(req.method)) {
    common_vendor.index.showToast({
      title: `暂不支持的请求方式: ${req.method}`,
      icon: "none"
    });
    return;
  }
  return new Promise((resolve, reject) => {
    common_vendor.index.request({
      url: config.config.baseUrl_api + req.url,
      method: req.method,
      data: req.params,
      header: getHeaders()
    }).then((res) => {
      switch (res.statusCode) {
        case 200:
          const data = res.data || {};
          if (data.code == 200) {
            resolve(data);
          } else {
            if (data.code == 60002 || data.code == 60001) {
              if (req.url != "/system/wx/getUserInfo") {
                reLogin();
              }
            } else if (data.code == 401 && req.url != "/system/wx/getUserInfo") {
              common_login.pcLogin().then(() => {
                return common_login.getNowOpenId();
              }).then(() => {
                return common_login.initData();
              }).then(() => {
                common_vendor.index.hideLoading();
                common_vendor.index.redirectTo({
                  url: getCurrentPages()[getCurrentPages().length - 1].$page.fullPath
                });
              });
            } else {
              if (!excludeUrls.includes(req.url)) {
                common_vendor.index.showToast({
                  title: data.msg,
                  icon: "none",
                  duration: 3e3
                });
              }
            }
            reject(res);
          }
          break;
        default:
          reject(res);
      }
    }).catch((res) => {
      reject(res);
    }).finally(() => {
    });
  });
};
function reLogin() {
  const currUser = common_vendor.index.getStorageSync("currUser") || {};
  if (currUser.openId) {
    const accountInfo = common_vendor.index.getAccountInfoSync();
    pcLoginByOpenId(accountInfo.miniProgram.appId, currUser.openId).then((res) => {
      let user = res.data;
      common_vendor.index.setStorageSync("token", "Bearer " + user.token);
      common_vendor.index.setStorageSync("currUser", user);
      common_vendor.index.showToast({
        title: "一走神把您的操作遗漏了,请重试",
        icon: "none"
      });
    }).catch(() => {
      common_vendor.index.showToast({
        title: "服务异常,请稍后重试",
        icon: "none"
      });
    });
  }
}
function pcLoginByOpenId(appId, openId) {
  return request({
    url: "/system/wx/loginByOpenId",
    method: "POST",
    params: {
      appId,
      openId
    }
  });
}
exports.request = request;