czl-web/js/main.js

474 lines
14 KiB
JavaScript
Raw Permalink 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.

initMenu();
function initMenu() {
$.ajax({
url: ctxPath + "/permissions/current",
type: "get",
async: false,
success: function (data) {
if (!$.isArray(data)) {
//
location.href = contentPath+'/login.html';
return;
}
var menu = $("#menu");
$.each(data, function (i, item) {
var a = $("<a href='javascript:;'></a>");
var css = item.css;
if (css != null && css != "") {
a.append(
"<i aria-hidden='true' class='fa " + css + "'></i>"
);
}
a.append("<cite>" + item.name + "</cite>");
a.attr("lay-id", item.id);
var href = item.href;
if (href != null && href != "") {
a.attr("data-url", href);
}
var li = $("<li class='layui-nav-item'></li>");
if (i == 0) {
li.addClass("layui-nav-itemed");
}
li.append(a);
menu.append(li);
//多级菜单
setChild(li, item.child);
});
},
});
}
function setChild(parentElement, child) {
if (child != null && child.length > 0) {
$.each(child, function (j, item2) {
var ca = $("<a href='javascript:;'></a>");
ca.attr("data-url", item2.href);
ca.attr("lay-id", item2.id);
var css2 = item2.css;
if (css2 != null && css2 != "") {
ca.append("<i aria-hidden='true' class='fa " + css2 + "'></i>");
}
ca.append("<cite>" + item2.name + "</cite>");
var dd = $("<dd></dd>");
dd.append(ca);
var dl = $("<dl class='layui-nav-child'></dl>");
dl.append(dd);
parentElement.append(dl);
// 递归
setChild(dd, item2.child);
});
}
}
// 登陆用户头像昵称
showLoginInfo();
function showLoginInfo() {
$.ajax({
type: "get",
url: ctxPath + "/users/current",
async: false,
success: function (data) {
$(".admin-header-user span").text(data.nickname);
$("#iframe").attr("src", "pages/home.html");
var pro = window.location.protocol;
var host = window.location.host;
var domain = pro + "//" + host;
var sex = data.sex;
var url = data.headImgUrl;
if (url == null || url == "") {
if (sex == 1) {
url ="img/avatars/sunny.png";
} else {
url ="img/avatars/1.png";
}
url =url;
} else {
url = domain + "/statics" + url;
}
var img = $(".admin-header-user img");
img.attr("src", url);
},
});
}
function logout() {
$.ajax({
type: "get",
url: ctxPath + "/logout",
success: function (data) {
localStorage.removeItem("token");
location.href = contentPath+'/login.html';
},
});
}
showCloud(1);
function showCloud(type) {
var pro = window.location.protocol;
var host = window.location.host;
var domain = pro + "//" + host;
var img = $(".admin-header-user-cloud img");
if (type == 1) {
img.attr("src", "img/cloudDownloadGrey.png");
} else {
img.attr("src", "img/cloudDownload.png");
}
}
// 获取img元素
var cloudImg = document.getElementById("cloud-img");
// 监听鼠标移入事件
cloudImg.addEventListener("mouseenter", function () {
// 鼠标移入img时的操作
showCloud(2);
//获取下载记录
getDownloadRecord();
});
function downloads(type) {
// alert(type)
}
// 监听鼠标移出事件
cloudImg.addEventListener("mouseleave", function () {
// 鼠标移出img时的操作
showCloud(1);
});
// 获取img元素
var cloudTmgContainer = document.getElementById("cloud-img-container");
// 监听鼠标移入事件
cloudTmgContainer.addEventListener("mouseenter", function () {
// 鼠标移入img时的操作
showCloud(2);
});
// 监听鼠标移出事件
cloudTmgContainer.addEventListener("mouseleave", function () {
// 鼠标移出img时的操作
showCloud(1);
});
function getDownloadRecord() {
$.ajax({
type: "post",
url: ctxPath + "/users/getDownloadRecord",
dataType: "json", // 服务器返回数据类型
async: false,
data: {},
success: function (data) {
var resMsg = data.resMsg;
if ("数据获取成功" == resMsg) {
let info = data.obj.fileBeans;
$("#cloud-img-record").empty();
var htmls = "";
if (info == null || info.length == 0) {
htmls = "<dd><span >暂无记录</span></dd>";
} else {
for (let i = 0; i < info.length; i++) {
var typePath = getTypeImg(info[i].fileName);
htmls +=
'<dd style="width:280px;height:50px">' +
'<div style="float:left">' +
'<img src="' +
typePath +
'" width="45"/>' +
"</div>" +
'<div style="float:left;color:gray;width:150px;font-size: 12px">' +
'<div style="height:20px;margin-top: 0px;font-size: 10px">' +
determineStrLength(info[i].fileName) +
"</div>" +
'<div style="height:20px;font-size: 10px">2023-10-11 10:10:10</div>' +
"</div>" +
"<div>" +
'<img src="./img/login/filetype/download.png" onclick="download(\'' +
info[i].filePath +
"','" +
info[i].fileName +
'\')" width="20"/>' +
'<img src="./img/login/filetype/chanel.png" onclick="chanel(\'' +
info[i].id +
'\')" width="20" style="margin-left: 5px"/>' +
"</div>" +
"</dd>";
}
}
$("#cloud-img-record").append(htmls);
}
},
error: function (err) {
console.log("获取下载记录数据出错:", err);
},
});
}
function chanel(id) {
var index = layer.confirm(
"确定要删除吗?",
{
btn: ["确定", "取消"],
},
function () {
$.ajax({
type: "POST",
async: false, // 默认异步true,false表示同步
url: ctxPath + "/users/delRecordById", // 请求地址
contentType: "application/x-www-form-urlencoded",
dataType: "json", // 服务器返回数据类型
data: { id: id }, //获取提交的表单字段
success: function (data) {
var resMsg = data.resMsg;
layer.close(index);
if ("数据获取成功" === resMsg) {
layer.msg("删除成功", { icon: 1, timeout: 2000 });
} else {
layer.msg("删除失败", { icon: 2, timeout: 2000 });
}
},
});
}
);
}
function delAll() {
var index = layer.confirm(
"确定清空记录吗?",
{
btn: ["确定", "取消"],
},
function () {
$.ajax({
type: "POST",
async: false, // 默认异步true,false表示同步
url: ctxPath + "/users/delAll", // 请求地址
contentType: "application/x-www-form-urlencoded",
dataType: "json", // 服务器返回数据类型
success: function (data) {
var resMsg = data.resMsg;
layer.close(index);
if ("数据获取成功" === resMsg) {
layer.msg("删除成功", { icon: 1, timeout: 2000 });
} else {
layer.msg("删除失败", { icon: 2, timeout: 2000 });
}
},
});
}
);
}
function determineStrLength(str) {
if (str.length > 13) {
return str.substr(0, 11) + "..";
}
return str;
}
var active;
layui.use(["layer", "element"], function () {
var $ = layui.jquery,
layer = layui.layer;
var element = layui.element; //导航的hover效果、二级菜单等功能需要依赖element模块
element.on("nav(demo)", function (elem) {
//layer.msg(elem.text());
});
//触发事件
active = {
tabAdd: function (obj) {
var lay_id = $(this).attr("lay-id");
var title =
$(this).html() +
'<i class="layui-icon" data-id="' +
lay_id +
'"></i>';
//新增一个Tab项
element.tabAdd("admin-tab", {
title: title,
content:
'<iframe src="' + $(this).attr("data-url") + '"></iframe>',
id: lay_id,
});
element.tabChange("admin-tab", lay_id);
},
tabDelete: function (lay_id) {
element.tabDelete("admin-tab", lay_id);
},
tabChange: function (lay_id) {
element.tabChange("admin-tab", lay_id);
},
};
//添加tab
$(document).on("click", "a", function () {
if (
!$(this)[0].hasAttribute("data-url") ||
$(this).attr("data-url") === ""
) {
return;
}
var tabs = $(".layui-tab-title").children();
var lay_id = $(this).attr("lay-id");
for (var i = 0; i < tabs.length; i++) {
if ($(tabs).eq(i).attr("lay-id") == lay_id) {
active.tabChange(lay_id);
return;
}
}
active["tabAdd"].call(this);
resize();
});
//iframe自适应
function resize() {
var $content = $(".admin-nav-card .layui-tab-content");
$content.height($(this).height() - 147);
$content.find("iframe").each(function () {
$(this).height($content.height());
});
}
$(window)
.on("resize", function () {
var $content = $(".admin-nav-card .layui-tab-content");
$content.height($(this).height() - 147);
$content.find("iframe").each(function () {
$(this).height($content.height());
});
})
.resize();
//toggle左侧菜单
$(".admin-side-toggle").on("click", function () {
var sideWidth = $("#admin-side").width();
if (sideWidth === 200) {
$("#admin-body").animate({
left: "0",
});
$("#admin-footer").animate({
left: "0",
});
$("#admin-side").animate({
width: "0",
});
} else {
$("#admin-body").animate({
left: "200px",
});
$("#admin-footer").animate({
left: "200px",
});
$("#admin-side").animate({
width: "200px",
});
}
});
//手机设备的简单适配
var treeMobile = $(".site-tree-mobile"),
shadeMobile = $(".site-mobile-shade");
treeMobile.on("click", function () {
$("body").addClass("site-mobile");
});
shadeMobile.on("click", function () {
$("body").removeClass("site-mobile");
});
});
function getTypeImg(str) {
if (
str.toLowerCase().indexOf("xlsx") > 0 ||
str.toLowerCase().indexOf("xls") > 0
) {
return "./img/login/filetype/excel.png";
} else if (str.toLowerCase().indexOf("pdf") > 0) {
return "./img/login/filetype/pdf.png";
} else if (str.toLowerCase().indexOf("ppt") > 0) {
return "./img/login/filetype/ppt.png";
} else if (str.toLowerCase().indexOf("txt") > 0) {
return "./img/login/filetype/txt.png";
} else if (str.toLowerCase().indexOf("vsdx") > 0) {
return "./img/login/filetype/visio.png";
} else if (
str.toLowerCase().indexOf("doc") > 0 ||
str.toLowerCase().indexOf("docx") > 0
) {
return "./img/login/filetype/word.png";
} else if (
str.toLowerCase().indexOf("zip") > 0 ||
str.toLowerCase().indexOf("rar") > 0
) {
return "./img/login/filetype/zip.png";
}
}
/**
* 获取 blob
* @param {String} url 目标文件地址
* @return {Promise}
*/
function getBlob(url) {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response);
}
};
xhr.send();
});
}
/**
* 保存
* @param {Blob} blob
* @param {String} filename 想要保存的文件名称
*/
function saveAs(blob, filename) {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename);
} else {
const link = document.createElement("a");
const body = document.querySelector("body");
link.href = window.URL.createObjectURL(blob);
link.download = filename;
// fix Firefox
link.style.display = "none";
body.appendChild(link);
link.click();
body.removeChild(link);
window.URL.revokeObjectURL(link.href);
}
}
/**
* 下载
* @param {String} url 目标文件地址
* @param {String} filename 想要保存的文件名称
*/
function download(url, filename) {
var truePath;
// window.location.href = '/download/download?filePath=' + data.filePath;
if (url.indexOf("http") != -1) {
truePath = url;
} else {
truePath = ctxPath + "/statics/" + url;
}
getBlob(truePath).then((blob) => {
saveAs(blob, filename);
});
}