2026-11-03 19:57:39 +03:30
|
|
|
import { computed, ref, watch } from 'vue'
|
|
|
|
|
import { lastPrice } from '@/utils/price'
|
|
|
|
|
import { usePriceWithoutType } from '@/utils/usePrice'
|
|
|
|
|
|
|
|
|
|
const BOARD_FEED_URL = 'https://price.zaarbox.ir/api/free'
|
|
|
|
|
|
|
|
|
|
type BoardRefs = { copper: number; dollar: number }
|
|
|
|
|
|
|
|
|
|
const boardCache = ((globalThis as typeof globalThis & {
|
|
|
|
|
__boursecuBoardRefs?: { started: boolean; refs: BoardRefs | null }
|
|
|
|
|
}).__boursecuBoardRefs ??= { started: false, refs: null })
|
|
|
|
|
|
|
|
|
|
function boardRowTotal(row: Record<string, unknown>): number {
|
|
|
|
|
const price = Math.round(Number(row.price) || 0)
|
|
|
|
|
if (price <= 0) return 0
|
|
|
|
|
return price + Math.round(Number(row.profit) || 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function worldCopperScore(row: Record<string, unknown>): number | null {
|
|
|
|
|
const key = String(row.key ?? '').trim().toLowerCase()
|
|
|
|
|
const name = String(row.name ?? '').trim()
|
|
|
|
|
if (key.includes('basemetal_london_copper')) return 0
|
|
|
|
|
if (name === 'TGJU فلز مس') return 1
|
|
|
|
|
if (name.includes('مس جهانی')) return 2
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function freeDollarScore(row: Record<string, unknown>): number | null {
|
|
|
|
|
const key = String(row.key ?? '').trim().toLowerCase()
|
|
|
|
|
const name = String(row.name ?? '').trim()
|
|
|
|
|
if (key.includes('copper') || name.includes('کاتد') || name.includes('مس')) return null
|
|
|
|
|
if (key === 'tgju_usd') return 0
|
|
|
|
|
if (name === 'TGJU دلار' || name === 'tgju دلار آمریکا') return 1
|
|
|
|
|
if (name.includes('دلار') && name.includes('آزاد')) return 2
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pickBoardRefs(rows: unknown[]) {
|
|
|
|
|
let copper = 0
|
|
|
|
|
let dollar = 0
|
|
|
|
|
let copperScore = Number.POSITIVE_INFINITY
|
|
|
|
|
let dollarScore = Number.POSITIVE_INFINITY
|
|
|
|
|
for (const item of rows) {
|
|
|
|
|
if (!item || typeof item !== 'object') continue
|
|
|
|
|
const row = item as Record<string, unknown>
|
|
|
|
|
const total = boardRowTotal(row)
|
|
|
|
|
if (total <= 0) continue
|
|
|
|
|
const copperRank = worldCopperScore(row)
|
|
|
|
|
if (copperRank !== null && copperRank < copperScore) {
|
|
|
|
|
copperScore = copperRank
|
|
|
|
|
copper = total
|
|
|
|
|
}
|
|
|
|
|
const dollarRank = freeDollarScore(row)
|
|
|
|
|
if (dollarRank !== null && dollarRank < dollarScore) {
|
|
|
|
|
dollarScore = dollarRank
|
|
|
|
|
dollar = total
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return copper > 0 && dollar > 0 ? { copper, dollar } : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sharedBoardRefs = ref<BoardRefs | null>(boardCache.refs)
|
|
|
|
|
|
|
|
|
|
export function useHomeQuoteRows() {
|
|
|
|
|
function ensureBoardRefs() {
|
|
|
|
|
const liveCopper = Number(lastPrice.value?.copper_world ?? 0)
|
|
|
|
|
const liveDollar = Number(lastPrice.value?.dollar_free ?? 0)
|
|
|
|
|
if (liveCopper > 0 && liveDollar > 0) return
|
|
|
|
|
if (boardCache.refs) {
|
|
|
|
|
sharedBoardRefs.value = boardCache.refs
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (boardCache.started) return
|
|
|
|
|
boardCache.started = true
|
|
|
|
|
void fetch(BOARD_FEED_URL)
|
|
|
|
|
.then((res) => res.json())
|
|
|
|
|
.then((rows: unknown) => {
|
|
|
|
|
if (!Array.isArray(rows)) return
|
|
|
|
|
const picked = pickBoardRefs(rows)
|
|
|
|
|
if (!picked) return
|
|
|
|
|
boardCache.refs = picked
|
|
|
|
|
sharedBoardRefs.value = picked
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
boardCache.started = false
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(lastPrice, () => ensureBoardRefs(), { immediate: true })
|
|
|
|
|
|
|
|
|
|
const quoteRows = computed(() => {
|
|
|
|
|
const fromServer = Number(lastPrice.value?.copper_world ?? 0) > 0
|
|
|
|
|
&& Number(lastPrice.value?.dollar_free ?? 0) > 0
|
|
|
|
|
const copper = fromServer
|
|
|
|
|
? Number(lastPrice.value?.copper_world)
|
|
|
|
|
: (sharedBoardRefs.value?.copper ?? 0)
|
|
|
|
|
const dollar = fromServer
|
|
|
|
|
? Number(lastPrice.value?.dollar_free)
|
|
|
|
|
: (sharedBoardRefs.value?.dollar ?? 0)
|
|
|
|
|
const tonText = String(lastPrice.value?.copper_ton ?? '')
|
|
|
|
|
const kiloText = String(lastPrice.value?.copper_kilo ?? '')
|
|
|
|
|
const ton = fromServer && tonText !== '' && tonText !== '0'
|
|
|
|
|
? Number(tonText)
|
|
|
|
|
: (copper > 0 && dollar > 0 ? copper * dollar : 0)
|
|
|
|
|
const kilo = fromServer && kiloText !== '' && kiloText !== '0'
|
|
|
|
|
? Number(kiloText)
|
|
|
|
|
: (ton > 0 ? ton / 1000 : 0)
|
|
|
|
|
const text = (value: number) => (value > 0 ? usePriceWithoutType(value) : '—')
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{ label: 'قیمت مس جهانی', value: text(copper), unit: copper > 0 ? 'دلار' : '' },
|
|
|
|
|
{ label: 'قیمت دلار بازار آزاد', value: text(dollar), unit: dollar > 0 ? 'ریال' : '' },
|
2026-11-18 18:26:18 +03:30
|
|
|
{ label: 'ارزش ذاتی هر تن', value: text(ton), unit: ton > 0 ? 'ریال' : '' },
|
|
|
|
|
{ label: 'ارزش ذاتی هر کیلوگرم', value: text(kilo), unit: kilo > 0 ? 'ریال' : '' },
|
2026-11-03 19:57:39 +03:30
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { quoteRows }
|
|
|
|
|
}
|