gz_gqj_web/js/public.js

279 lines
8.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* const dataUrl = 'http://127.0.0.1:21995/'; // 数据请求路径
const fileUrl = 'http://127.0.0.1:21995/statics'; // 文件路径
const signFileUrl = 'http://127.0.0.1:21995/statics'; // 签名文件路径
const viewFileUrl = 'http://192.168.0.14:8012/onlinePreview?url=' //14服务器预览文件 */
const dataUrl = 'http://192.168.0.14:21999/'; // 数据请求路径
const fileUrl = 'http://192.168.0.14:21999/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) {
let loadingMsg = layer.msg("数据导出中,请稍候...", { icon: 16, scrollbar: false, time: 0, });
let xhr = new XMLHttpRequest();
xhr.open("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();
}
// 格式化日对象
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) {
window.close();
}
}, function () {
window.close();
top.layer.close(layerIndex);
}, function () {
window.close();
});
}