import Vue from 'vue' /** * 全局商品预览弹窗方法 * @param {Object} options 预览参数 * @param {String} options.title 商品标题 * @param {String} options.imgUrl 商品图片地址 * @param {String} options.tipTitle 商品标签 * @param {String} options.productType 商品类型 * @param {String} options.probability 商品概率 * @param {Array} options.extraInfo 额外信息 * @returns {Promise} 返回Promise */ const preview = function(options = {}) { return new Promise((resolve) => { // 获取当前页面 const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; // 尝试获取全局预览组件 let previewComponent = null; // 尝试在App.vue中找到全局组件 try { const app = getApp(); if (app.$vm && app.$vm.$children) { // 在App.vue的子组件中查找 for (const child of app.$vm.$children) { if (child.$options.name === 'DetailPreviewPopup') { previewComponent = child; break; } } } } catch (e) { console.error('获取全局组件失败', e); } // 如果没找到全局组件,尝试在当前页面找 if (!previewComponent && currentPage && currentPage.$vm) { try { // 查找页面中的预览组件 const pageVm = currentPage.$vm; for (const child of pageVm.$children) { if (child.$options.name === 'DetailPreviewPopup') { previewComponent = child; break; } } } catch (e) { console.error('获取页面组件失败', e); } } // 如果还是没找到组件,发送全局事件 if (!previewComponent) { console.log('未找到组件实例,发送全局事件'); uni.$emit('global-preview-event', options); setTimeout(() => resolve(), 500); return; } // 使用找到的组件 console.log('找到预览组件,直接调用'); previewComponent.setPreviewData(options); setTimeout(() => resolve(previewComponent), 300); }); }; // 挂载到Vue原型上 Vue.prototype.$preview = preview; export default preview;