walletRecharge.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { defineStore } from 'pinia'
  2. import type { WalletRechargeConfigItem } from '~/types/api'
  3. import { walletApi } from '~/api/wallet'
  4. interface WalletRechargeState {
  5. /** 充值档位列表(保持与后端字段一致) */
  6. items: WalletRechargeConfigItem[]
  7. /** 是否正在加载配置 */
  8. loading: boolean
  9. /** 是否已经初始化过(避免重复请求) */
  10. initialized: boolean
  11. }
  12. export const useWalletRechargeStore = defineStore('walletRecharge', {
  13. state: (): WalletRechargeState => ({
  14. items: [],
  15. loading: false,
  16. initialized: false,
  17. }),
  18. actions: {
  19. /**
  20. * 加载钱包充值配置
  21. * 默认只在首次调用时请求,可以通过 force=true 强制刷新
  22. */
  23. async loadConfig(force = false) {
  24. if (this.initialized && !force)
  25. return
  26. this.loading = true
  27. try {
  28. const res = await walletApi.getRechargeConfig()
  29. this.items = res?.items ?? []
  30. this.initialized = true
  31. }
  32. catch (error) {
  33. // 错误在 http 拦截器已有统一处理,这里仅做兜底日志
  34. console.error('Failed to load wallet recharge config:', error)
  35. }
  36. finally {
  37. this.loading = false
  38. }
  39. },
  40. },
  41. })