/** * 数字基座内嵌加载帮助类 */ 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 = `

${appName}

${username}
`; 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; } }