// @/core/store/modules/floatWindowManager.js // Vuex 状态管理模块 import floatWindowManager from '@/core/utils/floatWindowManager'; const state = { // 浮窗状态 isInitialized: false, isVisible: false, badgeCount: 0, isLoading: false, error: null }; const getters = { // 浮窗是否准备就绪 isReady: (state) => state.isInitialized, // 浮窗是否可见 isVisible: (state) => state.isVisible, // 当前角标数量 badgeCount: (state) => state.badgeCount, // 是否正在加载 isLoading: (state) => state.isLoading, // 错误信息 error: (state) => state.error }; const mutations = { SET_INITIALIZED(state, isInitialized) { state.isInitialized = isInitialized; }, SET_VISIBLE(state, isVisible) { //console.log("浮窗已设置显示") state.isVisible = isVisible; }, SET_BADGE_COUNT(state, count) { state.badgeCount = count; }, SET_LOADING(state, isLoading) { state.isLoading = isLoading; }, SET_ERROR(state, error) { state.error = error; } }; const actions = { /** * 初始化浮窗 */ async initialize({ commit, dispatch }, options = {}) { try { commit('SET_LOADING', true); commit('SET_ERROR', null); const success = await floatWindowManager.init(options); if (success) { commit('SET_INITIALIZED', true); commit('SET_BADGE_COUNT', floatWindowManager.getBadgeCount()); // 设置默认点击行为 floatWindowManager.setOnClickListener(() => { dispatch('handleFloatClick'); }); } return success; } catch (error) { commit('SET_ERROR', error.message); console.error('初始化浮窗失败:', error); return false; } finally { commit('SET_LOADING', false); } }, async is_visible({commit, state}){ try{ if(!state.isVisible){ const success = floatWindowManager.show(); if (success) { commit('SET_VISIBLE', true); } } return state.isVisible; }catch(e){ console.log("is_visible",e); return false; } }, /** * 显示浮窗 */ async show({ commit, state }) { if (!state.isInitialized) { const initialized = await dispatch('initialize'); if (!initialized) return false; } const success = floatWindowManager.show(); if (success) { commit('SET_VISIBLE', true); } return success; }, /** * 隐藏浮窗 */ async hide({ commit, state }) { if (!state.isInitialized) return true; const success = floatWindowManager.hide(); if (success) { commit('SET_VISIBLE', false); } return success; }, /** * 设置角标数量 */ async setBadge({ commit }, count) { if (typeof count !== 'number' || count < 0) { throw new Error('角标数量必须为非负数字'); } const success = floatWindowManager.setBadge(count); if (success) { commit('SET_BADGE_COUNT', count); } return success; }, /** * 清除角标 */ async clearBadge({ commit }) { const success = floatWindowManager.clearBadge(); if (success) { commit('SET_BADGE_COUNT', 0); } return success; }, /** * 增加角标数量 */ async increaseBadge({ commit, state }, increment = 1) { const newCount = state.badgeCount + increment; return dispatch('setBadge', newCount); }, /** * 减少角标数量 */ async decreaseBadge({ commit, state }, decrement = 1) { const newCount = Math.max(0, state.badgeCount - decrement); return dispatch('setBadge', newCount); }, /** * 更新浮窗位置 */ async updatePosition({ state }, position) { if (!state.isInitialized) { throw new Error('浮窗未初始化'); } return floatWindowManager.updatePosition(position); }, /** * 设置点击回调 */ async setOnClickListener({ state }, callback) { if (!state.isInitialized) { throw new Error('浮窗未初始化'); } if (typeof callback !== 'function') { throw new Error('回调必须是一个函数'); } return floatWindowManager.setOnClickListener(callback); }, /** * 处理浮窗点击(默认行为) */ async handleFloatClick() { try { console.log('浮窗点击处理'); // 这里可以添加全局的点击处理逻辑 // 比如发送埋点、跳转页面等 uni.navigateTo({ url: '/pages/message/index' }); } catch (error) { console.error('浮窗点击处理失败:', error); } }, /** * 销毁浮窗 */ async destroy({ commit }) { try { floatWindowManager.destroy(); commit('SET_INITIALIZED', false); commit('SET_VISIBLE', false); commit('SET_BADGE_COUNT', 0); commit('SET_ERROR', null); return true; } catch (error) { console.error('销毁浮窗失败:', error); return false; } }, /** * 重置浮窗状态 */ async reset({ dispatch }) { await dispatch('destroy'); return dispatch('initialize'); } }; export default { namespaced: true, state, getters, mutations, actions };