gxd-vue-library
Version:
依赖与element Ui插件库,聚福宝福利PC端插件库
96 lines (82 loc) • 2.31 kB
JavaScript
;
import Vue from 'vue';
let __iframe = null;
class xdFrame {
constructor(){
this.setDoMain();
this.bus = new Vue();
}
setDoMain(){
let reg = /^(.*)\.(.*)\.(com|cn|org)$/;
let host = '' || window.location.host;
if(reg.test(host)) {
let temp = host.split('.');
temp = temp.splice(-2);
document.domain = temp.join('.');
}
}
/**
* @description 获取父窗口window对象
* @returns {any | Window}
*/
getParent(){
return parent.window
}
/**
* @description 获取子窗口window对象
* @param iframeId
* @returns {WindowProxy}
*/
getChildren(iframeId){
return document.getElementById(iframeId).contentWindow;
}
/**
* @description 发送数据方法
* @param event {String} 事件名称
* @param data {Object} 数据对象
* @param isParent {Boolean} 发送给子或者父窗口 默认:true true=>子传父 false=> 父传子
* @param iframeID {String} 当是获取子窗口时候,需要iframeID
*/
send(event, data , isParent=true, iframeID = ''){
let win = null;
if(isParent) win = this.getParent(); //获取父窗口window对象
else win = this.getChildren(iframeID); //获取子窗口window对象
win.XdBus.message(event, data); //调用子或者父窗口中的预设方法message方法进行数据透传
}
/**
* @description 接受数据方法(把接受到的数据发送给总线bus)
* @param event {String} 事件名称
* @param data {Object} 数据
*/
message(event, data){
this.bus.$emit(event, data);
}
/**
* @description 注册事件
* @param event {String} 事件名称
* @param callback {Function} 事件处理方法
*/
addEvent(event, callback){
this.bus.$on(event, callback);
}
/**
* @description 获取父级窗口中的全局方法引用
* @param fnName {String} 方法名字
*/
getParentApi(fnName){
return this.getParent()['$boardApi'][fnName];
}
/**
* @description 获取父级窗口中的全局属性值
* @param attr {String} 属性名字
*/
getParentData(attr) {
return this.getParent()['$boardData'][attr];
}
}
export default function () {
if(__iframe === null) {
window['XdBus'] = __iframe = new xdFrame();
}
return __iframe;
};