/** * 配置状态管理模块 * 小程序启动时一次性加载所有配置 */ import { defineStore } from 'pinia' import { getLastPopupDate, setLastPopupDate, getMemberAdClosedDate, setMemberAdClosedDate, getMemberAdClosedForever, setMemberAdClosedForever, getDefaultAvatar, setDefaultAvatar, getServiceAccountPopupCount, setServiceAccountPopupCount, getServiceAccountPopupDate, setServiceAccountPopupDate, getServiceAccountPopupLastTime, setServiceAccountPopupLastTime, getServiceAccountFollowed, setServiceAccountFollowed, getSubscribeReminderClosedDate, setSubscribeReminderClosedDate } from '../utils/storage.js' import { getAppConfig } from '../api/config.js' import { useUserStore } from './user.js' /** * 获取今天的日期字符串 (YYYY-MM-DD) * @returns {string} */ export function getTodayDateString() { const now = new Date() return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}` } export const useConfigStore = defineStore('config', { state: () => ({ // 是否已加载配置 isLoaded: false, // 首页配置 banners: [], kingKongs: [], // 系统配置 defaultAvatar: getDefaultAvatar() || '/static/logo.png', searchBanner: '', realNameBanner: '', butlerQrcode: '', // 管家指导二维码 displayPageImage: '', // 展示页长图 memberEntryImage: '', // 会员入口图 // 会员图标配置 memberIcons: { unlimitedMemberIcon: '', // 不限时会员图标 sincereMemberIcon: '', // 诚意会员图标 familyMemberIcon: '', // 家庭版会员图标 timeLimitedMemberIcon: '' // 限时会员图标 }, // 弹窗配置 dailyPopup: null, memberAdConfig: null, lastPopupDate: getLastPopupDate() || '', memberAdClosedDate: getMemberAdClosedDate() || '', memberAdClosedForever: getMemberAdClosedForever() || false, // 服务号关注弹窗配置 serviceAccountPopup: null, serviceAccountFollowed: getServiceAccountFollowed() || false, serviceAccountPopupCount: getServiceAccountPopupCount() || 0, serviceAccountPopupDate: getServiceAccountPopupDate() || '', serviceAccountPopupLastTime: getServiceAccountPopupLastTime() || 0, // 小程序订阅消息提醒配置 subscribeReminderConfig: null, subscribeReminderClosedDate: getSubscribeReminderClosedDate() || '', memberAdClosedThisSession: false, // 本次会话是否关闭了会员广告 // 实名认证价格 realNamePrice: 88, // 弹窗显示状态 showGenderPopup: false, showDailyPopup: false, showMemberAd: false, showServiceAccountPopup: false, showSubscribeReminder: false }), getters: { hasBanners: (state) => state.banners.length > 0, hasKingKongs: (state) => state.kingKongs.length > 0, hasDailyPopup: (state) => state.dailyPopup !== null, hasMemberAdConfig: (state) => state.memberAdConfig !== null && state.memberAdConfig.status === 1, getDefaultAvatar: (state) => state.defaultAvatar || '/static/logo.png', /** * 根据会员等级获取对应的图标URL * @param {number} memberLevel - 会员等级:1不限时会员 2诚意会员 3家庭版会员 * @returns {string} 图标URL */ getMemberIcon: (state) => (memberLevel) => { switch (memberLevel) { case 1: return state.memberIcons.unlimitedMemberIcon || '' case 2: return state.memberIcons.sincereMemberIcon || '' case 3: return state.memberIcons.familyMemberIcon || '' case 4: return state.memberIcons.timeLimitedMemberIcon || '' default: return '' } } }, actions: { /** * 加载所有配置(小程序启动时调用一次) */ async loadAppConfig() { if (this.isLoaded) return try { const res = await getAppConfig() if (res && res.data) { const config = res.data // 首页配置 this.banners = config.banners || [] this.kingKongs = config.kingKongs || [] // 系统配置 if (config.defaultAvatar) { this.defaultAvatar = config.defaultAvatar setDefaultAvatar(config.defaultAvatar) } this.searchBanner = config.searchBanner || '' this.realNameBanner = config.realNameBanner || '' this.butlerQrcode = config.butlerQrcode || '' this.displayPageImage = config.displayPageImage || '' this.memberEntryImage = config.memberEntryImage || '' // 会员图标配置 if (config.memberIcons) { this.memberIcons = { unlimitedMemberIcon: config.memberIcons.unlimitedMemberIcon || '', sincereMemberIcon: config.memberIcons.sincereMemberIcon || '', familyMemberIcon: config.memberIcons.familyMemberIcon || '', timeLimitedMemberIcon: config.memberIcons.timeLimitedMemberIcon || '' } } // 弹窗配置 this.dailyPopup = config.dailyPopup || null this.memberAdConfig = config.memberAdPopup || null this.serviceAccountPopup = config.serviceAccountPopup || null this.subscribeReminderConfig = config.subscribeReminderConfig || null // 实名认证价格 if (config.realNamePrice !== undefined && config.realNamePrice !== null) { this.realNamePrice = config.realNamePrice } this.isLoaded = true } } catch (error) { console.error('加载配置失败:', error) } }, /** * 检查并决定显示哪个弹窗 * @param {Object} userState - 用户状态 */ checkPopupDisplay(userState) { const today = getTodayDateString() const { genderPreference, isProfileCompleted, isMember } = userState // 重置所有弹窗状态 this.showGenderPopup = false this.showDailyPopup = false this.showMemberAd = false this.showSubscribeReminder = false // 性别选择弹窗优先级最高 if (genderPreference === 0 && !isProfileCompleted) { this.showGenderPopup = true return } // 每日弹窗 if (this.lastPopupDate !== today && this.dailyPopup) { this.showDailyPopup = true return } // 会员广告 if (!isMember && this.memberAdConfig && (this.memberAdConfig.status === 1 || this.memberAdConfig.Status === 1)) { const displayMode = this.memberAdConfig.displayMode || this.memberAdConfig.DisplayMode || 2 if (displayMode === 1) { this.showMemberAd = true } else if (displayMode === 2) { if (this.memberAdClosedDate !== today) { this.showMemberAd = true } } else if (displayMode === 3) { if (!this.memberAdClosedForever) { this.showMemberAd = true } } } }, closeGenderPopup() { this.showGenderPopup = false }, closeDailyPopup() { const today = getTodayDateString() this.showDailyPopup = false this.lastPopupDate = today setLastPopupDate(today) }, closeMemberAd() { const today = getTodayDateString() this.showMemberAd = false this.memberAdClosedThisSession = true // 标记本次会话已关闭会员广告 const displayMode = this.memberAdConfig?.displayMode || this.memberAdConfig?.DisplayMode || 2 if (displayMode === 2) { this.memberAdClosedDate = today setMemberAdClosedDate(today) } else if (displayMode === 3) { this.memberAdClosedForever = true setMemberAdClosedForever(true) } // displayMode === 1 时不保存任何状态,下次进入页面会重新显示 }, /** * 检查是否应该显示服务号关注弹窗 * 条件: * 1. 用户已登录 * 2. 用户未关注服务号(从后端获取真实状态) * 3. 弹窗已启用 * 4. 弹出后5分钟内不再弹出 * 5. 一天最多弹出3次 */ checkServiceAccountPopup() { // 从 userStore 获取真实的关注状态 const userStore = useUserStore() // 如果未登录,不显示 if (!userStore.isLoggedIn) { this.showServiceAccountPopup = false return } // 如果已关注服务号,不显示 if (userStore.isFollowServiceAccount) { this.showServiceAccountPopup = false return } // 如果没有配置服务号弹窗或未启用,不显示 if (!this.serviceAccountPopup || this.serviceAccountPopup.status !== 1) { this.showServiceAccountPopup = false return } const today = getTodayDateString() const now = Date.now() // 检查是否是新的一天,重置计数 if (this.serviceAccountPopupDate !== today) { this.serviceAccountPopupCount = 0 this.serviceAccountPopupDate = today setServiceAccountPopupCount(0) setServiceAccountPopupDate(today) } // 检查今天是否已弹出3次 if (this.serviceAccountPopupCount >= 3) { this.showServiceAccountPopup = false return } // 检查是否在5分钟内已弹出过 const fiveMinutes = 5 * 60 * 1000 if (now - this.serviceAccountPopupLastTime < fiveMinutes) { this.showServiceAccountPopup = false return } // 显示弹窗 this.showServiceAccountPopup = true }, /** * 关闭服务号关注弹窗 */ closeServiceAccountPopup() { this.showServiceAccountPopup = false // 更新弹出次数和时间 const today = getTodayDateString() const now = Date.now() this.serviceAccountPopupCount++ this.serviceAccountPopupLastTime = now this.serviceAccountPopupDate = today setServiceAccountPopupCount(this.serviceAccountPopupCount) setServiceAccountPopupLastTime(now) setServiceAccountPopupDate(today) }, /** * 标记用户已关注服务号 */ markServiceAccountFollowed() { this.serviceAccountFollowed = true this.showServiceAccountPopup = false setServiceAccountFollowed(true) }, /** * 检查是否应该显示小程序订阅消息提醒 * 条件: * 1. 用户未关注服务号(从后端获取真实状态) * 2. 今日已弹出3次服务号关注弹窗 * 3. 优先级低于会员广告(会员广告关闭后才显示) * 4. 今日未关闭过 * @param {boolean} isMember - 是否是会员 */ checkSubscribeReminder(isMember = false) { const today = getTodayDateString() const userStore = useUserStore() // 如果已关注服务号,不显示 if (userStore.isFollowServiceAccount) { this.showSubscribeReminder = false return } // 检查是否是新的一天,重置服务号弹窗计数 if (this.serviceAccountPopupDate !== today) { this.serviceAccountPopupCount = 0 } // 如果今日服务号弹窗未达到3次,不显示 if (this.serviceAccountPopupCount < 3) { this.showSubscribeReminder = false return } // 如果今日已关闭过,不显示 if (this.subscribeReminderClosedDate === today) { this.showSubscribeReminder = false return } // 如果会员广告正在显示,不显示(优先级低于会员广告) if (this.showMemberAd) { this.showSubscribeReminder = false return } // 如果本次会话关闭了会员广告,不显示 if (this.memberAdClosedThisSession) { this.showSubscribeReminder = false return } // 显示订阅消息提醒 this.showSubscribeReminder = true }, /** * 关闭小程序订阅消息提醒 */ closeSubscribeReminder() { const today = getTodayDateString() this.showSubscribeReminder = false this.subscribeReminderClosedDate = today setSubscribeReminderClosedDate(today) }, reset() { this.isLoaded = false this.banners = [] this.kingKongs = [] this.searchBanner = '' this.realNameBanner = '' this.dailyPopup = null this.memberAdConfig = null this.serviceAccountPopup = null this.subscribeReminderConfig = null this.memberAdClosedThisSession = false this.showGenderPopup = false this.showDailyPopup = false this.showMemberAd = false this.showServiceAccountPopup = false this.showSubscribeReminder = false } } })