├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

user.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { defineStore } from 'pinia'
  2. import { ref, computed } from 'vue'
  3. export const useUserStore = defineStore('user', () => {
  4. const userId = ref(null)
  5. const userInfo = ref(null)
  6. const vipInfo = ref(null)
  7. // 获取VIP状态
  8. const isVIP = computed(() => {
  9. if (!vipInfo.value) return false
  10. return new Date(vipInfo.value.expireTime) > new Date()
  11. })
  12. // 用户登录
  13. const login = async (username, password) => {
  14. const res = await uni.request({
  15. url: 'https://api.aiyadianzi.ltd/auth/login',
  16. method: 'POST',
  17. data: { username, password }
  18. })
  19. userId.value = res.data.userId
  20. userInfo.value = res.data.userInfo
  21. vipInfo.value = res.data.vipInfo
  22. uni.setStorageSync('token', res.data.token)
  23. }
  24. // 购买VIP
  25. const purchaseVIP = async (plan) => {
  26. const res = await uni.request({
  27. url: 'https://api.aiyadianzi.ltd/vip/purchase',
  28. method: 'POST',
  29. data: {
  30. userId: userId.value,
  31. planId: plan.id
  32. },
  33. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` }
  34. })
  35. vipInfo.value = {
  36. type: plan.type,
  37. expireTime: res.data.expireTime
  38. }
  39. }
  40. // 初始化用户
  41. const initUser = async () => {
  42. const token = uni.getStorageSync('token')
  43. if (!token) return
  44. try {
  45. const res = await uni.request({
  46. url: 'https://api.aiyadianzi.ltd/auth/profile',
  47. header: { Authorization: `Bearer ${token}` }
  48. })
  49. userId.value = res.data.userId
  50. userInfo.value = res.data.userInfo
  51. vipInfo.value = res.data.vipInfo
  52. } catch (err) {
  53. console.error('用户初始化失败', err)
  54. uni.removeStorageSync('token')
  55. }
  56. }
  57. return {
  58. userId,
  59. userInfo,
  60. vipInfo,
  61. isVIP,
  62. login,
  63. purchaseVIP,
  64. initUser
  65. }
  66. })