update
This commit is contained in:
+14
-6
@@ -15,7 +15,7 @@ let lastSubscribers = 0
|
||||
|
||||
export const price = ref<Price>()
|
||||
export const lastPrice = ref<Price>()
|
||||
export const olderPrice = ref<number | undefined>(0)
|
||||
export const olderPrice = ref<number | null>(null)
|
||||
|
||||
let lastPriceSourceAt = 0
|
||||
let consecutivePriceFetchFailures = 0
|
||||
@@ -162,13 +162,17 @@ export async function getLastPrices(): Promise<boolean> {
|
||||
|
||||
export function applyLastPricePayload(dataLast: Price) {
|
||||
const normalized = normalizePriceResponse(dataLast)
|
||||
if (lastPrice.value?.price_last != normalized.price_last) {
|
||||
olderPrice.value = lastPrice.value?.price_last
|
||||
const nextBuy = getTradeBuyPrice(normalized)
|
||||
const prevBuy = getTradeBuyPrice(lastPrice.value)
|
||||
|
||||
if (prevBuy > 0 && nextBuy > 0 && prevBuy !== nextBuy) {
|
||||
olderPrice.value = prevBuy
|
||||
}
|
||||
|
||||
lastPrice.value = normalized
|
||||
price.value = {
|
||||
...normalized,
|
||||
buy: getTradeBuyPrice(normalized),
|
||||
buy: nextBuy,
|
||||
sell: getTradeSellPrice(normalized),
|
||||
}
|
||||
|
||||
@@ -177,9 +181,13 @@ export function applyLastPricePayload(dataLast: Price) {
|
||||
}
|
||||
}
|
||||
|
||||
/** تغییر درصدی نسبت به قیمت خرید قبلی (آخرین بهروزرسانی) */
|
||||
export function percentPriceChanged() {
|
||||
if (!olderPrice.value) {
|
||||
const currentBuy = getTradeBuyPrice(lastPrice.value)
|
||||
const prevBuy = olderPrice.value
|
||||
if (!currentBuy || prevBuy == null || prevBuy <= 0 || prevBuy === currentBuy) {
|
||||
return 0
|
||||
}
|
||||
return ((lastPrice.value?.price_last! - olderPrice.value) * 100) / olderPrice.value
|
||||
|
||||
return ((currentBuy - prevBuy) * 100) / prevBuy
|
||||
}
|
||||
|
||||
@@ -9,28 +9,111 @@ export type SupportMessengerType =
|
||||
| 'eitaa'
|
||||
| 'soroush'
|
||||
|
||||
export interface SupportMessengerEntry {
|
||||
messenger: string
|
||||
id: string
|
||||
}
|
||||
|
||||
export interface SupportMessengerSettings {
|
||||
messengers?: SupportMessengerEntry[]
|
||||
messenger?: SupportMessengerType | string
|
||||
messengerId?: string
|
||||
messengerText?: string
|
||||
}
|
||||
|
||||
export interface SupportMessengerMeta {
|
||||
label: string
|
||||
icon: string
|
||||
accent: string
|
||||
}
|
||||
|
||||
export interface ResolvedSupportMessengerEntry extends SupportMessengerEntry {
|
||||
label: string
|
||||
icon: string
|
||||
accent: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export const supportMessengerSettings = ref<SupportMessengerSettings>({})
|
||||
export const supportMessengerPickerOpen = ref(false)
|
||||
|
||||
const VALID_SUPPORT_MESSENGERS = new Set([
|
||||
'bale',
|
||||
'telegram',
|
||||
'whatsapp',
|
||||
'rubika',
|
||||
'eitaa',
|
||||
'soroush',
|
||||
])
|
||||
|
||||
export const SUPPORT_MESSENGER_META: Record<string, SupportMessengerMeta> = {
|
||||
bale: { label: 'بله', icon: 'vuesax-linear:messages-2', accent: '#16a34a' },
|
||||
telegram: { label: 'تلگرام', icon: 'vuesax-linear:send-2', accent: '#0ea5e9' },
|
||||
whatsapp: { label: 'واتساپ', icon: 'vuesax-linear:whatsapp', accent: '#22c55e' },
|
||||
rubika: { label: 'روبیکا', icon: 'vuesax-linear:messages-2', accent: '#7c3aed' },
|
||||
eitaa: { label: 'ایتا', icon: 'vuesax-linear:messages-2', accent: '#f59e0b' },
|
||||
soroush: { label: 'سروش', icon: 'vuesax-linear:messages-2', accent: '#2563eb' },
|
||||
}
|
||||
|
||||
function getSupportSettingValue(
|
||||
settings: Array<{ key?: string; is_active?: boolean | number; value?: unknown }>,
|
||||
key: string
|
||||
settings: Array<{ key?: string; value?: unknown }>,
|
||||
key: string,
|
||||
) {
|
||||
const setting = settings.find((item) => item?.key === key)
|
||||
if (!setting) return ''
|
||||
|
||||
const value = setting.value
|
||||
if (value === undefined || value === null) return ''
|
||||
|
||||
return String(value).trim()
|
||||
}
|
||||
|
||||
export function buildSupportMessengerUrl(messenger?: string, messengerId?: string) {
|
||||
export function parseSupportMessengersJson(raw?: string | null): SupportMessengerEntry[] {
|
||||
const value = (raw ?? '').trim()
|
||||
if (!value) return []
|
||||
|
||||
try {
|
||||
const decoded = JSON.parse(value)
|
||||
if (!Array.isArray(decoded)) return []
|
||||
|
||||
return decoded
|
||||
.map((row) => ({
|
||||
messenger: String(row?.messenger ?? '').trim().toLowerCase(),
|
||||
id: String(row?.id ?? '').trim(),
|
||||
}))
|
||||
.filter((row) => VALID_SUPPORT_MESSENGERS.has(row.messenger) && row.id !== '')
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveSupportMessengersFromSettings(
|
||||
settings: Array<{ key?: string; value?: unknown }>,
|
||||
): SupportMessengerEntry[] {
|
||||
const fromJson = parseSupportMessengersJson(getSupportSettingValue(settings, 'support_messengers'))
|
||||
if (fromJson.length > 0) return fromJson
|
||||
|
||||
const legacyMessenger = getSupportSettingValue(settings, 'support_messenger').toLowerCase()
|
||||
const legacyId = getSupportSettingValue(settings, 'support_messenger_id')
|
||||
if (!legacyId) return []
|
||||
|
||||
const messenger = legacyMessenger || 'bale'
|
||||
if (!VALID_SUPPORT_MESSENGERS.has(messenger)) return []
|
||||
|
||||
return [{ messenger, id: legacyId }]
|
||||
}
|
||||
|
||||
export function resolveSupportMessengerMeta(messenger?: string): SupportMessengerMeta {
|
||||
const type = (messenger ?? '').trim().toLowerCase()
|
||||
return SUPPORT_MESSENGER_META[type] ?? {
|
||||
label: 'پشتیبانی',
|
||||
icon: 'vuesax-linear:headphone',
|
||||
accent: '#334155',
|
||||
}
|
||||
}
|
||||
|
||||
export function buildSupportMessengerUrl(
|
||||
messenger?: string,
|
||||
messengerId?: string,
|
||||
) {
|
||||
const id = (messengerId ?? '').trim()
|
||||
if (!id) return ''
|
||||
|
||||
@@ -62,12 +145,48 @@ export function buildSupportMessengerUrl(messenger?: string, messengerId?: strin
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveSupportMessengerEntries(
|
||||
settings?: SupportMessengerSettings,
|
||||
): ResolvedSupportMessengerEntry[] {
|
||||
const resolved = settings ?? supportMessengerSettings.value
|
||||
const source = resolved.messengers?.length
|
||||
? resolved.messengers
|
||||
: resolveLegacySingleMessenger(resolved)
|
||||
|
||||
return source
|
||||
.map((entry) => {
|
||||
const meta = resolveSupportMessengerMeta(entry.messenger)
|
||||
const url = buildSupportMessengerUrl(entry.messenger, entry.id)
|
||||
if (!url) return null
|
||||
return { ...entry, ...meta, url }
|
||||
})
|
||||
.filter((entry): entry is ResolvedSupportMessengerEntry => entry !== null)
|
||||
}
|
||||
|
||||
function resolveLegacySingleMessenger(settings: SupportMessengerSettings): SupportMessengerEntry[] {
|
||||
const messenger = (settings.messenger ?? '').trim().toLowerCase()
|
||||
const id = (settings.messengerId ?? '').trim()
|
||||
if (!messenger || !id) return []
|
||||
if (!VALID_SUPPORT_MESSENGERS.has(messenger)) return []
|
||||
return [{ messenger, id }]
|
||||
}
|
||||
|
||||
export async function openSupportMessengerEntry(entry: ResolvedSupportMessengerEntry | SupportMessengerEntry) {
|
||||
const url = 'url' in entry && entry.url
|
||||
? entry.url
|
||||
: buildSupportMessengerUrl(entry.messenger, entry.id)
|
||||
if (!url) return false
|
||||
window.open(url, '_blank', 'noopener,noreferrer')
|
||||
return true
|
||||
}
|
||||
|
||||
export async function loadSupportMessengerFromSettings() {
|
||||
try {
|
||||
const settings = await getSettings()
|
||||
if (!settings || !Array.isArray(settings)) return
|
||||
|
||||
supportMessengerSettings.value = {
|
||||
messengers: resolveSupportMessengersFromSettings(settings),
|
||||
messenger: getSupportSettingValue(settings, 'support_messenger'),
|
||||
messengerId: getSupportSettingValue(settings, 'support_messenger_id'),
|
||||
messengerText: getSupportSettingValue(settings, 'support_messenger_text'),
|
||||
|
||||
Reference in New Issue
Block a user