feat: switch crypto quotes to usd
This commit is contained in:
@@ -9,8 +9,8 @@ use think\facade\Log;
|
|||||||
|
|
||||||
class CryptoMarketService
|
class CryptoMarketService
|
||||||
{
|
{
|
||||||
private const CACHE_KEY = 'crypto_market_snapshot';
|
private const CACHE_KEY = 'crypto_market_snapshot_usd';
|
||||||
private const STALE_CACHE_KEY = 'crypto_market_snapshot_stale';
|
private const STALE_CACHE_KEY = 'crypto_market_snapshot_usd_stale';
|
||||||
|
|
||||||
public static function snapshot(): array
|
public static function snapshot(): array
|
||||||
{
|
{
|
||||||
@@ -60,7 +60,7 @@ class CryptoMarketService
|
|||||||
), '/');
|
), '/');
|
||||||
$ids = array_column($coinConfig, 'id');
|
$ids = array_column($coinConfig, 'id');
|
||||||
$marketUrl = $apiBase . '/coins/markets?' . http_build_query([
|
$marketUrl = $apiBase . '/coins/markets?' . http_build_query([
|
||||||
'vs_currency' => 'cny',
|
'vs_currency' => 'usd',
|
||||||
'ids' => implode(',', $ids),
|
'ids' => implode(',', $ids),
|
||||||
'order' => 'market_cap_desc',
|
'order' => 'market_cap_desc',
|
||||||
'per_page' => max(1, count($ids)),
|
'per_page' => max(1, count($ids)),
|
||||||
@@ -109,7 +109,7 @@ class CryptoMarketService
|
|||||||
'stale' => false,
|
'stale' => false,
|
||||||
'overview' => [
|
'overview' => [
|
||||||
'btc_market_cap_percentage' => round((float) ($globalData['market_cap_percentage']['btc'] ?? 0), 1),
|
'btc_market_cap_percentage' => round((float) ($globalData['market_cap_percentage']['btc'] ?? 0), 1),
|
||||||
'total_volume_cny' => (float) ($globalData['total_volume']['cny'] ?? 0),
|
'total_volume_usd' => (float) ($globalData['total_volume']['usd'] ?? 0),
|
||||||
],
|
],
|
||||||
'coins' => $coins,
|
'coins' => $coins,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ export interface CryptoMarketSnapshot {
|
|||||||
stale: boolean
|
stale: boolean
|
||||||
overview: {
|
overview: {
|
||||||
btc_market_cap_percentage: number
|
btc_market_cap_percentage: number
|
||||||
total_volume_cny: number
|
total_volume_usd: number
|
||||||
}
|
}
|
||||||
coins: CryptoMarketCoin[]
|
coins: CryptoMarketCoin[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,8 +101,8 @@
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="coin-row__price">
|
<view class="coin-row__price">
|
||||||
<text class="coin-row__price-value">{{ formatCnyPrice(coin.current_price) }}</text>
|
<text class="coin-row__price-value">{{ formatUsdPrice(coin.current_price) }}</text>
|
||||||
<text class="coin-row__market-cap">{{ formatMarketCap(coin.market_cap) }}</text>
|
<text class="coin-row__market-cap">{{ formatUsdMarketCap(coin.market_cap) }}</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="coin-row__badge" :class="changeClass(coin.price_change_percentage_24h)">
|
<view class="coin-row__badge" :class="changeClass(coin.price_change_percentage_24h)">
|
||||||
@@ -187,7 +187,7 @@ const displayCoins = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const btcShare = computed(() => `${btcMarketShare.value.toFixed(1)}%`)
|
const btcShare = computed(() => `${btcMarketShare.value.toFixed(1)}%`)
|
||||||
const totalVolumeText = computed(() => formatLargeCny(totalVolume.value))
|
const totalVolumeText = computed(() => formatLargeUsd(totalVolume.value))
|
||||||
const emptyTitle = computed(() => searchKey.value ? '没有匹配的币种' : '暂无自选币种')
|
const emptyTitle = computed(() => searchKey.value ? '没有匹配的币种' : '暂无自选币种')
|
||||||
const emptyDesc = computed(() => searchKey.value ? '换个关键词试试' : '点击行情右侧星标即可加入自选')
|
const emptyDesc = computed(() => searchKey.value ? '换个关键词试试' : '点击行情右侧星标即可加入自选')
|
||||||
|
|
||||||
@@ -199,7 +199,7 @@ const fetchMarket = async () => {
|
|||||||
allCoins.value = Array.isArray(data?.coins) ? data.coins : []
|
allCoins.value = Array.isArray(data?.coins) ? data.coins : []
|
||||||
updatedTime.value = data?.updated_time || '--:--'
|
updatedTime.value = data?.updated_time || '--:--'
|
||||||
btcMarketShare.value = Number(data?.overview?.btc_market_cap_percentage || 0)
|
btcMarketShare.value = Number(data?.overview?.btc_market_cap_percentage || 0)
|
||||||
totalVolume.value = Number(data?.overview?.total_volume_cny || 0)
|
totalVolume.value = Number(data?.overview?.total_volume_usd || 0)
|
||||||
failedLogos.value = []
|
failedLogos.value = []
|
||||||
loadError.value = allCoins.value.length === 0
|
loadError.value = allCoins.value.length === 0
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -295,25 +295,28 @@ const markLogoError = (symbol: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatCnyPrice = (value: number) => {
|
const formatUsdPrice = (value: number) => {
|
||||||
const price = Number(value || 0)
|
const price = Number(value || 0)
|
||||||
const digits = price >= 100 ? 2 : price >= 1 ? 2 : price >= 0.01 ? 4 : 6
|
const digits = price >= 100 ? 2 : price >= 1 ? 2 : price >= 0.01 ? 4 : 6
|
||||||
return `¥${price.toLocaleString('zh-CN', { minimumFractionDigits: digits, maximumFractionDigits: digits })}`
|
return `$${price.toLocaleString('en-US', { minimumFractionDigits: digits, maximumFractionDigits: digits })}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatMarketCap = (value: number) => {
|
const formatUsdMarketCap = (value: number) => {
|
||||||
const amount = Number(value || 0)
|
const amount = Number(value || 0)
|
||||||
if (amount >= 1e12) return `¥${(amount / 1e12).toFixed(2)}万亿`
|
if (amount >= 1e12) return `$${(amount / 1e12).toFixed(2)}T`
|
||||||
if (amount >= 1e8) return `¥${(amount / 1e8).toFixed(2)}亿`
|
if (amount >= 1e9) return `$${(amount / 1e9).toFixed(2)}B`
|
||||||
if (amount >= 1e4) return `¥${(amount / 1e4).toFixed(2)}万`
|
if (amount >= 1e6) return `$${(amount / 1e6).toFixed(2)}M`
|
||||||
return `¥${amount.toFixed(2)}`
|
if (amount >= 1e3) return `$${(amount / 1e3).toFixed(2)}K`
|
||||||
|
return `$${amount.toFixed(2)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatLargeCny = (value: number) => {
|
const formatLargeUsd = (value: number) => {
|
||||||
const amount = Number(value || 0)
|
const amount = Number(value || 0)
|
||||||
if (amount >= 1e8) return `¥${(amount / 1e8).toLocaleString('zh-CN', { maximumFractionDigits: 2 })}亿`
|
if (amount >= 1e12) return `$${(amount / 1e12).toLocaleString('en-US', { maximumFractionDigits: 2 })}T`
|
||||||
if (amount >= 1e4) return `¥${(amount / 1e4).toLocaleString('zh-CN', { maximumFractionDigits: 2 })}万`
|
if (amount >= 1e9) return `$${(amount / 1e9).toLocaleString('en-US', { maximumFractionDigits: 2 })}B`
|
||||||
return `¥${amount.toLocaleString('zh-CN', { maximumFractionDigits: 2 })}`
|
if (amount >= 1e6) return `$${(amount / 1e6).toLocaleString('en-US', { maximumFractionDigits: 2 })}M`
|
||||||
|
if (amount >= 1e3) return `$${(amount / 1e3).toLocaleString('en-US', { maximumFractionDigits: 2 })}K`
|
||||||
|
return `$${amount.toLocaleString('en-US', { maximumFractionDigits: 2 })}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatChange = (value: number) => {
|
const formatChange = (value: number) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user