Files
boursecu_user/src/utils/referralShare.ts
T

98 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-06-17 17:39:41 +03:30
import { message } from 'ant-design-vue'
import { useSettingsStore } from '@/store/settingsStore'
import { getConfigApp } from '@/utils/config'
export function getReferralShareTitle(projectName?: string | null) {
return projectName ?? getConfigApp('name_fa') ?? getConfigApp('name') ?? 'طلا'
}
export const DEFAULT_INVITE_LINK_TEXT = `⭐️الان طلا بخر؛ آخر ماه سودشو ببر!
با خرید طلای آبشده در مقدارهای کم ولی مستمر، سود بیشتری ببر!
این دعوت نامه از طرف "٪نام" عزیز برای شما ارسال شده تا شما هم از بازار طلای ما جا نمونید.
روی لینک زیر کلیک کنید و ثبت نام خود را تکمیل کنید.
٪لینک`
export function getInviteLinkTemplate(): string {
try {
const fromSettings = String(useSettingsStore().getSettingByKey('invite_link_text') ?? '').trim()
if (fromSettings) {
return fromSettings
}
} catch {
// settings store may not be ready yet
}
return DEFAULT_INVITE_LINK_TEXT
}
/** جایگزینی ٪نام / %نام و ٪لینک / %لینک در قالب متن دعوت */
export function renderInviteLinkText(
template: string,
options: { userName?: string | null; link?: string | null },
): string {
const userName = String(options.userName ?? '').trim()
const link = String(options.link ?? '').trim()
return template
.replace(/[%٪]\s*لینک/gu, link)
.replace(/[%٪]\s*نام/gu, userName)
}
function resolveReferralRegisterLink(referralId?: string | number | null): string {
2026-06-17 17:39:41 +03:30
const id = String(referralId ?? '').trim()
if (!id) return ''
const query = `register=true&id=${encodeURIComponent(id)}`
2026-06-17 17:39:41 +03:30
let base = ''
try {
base = String(useSettingsStore().getSettingByKey('app_url') ?? '').trim()
} catch {
base = ''
}
if (base) {
return `${base.replace(/\/+$/, '')}/login?${query}`
2026-06-17 17:39:41 +03:30
}
if (typeof window !== 'undefined' && window.location?.origin) {
return `${window.location.origin}/login?${query}`
2026-06-17 17:39:41 +03:30
}
return `/login?${query}`
2026-06-17 17:39:41 +03:30
}
export function getReferralShareText(options: {
userName?: string | null
projectName?: string | null
referralId?: string | number | null
}) {
const userName = options.userName ?? ''
const link = resolveReferralRegisterLink(options.referralId)
2026-06-17 17:39:41 +03:30
return renderInviteLinkText(getInviteLinkTemplate(), { userName, link })
}
export async function shareReferralText(text: string, title?: string | null): Promise<void> {
const shareTitle = title ?? getReferralShareTitle()
if (typeof navigator !== 'undefined' && typeof navigator.share === 'function') {
try {
await navigator.share({ title: shareTitle, text })
return
} catch (err) {
if ((err as Error)?.name === 'AbortError') {
return
}
}
}
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text)
message.info('متن دعوتنامه رونوشت شد')
return
}
message.error('اشتراک‌گذاری در این مرورگر پشتیبانی نمی‌شود')
}