Files
UserhomeFront/public/js/bschload.original.bak.js
chenxing 4279f99be1
deploy-userbase-front / build-and-deploy (push) Failing after 1m54s
build / build (push) Successful in 3m36s
feat: 接入 UserHome 前端业务代码(含真实 src/依赖,保留 projman 标准 CI)
2026-09-09 22:37:59 +08:00

129 lines
3.9 KiB
JavaScript

/**
* 数字基座内嵌加载帮助类
*/
class BschLoadUtil {
#isDev = false;
#appId = null;
#headerInfo = {};
constructor() {
if (!BschLoadUtil.instance) {
this.isSelf = window.self === window.top;
this.ticket = null;
this.roleInfo = {
roleId: null,
schId: null,
childId: null,
};
BschLoadUtil.instance = this;
}
return BschLoadUtil.instance;
}
getTicket() {
if (this.#isDev) return Promise.resolve({ ticket: this.ticket, roleInfo: this.roleInfo });
else
return new Promise((resolve, reject) => {
window.addEventListener(
"message",
e => {
var { id, ticket, roleInfo, headerInfo } = this.roleDecode(e.data);
this.#appId = id;
this.#headerInfo = headerInfo;
this.ticket = ticket;
this.roleInfo = roleInfo;
resolve({ ticket, roleInfo });
},
false
);
});
}
replaceHeaderStyle(el) {
if (this.#headerInfo == null) return false;
const { appName, avatar, username } = this.#headerInfo;
let headerStr = `
<header class="header-container fix">
<h4 class="fl align-middle">${appName}</h4>
<div class="header-aside fr">
<img id="js_comp_user_phone" src="${avatar}" class="fl align-middle">
<a id="js_comp_user_name" class="name fl align-middle text-ellipsis" href="javascript:void(0);">${username}</a>
<div class="delimiter fl align-middle"></div>
<a href="javascript:;" id="js_comp_operate" class="logout fl align-middle" title="返回桌面">
<i class="iconfont icon-shouye"></i>
</a>
</div>
</header>
`;
const _this = this;
$(el).append(headerStr);
$(el)
.find("#js_comp_user_phone")
.click(() => _this.showUserInfo());
$(el)
.find("#js_comp_user_name")
.click(() => _this.showUserInfo());
$(el)
.find("#js_comp_operate")
.click(() => _this.goBackDesktop());
}
goBackDesktop() {
if (this.#isDev) {
console.error("调试模式,无效操作!");
return;
}
parent.postMessage(JSON.stringify({ id: this.#appId, code: 1 }), "*");
}
reloadParent() {
if (this.#isDev) {
console.error("调试模式,无效操作!");
return;
}
parent.postMessage(JSON.stringify({ id: this.#appId, code: 2 }), "*");
}
showUserInfo() {
if (this.#isDev) {
console.error("调试模式,无效操作!");
return;
}
parent.postMessage(JSON.stringify({ id: this.#appId, code: 3 }), "*");
}
roleDecode(roleInfoStr) {
try {
var str = window.atob(roleInfoStr);
str = decodeURIComponent(str);
var obj = JSON.parse(str);
return obj;
} catch (error) {
console.error(error);
return {
id: null,
ticket: "",
roleInfo: {},
};
}
}
setDevTicket(ticket, roleInfo) {
if (this.isSelf) {
this.ticket = ticket;
this.roleInfo = roleInfo;
this.#isDev = true;
}
}
getIsSelf() {
if (this.#isDev) return false;
else return this.isSelf;
}
static getInstance() {
if (!this.instance) {
return (this.instance = new BschLoadUtil());
}
return this.instance;
}
}