fix some things
This commit is contained in:
@@ -32,7 +32,7 @@
|
||||
<script lang="ts" setup>
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import BasicIcon from '@/components/BasicPacks/BasicIcon.vue'
|
||||
import { getTvProducts } from '@/api/products'
|
||||
import { getTvProducts, getTvServerTime } from '@/api/products'
|
||||
import type { Product } from '@/models/products'
|
||||
import { getConfigApp } from '@/utils/config'
|
||||
import { getTvProductBuyPrice, getTvProductSellPrice, isTvProductPriceInactive } from '@/utils/productPrice'
|
||||
@@ -47,6 +47,7 @@ const REFRESH_MS = 15000
|
||||
const loading = ref(true)
|
||||
const products = ref<Product[]>([])
|
||||
const now = ref(Date.now())
|
||||
let serverOffsetMs = 0
|
||||
|
||||
let clockTimer: ReturnType<typeof setInterval> | undefined
|
||||
let listTimer: ReturnType<typeof setInterval> | undefined
|
||||
@@ -57,6 +58,7 @@ const shopName = computed(
|
||||
|
||||
const clockText = computed(() =>
|
||||
new Date(now.value).toLocaleTimeString('fa-IR', {
|
||||
timeZone: 'Asia/Tehran',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
@@ -99,10 +101,27 @@ const rows = computed(() => {
|
||||
})
|
||||
})
|
||||
|
||||
function normalizeTvProduct(raw: Product): Product {
|
||||
const image = (raw as Product & { image?: string | null }).image
|
||||
if (image && !raw.img) {
|
||||
return { ...raw, img: image }
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
async function syncServerClock() {
|
||||
const t0 = Date.now()
|
||||
const payload = await getTvServerTime()
|
||||
const t1 = Date.now()
|
||||
if (!payload?.unix) return
|
||||
const rttHalf = (t1 - t0) / 2
|
||||
serverOffsetMs = payload.unix * 1000 + rttHalf - t1
|
||||
}
|
||||
|
||||
async function loadProducts() {
|
||||
try {
|
||||
const list = await getTvProducts()
|
||||
products.value = Array.isArray(list) ? list : []
|
||||
products.value = (Array.isArray(list) ? list : []).map(normalizeTvProduct)
|
||||
} catch {
|
||||
if (!products.value.length) {
|
||||
products.value = []
|
||||
@@ -112,17 +131,20 @@ async function loadProducts() {
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
const settingsStore = useSettingsStore()
|
||||
initPriceRealtimeChannel((key) => settingsStore.getSettingByKey(key))
|
||||
startLastPricePolling()
|
||||
void getLastPrices()
|
||||
await syncServerClock()
|
||||
now.value = Date.now() + serverOffsetMs
|
||||
void loadProducts()
|
||||
clockTimer = setInterval(() => {
|
||||
now.value = Date.now()
|
||||
now.value = Date.now() + serverOffsetMs
|
||||
}, 1000)
|
||||
listTimer = setInterval(() => {
|
||||
void loadProducts()
|
||||
void syncServerClock()
|
||||
}, REFRESH_MS)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user