547 lines
18 KiB
JavaScript
547 lines
18 KiB
JavaScript
// const dataUrl = 'http://localhost:21522/'; // 数据请求路径
|
||
// const fileUrl = 'http://127.0.0.1:21522/gz_car/statics'; // 文件路径
|
||
const viewFileUrl = 'http://192.168.0.14:8012/onlinePreview?url=' //14服务器预览文件
|
||
|
||
const dataUrl = 'http://192.168.0.16:21522/gz-car/'; // 数据请求路径
|
||
const fileUrl = 'http://192.168.0.16:21522/gz-car/statics'; // 文件路径
|
||
const signFileUrl = 'http://127.0.0.1:21995/statics'; // 签名文件路径
|
||
// const viewFileUrl = 'http://192.168.0.14:8012/onlinePreview?url=' //14服务器预览文件 */
|
||
|
||
/* 请求 */
|
||
function ajaxRequest(url, type, data, async, beforeFn, successFn, errorFn, contentType) {
|
||
$.ajax({
|
||
url: url,
|
||
type: type,
|
||
headers: {
|
||
"authorization": sessionStorage.getItem("gz-token"),
|
||
},
|
||
data: data,
|
||
async: async,
|
||
beforeSend: beforeFn,
|
||
contentType: contentType || "application/x-www-form-urlencoded; charset=utf-8",
|
||
success: function (data) {
|
||
if (data.code === 401) {
|
||
closeWindowOpen();
|
||
}
|
||
successFn(data);
|
||
},
|
||
error: function (error) {
|
||
errorFn(error)
|
||
}
|
||
});
|
||
}
|
||
|
||
/* 文件上传请求 */
|
||
function ajaxRequestByUploadFile(url, data, beforeFn, successFn, errorFn) {
|
||
$.ajax({
|
||
url: url,
|
||
headers: {
|
||
"authorization": sessionStorage.getItem("gz-token"),
|
||
},
|
||
type: 'POST',
|
||
dataType: 'json',
|
||
contentType: 'multipart/form-data',
|
||
processData: false,
|
||
contentType: false,
|
||
data: data,
|
||
beforeSend: beforeFn,
|
||
success: function (data) {
|
||
if (data.code === 401) {
|
||
closeWindowOpen();
|
||
}
|
||
successFn(data);
|
||
},
|
||
error: function (error) {
|
||
errorFn(error)
|
||
}
|
||
});
|
||
}
|
||
|
||
/* 请求错误 */
|
||
function errorFn(xhr, status, error) {
|
||
if (xhr.status === 0) {
|
||
// 网络连接失败
|
||
console.error("网络连接失败,请检查网络是否正常");
|
||
} else {
|
||
// 请求出现其他错误
|
||
console.error("ajax请求错误:" + error);
|
||
}
|
||
}
|
||
|
||
// 公共导出excel
|
||
function exportExcelUtil(url, fileName, params, type) {
|
||
let loadingMsg = layer.msg("数据导出中,请稍候...", { icon: 16, scrollbar: false, time: 0, });
|
||
let xhr = new XMLHttpRequest();
|
||
xhr.open(type ? "GET" : "POST", url, true);
|
||
xhr.responseType = "blob"; // 转换流
|
||
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
|
||
xhr.setRequestHeader("authorization", sessionStorage.getItem("gz-token"));
|
||
xhr.onload = function () {
|
||
layer.close(loadingMsg);
|
||
if (this.status === 200) {
|
||
let blob = this.response;
|
||
var a = document.createElement("a");
|
||
var url = window.URL.createObjectURL(blob);
|
||
a.href = url;
|
||
a.download = fileName + ".xlsx"; // 文件名
|
||
} else {
|
||
layer.msg("数据发生异常,请稍后重试", { icon: 16, scrollbar: false, time: 2000 });
|
||
}
|
||
a.click();
|
||
window.URL.revokeObjectURL(url);
|
||
};
|
||
xhr.send(params);
|
||
}
|
||
|
||
|
||
// 下载文件的公共方法
|
||
function downLoadFileUtil(url, fileName, params) {
|
||
let loadingMsg = layer.msg("文件导出中,请稍候...", { icon: 16, scrollbar: false, time: 0, });
|
||
let xhr = new XMLHttpRequest();
|
||
xhr.open("GET", url, true);
|
||
xhr.responseType = "blob"; // 转换流
|
||
xhr.setRequestHeader("authorization", sessionStorage.getItem("gz-token"));
|
||
xhr.onload = function () {
|
||
layer.close(loadingMsg);
|
||
if (this.status === 200) {
|
||
let blob = this.response;
|
||
var a = document.createElement("a");
|
||
var url = window.URL.createObjectURL(blob);
|
||
a.href = url;
|
||
a.download = fileName; // 文件名
|
||
} else {
|
||
layer.msg("文件导出异常,请稍后重试", { icon: 16, scrollbar: false, time: 2000 });
|
||
}
|
||
a.click();
|
||
window.URL.revokeObjectURL(url);
|
||
};
|
||
xhr.send();
|
||
}
|
||
|
||
function excelUpload(element, url) {
|
||
let dataList = [];
|
||
// 导入提示
|
||
let loadingMsg = layer.msg('数据导入中,请稍候...', { icon: 16, scrollbar: false, time: 0 });
|
||
//④创建一个formData对象
|
||
let formData = new FormData();
|
||
//⑤获取传入元素的val
|
||
//⑥获取files
|
||
let files = $(element)[0].files[0];
|
||
//⑦将name 和 files 添加到formData中,键值对形式
|
||
formData.append("file", files);
|
||
$.ajax({
|
||
headers: {
|
||
"authorization": sessionStorage.getItem("gz-token"),
|
||
},
|
||
url: url,
|
||
type: 'POST',
|
||
data: formData,
|
||
async: false,
|
||
dataType: 'json',
|
||
processData: false,// ⑧告诉jQuery不要去处理发送的数据
|
||
contentType: false, // ⑨告诉jQuery不要去设置Content-Type请求头
|
||
beforeSend: function () {
|
||
},
|
||
success: function (result) {
|
||
layer.close(loadingMsg);
|
||
$(element).val("");
|
||
if (result.code === 200) {
|
||
dataList = result.data;
|
||
layer.msg('导入成功', { icon: 1 });
|
||
} else if (result.code === 401) {
|
||
closeWindowOpen();
|
||
} else {
|
||
return layer.msg('导入的数据:' + result.msg, { icon: 2 });
|
||
}
|
||
},
|
||
error: function (result) {
|
||
layer.close(loadingMsg);
|
||
$(element).val("");
|
||
}
|
||
});
|
||
return dataList;
|
||
}
|
||
|
||
// 格式化日对象
|
||
const getNowDate = () => {
|
||
var date = new Date();
|
||
var sign2 = ":";
|
||
var year = date.getFullYear() // 年
|
||
var month = date.getMonth() + 1; // 月
|
||
var day = date.getDate(); // 日
|
||
var hour = date.getHours(); // 时
|
||
var minutes = date.getMinutes(); // 分
|
||
var seconds = date.getSeconds() //秒
|
||
var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
|
||
var week = weekArr[date.getDay()];
|
||
// 给一位数的数据前面加 “0”
|
||
if (month >= 1 && month <= 9) {
|
||
month = "0" + month;
|
||
}
|
||
if (day >= 0 && day <= 9) {
|
||
day = "0" + day;
|
||
}
|
||
if (hour >= 0 && hour <= 9) {
|
||
hour = "0" + hour;
|
||
}
|
||
if (minutes >= 0 && minutes <= 9) {
|
||
minutes = "0" + minutes;
|
||
}
|
||
if (seconds >= 0 && seconds <= 9) {
|
||
seconds = "0" + seconds;
|
||
}
|
||
return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
|
||
}
|
||
// 空值赋值
|
||
function setNullValue(value) {
|
||
if (value === 0) {
|
||
return 0;
|
||
} else if (!value || value === 'null') {
|
||
return '';
|
||
}
|
||
return value;
|
||
}
|
||
|
||
// 数字空值赋值
|
||
function setNullNumValue(value) {
|
||
if (value === 0) {
|
||
return 0;
|
||
} else if (!value || value === 'null') {
|
||
return 0;
|
||
}
|
||
return value;
|
||
}
|
||
|
||
function getUrlParam(key) {
|
||
var href = window.location.href;
|
||
var url = href.split("?");
|
||
if (url.length <= 1) {
|
||
return "";
|
||
}
|
||
var params = url[1].split("&");
|
||
|
||
for (var i = 0; i < params.length; i++) {
|
||
var param = params[i].split("=");
|
||
if (key == param[0]) {
|
||
return param[1];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 预览文件
|
||
function commonViewFile(params) {
|
||
let path = fileUrl + params + '?auth=' + sessionStorage.getItem("gz-token");
|
||
// path = fileUrl + params
|
||
console.log(path);
|
||
let encodePath = encodeURIComponent(useBase64.encode64(path));
|
||
window.open(viewFileUrl + encodePath + '&token=' + generateToken());
|
||
}
|
||
|
||
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||
const useBase64 = {
|
||
encode64: (e) => {
|
||
let t = "";
|
||
let f = 0;
|
||
e = useBase64.encodeUTF8(e);
|
||
while (f < e.length) {
|
||
const n = e.charCodeAt(f++);
|
||
const r = e.charCodeAt(f++);
|
||
const i = e.charCodeAt(f++);
|
||
let s = n >> 2;
|
||
let o = (n & 3) << 4 | r >> 4;
|
||
let u = (r & 15) << 2 | i >> 6;
|
||
let a = i & 63;
|
||
if (isNaN(r)) {
|
||
u = a = 64;
|
||
} else if (isNaN(i)) {
|
||
a = 64;
|
||
}
|
||
t += _keyStr[s] + _keyStr[o] + _keyStr[u] + _keyStr[a];
|
||
}
|
||
return t;
|
||
},
|
||
decode64: (e) => {
|
||
let t = "";
|
||
let f = 0;
|
||
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
|
||
while (f < e.length) {
|
||
const s = _keyStr.indexOf(e.charAt(f++));
|
||
const o = _keyStr.indexOf(e.charAt(f++));
|
||
const u = _keyStr.indexOf(e.charAt(f++));
|
||
const a = _keyStr.indexOf(e.charAt(f++));
|
||
let n = s << 2 | o >> 4;
|
||
let r = (o & 15) << 4 | u >> 2;
|
||
let i = (u & 3) << 6 | a;
|
||
t += String.fromCharCode(n);
|
||
if (u !== 64) {
|
||
t += String.fromCharCode(r);
|
||
}
|
||
if (a !== 64) {
|
||
t += String.fromCharCode(i);
|
||
}
|
||
}
|
||
return useBase64.decodeUTF8(t);
|
||
},
|
||
|
||
encodeUTF8: (input) => {
|
||
return unescape(encodeURIComponent(input));
|
||
},
|
||
|
||
decodeUTF8: (input) => {
|
||
return decodeURIComponent(escape(input));
|
||
},
|
||
}
|
||
|
||
/* 校验查询关键字是否包含特殊字符 */
|
||
function checkValue(value) {
|
||
if (value) {
|
||
let pattern = new RegExp("[%_<>]");
|
||
if (pattern.test(value)) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
// 页面401关闭页面
|
||
function closeWindowOpen() {
|
||
let layerIndex = top.layer.confirm('登录已过期,请点击确定后重新登录!', {
|
||
btn: ['确认', '取消'],
|
||
'title': '操作提示',
|
||
move: false,
|
||
cancel: function (index, layero) {
|
||
sessionStorage.removeItem('gz-token');
|
||
window.close();
|
||
}
|
||
}, function () {
|
||
sessionStorage.removeItem('gz-token');
|
||
window.close();
|
||
top.layer.close(layerIndex);
|
||
}, function () {
|
||
sessionStorage.removeItem('gz-token');
|
||
window.close();
|
||
});
|
||
}
|
||
|
||
// 最近一周
|
||
function getNearOnceweek() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
// 计算当前日期往前推七天的日期
|
||
const sevenDaysAgo = new Date(today);
|
||
sevenDaysAgo.setDate(today.getDate() - 7);
|
||
// 格式化日期为 YYYY-MM-DD 格式
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化七天前的日期、当前日期
|
||
const sevenDaysAgoStr = formatDate(sevenDaysAgo);
|
||
const nowDateStr = formatDate(today);
|
||
return [sevenDaysAgoStr, nowDateStr];
|
||
}
|
||
|
||
// 最近一月
|
||
function getNearOnceMonth() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
// 获取当前日期的年份和日期
|
||
const currentYear = today.getFullYear();
|
||
const currentDate = today.getDate();
|
||
// 获取当前日期的月份,并减去1来得到前一个月的月份
|
||
// 注意:月份是从0开始的,所以1月是0,12月是11
|
||
let previousMonth = today.getMonth() - 1;
|
||
// 检查是否减到了前一年(即,如果当前月份是1月,那么前一个月应该是上一年的12月)
|
||
let previousYear = currentYear;
|
||
if (previousMonth < 0) {
|
||
previousMonth = 11; // 上一年的12月
|
||
previousYear -= 1; // 上一年的年份
|
||
}
|
||
// 创建前一个月的日期对象,注意这里我们使用了完整的年月日来构造
|
||
const previousMonthDate = new Date(previousYear, previousMonth, currentDate);
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化前一个月的日期
|
||
const previousMonthDateStr = formatDate(previousMonthDate);
|
||
const nowDateStr = formatDate(today);
|
||
return [previousMonthDateStr, nowDateStr];
|
||
}
|
||
|
||
// 最近三月
|
||
function getNearThreeMonth() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
|
||
// 获取当前日期的年份、月份和日期
|
||
const currentYear = today.getFullYear();
|
||
const currentMonth = today.getMonth();
|
||
const currentDate = today.getDate();
|
||
|
||
// 计算前三个月的月份
|
||
let previousThreeMonths = currentMonth - 3;
|
||
|
||
// 处理月份小于0的情况,即需要调整年份
|
||
let previousYear = currentYear;
|
||
while (previousThreeMonths < 0) {
|
||
previousThreeMonths += 12; // 加12个月回到上一年
|
||
previousYear -= 1; // 上一年的年份
|
||
}
|
||
const previousThreeMonthsDate = new Date(previousYear, previousThreeMonths, currentDate);
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,输出时需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化前三个月的日期
|
||
const previousThreeMonthsDateStr = formatDate(previousThreeMonthsDate);
|
||
const nowDateStr = formatDate(today);
|
||
return [previousThreeMonthsDateStr, nowDateStr];
|
||
}
|
||
|
||
// 最近六月
|
||
function getNearSixMonth() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
// 获取当前日期的年份、月份(注意月份从0开始)和日期
|
||
const currentYear = today.getFullYear();
|
||
const currentMonth = today.getMonth();
|
||
const currentDate = today.getDate();
|
||
// 计算前6个月的月份
|
||
let previousSixMonths = currentMonth - 6;
|
||
// 处理月份小于0的情况,需要调整年份
|
||
let previousYear = currentYear;
|
||
while (previousSixMonths < 0) {
|
||
previousSixMonths += 12; // 加12个月回到上一年
|
||
previousYear -= 1; // 年份减1
|
||
}
|
||
const previousSixMonthsDate = new Date(previousYear, previousSixMonths, currentDate);
|
||
// 格式化日期为 YYYY-MM-DD 格式
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份输出时需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化前6个月的日期
|
||
const previousSixMonthsDateStr = formatDate(previousSixMonthsDate);
|
||
const nowDateStr = formatDate(today);
|
||
return [previousSixMonthsDateStr, nowDateStr];
|
||
}
|
||
|
||
// 去年一整年
|
||
function getLastYear() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
// 获取当前日期的年份,并计算去年的年份
|
||
const currentYear = today.getFullYear();
|
||
const lastYear = currentYear - 1;
|
||
// 构造去年开始日期(1月1日)
|
||
const startOfLastYear = new Date(lastYear, 0, 1); // 月份从0开始,0代表1月
|
||
// 构造去年结束日期(12月31日)
|
||
const endOfLastYear = new Date(lastYear, 11, 31); // 月份从0开始,11代表12月
|
||
// 格式化日期为 YYYY-MM-DD 格式
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份输出时需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化去年开始日期和结束日期
|
||
const startOfLastYearStr = formatDate(startOfLastYear);
|
||
const endOfLastYearStr = formatDate(endOfLastYear);
|
||
return [startOfLastYearStr, endOfLastYearStr];
|
||
}
|
||
|
||
// 本年度
|
||
function getNowYear() {
|
||
// 获取当前日期
|
||
const today = new Date();
|
||
// 获取当前日期的年份
|
||
const currentYear = today.getFullYear();
|
||
// 构造本年开始日期(1月1日)
|
||
const startOfThisYear = new Date(currentYear, 0, 1); // 月份从0开始,0代表1月
|
||
// 构造本年结束日期(12月31日)
|
||
const endOfThisYear = new Date(currentYear, 11, 31); // 月份从0开始,11代表12月
|
||
// 格式化日期为 YYYY-MM-DD 格式
|
||
const formatDate = (date) => {
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份输出时需要加1
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
return `${year}-${month}-${day}`;
|
||
};
|
||
// 格式化本年开始日期和结束日期
|
||
const startOfThisYearStr = formatDate(startOfThisYear);
|
||
const endOfThisYearStr = formatDate(endOfThisYear);
|
||
// 输出结果
|
||
console.log(`本年一整年的开始日期: ${startOfThisYearStr}`);
|
||
console.log(`本年一整年的结束日期: ${endOfThisYearStr}`);
|
||
return [startOfThisYearStr, endOfThisYearStr];
|
||
}
|
||
|
||
|
||
// 数字转大写
|
||
function numToChinese(num) {
|
||
const chineseNumbers = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
|
||
if (num >= 1 && num <= 10) {
|
||
return chineseNumbers[num - 1];
|
||
} else if (num === 11) {
|
||
return '十一';
|
||
} else if (num === 12) {
|
||
return '十二';
|
||
} else if (num === 13) {
|
||
return '十三';
|
||
} else if (num === 14) {
|
||
return '十四';
|
||
} else if (num === 15) {
|
||
return '十五';
|
||
} else if (num === 16) {
|
||
return '十六';
|
||
} else if (num === 17) {
|
||
return '十七';
|
||
} else if (num === 18) {
|
||
return '十八';
|
||
} else if (num === 19) {
|
||
return '十九';
|
||
} else if (num === 20) {
|
||
return '二十';
|
||
} else if (num === 21) {
|
||
return '二十一';
|
||
} else if (num === 22) {
|
||
return '二十二';
|
||
} else if (num === 23) {
|
||
return '二十三';
|
||
} else if (num === 24) {
|
||
return '二十四';
|
||
} else if (num === 25) {
|
||
return '二十五';
|
||
} else if (num === 26) {
|
||
return '二十六';
|
||
} else if (num === 27) {
|
||
return '二十七';
|
||
} else if (num === 28) {
|
||
return '二十八';
|
||
} else if (num === 29) {
|
||
return '二十九';
|
||
} else if (num === 30) {
|
||
return '三十';
|
||
} else if (num === 31) {
|
||
return '三十一';
|
||
} else if (num === 32) {
|
||
return '三十二';
|
||
} else if (num === 33) {
|
||
return '三十三';
|
||
} else if (num === 34) {
|
||
return '三十四';
|
||
} else if (num === 35) {
|
||
return '三十五';
|
||
} else if (num === 36) {
|
||
return '三十六';
|
||
}
|
||
} |