admin
2 天以前 0d73c09676e64231cbdcd174e2ab88b36310b9bd
还原代码
1个文件已修改
248 ■■■■ 已修改文件
Pda/js/public.js 248 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Pda/js/public.js
@@ -8,150 +8,148 @@
//var IP = "http://192.168.62.200:8888/api";
//运行
var IP = "https://localhost:44363/api";
//本地发布
//var IP = "http://localhost:8034/api";
jQuery.support.cors = true;
function sendData(url, data, type, callbackFun) {
    // callbackFun("{}");
    // return;
    var deferred = $.Deferred();
    if (type != "get") {
        data = JSON.stringify(data);
    }
    if (url != IP + "/WeatherForecast/Login") {
        if (!$.cookie("token")) {
            callbackFun("登录人信息已失效");
            try {
                parent.window.location.href = "/View/login.html";
            } catch (error) {
                window.location.href = "/View/login.html";
            }
            return;
        }
        if (isTokenExpired($.cookie("token"))) {
            try {
                parent.window.location.href = "/View/login.html";
            } catch (error) {
                window.location.href = "/View/login.html";
            }
            return;
        }
    }
    $.ajax({
        url: url,
        data: data,
        type: type,
        headers: {
            "Content-Type": "application/json",
            ToKen: $.cookie("token"),
        },
        timeout: 45000,
        async: true,
        cache: false,
        beforeSend: function (xhr, settings) { },
        success: function (res, status, xhr) {
            callbackFun(res);
        },
        error: function (xhr, status, error) {
            callbackFun(xhr, status, error);
            // layer.msg(xhr.statusText, {
            //   icon: 2,
            //   time: 2000 //2秒关闭(如果不配置,默认是3秒)
            // }, function() {});
        },
    });
  // callbackFun("{}");
  // return;
  var deferred = $.Deferred();
  if (type != "get") {
    data = JSON.stringify(data);
  }
  if (url != IP + "/WeatherForecast/Login") {
    if (!$.cookie("token")) {
      callbackFun("登录人信息已失效");
      try {
        parent.window.location.href = "/View/login.html";
      } catch (error) {
        window.location.href = "/View/login.html";
      }
      return;
    }
    if (isTokenExpired($.cookie("token"))) {
      try {
        parent.window.location.href = "/View/login.html";
      } catch (error) {
        window.location.href = "/View/login.html";
      }
      return;
    }
  }
  $.ajax({
    url: url,
    data: data,
    type: type,
    headers: {
      "Content-Type": "application/json",
      ToKen: $.cookie("token"),
    },
    timeout: 45000,
    async: true,
    cache: false,
    beforeSend: function (xhr, settings) {},
    success: function (res, status, xhr) {
      callbackFun(res);
    },
    error: function (xhr, status, error) {
      callbackFun(xhr, status, error);
      // layer.msg(xhr.statusText, {
      //   icon: 2,
      //   time: 2000 //2秒关闭(如果不配置,默认是3秒)
      // }, function() {});
    },
  });
}
function synData(url, data, type, callbackFun) {
    var deferred = $.Deferred();
    if (type != "get") {
        data = JSON.stringify(data);
    }
    if (url != IP + "/WeatherForecast/Login") {
        if (!$.cookie("token")) {
            callbackFun("登录人信息已失效");
            try {
                parent.window.location.href = "/View/login.html";
            } catch (error) {
                window.location.href = "/View/login.html";
            }
            return;
        }
        if (isTokenExpired($.cookie("token"))) {
            try {
                parent.window.location.href = "/View/login.html";
            } catch (error) {
                window.location.href = "/View/login.html";
            }
            return;
        }
    }
    $.ajax({
        url: url,
        data: data,
        type: type,
        headers: {
            "Content-Type": "application/json",
            ToKen: $.cookie("token"),
        },
        timeout: 45000,
        async: false,
        cache: false,
        beforeSend: function (xhr, settings) { },
        success: function (res, status, xhr) {
            callbackFun(res);
        },
        error: function (xhr, status, error) {
            console.log("失败了");
            console.log(xhr);
            callbackFun(xhr, status, error);
        },
    });
  var deferred = $.Deferred();
  if (type != "get") {
    data = JSON.stringify(data);
  }
  if (url != IP + "/WeatherForecast/Login") {
    if (!$.cookie("token")) {
      callbackFun("登录人信息已失效");
      try {
        parent.window.location.href = "/View/login.html";
      } catch (error) {
        window.location.href = "/View/login.html";
      }
      return;
    }
    if (isTokenExpired($.cookie("token"))) {
      try {
        parent.window.location.href = "/View/login.html";
      } catch (error) {
        window.location.href = "/View/login.html";
      }
      return;
    }
  }
  $.ajax({
    url: url,
    data: data,
    type: type,
    headers: {
      "Content-Type": "application/json",
      ToKen: $.cookie("token"),
    },
    timeout: 45000,
    async: false,
    cache: false,
    beforeSend: function (xhr, settings) {},
    success: function (res, status, xhr) {
      callbackFun(res);
    },
    error: function (xhr, status, error) {
      console.log("失败了");
      console.log(xhr);
      callbackFun(xhr, status, error);
    },
  });
}
$("#navIcon").click(function () {
    if ($(".nav_list").css("display") == "none") {
        $(".nav_list").show();
    } else {
        $(".nav_list").hide();
    }
  if ($(".nav_list").css("display") == "none") {
    $(".nav_list").show();
  } else {
    $(".nav_list").hide();
  }
});
//深拷贝
function deepCopy(obj) {
    let newobj = null; // 接受拷贝的新对象
    if (typeof obj == "object" && typeof obj !== null) {
        // 判断是否是引用类型
        newobj = obj instanceof Array ? [] : {}; // 判断是数组还是对象
        for (var i in obj) {
            newobj[i] = deepCopy(obj[i]); // 判断下一级是否还是引用类型
        }
    } else {
        newobj = obj;
    }
  let newobj = null; // 接受拷贝的新对象
  if (typeof obj == "object" && typeof obj !== null) {
    // 判断是否是引用类型
    newobj = obj instanceof Array ? [] : {}; // 判断是数组还是对象
    for (var i in obj) {
      newobj[i] = deepCopy(obj[i]); // 判断下一级是否还是引用类型
    }
  } else {
    newobj = obj;
  }
    return newobj;
  return newobj;
}
function isTokenExpired(token) {
    if (token == undefined || token == "") {
        return true;
    }
    const tokenParts = token.split(".");
    if (tokenParts.length !== 3) {
        return true; // JWT 格式不正确
    }
    const payloadBase64 = tokenParts[1];
    const payload = JSON.parse(atob(payloadBase64));
  if (token == undefined || token == "") {
    return true;
  }
  const tokenParts = token.split(".");
  if (tokenParts.length !== 3) {
    return true; // JWT 格式不正确
  }
  const payloadBase64 = tokenParts[1];
  const payload = JSON.parse(atob(payloadBase64));
    if (!payload || !payload.exp) {
        return true; // 没有有效载荷或者没有过期时间
    }
  if (!payload || !payload.exp) {
    return true; // 没有有效载荷或者没有过期时间
  }
    const now = Date.now() / 1000; // 当前时间戳(秒)
    const expiration = payload.exp;
  const now = Date.now() / 1000; // 当前时间戳(秒)
  const expiration = payload.exp;
    return now >= expiration;
  return now >= expiration;
}