// @/core/utils/floatWindowManager.js // 实际的浮窗操作类 class FloatWindowManager { constructor() { this.floatView = null; this.badgeCount = 0; this.isInitialized = false; this._customClickListener = null; // 添加:浮窗位置状态管理 this.currentPosition = { left: 100, top: 100 }; this.floatSize = { width: 40, height: 40 }; } /** * 初始化浮窗 * @param {Object} options - 初始化选项 */ init(options = {}) { //console.log('[FloatWindow] 开始初始化浮窗...'); if (this.isInitialized) { //console.warn('[FloatWindow] 浮窗管理器已初始化,跳过重复初始化'); return Promise.resolve(true); } return new Promise((resolve, reject) => { try { // #ifdef APP-PLUS //console.log('[FloatWindow] 检测到 APP-PLUS 环境'); if (!plus) { const error = new Error('plus 对象不存在'); //console.error('[FloatWindow] 错误:', error); reject(error); return; } if (!plus.nativeObj) { const error = new Error('plus.nativeObj 对象不存在'); //console.error('[FloatWindow] 错误:', error); reject(error); return; } //console.log('[FloatWindow] 检查通过,开始配置...'); // 默认配置 const config = { size: { width: 40, height: 40 }, position: { }, opacity: 0.5, ...options }; //console.log('[FloatWindow] 配置信息:', config); try { // 获取屏幕信息 const systemInfo = uni.getSystemInfoSync(); console.log('[FloatWindow] 系统信息:', { windowWidth: systemInfo.windowWidth, windowHeight: systemInfo.windowHeight }); const screenWidth = systemInfo.windowWidth || 375; const screenHeight = systemInfo.windowHeight || 667; // 计算默认位置(右下角) const defaultLeft = screenWidth - config.size.width - 10; const defaultTop = screenHeight - config.size.height - 100; // 更新内部位置状态 this.currentPosition.left = defaultLeft; this.currentPosition.top = defaultTop; this.floatSize.width = config.size.width; this.floatSize.height = config.size.height; /*console.log('[FloatWindow] 计算默认位置:', { defaultLeft, defaultTop, currentPosition: this.currentPosition, floatSize: this.floatSize });*/ // 清理旧的浮窗 this._safeDestroy(); //console.log('[FloatWindow] 开始创建 Native View...'); // 创建新的浮窗 this.floatView = new plus.nativeObj.View('globalFloatView', { top: this.currentPosition.top + 'px', left: this.currentPosition.left + 'px', width: this.floatSize.width + 'px', height: this.floatSize.height + 'px', opacity: config.opacity, position: 'absolute' }); /*console.log('[FloatWindow] Native View 创建成功:', this.floatView,{ top: this.currentPosition.top + 'px', left: this.currentPosition.left + 'px', width: this.floatSize.width + 'px', height: this.floatSize.height + 'px', opacity: config.opacity, position: 'absolute' });*/ // 绘制内容 this._safeDrawContent(); // 添加事件监听 this._safeAddEventListeners(); this.isInitialized = true; //console.log('[FloatWindow] ✅ 浮窗初始化成功'); resolve(true); } catch (innerError) { //console.error('[FloatWindow] ❌ 内部初始化错误:', innerError); //console.error('[FloatWindow] 错误堆栈:', innerError.stack); reject(innerError); } // #endif // #ifndef APP-PLUS //console.warn('[FloatWindow] 非APP环境,跳过浮窗初始化'); const error = new Error('非APP环境,无法创建浮窗'); reject(error); // #endif } catch (outerError) { //console.error('[FloatWindow] ❌ 初始化外层错误:', outerError); //console.error('[FloatWindow] 错误堆栈:', outerError.stack); reject(outerError); } }); } /** * 显示浮窗 */ show() { //console.log('[FloatWindow] 显示浮窗请求...'); if (!this.floatView) { //console.warn('[FloatWindow] 浮窗未初始化,无法显示'); return false; } try { //console.log('[FloatWindow] 正在显示浮窗...'); this.floatView.show(); //console.log('[FloatWindow] ✅ 浮窗显示成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 显示浮窗失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 隐藏浮窗 */ hide() { //console.log('[FloatWindow] 隐藏浮窗请求...'); if (!this.floatView) { //console.warn('[FloatWindow] 浮窗未初始化,无法隐藏'); return false; } try { //console.log('[FloatWindow] 正在隐藏浮窗...'); this.floatView.hide(); //console.log('[FloatWindow] ✅ 浮窗隐藏成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 隐藏浮窗失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 设置角标数量 * @param {number} count - 角标数量 */ setBadge(count) { //console.log('[FloatWindow] 设置角标请求,数量:', count); if (!this.floatView) { //console.warn('[FloatWindow] 浮窗未初始化,无法设置角标'); return false; } try { if (typeof count !== 'number') { //console.warn('[FloatWindow] 角标数量必须是数字,当前:', count); count = parseInt(count) || 0; } this.badgeCount = Math.max(0, Math.floor(count)); //console.log('[FloatWindow] 更新角标数量为:', this.badgeCount); // 清除并重新绘制 //console.log('[FloatWindow] 重新绘制浮窗内容...'); this.floatView.reset(); this._safeDrawContent(); if (this.badgeCount > 0) { //console.log('[FloatWindow] 绘制角标...'); this._safeDrawBadge(); } //console.log('[FloatWindow] ✅ 角标设置成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 设置角标失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 清除角标 */ clearBadge() { //console.log('[FloatWindow] 清除角标请求'); return this.setBadge(0); } /** * 获取当前角标数量 */ getBadgeCount() { return this.badgeCount; } /** * 更新浮窗位置 * @param {Object} position - 新位置 {left, top} */ updatePosition(position) { //console.log('[FloatWindow] 更新位置请求:', position); if (!this.floatView) { //console.warn('[FloatWindow] 浮窗未初始化,无法更新位置'); return false; } try { if (!position || typeof position.left !== 'number' || typeof position.top !== 'number') { //console.error('[FloatWindow] 位置参数无效:', position); return false; } // 更新内部位置状态 this.currentPosition.left = position.left; this.currentPosition.top = position.top; //console.log('[FloatWindow] 正在更新位置...'); this.floatView.setStyle({ left: position.left + 'px', top: position.top + 'px' }); //console.log('[FloatWindow] ✅ 位置更新成功,新位置:', this.currentPosition); return true; } catch (error) { //console.error('[FloatWindow] ❌ 更新位置失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 设置浮窗点击回调 * @param {Function} callback - 点击回调函数 */ setOnClickListener(callback) { //console.log('[FloatWindow] 设置点击回调请求'); if (!this.floatView) { //console.warn('[FloatWindow] 浮窗未初始化,无法设置点击回调'); return false; } if (typeof callback !== 'function') { //console.error('[FloatWindow] 回调参数必须是函数'); return false; } try { this._customClickListener = callback; //console.log('[FloatWindow] ✅ 点击回调设置成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 设置点击监听失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 获取当前浮窗位置 */ getCurrentPosition() { //console.log('[FloatWindow] 获取当前位置:', this.currentPosition); return { ...this.currentPosition }; } /** * 销毁浮窗 */ destroy() { //console.log('[FloatWindow] 销毁浮窗请求'); return this._safeDestroy(); } /** * 检查浮窗是否已初始化 */ isReady() { const ready = this.isInitialized && this.floatView !== null; //console.log('[FloatWindow] 检查浮窗就绪状态:', ready); return ready; } // ============ 私有方法(安全版本) ============ /** * 安全销毁浮窗 */ _safeDestroy() { try { if (this.floatView) { //console.log('[FloatWindow] 正在销毁浮窗实例...'); // 先隐藏 try { this.floatView.hide(); //console.log('[FloatWindow] 浮窗已隐藏'); } catch (hideError) { //console.warn('[FloatWindow] 隐藏浮窗时出错:', hideError); } // 尝试关闭 try { this.floatView.close(); //console.log('[FloatWindow] 浮窗已关闭'); } catch (closeError) { //console.warn('[FloatWindow] 关闭浮窗时出错:', closeError); } this.floatView = null; //console.log('[FloatWindow] 浮窗实例已置空'); } this.isInitialized = false; this.badgeCount = 0; this._customClickListener = null; //console.log('[FloatWindow] ✅ 浮窗销毁完成'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 销毁浮窗失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 安全绘制浮窗内容 */ _safeDrawContent() { try { if (!this.floatView) { //console.warn('[FloatWindow] 浮窗实例不存在,无法绘制内容'); return false; } //console.log('[FloatWindow] 开始绘制浮窗内容...'); // 绘制圆形背景 this.floatView.drawRect( { color: 'rgba(0,0,0,1)', radius: (this.floatSize.width / 2) + 'px' }, { top: '0px', left: '0px', width: this.floatSize.width + 'px', height: this.floatSize.height + 'px' } ); //console.log('[FloatWindow] 背景绘制完成'); // 绘制图标 this.floatView.drawText('💬', { top: '0px', left: '0px', width: '100%', height: '100%' }, { align: 'center', verticalAlign: 'middle', color: '#FFFFFF', size: '24px', weight: 'bold' }); //console.log('[FloatWindow] ✅ 内容绘制成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 绘制内容失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 安全绘制角标 */ _safeDrawBadge() { try { if (!this.floatView) { //console.warn('[FloatWindow] 浮窗实例不存在,无法绘制角标'); return false; } if (this.badgeCount <= 0) { //console.log('[FloatWindow] 角标数量为0,跳过绘制'); return false; } //console.log('[FloatWindow] 开始绘制角标...'); const badgeSize = 18; const badgeLeft = this.floatSize.width - badgeSize; const badgeText = this.badgeCount > 99 ? '99+' : this.badgeCount.toString(); const fontSize = this.badgeCount > 9 ? '9px' : '11px'; /*console.log('[FloatWindow] 角标参数:', { badgeSize, badgeLeft, badgeText, fontSize, floatWidth: this.floatSize.width });*/ // 绘制红色角标背景 this.floatView.drawRect( { color: 'rgba(255,0,0,1)', radius: badgeSize / 2 + 'px' }, { top: '0px', left: badgeLeft + 'px', width: badgeSize + 'px', height: badgeSize + 'px' } ); //console.log('[FloatWindow] 角标背景绘制完成'); // 绘制数字 this.floatView.drawText(badgeText, { top: '0px', left: badgeLeft + 'px', width: badgeSize + 'px', height: badgeSize + 'px' }, { align: 'center', verticalAlign: 'middle', color: '#FFFFFF', size: fontSize, weight: 'bold' }); //console.log('[FloatWindow] ✅ 角标绘制成功:', badgeText); return true; } catch (error) { //console.error('[FloatWindow] ❌ 绘制角标失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 安全添加事件监听 */ _safeAddEventListeners() { try { if (!this.floatView) { //console.warn('[FloatWindow] 浮窗实例不存在,无法添加事件监听'); return false; } //console.log('[FloatWindow] 开始添加事件监听...'); let isDragging = false; let startX = 0, startY = 0; let touchStartTime = 0; let dragStartLeft = 0, dragStartTop = 0; //console.log('[FloatWindow] 事件监听器已配置'); // 点击事件 this.floatView.addEventListener('click', (e) => { try { const now = Date.now(); //console.log('[FloatWindow] 点击事件触发,拖动状态:', isDragging, '时间差:', now - touchStartTime); if (!isDragging && (now - touchStartTime < 300)) { //console.log('[FloatWindow] 执行点击处理...'); this._safeHandleClick(); } } catch (error) { //console.error('[FloatWindow] 点击事件处理错误:', error); } }); // 触摸开始 this.floatView.addEventListener('touchstart', (touch) => { try { startX = touch.screenX || touch.clientX; startY = touch.screenY || touch.clientY; touchStartTime = Date.now(); isDragging = false; // 记录拖拽开始时的位置 dragStartLeft = this.currentPosition.left; dragStartTop = this.currentPosition.top; /*console.log('[FloatWindow] 触摸开始:', { startX, startY, touchStartTime, currentPosition: this.currentPosition, dragStartLeft, dragStartTop });*/ this.floatView.setStyle({ opacity: 0.8 }); //console.log('[FloatWindow] 触摸开始样式已更新'); } catch (error) { //console.error('[FloatWindow] 触摸开始事件处理错误:', error); } }); // 触摸移动 this.floatView.addEventListener('touchmove', (touch) => { try { const currentX = touch.screenX || touch.clientX; const currentY = touch.screenY || touch.clientY; const moveX = Math.abs(currentX - startX); const moveY = Math.abs(currentY - startY); //console.log('[FloatWindow] 触摸移动:', { currentX, currentY, moveX, moveY }); if (moveX > 5 || moveY > 5) { isDragging = true; //console.log('[FloatWindow] 开始拖拽'); // 基于拖拽开始时的位置计算新位置 let newLeft = dragStartLeft + (currentX - startX); let newTop = dragStartTop + (currentY - startY); // 边界检查 const systemInfo = uni.getSystemInfoSync(); /*console.log('[FloatWindow] 系统边界:', { width: systemInfo.windowWidth, height: systemInfo.windowHeight });*/ newLeft = Math.max(0, Math.min(newLeft, systemInfo.windowWidth - this.floatSize.width)); newTop = Math.max(0, Math.min(newTop, systemInfo.windowHeight - this.floatSize.height)); //console.log('[FloatWindow] 新位置:', { newLeft, newTop }); // 更新位置 this.updatePosition({ left: newLeft, top: newTop }); } } catch (error) { //console.error('[FloatWindow] 触摸移动事件处理错误:', error); } }); // 触摸结束 this.floatView.addEventListener('touchend', () => { try { //console.log('[FloatWindow] 触摸结束,拖拽状态:', isDragging); this.floatView.setStyle({ opacity: 1 }); //console.log('[FloatWindow] 触摸结束样式已恢复'); if (isDragging) { //console.log('[FloatWindow] 拖拽结束,执行吸附逻辑'); this._safeAttachToEdge(); } setTimeout(() => { isDragging = false; //console.log('[FloatWindow] 拖拽状态已重置'); }, 100); } catch (error) { //console.error('[FloatWindow] 触摸结束事件处理错误:', error); } }); //console.log('[FloatWindow] ✅ 事件监听添加成功'); return true; } catch (error) { //console.error('[FloatWindow] ❌ 添加事件监听失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } /** * 安全处理点击事件 */ _safeHandleClick() { try { //console.log('[FloatWindow] 处理浮窗点击事件...'); // 如果有自定义点击回调,则执行 if (this._customClickListener && typeof this._customClickListener === 'function') { //console.log('[FloatWindow] 执行自定义点击回调'); this._customClickListener(); } else { // 默认行为:跳转到设置页面 //console.log('[FloatWindow] 执行默认行为:跳转到设置页面'); uni.navigateTo({ url: '/pages/message/index', fail: (err) => { //console.error('[FloatWindow] 跳转失败:', err); } }); } //console.log('[FloatWindow] ✅ 点击事件处理完成'); } catch (error) { //console.error('[FloatWindow] ❌ 处理点击事件失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); } } /** * 安全吸附到边缘 */ _safeAttachToEdge() { try { if (!this.floatView) { //console.warn('[FloatWindow] 浮窗实例不存在,无法吸附'); return false; } //console.log('[FloatWindow] 开始吸附到边缘...'); //console.log('[FloatWindow] 当前矩形位置:', this.currentPosition); const systemInfo = uni.getSystemInfoSync(); const centerX = this.currentPosition.left + (this.floatSize.width / 2); /*console.log('[FloatWindow] 计算中心点:', { centerX, windowWidth: systemInfo.windowWidth, currentLeft: this.currentPosition.left, halfWidth: this.floatSize.width / 2 });*/ let newLeft = this.currentPosition.left; if (centerX < systemInfo.windowWidth / 2) { newLeft = 0; // 左边 //console.log('[FloatWindow] 吸附到左边'); } else { newLeft = systemInfo.windowWidth - this.floatSize.width; // 右边 //console.log('[FloatWindow] 吸附到右边'); } /*console.log('[FloatWindow] 吸附后位置:', { newLeft, top: this.currentPosition.top, floatSize: this.floatSize });*/ const result = this.updatePosition({ left: newLeft, top: this.currentPosition.top }); //console.log('[FloatWindow] ✅ 吸附完成,结果:', result); return result; } catch (error) { //console.error('[FloatWindow] ❌ 吸附到边缘失败:', error); //console.error('[FloatWindow] 错误堆栈:', error.stack); return false; } } } // 创建单例实例 const floatWindowManager = new FloatWindowManager(); export default floatWindowManager;