From e859639e0fbc18099303442ecfc280465d1672a0 Mon Sep 17 00:00:00 2001 From: binbin0314 Date: Fri, 11 Jun 2021 18:26:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=A0=E5=8F=82=E6=8E=A5=E6=94=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/sdk.js | 87 +++++++++++++++++++++++++----------------- src/App.vue | 91 ++++++++++++++++++++++++++++++-------------- src/plugins/axios.js | 5 +-- 3 files changed, 117 insertions(+), 66 deletions(-) diff --git a/public/sdk.js b/public/sdk.js index 35b9312..d4759bf 100644 --- a/public/sdk.js +++ b/public/sdk.js @@ -1,81 +1,98 @@ +/* + * @Author: aBin + * @email: binbin0314@126.com + * @Date: 2021-04-19 10:23:19 + * @LastEditors: aBin + * @LastEditTime: 2021-06-09 09:24:35 + */ +/** + * ccsens tall sdk.js + * v1.0.0 + * 父组件调用 TallPlugin.init()即可 + * 监听message消息, 如果是created消息 就把tall的参数传递过来 + */ + (function(window) { + // 单例局部变量 var _instance = null; + // 对外暴露TallPlugin类 window.TallPlugin = function(config) { this.config = config; this.props = null; + this.parent = '*'; }; // 初始化并保证是单例 - TallPlugin.init = function(config) { + TallPlugin.init = function(callback, config) { if (!_instance) { _instance = new TallPlugin(config); + _instance.config = config; + _instance.parent = window.parent.origin; + // DOM加载完成 + window.addEventListener('DOMContentLoaded', _instance.mounted, false); + // window onload + window.addEventListener('load', _instance.loaded, false); + // destroy + window.addEventListener('unload', _instance.destroy, false); + // error + window.addEventListener('error', _instance.error, false); + _instance.onMessage(callback); } return _instance; }; - function postMsg(message) { - let origin = '*'; - window.postMessage(message, origin); - } - - TallPlugin.prototype.created = function(callback) { - console.log('created begin'); + TallPlugin.prototype.onMessage = function(callback) { var _this = this; - postMsg('created'); window.addEventListener( 'message', - function({ data, origin }) { - console.log('on created message, data, origin: ', data, origin); + function(event) { try { - var target = JSON.parse(data); - if (target.success) { - _this.props = JSON.parse(data); - callback && typeof callback === 'function' && callback.call(_this, _this.props); + if (_this.parent === event.origin) { + // 是父窗体传来的消息 + console.log('event.data:', event.data); + var data = JSON.parse(event.data); + _this.props = data || null; + callback && typeof callback === 'function' && callback.call(_this, data); + } else { + _this.props = null; } - // else { - // _this.props = null; - // } } catch (e) { + // console.error(`TallPlugin warn: ${e}`) _this.props = null; } }, false, ); - // DOM加载完成 - window.addEventListener('DOMContentLoaded', this.mounted, false); - // window onload - window.addEventListener('load', this.loaded, false); - // destroy - window.addEventListener('unload', this.destroy, false); - // error - window.addEventListener('error', this.error, false); return this; }; + // DOMContentLoaded DOM加载完成触发 TallPlugin.prototype.mounted = function(callback) { - console.log('mounted'); - postMsg('mounted'); + var _this = this; + window.postMessage('created', _this.parent); callback && typeof callback === 'function' && callback.call(this); return this; }; - + // load window onload触发 TallPlugin.prototype.loaded = function(callback) { - console.log('loaded'); - postMsg('loaded'); + var _this = this; + window.postMessage('loaded', _this.parent); callback && typeof callback === 'function' && callback.call(this); return this; }; + // 子窗体销毁触发 TallPlugin.prototype.destroy = function(callback) { - console.log('destroy'); - postMsg('destroy'); + var _this = this; + window.postMessage('destroy', _this.parent); callback && typeof callback === 'function' && callback.call(this); return this; }; + // error触发 TallPlugin.prototype.error = function(callback) { - console.log('error'); - postMsg('error'); + var _this = this; + window.postMessage('error', _this.parent); callback && typeof callback === 'function' && callback.call(this); return this; }; diff --git a/src/App.vue b/src/App.vue index 82a7ba4..c884870 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,12 +3,12 @@ * @email: 18603454788@163.com * @Date: 2021-04-19 10:23:19 * @LastEditors: aBin - * @LastEditTime: 2021-05-31 15:55:52 + * @LastEditTime: 2021-06-11 15:50:57 -->