|
|
@@ -0,0 +1,75 @@
|
|
|
+import { computed } from 'vue'
|
|
|
+import { storeToRefs } from 'pinia'
|
|
|
+import { useWalletRechargeStore } from '~/stores/walletRecharge'
|
|
|
+import { useAuth } from './useAuth'
|
|
|
+import { useTopupPopup } from './useTopupPopup'
|
|
|
+import { formatNumber } from '~/utils/helpers'
|
|
|
+
|
|
|
+// 充值流程组合式函数
|
|
|
+// - 从后端加载充值配置并缓存在 Pinia
|
|
|
+// - 把后端字段映射到 Topup 弹窗所需的档位结构
|
|
|
+// - 统一通过 useTopupPopup 打开充值弹窗
|
|
|
+export const useRecharge = () => {
|
|
|
+ const walletRechargeStore = useWalletRechargeStore()
|
|
|
+ const { items, loading } = storeToRefs(walletRechargeStore)
|
|
|
+
|
|
|
+ const { wallet } = useAuth()
|
|
|
+ const { open: openTopupPopup } = useTopupPopup()
|
|
|
+
|
|
|
+ const hasConfig = computed(() => items.value.length > 0)
|
|
|
+
|
|
|
+ const ensureConfig = async (force = false) => {
|
|
|
+ if (!force && walletRechargeStore.initialized && items.value.length > 0)
|
|
|
+ return
|
|
|
+
|
|
|
+ await walletRechargeStore.loadConfig(force)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 打开充值弹窗:
|
|
|
+ * - 确保已经加载充值配置
|
|
|
+ * - 按照 Topup 弹窗要求的结构构建档位数据
|
|
|
+ */
|
|
|
+ const openRecharge = async () => {
|
|
|
+ await ensureConfig()
|
|
|
+
|
|
|
+ const configItems = items.value
|
|
|
+ if (!configItems || configItems.length === 0) {
|
|
|
+ console.warn('Wallet recharge config is empty')
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const balance = wallet.value?.diamond ?? 0
|
|
|
+
|
|
|
+ const packages = configItems.map(item => ({
|
|
|
+ id: item.id,
|
|
|
+ // UI 内部字段为 diamonds,这里保持与后端 diamond 一一对应
|
|
|
+ diamonds: item.diamond,
|
|
|
+ // 金额使用千分位展示,货币前缀在组件内处理(IDR {{ priceLabel }})
|
|
|
+ priceLabel: formatNumber(item.amount),
|
|
|
+ }))
|
|
|
+
|
|
|
+ // 支付方式目前前端写死;如果后端后续下发支付方式,可以从接口映射
|
|
|
+ const methods = [
|
|
|
+ { id: 'payermax', name: 'PayerMax' },
|
|
|
+ ]
|
|
|
+
|
|
|
+ openTopupPopup({
|
|
|
+ balance,
|
|
|
+ packages,
|
|
|
+ methods,
|
|
|
+ defaultPackageId: packages[0]?.id,
|
|
|
+ defaultMethodId: methods[0]?.id,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ // state
|
|
|
+ loading,
|
|
|
+ items,
|
|
|
+ hasConfig,
|
|
|
+ // actions
|
|
|
+ ensureConfig,
|
|
|
+ openRecharge,
|
|
|
+ }
|
|
|
+}
|