Files
boursecu_user/src/utils/price.ts
T

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2026-01-18 13:01:46 +03:30
import {loading} from '@/utils/index'
import {getLastPrice, getPrice} from '@/api/home'
import {ref} from 'vue'
import type {Price} from '@/models/home'
let interval: any
export const price = ref<Price>()
export const lastPrice = ref<Price>()
export const olderPrice = ref<number| undefined>(0)
export async function setIntervalGetAllPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
}
export async function setIntervalGetLastPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
// await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
}
export async function getUpPrice() {
try {
// loading.value = true
const data = await getPrice()
// const dataLast = await getLastPrice()
// if (lastPrice.value?.price_last != dataLast.price_last) {
// olderPrice.value = lastPrice.value?.price_last
// }
price.value = data
// lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
}
}
export async function getLastPrices() {
try {
// loading.value = true
// const data = await getPrice()
const dataLast = await getLastPrice()
if (lastPrice.value?.price_last != dataLast.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
// price.value = data
lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
}
}
export function stopPriceInterval() {
// console.log('stop')
clearInterval(interval);
}
export function percentPriceChanged() {
// console.log(olderPrice.value)
// console.log(lastPrice.value?.price_last)
if (!olderPrice.value)
return 0
return ((lastPrice.value?.price_last-olderPrice.value) *100)/olderPrice.value
}