diff --git a/index.html b/index.html index 8e95d78..1ba5f39 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,6 @@ - diff --git a/src/main.ts b/src/main.ts index b9f21dd..4661a58 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,6 @@ import 'virtual:uno.css' import {computed, createApp, nextTick, ref, watch} from 'vue' import { createPinia } from 'pinia' import '@unocss/reset/tailwind-compat.css' -import VueApexCharts from 'vue3-apexcharts' import App from './App.vue' import router from './router' import { authenticateWithFingerprint, getToken, isIphone, removeToken } from '@/utils' @@ -25,7 +24,6 @@ app.use(VueLazyLoad, { // options... }) app.use(router) -app.use(VueApexCharts) const userStore = useUserStore() const walletStore = useWalletStore() diff --git a/src/views/base/home/components/PriceChart.vue b/src/views/base/home/components/PriceChart.vue index 8d650b8..afbe168 100644 --- a/src/views/base/home/components/PriceChart.vue +++ b/src/views/base/home/components/PriceChart.vue @@ -6,104 +6,196 @@ هفته گذشته ماه گذشته -
- +
+
+
+ داده نمودار در دسترس نیست +
\ No newline at end of file + diff --git a/src/views/base/home/composables/useBanners.ts b/src/views/base/home/composables/useBanners.ts index de7dfb6..72ae9d7 100644 --- a/src/views/base/home/composables/useBanners.ts +++ b/src/views/base/home/composables/useBanners.ts @@ -1,6 +1,28 @@ import { ref } from 'vue' import { getConstByKey } from '@/api/const' +function parseJsonArray(raw: unknown): unknown[] { + if (raw == null || raw === '') { + return [] + } + if (Array.isArray(raw)) { + return raw + } + if (typeof raw !== 'string') { + return [] + } + const trimmed = raw.trim() + if (!trimmed) { + return [] + } + try { + const parsed = JSON.parse(trimmed) + return Array.isArray(parsed) ? parsed : [] + } catch { + return [] + } +} + export function useBanners() { const banners = ref([]) const loading = ref(false) @@ -9,29 +31,33 @@ export function useBanners() { try { loading.value = true const response = await getConstByKey('header_banner') - if (!response) return + if (!response) { + banners.value = [] + return + } - let list = [] + let list: unknown[] = [] if (typeof response === 'string') { - list = JSON.parse(response) + list = parseJsonArray(response) } else if (typeof response === 'object' && !Array.isArray(response) && 'value' in response) { - list = typeof response.value === 'string' ? JSON.parse(response.value) : response.value + list = parseJsonArray((response as { value?: unknown }).value) } else if (Array.isArray(response) && response.length > 0) { - const first = response[0] - if (first.value) { - list = typeof first.value === 'string' ? JSON.parse(first.value) : first.value + const first = response[0] as { value?: unknown; icon?: unknown } + if (first.value != null) { + list = parseJsonArray(first.value) } else if (first.icon) { list = response } } - banners.value = Array.isArray(list) ? list.filter(b => b.show === true) : [] + banners.value = list.filter((b: { show?: boolean }) => b?.show === true) } catch (error) { console.error('Error loading banners:', error) + banners.value = [] } finally { loading.value = false } } return { banners, loading, loadBanners } -} \ No newline at end of file +} diff --git a/src/views/base/home/composables/usePriceChart.ts b/src/views/base/home/composables/usePriceChart.ts index c24be2f..773d776 100644 --- a/src/views/base/home/composables/usePriceChart.ts +++ b/src/views/base/home/composables/usePriceChart.ts @@ -1,24 +1,104 @@ import { ref } from 'vue' -import { getAllPrices } from '@/api/home' +import { getAllPrices, getChart } from '@/api/home' + +type ChartPoint = [number, number] + +function mapTimelinePoints(items: unknown[]): ChartPoint[] { + if (!Array.isArray(items)) { + return [] + } + + return items + .map((item) => { + const row = item as { timestamp?: number; value?: number; price?: number } + let timestamp = Number(row.timestamp) + const value = Number(row.value ?? row.price ?? 0) + + if (!Number.isFinite(timestamp) || !Number.isFinite(value) || value <= 0) { + return null + } + + if (timestamp > 0 && timestamp < 1e11) { + timestamp *= 1000 + } + + return [timestamp, value] as ChartPoint + }) + .filter((point): point is ChartPoint => point !== null) +} + +async function fetchCandleFallback(): Promise { + try { + const rows = await getChart() + if (!Array.isArray(rows)) { + return [] + } + + return rows + .map((row) => { + if (!row) { + return null + } + + const dateRaw = Array.isArray(row) ? row[0] : row.date + const candle = Array.isArray(row) ? row[1] : row.candle + let value = 0 + + if (Array.isArray(candle)) { + value = Number(candle[3] ?? candle[2] ?? candle[0] ?? 0) + } else if (candle && typeof candle === 'object') { + const candleRow = candle as { end?: number; high?: number; start?: number } + value = Number(candleRow.end ?? candleRow.high ?? candleRow.start ?? 0) + } else { + value = Number(candle ?? 0) + } + + let timestamp = typeof dateRaw === 'number' + ? dateRaw + : new Date(String(dateRaw)).getTime() + + if (timestamp > 0 && timestamp < 1e11) { + timestamp *= 1000 + } + + if (!Number.isFinite(timestamp) || !Number.isFinite(value) || value <= 0) { + return null + } + + return [timestamp, value] as ChartPoint + }) + .filter((point): point is ChartPoint => point !== null) + } catch { + return [] + } +} export function usePriceChart() { - const priceChartSeries = ref([{ name: 'قیمت', data: [] }]) + const priceChartSeries = ref([{ name: 'قیمت', data: [] as ChartPoint[] }]) const selection = ref('DAILY') const chartLoading = ref(false) - const toPersianNumber = (num: number) => { - return num.toString().replace(/\d/g, (d) => '۰۱۲۳۴۵۶۷۸۹'[d]) - } - const fetchPrices = async () => { try { chartLoading.value = true const response = await getAllPrices() - const data = response.data?.prices - const selectedData = data[selection.value] || [] - priceChartSeries.value[0].data = selectedData.map((item: any) => [item.timestamp, item.value - 1]) + const prices = response?.data?.prices ?? response?.prices + + let points: ChartPoint[] = [] + if (prices && typeof prices === 'object') { + const selectedData = prices[selection.value] + points = mapTimelinePoints(Array.isArray(selectedData) ? selectedData : []) + } + + if (points.length === 0) { + points = await fetchCandleFallback() + } + + priceChartSeries.value = [{ name: 'قیمت', data: points }] } catch (error) { console.error('Error fetching prices:', error) + const fallback = await fetchCandleFallback() + priceChartSeries.value = [{ name: 'قیمت', data: fallback }] } finally { chartLoading.value = false }