Files
BlackFruit-UI/common/common.js
yiqiu a35593bf8a
All checks were successful
continuous-integration/drone/push Build is passing
修复
2025-11-21 17:41:45 +08:00

569 lines
19 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.
// 消息弹窗
function showMessage(type, message, duration) {
if ($("#alert-container").length <= 0) {
$("body").append(`
<div id="alert-container"></div>
`);
}
const alertClass = "alert-" + type;
const html =
'<div class="alert ' +
alertClass +
' show alert-dismissible" role="alert">' +
message +
'<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
'<span aria-hidden="true">&times;</span>' +
"</button>" +
"</div>";
const $alert = $(html).appendTo("#alert-container");
setTimeout(function () {
$alert.alert("close");
}, duration);
// 清空表单
}
$(function () {
const headBgcList = [
"#3699FF",
"#57C3EA",
"#5CC2D7",
"#EF8BA2",
"#C1DB81",
"#F1978C",
"#F08968",
];
// 获取账户信息和实名认证信息
const initData = () => {
if (localStorage.jwt) {
$.ajax({
url: " /console/v1/account",
method: "get",
headers: {
Authorization: "Bearer" + " " + localStorage.jwt,
},
success: function (res) {
if (res.status === 200) {
const obj = res.data.account;
$(".no-login").attr("style", "display:none");
$(".login-in").attr("style", "display:flex");
$("#username").text(res.data.account.username);
const reg = /^[a-zA-Z]+$/;
if (reg.test(res.data.account.username.substring(0, 1))) {
obj.firstName = res.data.account.username
.substring(0, 1)
.toUpperCase();
$("#headImg").text(
res.data.account.username.substring(0, 1).toUpperCase()
);
} else {
obj.firstName = res.data.account.username.substring(0, 1);
$("#headImg").text(res.data.account.username.substring(0, 1));
}
if (sessionStorage.headBgc) {
$("#headImg").attr(
"style",
`background:${sessionStorage.headBgc}`
);
} else {
const index = Math.round(
Math.random() * (headBgcList.length - 1)
);
$("#headImg").attr("style", `background:${headBgcList[index]}`);
sessionStorage.headBgc = headBgcList[index];
}
sessionStorage.accountInfo = JSON.stringify(obj);
} else {
$(".login-in").attr("style", "display:none");
$(".no-login").attr("style", "display:block");
}
},
});
$.ajax({
url: " /console/v1/certification/info",
method: "get",
headers: {
Authorization: "Bearer" + " " + localStorage.jwt,
},
success: function (res) {
if (res.status === 200) {
if (res.data.is_certification) {
$("#isCertification").attr("style", "display:inline-block");
$("#noCertification").attr("style", "display:none");
sessionStorage.is_certification = true;
} else {
$("#isCertification").attr("style", "display:none");
$("#noCertification").attr("style", "display:inline-block");
sessionStorage.is_certification = false;
}
} else {
$(".login-in").attr("style", "display:none");
$(".no-login").attr("style", "display:block");
}
},
});
} else {
$(".login-in").attr("style", "display:none");
$(".no-login").attr("style", "display:block");
}
};
// 获取通用配置信息(先用系统 /console/v1/common若有插件则再用插件数据覆盖部分字段
const getCommentInfo = () => {
$.ajax({
url: "/console/v1/common",
method: "get",
headers: {
Authorization: "Bearer" + " " + localStorage.jwt,
},
success: function (res) {
if (!res || res.status !== 200 || !res.data) {
return;
}
var baseData = res.data || {};
// 再尝试从主题配置插件读取额外配置,用于覆盖旧数据
$.ajax({
url: "/console/v1/theme/config",
method: "get",
headers: {
Authorization: "Bearer" + " " + localStorage.jwt,
},
success: function (pluginRes) {
if (pluginRes && pluginRes.status === 200 && pluginRes.data) {
var cfg = pluginRes.data || {};
// SEO如配置了插件 SEO则覆盖页面标题与 meta
if (cfg.seo) {
if (cfg.seo.title) {
document.title = cfg.seo.title;
}
if (cfg.seo.keywords) {
var $kw = $('meta[name="keywords"]');
if ($kw.length) {
$kw.attr("content", cfg.seo.keywords);
}
}
if (cfg.seo.description) {
var $desc = $('meta[name="description"]');
if ($desc.length) {
$desc.attr("content", cfg.seo.description);
}
}
}
// 覆盖基础字段:企业信息 & ICP 等
baseData.enterprise_name =
cfg.enterprise_name || baseData.enterprise_name;
baseData.enterprise_telephone =
cfg.enterprise_telephone || baseData.enterprise_telephone;
baseData.enterprise_mailbox =
cfg.enterprise_mailbox || baseData.enterprise_mailbox;
baseData.enterprise_qrcode =
cfg.enterprise_qrcode || baseData.enterprise_qrcode;
baseData.official_website_logo =
cfg.official_website_logo || baseData.official_website_logo;
baseData.online_customer_service_link =
cfg.online_customer_service_link ||
baseData.online_customer_service_link;
baseData.icp_info = cfg.icp_info || baseData.icp_info;
baseData.icp_info_link =
cfg.icp_info_link || baseData.icp_info_link;
baseData.public_security_network_preparation =
cfg.public_security_network_preparation ||
baseData.public_security_network_preparation;
baseData.public_security_network_preparation_link =
cfg.public_security_network_preparation_link ||
baseData.public_security_network_preparation_link;
baseData.telecom_appreciation =
cfg.telecom_appreciation || baseData.telecom_appreciation;
baseData.copyright_info =
cfg.copyright_info || baseData.copyright_info;
baseData.cloud_product_link =
cfg.cloud_product_link || baseData.cloud_product_link;
baseData.dcim_product_link =
cfg.dcim_product_link || baseData.dcim_product_link;
baseData.terms_service_url =
cfg.terms_service_url || baseData.terms_service_url;
baseData.terms_privacy_url =
cfg.terms_privacy_url || baseData.terms_privacy_url;
// 数组字段:优先使用插件配置
if (cfg.friendly_link) {
baseData.friendly_link = cfg.friendly_link;
}
if (cfg.honor) {
baseData.honor = cfg.honor;
}
if (cfg.partner) {
baseData.partner = cfg.partner;
}
if (cfg.feedback_type) {
baseData.feedback_type = cfg.feedback_type;
}
if (cfg.header_nav) {
baseData.header_nav = cfg.header_nav;
}
if (cfg.footer_nav) {
baseData.footer_nav = cfg.footer_nav;
}
if (cfg.side_floating_window) {
baseData.side_floating_window = cfg.side_floating_window;
}
}
sessionStorage.commentData = JSON.stringify(baseData);
setCommData();
},
error: function () {
// 插件不可用则直接使用系统默认配置
sessionStorage.commentData = JSON.stringify(baseData);
setCommData();
},
});
},
});
};
// 根据插件配置重绘顶部导航header_nav
const renderHeaderNav = (commentObj) => {
var navData = commentObj && commentObj.header_nav;
if (!Array.isArray(navData) || navData.length === 0) {
return;
}
// 更新 Logo 点击链接
var home = navData[0] || {};
if (home.file_address) {
$(".nav-icon a").attr("href", home.file_address);
}
var $menu = $(".nav-menu");
var $navContSection = $(".nav-cont .section-content");
if (!$menu.length || !$navContSection.length) {
return;
}
// 顶部一级导航
$menu.empty();
for (var i = 1; i < navData.length; i++) {
var item = navData[i] || {};
var name = item.name || "";
var file = item.file_address || "";
var blank = Number(item.blank) === 1;
if (!name) {
continue;
}
if (file) {
var $a = $("<a></a>")
.attr("href", file)
.attr("target", blank ? "_blank" : "_self");
$a.append($("<div></div>").addClass("nav-item").text(name));
$menu.append($a);
} else {
$menu.append($("<div></div>").addClass("nav-item").text(name));
}
}
// 下拉菜单
$navContSection.empty();
for (var j = 1; j < navData.length; j++) {
var parent = navData[j] || {};
var children = Array.isArray(parent.children) ? parent.children : [];
if (!children.length) {
$navContSection.append(
$("<div></div>").addClass("nav-cont-menu nav-cont-empty")
);
continue;
}
var $menuWrap = $("<div></div>").addClass(
"nav-cont-menu animated slideInDown"
);
var $content = $("<div></div>").addClass("nav-content");
children.forEach(function (child) {
if (!child) return;
var cname = child.name || "";
var curl = child.file_address || "";
var cblank = Number(child.blank) === 1;
var cicon = child.icon || "";
var cdesc = child.description || "";
if (!cname && !curl) return;
var $link = $("<a></a>");
if (curl) {
$link.attr("href", curl).attr("target", cblank ? "_blank" : "_self");
} else {
$link.attr("href", "javascript:;");
}
var $box = $("<div></div>").addClass("nav-item-box");
if (cicon) {
$box.append($("<img>").attr("src", cicon).attr("alt", ""));
}
var $titleWrap = $("<div></div>").addClass("item-box-title");
$titleWrap.append($("<div></div>").addClass("title").text(cname));
$titleWrap.append($("<div></div>").addClass("desc").text(cdesc));
$box.append($titleWrap);
$link.append($box);
$content.append($link);
});
$menuWrap.append($content);
$navContSection.append($menuWrap);
}
// 重绑 hover 事件,避免新节点没有事件
$(".nav-menu .nav-item").off("mouseenter mouseleave");
$(".nav-cont").off("mouseenter mouseleave");
initHeader();
};
// 设置通用信息函数
const setCommData = () => {
const commentObj = JSON.parse(sessionStorage.commentData);
$("#enterprise_name").text(commentObj.enterprise_name);
$(".com-contact-tel").text(commentObj.enterprise_telephone);
$("#enterprise_telephone").text(
`联系电话:${commentObj.enterprise_telephone}`
);
$("#enterprise_mailbox").text(`联系邮箱:${commentObj.enterprise_mailbox}`);
$("#enterprise_qrcode").attr("src", commentObj.enterprise_qrcode);
$("#logo").attr("src", commentObj.official_website_logo);
if (commentObj.friendly_link.length > 0) {
$("#index #footerLink").attr("style", "display: block;");
// 先清空,再填充,避免重复叠加
$("#footerLink a").remove();
commentObj.friendly_link.forEach((item) => {
$("#footerLink").append(
`<a href=${item.url} target="_blank" rel="nofollow">${item.name}</a>`
);
});
} else {
$("#footerLink").attr("style", "display: none;");
}
// 统一覆盖底部备案信息和版权
$("#footerRecord").html(`
<div class='left-info'>
<a href='${commentObj.icp_info_link}' target="_blank" rel="nofollow">${commentObj.icp_info}</a>
<a href='${commentObj.public_security_network_preparation_link}' target="_blank" rel="nofollow">${commentObj.public_security_network_preparation}</a>
<span>${commentObj.telecom_appreciation}</span>
</div>
<span>${commentObj.copyright_info}</span>
`);
$("#terms_service_url").click(function () {
location.href = commentObj.terms_service_url;
});
$("#terms_privacy_url").click(function () {
location.href = commentObj.terms_privacy_url;
});
$(".buy-cloud").click(function () {
location.href = commentObj.cloud_product_link;
});
$(".buy-dcim-btn").click(function () {
location.href = commentObj.dcim_product_link;
});
// 顶部导航改用插件 header_nav
renderHeaderNav(commentObj);
};
$(".line-server-btn").click(function () {
const commentObj = sessionStorage.commentData
? JSON.parse(sessionStorage.commentData)
: null;
if (commentObj && commentObj.online_customer_service_link) {
window.open(commentObj.online_customer_service_link);
}
});
// 跳转函数
const goOtherPage = (url) => {
sessionStorage.redirectUrl = location.href;
location.href = url;
};
function initHeader() {
let showIndex = 0;
$(".nav-menu .nav-item").hover(
function () {
const index = $(".nav-menu .nav-item").index($(this));
$(".nav-cont .nav-cont-menu")
.eq(index)
.attr("style", "display: block;");
// $('.nav-cont').attr('style','display: block;')
if (
!$(".nav-cont .nav-cont-menu").eq(index).hasClass("nav-cont-empty")
) {
const height = $(".nav-cont .nav-cont-menu").eq(index).height();
$(".nav-cont").attr("style", `height: ${height}px;`);
}
showIndex = index;
},
function () {
const index = $(".nav-menu .nav-item").index($(this));
$(".nav-cont ").eq(index).attr("style", "display: none;");
$(".nav-cont .nav-cont-menu").eq(index).attr("style", "display: none;");
$(".nav-cont").attr("style", "height:0");
}
);
$(".nav-cont").hover(
function () {
//$('.nav-cont ').attr('style','display: block;')
$(".nav-cont .nav-cont-menu")
.eq(showIndex)
.attr("style", "display: block;");
//if (showIndex != 0) {
if (!$(this).hasClass("nav-cont-empty")) {
const height = $(".nav-cont .nav-cont-menu").eq(showIndex).height();
$(".nav-cont").attr("style", `height: ${height}px;`);
}
},
function () {
//$('.nav-cont ').attr('style','display: none;')
$(".nav-cont .nav-cont-menu")
.eq(showIndex)
.attr("style", "display: none;");
$(".nav-cont").attr("style", "height:0");
}
);
if (localStorage.jwt) {
if (sessionStorage.accountInfo) {
const obj = JSON.parse(sessionStorage.accountInfo);
$(".no-login").attr("style", "display:none");
$(".login-in").attr("style", "display:flex");
$("#username").text(obj.username);
$("#headImg").text(obj.firstName);
$("#headImg").attr("style", `background:${sessionStorage.headBgc}`);
if (sessionStorage.is_certification == true) {
$("#isCertification").attr("style", "display:inline-block");
$("#noCertification").attr("style", "display:none");
} else {
$("#isCertification").attr("style", "display:none");
$("#noCertification").attr("style", "display:inline-block");
}
}
initData();
} else {
$(".login-in").attr("style", "display:none");
$(".no-login").attr("style", "display:block");
}
// 退出登录
$("#logout").click(function () {
localStorage.removeItem("jwt");
initData();
});
// 点击登录
$("#loginBtn").click(function () {
goOtherPage("/login.htm");
});
// 点击注册
$("#registBtn").click(function () {
goOtherPage("/regist.htm");
});
// 点击账户信息
$("#accountBtn").click(function () {
location.href = "/account.htm";
});
// 未付款订单
$("#financeBtn").click(function () {
location.href = "/finance.htm";
});
// 我的工单
$("#ticketBtn").click(function () {
location.href = "/plugin/27/ticket.htm";
});
// 购物车
$("#shopping-cart").click(function () {
location.href = "/cart/shoppingCar.htm";
});
}
function initFooter() {
// 始终从后端获取最新通用配置,避免缓存导致配置不生效
getCommentInfo();
}
// 首页渲染
// $("#header").load("/web/BlackFruit-web/public/header.html", function () {
// initHeader();
// });
initHeader();
// 底部渲染
// $("#footer").load("/web/BlackFruit-web/public/footer.html", function () {
// initFooter();
// });
initFooter();
const resize = function () {
const width = $(window).width();
const num = width / 1400;
if (1000 < width && width < 1440) {
$("section").attr(
"style",
"width:1400px;transform: scaleX(" + num + ");transform-origin: 0 0;"
);
} else {
$("section").attr("style", "");
}
};
resize();
window.addEventListener("resize", resize);
$(".input-search-s ").click(function () {
$(".input-search-select .select-box").toggle();
});
$(".input-search-r").click(function () {
$(".input-search-select .select-box").toggle();
});
$(".input-search-select").on("click", ".select-box-item", function () {
$(".input-search-text").text($(this).text());
$(".input-search-select .select-box").toggle();
});
/* 招聘tab切换 */
$(".recuit-btn-group a").each(function (ind, el) {
$(el).click(function () {
$(this).addClass("active").siblings().removeClass("active");
$(".recuit-content .recuit-box").eq(ind).show().siblings().hide();
});
});
/* 常见问题toggle */
$(".agent-question .item").eq(0).addClass("active");
$(".agent-question .item").eq(0).find(".des").slideDown(300);
$(".agent-question .item").each(function (ind, el) {
$(el)
.find(".open")
.click(function () {
$(this).parent(".title").parent(".item").find(".des").slideDown(300);
$(this)
.parent(".title")
.parent(".item")
.addClass("active")
.siblings()
.removeClass("active")
.find(".des")
.slideUp(300);
});
$(el)
.find(".down")
.click(function () {
$(this)
.parent(".des")
.slideUp(300)
.parent(".item")
.removeClass("active");
});
});
$("#documentBtn").click(function () {
location.href = "./document.html";
});
$(".go-ticket-btn").click(function () {
location.href = "home.htm";
});
});