86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
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
|
|
} |