Files
gold_land_user/src/pages/MarketPage.vue
T
safavimohadeseh78andCursor e3f0457dc9
Frontend CI / unit (test, type-check, build) (push) Successful in 1m11s
Frontend CI / browser E2E (frontend + pinned backend) (push) Failing after 2m48s
feat: complete server-backed six-lesson app and trial tooling
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-12 14:40:37 +03:30

530 lines
13 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import PageBackButton from '@/components/common/PageBackButton.vue'
import { TrendingUp, Heart } from 'lucide-vue-next'
import AppModal from '@/components/common/AppModal.vue'
import MarketChart from '@/components/market/MarketChart.vue'
import { useUserStore } from '@/stores/user'
import { useWalletStore } from '@/stores/wallet'
import { useMarketStore, type TimeRange } from '@/stores/market'
import { formatNumber, formatDecimal, formatCurrency, formatPercent } from '@/utils/storage'
import { tomansForGoldPurchase, tomansForGoldSale } from '@/utils/goldTrade'
import { useToast } from '@/composables/useToast'
import { illustrations } from '@/assets/illustrations'
const router = useRouter()
const userStore = useUserStore()
const walletStore = useWalletStore()
const marketStore = useMarketStore()
const toast = useToast()
const timeRanges: { key: TimeRange; label: string }[] = [
{ key: '1d', label: '۱ روز' },
{ key: '7d', label: '۷ روز' },
{ key: '30d', label: '۳۰ روز' },
{ key: '90d', label: '۹۰ روز' },
{ key: '1y', label: '۱ سال' },
]
const showBuyModal = ref(false)
const showSellModal = ref(false)
const tradeGrams = ref(1)
const tradeBuyEstimate = computed(() => {
const g = Number(tradeGrams.value)
if (!Number.isFinite(g) || g <= 0) return null
return tomansForGoldPurchase(g)
})
const tradeSellEstimate = computed(() => {
const g = Number(tradeGrams.value)
if (!Number.isFinite(g) || g <= 0) return null
return tomansForGoldSale(g)
})
const assetValue = computed(() => walletStore.goldGrams * marketStore.currentPrice)
function openBuy() { tradeGrams.value = 1; showBuyModal.value = true }
function openSell() { tradeGrams.value = 1; showSellModal.value = true }
async function confirmBuy() {
const result = await walletStore.buyGold(tradeGrams.value, marketStore.currentPrice)
result.success ? (toast.success(result.message), showBuyModal.value = false) : toast.error(result.message)
}
async function confirmSell() {
const result = await walletStore.sellGold(tradeGrams.value, marketStore.currentPrice)
result.success ? (toast.success(result.message), showSellModal.value = false) : toast.error(result.message)
}
</script>
<template>
<div class="market-page">
<!-- Header -->
<header class="market-header card-enter">
<PageBackButton @click="router.push('/')" />
<div class="market-header__title">
<img :src="illustrations.goldBar" alt="" class="market-header__title-icon" aria-hidden="true" />
<h1>بازار طلا</h1>
</div>
<div class="market-header__coins">
<img :src="illustrations.coinLarge" alt="" class="market-header__coin-icon" aria-hidden="true" />
<span>{{ formatNumber(userStore.coins) }}</span>
</div>
</header>
<!-- Price + Chart -->
<section class="price-section card-enter" style="animation-delay:0.04s">
<p class="price-section__label">قیمت هر گرم طلا</p>
<p class="price-section__amount">{{ formatNumber(marketStore.currentPrice) }}</p>
<div class="price-section__meta">
<span class="price-section__unit">تومان</span>
<span class="price-section__change">
<TrendingUp :size="14" stroke-width="2.5" />
{{ formatPercent(marketStore.dailyChange) }} (امروز)
</span>
</div>
<MarketChart />
<div class="time-filters">
<button
v-for="range in timeRanges"
:key="range.key"
class="time-filter"
:class="{ 'time-filter--active': marketStore.selectedRange === range.key }"
@click="marketStore.setRange(range.key)"
>
{{ range.label }}
</button>
</div>
</section>
<!-- Buy / Sell -->
<div class="trade-buttons card-enter" style="animation-delay:0.08s">
<button type="button" class="trade-btn trade-btn--buy" @click="openBuy">خرید</button>
<button type="button" class="trade-btn trade-btn--sell" @click="openSell">فروش</button>
</div>
<!-- Assets -->
<section class="asset-card card-enter" style="animation-delay:0.12s">
<div class="asset-card__info">
<div class="asset-card__title">
<Heart :size="14" fill="#f4ae21" color="#f4ae21" aria-hidden="true" />
<span>دارایی من</span>
</div>
<p class="asset-card__grams">
{{ formatDecimal(walletStore.goldGrams) }}
<span class="asset-card__unit">گرم</span>
</p>
<p class="asset-card__value">
<img :src="illustrations.coinLarge" alt="" class="asset-card__coin" aria-hidden="true" />
<span>{{ formatNumber(assetValue) }}</span>
<span class="asset-card__value-unit">تومان</span>
</p>
</div>
<img
:src="illustrations.treasureChest"
alt=""
class="asset-card__chest"
aria-hidden="true"
/>
</section>
<AppModal :open="showBuyModal" title="خرید طلا" @close="showBuyModal = false">
<div class="trade-form">
<label class="trade-form__label">مقدار (گرم)</label>
<input
v-model.number="tradeGrams"
type="number"
min="0.000001"
step="0.000001"
class="trade-form__input"
dir="ltr"
/>
<p class="trade-form__cost">
<template v-if="tradeBuyEstimate != null">
برآورد هزینه (خرید به بالا): {{ formatCurrency(tradeBuyEstimate) }}
</template>
<template v-else>مقدار نامعتبر</template>
</p>
<p class="trade-form__hint">مبلغ نهایی پس از تأیید از سرور ثبت می‌شود.</p>
<p class="trade-form__balance">موجودی: {{ formatCurrency(walletStore.cash) }}</p>
</div>
<template #footer>
<button type="button" class="trade-btn trade-btn--buy trade-btn--modal" @click="confirmBuy">
تأیید خرید
</button>
</template>
</AppModal>
<AppModal :open="showSellModal" title="فروش طلا" @close="showSellModal = false">
<div class="trade-form">
<label class="trade-form__label">مقدار (گرم)</label>
<input
v-model.number="tradeGrams"
type="number"
min="0.000001"
step="0.000001"
class="trade-form__input"
dir="ltr"
/>
<p class="trade-form__cost">
<template v-if="tradeSellEstimate != null">
برآورد دریافتی (فروش به پایین): {{ formatCurrency(tradeSellEstimate) }}
</template>
<template v-else>مقدار نامعتبر</template>
</p>
<p class="trade-form__hint">مبلغ نهایی پس از تأیید از سرور ثبت می‌شود.</p>
<p class="trade-form__balance">طلای موجود: {{ formatDecimal(walletStore.goldGrams) }} گرم</p>
</div>
<template #footer>
<button type="button" class="trade-btn trade-btn--sell trade-btn--modal" @click="confirmSell">
تأیید فروش
</button>
</template>
</AppModal>
</div>
</template>
<style scoped>
.market-page {
min-height: 100dvh;
padding: 0 var(--page-padding) calc(var(--nav-height) + var(--nav-bottom-offset) + env(safe-area-inset-bottom, 0px) + 16px);
}
/* ── Header ── */
.market-header {
display: grid;
grid-template-columns: 40px 1fr auto;
align-items: center;
direction: rtl;
gap: 8px;
height: 52px;
margin-bottom: 18px;
padding-top: 4px;
}
.market-header :deep(.page-back-btn) {
grid-column: 1;
justify-self: start;
}
.market-header__title {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
direction: rtl;
}
.market-header__title-icon {
width: 28px;
height: 28px;
object-fit: contain;
flex-shrink: 0;
}
.market-header__title h1 {
font-size: 1.125rem;
font-weight: 800;
color: var(--text-main);
white-space: nowrap;
}
.market-header__coins {
grid-column: 3;
justify-self: end;
display: inline-flex;
align-items: center;
gap: 6px;
min-height: 34px;
padding: 5px 12px 5px 10px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.08);
font-size: 0.8125rem;
font-weight: 800;
color: var(--text-main);
}
.market-header__coin-icon {
width: 20px;
height: 20px;
object-fit: contain;
}
/* ── Price section ── */
.price-section {
margin-bottom: 16px;
padding: 16px 14px 14px;
background: transparent;
border: 1px solid rgba(174, 185, 210, 0.28);
border-radius: 22px;
}
.price-section__label {
text-align: center;
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-muted);
margin-bottom: 8px;
}
.price-section__amount {
text-align: center;
font-size: clamp(1.75rem, 7vw, 2rem);
font-weight: 800;
color: var(--gold);
line-height: 1.1;
margin-bottom: 8px;
letter-spacing: -0.02em;
}
.price-section__meta {
display: flex;
align-items: center;
justify-content: center;
gap: 24px;
direction: ltr;
margin-bottom: 6px;
}
.price-section__unit {
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-muted);
}
.price-section__change {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 0.8125rem;
font-weight: 700;
color: #32d74b;
direction: rtl;
}
.time-filters {
display: flex;
gap: 6px;
margin-top: 12px;
}
.time-filter {
flex: 1;
min-height: 34px;
padding: 7px 4px;
border-radius: 12px;
font-size: 0.6875rem;
font-weight: 700;
background: rgba(255, 255, 255, 0.04);
color: var(--text-muted);
border: 1px solid rgba(255, 255, 255, 0.07);
transition: all var(--transition-fast);
}
.time-filter--active {
background: rgba(255, 255, 255, 0.08);
color: var(--text-main);
border-color: rgba(174, 185, 210, 0.35);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}
/* ── Trade buttons ── */
.trade-buttons {
position: relative;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
direction: ltr;
margin-bottom: 22px;
}
.trade-buttons::after {
content: '';
position: absolute;
left: 50%;
bottom: -8px;
transform: translateX(-50%);
width: 110px;
height: 2px;
border-radius: 999px;
background: linear-gradient(
90deg,
transparent 0%,
rgba(241, 140, 22, 0.08) 12%,
rgba(241, 140, 22, 0.22) 28%,
rgba(241, 140, 22, 0.9) 50%,
rgba(241, 140, 22, 0.22) 72%,
rgba(241, 140, 22, 0.08) 88%,
transparent 100%
);
box-shadow: 0 0 8px rgba(241, 140, 22, 0.55);
pointer-events: none;
}
.trade-btn {
min-height: 52px;
border: none;
border-radius: 16px;
font-family: inherit;
font-size: 1.0625rem;
font-weight: 800;
color: #fff;
cursor: pointer;
transition: transform var(--transition-fast), filter var(--transition-fast);
position: relative;
overflow: hidden;
}
.trade-btn::before {
content: '';
position: absolute;
inset: 0;
background: linear-gradient(180deg, rgba(255, 255, 255, 0.16) 0%, transparent 48%);
pointer-events: none;
}
.trade-btn:active {
transform: scale(0.98);
}
.trade-btn--buy {
background: linear-gradient(180deg, #34d058 0%, #22b54b 52%, #138d36 100%);
}
.trade-btn--sell {
background: linear-gradient(180deg, #ff5060 0%, #e63443 52%, #b8172d 100%);
}
.trade-btn--modal {
width: 100%;
}
/* ── Asset card ── */
.asset-card {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-height: 118px;
padding: 16px 18px;
background: #1a223a;
border: 1px solid rgba(174, 185, 210, 0.28);
border-radius: 20px;
overflow: hidden;
}
.asset-card__info {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 4px;
direction: rtl;
text-align: right;
}
.asset-card__title {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-muted);
}
.asset-card__grams {
font-size: 1.625rem;
font-weight: 800;
color: var(--text-main);
line-height: 1.15;
}
.asset-card__unit {
font-size: 0.875rem;
font-weight: 600;
color: var(--text-muted);
margin-right: 4px;
}
.asset-card__value {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 0.875rem;
font-weight: 700;
color: var(--text-main);
}
.asset-card__coin {
width: 18px;
height: 18px;
object-fit: contain;
flex-shrink: 0;
}
.asset-card__value-unit {
font-size: 0.8125rem;
font-weight: 600;
color: var(--text-muted);
}
.asset-card__chest {
width: 88px;
height: 88px;
object-fit: contain;
flex-shrink: 0;
margin-left: -4px;
}
/* ── Trade modal form ── */
.trade-form {
display: flex;
flex-direction: column;
gap: 10px;
}
.trade-form__label {
font-size: 0.875rem;
font-weight: 700;
}
.trade-form__input {
padding: 12px 14px;
border-radius: var(--radius-sm);
border: 1px solid rgba(118, 75, 190, 0.45);
background: #040817;
color: var(--text-main);
font-family: inherit;
font-size: 1rem;
font-weight: 700;
appearance: textfield;
-moz-appearance: textfield;
}
.trade-form__input::-webkit-outer-spin-button,
.trade-form__input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.trade-form__cost {
font-size: 1.0625rem;
font-weight: 800;
color: var(--gold-light);
}
.trade-form__hint {
font-size: 0.75rem;
color: var(--text-muted);
line-height: 1.5;
margin: 0;
}
.trade-form__balance {
font-size: 0.8125rem;
color: var(--text-muted);
}
</style>