Files
boursecu_user/src/utils/price.ts
T

95 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-06-03 22:04:45 +03:30
import { getLastPrice, getPrice } from '@/api/home'
import type { Price, PriceUp } from '@/models/home'
import { ref } from 'vue'
2026-01-18 13:01:46 +03:30
2026-06-03 22:04:45 +03:30
let interval: ReturnType<typeof setInterval> | undefined
/** @deprecated از lastPrice استفاده کنید — برای سازگاری با کد قدیمی sync می‌شود */
2026-01-18 13:01:46 +03:30
export const price = ref<Price>()
export const lastPrice = ref<Price>()
2026-06-03 22:04:45 +03:30
export const olderPrice = ref<number | undefined>(0)
2026-06-06 15:48:27 +03:30
/** buy/sell نمایش و معامله — همان buy/sell/price_last از /api/price/last */
2026-06-03 22:04:45 +03:30
export function getTradeBuyPrice(p?: Price | null) {
2026-06-06 15:48:27 +03:30
return p?.buy ?? p?.price_last ?? p?.up?.buy ?? 0
2026-06-03 22:04:45 +03:30
}
export function getTradeSellPrice(p?: Price | null) {
2026-06-06 15:48:27 +03:30
return p?.sell ?? p?.up?.sell ?? 0
2026-06-03 22:04:45 +03:30
}
2026-06-06 15:48:27 +03:30
/** up جدا نگه داشته می‌شود؛ buy/sell/price_last از lastPrice سرور دست‌نخورده می‌ماند */
2026-06-03 22:04:45 +03:30
export function normalizePriceResponse(data: Price): Price {
2026-06-06 15:48:27 +03:30
return data
2026-06-03 22:04:45 +03:30
}
2026-01-18 13:01:46 +03:30
export async function setIntervalGetAllPrice() {
2026-06-03 22:04:45 +03:30
try {
interval = setInterval(async () => {
await getLastPrices()
}, 10000)
} catch (e) {
console.log(e)
}
2026-01-18 13:01:46 +03:30
}
export async function setIntervalGetLastPrice() {
2026-06-03 22:04:45 +03:30
return setIntervalGetAllPrice()
2026-01-18 13:01:46 +03:30
}
2026-06-03 22:04:45 +03:30
/** @deprecated فقط getLastPrices را صدا بزنید */
2026-01-18 13:01:46 +03:30
export async function getUpPrice() {
2026-06-03 22:04:45 +03:30
await getLastPrices()
}
async function fetchUpPrices(): Promise<PriceUp | undefined> {
try {
const up = await getPrice()
if (up?.buy != null && up?.sell != null) {
return { buy: up.buy, sell: up.sell }
2026-01-18 13:01:46 +03:30
}
2026-06-03 22:04:45 +03:30
} catch (e) {
console.log(e)
}
2026-01-18 13:01:46 +03:30
}
export async function getLastPrices() {
2026-06-03 22:04:45 +03:30
try {
let dataLast = await getLastPrice()
if (!dataLast?.up) {
const up = await fetchUpPrices()
if (up) {
dataLast = { ...dataLast, up }
}
2026-01-18 13:01:46 +03:30
}
2026-06-03 22:04:45 +03:30
const normalized = normalizePriceResponse(dataLast)
if (lastPrice.value?.price_last != normalized.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
lastPrice.value = normalized
price.value = {
...normalized,
buy: getTradeBuyPrice(normalized),
sell: getTradeSellPrice(normalized),
}
} catch (e) {
console.log(e)
}
2026-01-18 13:01:46 +03:30
}
export function stopPriceInterval() {
2026-06-03 22:04:45 +03:30
if (interval) {
clearInterval(interval)
interval = undefined
}
2026-01-18 13:01:46 +03:30
}
export function percentPriceChanged() {
2026-06-03 22:04:45 +03:30
if (!olderPrice.value) {
return 0
}
return ((lastPrice.value?.price_last! - olderPrice.value) * 100) / olderPrice.value
}