|
@@ -0,0 +1,74 @@
|
|
|
+import Vue from 'vue'
|
|
|
+import PCLeaveDialog from '@/lib/pc/content-dialog.vue'
|
|
|
+import MobileLeavePopup from '@/lib/mobile/content-popup.vue'
|
|
|
+import { isDOMElement } from '@/utils/utils'
|
|
|
+
|
|
|
+const instanceMap = {
|
|
|
+ wx: null,
|
|
|
+ app: null,
|
|
|
+ pc: null,
|
|
|
+}
|
|
|
+
|
|
|
+export function createInstance(platform, options = {}) {
|
|
|
+ const tryInstance = instanceMap[platform]
|
|
|
+ if (tryInstance) {
|
|
|
+ console.log('已存在实例, 返回已存在的实例', tryInstance)
|
|
|
+ return tryInstance
|
|
|
+ }
|
|
|
+
|
|
|
+ const componentMap = {
|
|
|
+ wx: MobileLeavePopup,
|
|
|
+ app: MobileLeavePopup,
|
|
|
+ pc: PCLeaveDialog,
|
|
|
+ }
|
|
|
+ const MyComponent = componentMap[platform]
|
|
|
+ if (!MyComponent)
|
|
|
+ return console.error('未定义平台')
|
|
|
+
|
|
|
+ const { source } = options
|
|
|
+
|
|
|
+ // 创建组件构造器
|
|
|
+ const ComponentClass = Vue.extend(MyComponent)
|
|
|
+ // 创建实例并挂载到临时元素
|
|
|
+ const instance = new ComponentClass({
|
|
|
+ propsData: {
|
|
|
+ platform,
|
|
|
+ source,
|
|
|
+ }
|
|
|
+ }).$mount() // 不传入选择器,手动挂载
|
|
|
+ instanceMap[platform] = instance
|
|
|
+ // 销毁组件
|
|
|
+ // instance.$destroy();
|
|
|
+ // instance.$el.remove();
|
|
|
+ return instance
|
|
|
+}
|
|
|
+
|
|
|
+export async function doLeave(options = {}) {
|
|
|
+ const {
|
|
|
+ source, // 必传
|
|
|
+ source_desc, // 必传
|
|
|
+ el = 'body', // 传选择器字符串,或者 DOM 元素
|
|
|
+ platform = 'pc',
|
|
|
+ } = options
|
|
|
+
|
|
|
+ if (!source) {
|
|
|
+ return console.error('source必传')
|
|
|
+ }
|
|
|
+ if (!source_desc) {
|
|
|
+ return console.error('source_desc必传')
|
|
|
+ }
|
|
|
+
|
|
|
+ const instance = createInstance(platform)
|
|
|
+ if (instance) {
|
|
|
+ instance.source = source
|
|
|
+ instance.sourceDesc = source_desc
|
|
|
+ instance.updateVisible(true)
|
|
|
+ // 将组件的 DOM 添加到页面
|
|
|
+ if (isDOMElement(el)) {
|
|
|
+ el.appendChild(instance.$el)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ document.querySelector(el).appendChild(instance.$el)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|