209 lines
5.8 KiB
JavaScript
209 lines
5.8 KiB
JavaScript
/**
|
|
* 配置状态管理模块
|
|
* Requirements: 2.1, 2.2, 3.1, 3.2, 3.3
|
|
*/
|
|
|
|
import { defineStore } from 'pinia'
|
|
import {
|
|
getLastPopupDate, setLastPopupDate,
|
|
getMemberAdClosedDate, setMemberAdClosedDate,
|
|
getGenderPreference
|
|
} from '../utils/storage.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')}`
|
|
}
|
|
|
|
/**
|
|
* 配置状态定义
|
|
* @typedef {Object} ConfigState
|
|
* @property {Array} banners - Banner轮播图列表
|
|
* @property {Array} kingKongs - 金刚位导航列表
|
|
* @property {Object|null} dailyPopup - 每日弹窗配置
|
|
* @property {string} lastPopupDate - 上次显示弹窗的日期
|
|
* @property {string} memberAdClosedDate - 会员广告关闭日期
|
|
* @property {boolean} showGenderPopup - 是否显示性别选择弹窗
|
|
* @property {boolean} showDailyPopup - 是否显示每日弹窗
|
|
* @property {boolean} showMemberAd - 是否显示会员广告
|
|
*/
|
|
|
|
export const useConfigStore = defineStore('config', {
|
|
state: () => ({
|
|
// 首页配置
|
|
banners: [],
|
|
kingKongs: [],
|
|
|
|
// 弹窗配置
|
|
dailyPopup: null,
|
|
lastPopupDate: getLastPopupDate() || '',
|
|
memberAdClosedDate: getMemberAdClosedDate() || '',
|
|
|
|
// 弹窗显示状态
|
|
showGenderPopup: false,
|
|
showDailyPopup: false,
|
|
showMemberAd: false
|
|
}),
|
|
|
|
getters: {
|
|
/**
|
|
* 是否有Banner数据
|
|
*/
|
|
hasBanners: (state) => state.banners.length > 0,
|
|
|
|
/**
|
|
* 是否有金刚位数据
|
|
*/
|
|
hasKingKongs: (state) => state.kingKongs.length > 0,
|
|
|
|
/**
|
|
* 是否有每日弹窗
|
|
*/
|
|
hasDailyPopup: (state) => state.dailyPopup !== null
|
|
},
|
|
|
|
actions: {
|
|
/**
|
|
* 设置首页配置
|
|
* @param {Object} config - 首页配置
|
|
* @param {Array} config.banners - Banner列表
|
|
* @param {Array} config.kingKongs - 金刚位列表
|
|
*/
|
|
setHomeConfig(config) {
|
|
if (config.banners) {
|
|
this.banners = config.banners
|
|
}
|
|
if (config.kingKongs) {
|
|
this.kingKongs = config.kingKongs
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 设置每日弹窗配置
|
|
* @param {Object} popup - 弹窗配置
|
|
*/
|
|
setDailyPopup(popup) {
|
|
this.dailyPopup = popup
|
|
},
|
|
|
|
/**
|
|
* 检查并决定显示哪个弹窗
|
|
* Property 5: Popup Priority Logic - 性别选择弹窗优先级最高
|
|
* Property 6: Daily Popup Display - 每日首次显示弹窗
|
|
* Property 7: Member Ad Visibility - 非会员且未关闭时显示广告
|
|
*
|
|
* @param {Object} userState - 用户状态
|
|
* @param {number} userState.genderPreference - 性别偏好
|
|
* @param {boolean} userState.isProfileCompleted - 资料是否完成
|
|
* @param {boolean} userState.isMember - 是否会员
|
|
*/
|
|
checkPopupDisplay(userState) {
|
|
const today = getTodayDateString()
|
|
const { genderPreference, isProfileCompleted, isMember } = userState
|
|
|
|
// 重置所有弹窗状态
|
|
this.showGenderPopup = false
|
|
this.showDailyPopup = false
|
|
this.showMemberAd = false
|
|
|
|
// Property 5: 性别选择弹窗优先级最高
|
|
// 当用户未选择性别偏好且未完成资料时显示
|
|
if (genderPreference === 0 && !isProfileCompleted) {
|
|
this.showGenderPopup = true
|
|
return // 性别弹窗优先级最高,显示后不再检查其他弹窗
|
|
}
|
|
|
|
// Property 6: 每日弹窗显示
|
|
// 当今天还没显示过弹窗时显示
|
|
if (this.lastPopupDate !== today && this.dailyPopup) {
|
|
this.showDailyPopup = true
|
|
return // 每日弹窗显示后不再检查会员广告
|
|
}
|
|
|
|
// Property 7: 会员广告可见性
|
|
// 非会员且今天未关闭广告时显示
|
|
if (!isMember && this.memberAdClosedDate !== today) {
|
|
this.showMemberAd = true
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 关闭性别选择弹窗
|
|
*/
|
|
closeGenderPopup() {
|
|
this.showGenderPopup = false
|
|
},
|
|
|
|
/**
|
|
* 关闭每日弹窗并记录日期
|
|
*/
|
|
closeDailyPopup() {
|
|
const today = getTodayDateString()
|
|
this.showDailyPopup = false
|
|
this.lastPopupDate = today
|
|
setLastPopupDate(today)
|
|
},
|
|
|
|
/**
|
|
* 关闭会员广告并记录日期
|
|
* Property 7: 关闭后当天不再显示
|
|
*/
|
|
closeMemberAd() {
|
|
const today = getTodayDateString()
|
|
this.showMemberAd = false
|
|
this.memberAdClosedDate = today
|
|
setMemberAdClosedDate(today)
|
|
},
|
|
|
|
/**
|
|
* 判断是否应该显示性别选择弹窗
|
|
* Property 5: Popup Priority Logic
|
|
* @param {number} genderPreference - 性别偏好
|
|
* @param {boolean} isProfileCompleted - 资料是否完成
|
|
* @returns {boolean}
|
|
*/
|
|
shouldShowGenderPopup(genderPreference, isProfileCompleted) {
|
|
return genderPreference === 0 && !isProfileCompleted
|
|
},
|
|
|
|
/**
|
|
* 判断是否应该显示每日弹窗
|
|
* Property 6: Daily Popup Display
|
|
* @param {string} lastShownDate - 上次显示日期
|
|
* @param {string} today - 今天日期
|
|
* @returns {boolean}
|
|
*/
|
|
shouldShowDailyPopup(lastShownDate, today) {
|
|
return lastShownDate !== today
|
|
},
|
|
|
|
/**
|
|
* 判断是否应该显示会员广告
|
|
* Property 7: Member Ad Visibility
|
|
* @param {boolean} isMember - 是否会员
|
|
* @param {string} adClosedDate - 广告关闭日期
|
|
* @param {string} today - 今天日期
|
|
* @returns {boolean}
|
|
*/
|
|
shouldShowMemberAd(isMember, adClosedDate, today) {
|
|
return !isMember && adClosedDate !== today
|
|
},
|
|
|
|
/**
|
|
* 重置配置状态
|
|
*/
|
|
reset() {
|
|
this.banners = []
|
|
this.kingKongs = []
|
|
this.dailyPopup = null
|
|
this.showGenderPopup = false
|
|
this.showDailyPopup = false
|
|
this.showMemberAd = false
|
|
}
|
|
}
|
|
})
|