import { defineStore } from 'pinia' import { authApi, profileApi } from '../utils/api' export const useUserStore = defineStore('user', { state: () => ({ token: uni.getStorageSync('token') || '', nickname: '', avatarUrl: '', isVip: false, vipType: null, vipEndTime: null, referralCode: '', // Daily quota usedCharts: 0, maxCharts: 3, usedChats: 0, maxChats: 3, shareCount: 0, shareLimit: 1, // Commission totalCommission: 0, availableCommission: 0, // US-5.1 Profile fields profileIncomplete: false, gender: 0, birthDate: null, bio: '', city: '', tags: '', lookingFor: 0, profileComplete: false, birthYear: 0, birthMonth: 0, birthDay: 0 }), getters: { isLoggedIn: (state) => !!state.token, isVipActive: (state) => { if (!state.vipEndTime) return false return new Date(state.vipEndTime) > new Date() } }, actions: { async login(openid, nickname, avatarUrl, referralCode) { const data = { openid, nickname, avatarUrl } if (referralCode) data.referralCode = referralCode const res = await authApi.login(data) const tokenStr = res.token || res this.token = tokenStr uni.setStorageSync('token', tokenStr) this.nickname = nickname this.avatarUrl = avatarUrl // US-5.1 §5-7: Check profile completion this.profileIncomplete = res.profileIncomplete === true }, async wechatLogin(code, nickname, avatarUrl) { const data = { code, nickname, avatarUrl } const res = await authApi.login(data) const tokenStr = res.token || res this.token = tokenStr uni.setStorageSync('token', tokenStr) this.nickname = nickname || '微信用户' this.avatarUrl = avatarUrl || '' // US-5.1 §5-7: Check profile completion this.profileIncomplete = res.profileIncomplete === true // Fetch full profile after login try { await this.fetchProfile() } catch (e) {} }, async fetchProfile() { const user = await profileApi.info() this.nickname = user.nickname this.avatarUrl = user.avatarUrl this.referralCode = user.referralCode this.isVip = !!user.vipEndTime && new Date(user.vipEndTime) > new Date() this.vipType = user.vipType || null this.vipEndTime = user.vipEndTime // US-5.1 profile fields this.gender = user.gender || 0 this.birthDate = user.birthDate || null this.bio = user.bio || '' this.city = user.city || '' this.tags = user.tags || '' this.lookingFor = user.lookingFor || 0 this.profileComplete = !!user.profileComplete this.birthYear = user.birthYear || 0 this.birthMonth = user.birthMonth || 0 this.birthDay = user.birthDay || 0 this.profileIncomplete = !this.profileComplete }, async fetchQuota() { try { const quota = await profileApi.quota() this.usedCharts = quota.dailyChartCount || 0 this.maxCharts = quota.chartLimit > 0 ? quota.chartLimit : 999 this.usedChats = quota.dailyChatCount || 0 this.maxChats = quota.chatLimit > 0 ? quota.chatLimit : 999 this.shareCount = quota.dailyShareCount || 0 this.shareLimit = quota.shareLimit > 0 ? quota.shareLimit : 1 } catch (e) { /* ignore */ } }, async fetchCommissionSummary() { try { const summary = await profileApi.commissionSummary() this.totalCommission = summary.totalEarnings || 0 this.availableCommission = summary.availableBalance || 0 } catch (e) { /* ignore */ } }, logout() { this.token = '' this.nickname = '' this.avatarUrl = '' this.usedCharts = 0 this.usedChats = 0 this.shareCount = 0 this.shareLimit = 1 this.totalCommission = 0 this.availableCommission = 0 this.profileIncomplete = false this.gender = 0 this.birthDate = null this.bio = '' this.city = '' this.tags = '' this.lookingFor = 0 this.profileComplete = false this.birthYear = 0 this.birthMonth = 0 this.birthDay = 0 uni.removeStorageSync('token') } } })