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)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { useFullscreen } from '@vueuse/core'
|
||||
import type { Product } from '@/models/products'
|
||||
import { getTvProducts } from '@/api/products'
|
||||
import { getTvProducts, getTvServerTime } from '@/api/products'
|
||||
import { getConfigApp } from '@/utils/config'
|
||||
import { useSettingsStore } from '@/store/settingsStore'
|
||||
import { getTvProductBuyPrice, getTvProductSellPrice, isTvProductPriceInactive } from '@/utils/productPrice'
|
||||
@@ -73,7 +73,10 @@ function isBankCoin(product: Product) {
|
||||
const title = (product.title ?? '').trim()
|
||||
if (group === 'سکه' || group === 'سکه بانکی') return true
|
||||
if (group.includes('سکه') && !group.includes('پارسیان')) return true
|
||||
return title.includes('بانکی')
|
||||
if (title.includes('بانکی')) return true
|
||||
// API سبک TV ممکن است group_type نداشته باشد
|
||||
if (title.includes('سکه') && !title.includes('پارسیان')) return true
|
||||
return false
|
||||
}
|
||||
|
||||
function bankCoinRank(product: Product) {
|
||||
@@ -116,6 +119,8 @@ export function useTvMonitorPage() {
|
||||
let clockTimer: ReturnType<typeof setInterval> | undefined
|
||||
let cursorTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let listTimer: ReturnType<typeof setInterval> | undefined
|
||||
/** اختلاف میلیثانیهٔ ساعت سرور (Asia/Tehran) با Date.now محلی */
|
||||
let serverOffsetMs = 0
|
||||
|
||||
const brandName = computed(
|
||||
() => String(getConfigApp('name_fa') || getConfigApp('name') || 'تابلو قیمت'),
|
||||
@@ -130,6 +135,23 @@ export function useTvMonitorPage() {
|
||||
return marked.length ? marked : list
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
const parsianCoins = computed(() =>
|
||||
catalogProducts.value
|
||||
.filter(isParsianCoin)
|
||||
@@ -207,7 +229,7 @@ export function useTvMonitorPage() {
|
||||
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 = []
|
||||
@@ -218,7 +240,7 @@ export function useTvMonitorPage() {
|
||||
}
|
||||
|
||||
function updateClock() {
|
||||
const now = moment()
|
||||
const now = moment(Date.now() + serverOffsetMs).utcOffset(210)
|
||||
nowLabel.value = now.format('HH:mm:ss')
|
||||
dateLabel.value = now.format('dddd jD jMMMM jYYYY')
|
||||
}
|
||||
@@ -313,6 +335,7 @@ export function useTvMonitorPage() {
|
||||
|
||||
onMounted(async () => {
|
||||
document.documentElement.classList.add(TV_HTML_CLASS)
|
||||
await syncServerClock()
|
||||
updateClock()
|
||||
hideCursorSoon()
|
||||
clockTimer = setInterval(updateClock, 1000)
|
||||
@@ -325,6 +348,7 @@ export function useTvMonitorPage() {
|
||||
void loadProducts()
|
||||
listTimer = setInterval(() => {
|
||||
void loadProducts()
|
||||
void syncServerClock()
|
||||
}, REFRESH_MS)
|
||||
|
||||
document.title = `${brandName.value} | تابلو قیمت`
|
||||
|
||||
Reference in New Issue
Block a user