Files
ghatreh_user/src/utils/supportMessenger.ts
T
2026-06-29 15:27:35 +03:30

79 lines
2.1 KiB
TypeScript

import { ref } from 'vue'
import { getSettings } from '@/api/configuration'
export type SupportMessengerType =
| 'bale'
| 'telegram'
| 'whatsapp'
| 'rubika'
| 'eitaa'
| 'soroush'
export interface SupportMessengerSettings {
messenger?: SupportMessengerType | string
messengerId?: string
messengerText?: string
}
export const supportMessengerSettings = ref<SupportMessengerSettings>({})
function getSupportSettingValue(
settings: Array<{ key?: string; is_active?: boolean | number; 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) {
const id = (messengerId ?? '').trim()
if (!id) return ''
if (/^https?:\/\//i.test(id)) return id
const type = (messenger ?? '').trim().toLowerCase()
const username = id.replace(/^@/, '')
if (!type) return ''
switch (type) {
case 'bale':
if (id.includes('ble.ir')) {
return id.startsWith('http') ? id : `https://${id.replace(/^\/\//, '')}`
}
return `https://ble.ir/${username}`
case 'telegram':
return `https://t.me/${username}`
case 'whatsapp':
return `https://wa.me/${username.replace(/\D/g, '')}`
case 'rubika':
return `https://rubika.ir/${username}`
case 'eitaa':
return `https://eitaa.com/${username}`
case 'soroush':
return `https://splus.ir/splus/${username}`
default:
return ''
}
}
export async function loadSupportMessengerFromSettings() {
try {
const settings = await getSettings()
if (!settings || !Array.isArray(settings)) return
supportMessengerSettings.value = {
messenger: getSupportSettingValue(settings, 'support_messenger'),
messengerId: getSupportSettingValue(settings, 'support_messenger_id'),
messengerText: getSupportSettingValue(settings, 'support_messenger_text'),
}
} catch (e) {
console.log(e)
}
}