| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import { defineStore } from 'pinia'
- import type { WalletRechargeConfigItem } from '~/types/api'
- import { walletApi } from '~/api/wallet'
- interface WalletRechargeState {
- /** 充值档位列表(保持与后端字段一致) */
- items: WalletRechargeConfigItem[]
- /** 是否正在加载配置 */
- loading: boolean
- /** 是否已经初始化过(避免重复请求) */
- initialized: boolean
- }
- export const useWalletRechargeStore = defineStore('walletRecharge', {
- state: (): WalletRechargeState => ({
- items: [],
- loading: false,
- initialized: false,
- }),
- actions: {
- /**
- * 加载钱包充值配置
- * 默认只在首次调用时请求,可以通过 force=true 强制刷新
- */
- async loadConfig(force = false) {
- if (this.initialized && !force)
- return
- this.loading = true
- try {
- const res = await walletApi.getRechargeConfig()
- this.items = res?.items ?? []
- this.initialized = true
- }
- catch (error) {
- // 错误在 http 拦截器已有统一处理,这里仅做兜底日志
- console.error('Failed to load wallet recharge config:', error)
- }
- finally {
- this.loading = false
- }
- },
- },
- })
|