user.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { defineStore } from 'pinia'
  2. import { authApi, profileApi } from '../utils/api'
  3. export const useUserStore = defineStore('user', {
  4. state: () => ({
  5. token: uni.getStorageSync('token') || '',
  6. nickname: '',
  7. avatarUrl: '',
  8. isVip: false,
  9. vipType: null,
  10. vipEndTime: null,
  11. referralCode: '',
  12. // Daily quota
  13. usedCharts: 0,
  14. maxCharts: 3,
  15. usedChats: 0,
  16. maxChats: 3,
  17. shareCount: 0,
  18. shareLimit: 1,
  19. // Commission
  20. totalCommission: 0,
  21. availableCommission: 0,
  22. // US-5.1 Profile fields
  23. profileIncomplete: false,
  24. gender: 0,
  25. birthDate: null,
  26. bio: '',
  27. city: '',
  28. tags: '',
  29. lookingFor: 0,
  30. profileComplete: false,
  31. birthYear: 0,
  32. birthMonth: 0,
  33. birthDay: 0
  34. }),
  35. getters: {
  36. isLoggedIn: (state) => !!state.token,
  37. isVipActive: (state) => {
  38. if (!state.vipEndTime) return false
  39. return new Date(state.vipEndTime) > new Date()
  40. }
  41. },
  42. actions: {
  43. async login(openid, nickname, avatarUrl, referralCode) {
  44. const data = { openid, nickname, avatarUrl }
  45. if (referralCode) data.referralCode = referralCode
  46. const res = await authApi.login(data)
  47. const tokenStr = res.token || res
  48. this.token = tokenStr
  49. uni.setStorageSync('token', tokenStr)
  50. this.nickname = nickname
  51. this.avatarUrl = avatarUrl
  52. // US-5.1 §5-7: Check profile completion
  53. this.profileIncomplete = res.profileIncomplete === true
  54. },
  55. async wechatLogin(code, nickname, avatarUrl) {
  56. const data = { code, nickname, avatarUrl }
  57. const res = await authApi.login(data)
  58. const tokenStr = res.token || res
  59. this.token = tokenStr
  60. uni.setStorageSync('token', tokenStr)
  61. this.nickname = nickname || '微信用户'
  62. this.avatarUrl = avatarUrl || ''
  63. // US-5.1 §5-7: Check profile completion
  64. this.profileIncomplete = res.profileIncomplete === true
  65. // Fetch full profile after login
  66. try { await this.fetchProfile() } catch (e) {}
  67. },
  68. async fetchProfile() {
  69. const user = await profileApi.info()
  70. this.nickname = user.nickname
  71. this.avatarUrl = user.avatarUrl
  72. this.referralCode = user.referralCode
  73. this.isVip = !!user.vipEndTime && new Date(user.vipEndTime) > new Date()
  74. this.vipType = user.vipType || null
  75. this.vipEndTime = user.vipEndTime
  76. // US-5.1 profile fields
  77. this.gender = user.gender || 0
  78. this.birthDate = user.birthDate || null
  79. this.bio = user.bio || ''
  80. this.city = user.city || ''
  81. this.tags = user.tags || ''
  82. this.lookingFor = user.lookingFor || 0
  83. this.profileComplete = !!user.profileComplete
  84. this.birthYear = user.birthYear || 0
  85. this.birthMonth = user.birthMonth || 0
  86. this.birthDay = user.birthDay || 0
  87. this.profileIncomplete = !this.profileComplete
  88. },
  89. async fetchQuota() {
  90. try {
  91. const quota = await profileApi.quota()
  92. this.usedCharts = quota.dailyChartCount || 0
  93. this.maxCharts = quota.chartLimit > 0 ? quota.chartLimit : 999
  94. this.usedChats = quota.dailyChatCount || 0
  95. this.maxChats = quota.chatLimit > 0 ? quota.chatLimit : 999
  96. this.shareCount = quota.dailyShareCount || 0
  97. this.shareLimit = quota.shareLimit > 0 ? quota.shareLimit : 1
  98. } catch (e) { /* ignore */ }
  99. },
  100. async fetchCommissionSummary() {
  101. try {
  102. const summary = await profileApi.commissionSummary()
  103. this.totalCommission = summary.totalEarnings || 0
  104. this.availableCommission = summary.availableBalance || 0
  105. } catch (e) { /* ignore */ }
  106. },
  107. logout() {
  108. this.token = ''
  109. this.nickname = ''
  110. this.avatarUrl = ''
  111. this.usedCharts = 0
  112. this.usedChats = 0
  113. this.shareCount = 0
  114. this.shareLimit = 1
  115. this.totalCommission = 0
  116. this.availableCommission = 0
  117. this.profileIncomplete = false
  118. this.gender = 0
  119. this.birthDate = null
  120. this.bio = ''
  121. this.city = ''
  122. this.tags = ''
  123. this.lookingFor = 0
  124. this.profileComplete = false
  125. this.birthYear = 0
  126. this.birthMonth = 0
  127. this.birthDay = 0
  128. uni.removeStorageSync('token')
  129. }
  130. }
  131. })