chengsc
2024-07-21 e75362cc5d11d900068a26232eb97ebd30203d48
HTML/js/public.js
@@ -39,6 +39,15 @@
    if (!$.cookie('token')) {
      callbackFun("登录人信息已失效");
    }
    if(isExpired = isTokenExpired($.cookie('token'))){
      try{
        parent.window.location.href = '/views/Login.html';
      }
      catch(error){
        window.location.href = '/views/Login.html';
      }
      return;
    }
  } 
  $.ajax({
    url: url,
@@ -55,19 +64,33 @@
    success: function (res, status, xhr) {
      // console.log(res);
      // console.log(status);
      callbackFun(res);
    },
    error: function (res, status, error) {
      // console.log(res);
      // console.log(status);
      callbackFun(res, status, error);
    error: function (res, status, error) {
      callbackFun(res.responseJSON, status, error);
      // layer.msg(res.statusText, {
      //   icon: 2,
      //   time: 2000 //2秒关闭(如果不配置,默认是3秒)
      // }, function() {});
    },
  });
}
function isTokenExpired(token) {
  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; // 没有有效载荷或者没有过期时间
  }
  const now = Date.now() / 1000; // 当前时间戳(秒)
  const expiration = payload.exp;
  return now >= expiration;
}
function synData(url, data, type, callbackFun) {
  var deferred = $.Deferred();
@@ -180,9 +203,9 @@
  var bodyHeight = $("#body").outerHeight();
  var topHeight = $("#top").outerHeight();
  var centerHeight = $("#center").outerHeight();
//   console.log(bodyHeight);
//   console.log(topHeight);
//   console.log(centerHeight);
  console.log(bodyHeight);
  console.log(topHeight);
  console.log(centerHeight);
  var hh = bodyHeight - topHeight-centerHeight - 26 ;//6是body上内边距 
  return hh;
@@ -282,4 +305,83 @@
      });
    });
  }  
}
//深拷贝
function deepClone(source) {
  if (typeof source !== 'object' || source == null) {
    return source;
  }
  const target = Array.isArray(source) ? [] : {};
  for (const key in source) {
    if (Object.prototype.hasOwnProperty.call(source, key)) {
      if (typeof source[key] === 'object' && source[key] !== null) {
        target[key] = deepClone(source[key]);
      } else {
        target[key] = source[key];
      }
    }
  }
  return target;
}
/**
 * 将表单赋值为指定的对象
 * @param {Object} data - 包含表单数据的对象
 * @param {String} formSelector - 表单的选择器,例如 '#myForm' 或 '.myForm'
 */
function setFormData(data, formSelector) {
  var $form = $(formSelector);
  $.each(data, function(key, value) {
      var $field = $form.find('[name=' + key + ']');
      if ($field.length > 0) {
          var fieldType = $field.attr('type');
          switch (fieldType) {
              case 'checkbox':
                  if (Array.isArray(value)) {
                      $field.each(function() {
                          $(this).prop('checked', value.includes($(this).val()));
                      });
                  } else {
                      $field.prop('checked', value);
                  }
                  break;
              case 'radio':
                  $field.filter('[value=' + value + ']').prop('checked', true);
                  break;
              default:
                  $field.val(value);
                  break;
          }
      }
  });
}
//分页格式化
var FnParseData = function (res) {
  return {
    "code": res.code == 0 ? 0 : res.code, // 解析接口状态
    "msg": res.msg, // 解析提示文本
    "count": res.data.Total, // 解析数据长度
    "data": res.data.Items // 解析数据列表
  };
}
//调用打印
function openPrintDialog(printData, printType){
  layer.open({
    type: 2,
    title: '打印',
    // content: '../../js/hiprint/custom.html',
    content: '/views/SystemSettings/PrintModule.html',
    maxmin: true,
    area: ['100%', '100%'],
    data: printData,
    success: function (layero, index) {
      var body = layer.getChildFrame('body',index);
      body.find('#printData').val(JSON.stringify(printData));
      body.find('#printType').val(printType);
    }
  });
}