From 2ceb735d4813dcae044c667a89df66d45cb0dec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E4=B8=81?= Date: Thu, 9 Apr 2026 00:07:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=BA=E6=8C=81=E4=BB=93=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E4=BD=99=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 3 ++- src/api/mockData.ts | 4 ++++ src/api/position.ts | 20 ++++++++++++++++++-- src/locales/en.json | 1 + src/locales/ja.json | 1 + src/locales/ko.json | 1 + src/locales/zh-CN.json | 1 + src/locales/zh-TW.json | 3 ++- src/stores/user.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/views/Profile.vue | 16 +++++++++------- src/views/Wallet.vue | 28 +++++++++++++++++++++++----- 11 files changed, 104 insertions(+), 16 deletions(-) diff --git a/src/App.vue b/src/App.vue index 9208f49..5b37c90 100644 --- a/src/App.vue +++ b/src/App.vue @@ -57,6 +57,7 @@ async function refreshUserData() { await nextTick() await userStore.fetchUserInfo() await userStore.fetchUsdcBalance() + await userStore.fetchPositionsValue() } onMounted(() => { @@ -132,7 +133,7 @@ watch( padding="4 12" @click="$router.push('/wallet')" > - ${{ userStore.balance }} + ${{ userStore.totalAssetValue }} 0 && costUsd > 0) { - const avgCents = Math.round((costUsd / size) * 100) + const avgVal = costUsd / size + const avgCents = Math.round(avgVal * 100) const avgStr = `${avgCents}¢` const prices = pos.market?.outcomePrices + let nowVal: number | null = null if (prices?.length) { const idx = getOutcomePriceIndex(pos.market?.outcomes, outcome) - const nowVal = typeof prices[idx] === 'string' ? parseFloat(prices[idx] as string) : Number(prices[idx]) + nowVal = typeof prices[idx] === 'string' ? parseFloat(prices[idx] as string) : Number(prices[idx]) const nowCents = Number.isFinite(nowVal) ? Math.round(nowVal * 100) : null avgNow = nowCents != null ? `${avgStr} → ${nowCents}¢` : avgStr } else { avgNow = avgStr } + + if (nowVal != null && Number.isFinite(nowVal)) { + currentValueNum = size * nowVal + } + change = currentValueNum - costUsd + changePct = costUsd > 0 ? (change / costUsd) * 100 : 0 } const marketID = String(pos.marketID ?? pos.market?.ID ?? '') @@ -252,6 +264,10 @@ export function mapPositionToDisplayItem(pos: ClobPositionItem): PositionDisplay bet, toWin, value, + currentValueNum, + valueChange: `${change >= 0 ? '+' : ''}$${Math.abs(change).toFixed(2)}`, + valueChangePct: `${changePct.toFixed(2)}%`, + valueChangeLoss: change < 0, sellOutcome: outcome, outcomeWord: outcome, outcomeTag, diff --git a/src/locales/en.json b/src/locales/en.json index 2834999..457de34 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -203,6 +203,7 @@ "weeksAgo": "{n}w ago" }, "wallet": { + "availableAssets": "Available Assets", "portfolio": "Portfolio", "today": "Today", "deposit": "Deposit", diff --git a/src/locales/ja.json b/src/locales/ja.json index dfc4807..03f2ec9 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -203,6 +203,7 @@ "weeksAgo": "{n}週間前" }, "wallet": { + "availableAssets": "利用可能資産", "portfolio": "ポートフォリオ", "today": "今日", "deposit": "入金", diff --git a/src/locales/ko.json b/src/locales/ko.json index 4cdb4a0..54983c7 100644 --- a/src/locales/ko.json +++ b/src/locales/ko.json @@ -203,6 +203,7 @@ "weeksAgo": "{n}주 전" }, "wallet": { + "availableAssets": "사용 가능 자산", "portfolio": "포트폴리오", "today": "오늘", "deposit": "입금", diff --git a/src/locales/zh-CN.json b/src/locales/zh-CN.json index 279a26c..a674a7f 100644 --- a/src/locales/zh-CN.json +++ b/src/locales/zh-CN.json @@ -203,6 +203,7 @@ "weeksAgo": "{n}周前" }, "wallet": { + "availableAssets": "可用资产", "portfolio": "资产组合", "today": "今日", "deposit": "入金", diff --git a/src/locales/zh-TW.json b/src/locales/zh-TW.json index d78d86a..756a952 100644 --- a/src/locales/zh-TW.json +++ b/src/locales/zh-TW.json @@ -203,7 +203,8 @@ "weeksAgo": "{n}週前" }, "wallet": { - "portfolio": "資產組合", + "availableAssets": "可用資產", + "portfolio": "資產組合", "today": "今日", "deposit": "入金", "withdraw": "提現", diff --git a/src/stores/user.ts b/src/stores/user.ts index b347eaa..5263e1c 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -4,6 +4,9 @@ import { getUsdcBalance, formatUsdcBalance, getUserInfo } from '@/api/user' import { getUserWsUrl, BASE_URL } from '@/api/request' import { jsonInBlacklist } from '@/api/jwt' import { UserSdk, type BalanceData, type PositionData } from '../../sdk/userSocket' +import { getPositionList, mapPositionToDisplayItem } from '@/api/position' +import { USE_MOCK_WALLET } from '@/config/mock' +import { MOCK_WALLET_POSITIONS } from '@/api/mockData' export interface UserInfo { /** 用户 ID(API 可能返回 id 或 ID) */ @@ -82,6 +85,14 @@ export const useUserStore = defineStore('user', () => { /** 钱包余额显示,如 "0.00",可从接口或 UserSocket 推送更新 */ const balance = ref('0.00') + const positionsValue = ref(0) + + /** 总资产:余额 + 仓位当前价值 */ + const totalAssetValue = computed(() => { + const bal = parseFloat(balance.value) || 0 + return (bal + positionsValue.value).toFixed(2) + }) + let userSdkRef: UserSdk | null = null const positionUpdateCallbacks: ((data: PositionData & Record) => void)[] = [] @@ -109,6 +120,7 @@ export const useUserStore = defineStore('user', () => { }) sdk.onPositionUpdate((data) => { positionUpdateCallbacks.forEach((cb) => cb(data as PositionData & Record)) + fetchPositionsValue() }) sdk.onConnect(() => {}) sdk.onDisconnect(() => {}) @@ -231,6 +243,33 @@ export const useUserStore = defineStore('user', () => { } } + /** 获取仓位并更新仓位总价值 */ + async function fetchPositionsValue() { + if (USE_MOCK_WALLET) { + positionsValue.value = MOCK_WALLET_POSITIONS.reduce((acc, p) => acc + (p.currentValueNum || 0), 0) + return + } + const headers = getAuthHeaders() + if (!headers) return + try { + const uid = user.value?.id ?? user.value?.ID + const userID = uid != null ? Number(uid) : undefined + if (!userID || !Number.isFinite(userID)) return + + const res = await getPositionList( + { page: 1, pageSize: 1000, userID }, + { headers }, + ) + if (res.code === 0 || res.code === 200) { + const list = res.data?.list ?? [] + const posList = list.map(mapPositionToDisplayItem) + positionsValue.value = posList.reduce((acc, p) => acc + (p.currentValueNum || 0), 0) + } + } catch (e) { + console.error('[fetchPositionsValue] 请求失败:', e) + } + } + return { token, user, @@ -238,12 +277,15 @@ export const useUserStore = defineStore('user', () => { displayName, avatarUrl, balance, + positionsValue, + totalAssetValue, setUser, logout, clearLocalSession, getAuthHeaders, fetchUsdcBalance, fetchUserInfo, + fetchPositionsValue, connectUserSocket, disconnectUserSocket, onPositionUpdate, diff --git a/src/views/Profile.vue b/src/views/Profile.vue index 18f1d62..e9b67d9 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -46,7 +46,9 @@ {{ t('profile.walletDetail') }} > -
${{ totalBalance }}
+
+ ${{ totalBalance }} +
{{ t('profile.walletSub', { available: availableBalance, frozen: frozenBalance }) }}
@@ -244,11 +246,11 @@ const userNameRaw = computed(() => { }) /** 显示用:统一取 nickName(userStore.displayName) */ const displayName = computed(() => userStore.displayName || t('profile.defaultName')) -const userIdText = computed(() => { - const uid = rawUser.value.id ?? rawUser.value.ID - if (uid == null || uid === '') return '--' - return String(uid) -}) +// const userIdText = computed(() => { +// const uid = rawUser.value.id ?? rawUser.value.ID +// if (uid == null || uid === '') return '--' +// return String(uid) +// }) const avatarImage = computed(() => userStore.avatarUrl || '') const avatarText = computed(() => { const first = displayName.value.trim().charAt(0) @@ -265,7 +267,7 @@ const hasVip = computed(() => { }) }) const userTag = computed(() => (hasVip.value ? t('profile.vipTrader') : t('profile.trader'))) -const totalBalance = computed(() => userStore.balance || '0.00') +const totalBalance = computed(() => userStore.totalAssetValue || '0.00') const availableBalance = computed(() => { const val = readStringFromUser(['availableBalance', 'available', 'walletAvailable']) return val || totalBalance.value diff --git a/src/views/Wallet.vue b/src/views/Wallet.vue index 969aeb1..726334c 100644 --- a/src/views/Wallet.vue +++ b/src/views/Wallet.vue @@ -1,12 +1,15 @@