203 lines
6.5 KiB
TypeScript
203 lines
6.5 KiB
TypeScript
import { message } from 'ant-design-vue'
|
|
import { useSettingsStore } from '@/store/settingsStore'
|
|
import { useUserStore } from '@/store/userStore'
|
|
|
|
export interface DepositGuideVars {
|
|
accountHolder?: string | null
|
|
cardNumber?: string | null
|
|
sheba?: string | null
|
|
/** نام مشتری واردشده — برای پیام پشتیبانی واریز */
|
|
customerName?: string | null
|
|
}
|
|
|
|
export const DEFAULT_DEPOSIT_GUIDE_TEXT =
|
|
'جهت شارژ حساب کاربری خود، کلمه «شماره حساب» را برای پشتیبانی ارسال نمایید.'
|
|
|
|
export const DEFAULT_DEPOSIT_SUPPORT_MESSAGE_TEXT =
|
|
'باسلام، %نام هستم. کدپیگیری و مبلغ واریزی من:'
|
|
|
|
/** الگوی متغیر نام — ٪نام / %نام / % نام / %نام% */
|
|
const NAME_PLACEHOLDER_RE = /[%٪]\s*نام(?:\s*[%٪])?/gu
|
|
const CARD_PLACEHOLDER_RE = /[%٪]\s*کارت(?:\s*[%٪])?/gu
|
|
const SHEBA_PLACEHOLDER_RE = /[%٪]\s*شبا(?:\s*[%٪])?/gu
|
|
|
|
export function getCustomerNameFromUser(): string {
|
|
try {
|
|
const user = useUserStore().userInfo
|
|
const fullName = [user?.fname, user?.lname].filter(Boolean).join(' ').trim()
|
|
return fullName
|
|
} catch {
|
|
return ''
|
|
}
|
|
}
|
|
|
|
/** نام نمایشی در پیام پشتیبانی: اول مشتری، بعد نام صاحب حساب تنظیمات */
|
|
function resolveSupportMessageName(vars: DepositGuideVars): string {
|
|
const customerName = String(vars.customerName ?? '').trim()
|
|
if (customerName) return customerName
|
|
return String(vars.accountHolder ?? '').trim()
|
|
}
|
|
|
|
/** فقط %نام — برای پیام SMS/پیامرسان پشتیبانی (نه کارت/شبا) */
|
|
export function renderDepositSupportMessageText(
|
|
template: string,
|
|
vars: DepositGuideVars,
|
|
): string {
|
|
const name = resolveSupportMessageName(vars)
|
|
return template.replace(NAME_PLACEHOLDER_RE, name)
|
|
}
|
|
|
|
/** جایگزینی متغیرها در متن راهنمای واریز — ٪نام / %نام، ٪کارت / %کارت، ٪شبا / %شبا */
|
|
export function renderDepositGuideText(
|
|
template: string,
|
|
vars: DepositGuideVars,
|
|
): string {
|
|
const accountHolder = String(vars.accountHolder ?? '').trim()
|
|
const cardNumber = String(vars.cardNumber ?? '').trim()
|
|
const sheba = String(vars.sheba ?? '').trim()
|
|
|
|
return template
|
|
.replace(SHEBA_PLACEHOLDER_RE, sheba)
|
|
.replace(CARD_PLACEHOLDER_RE, cardNumber)
|
|
.replace(NAME_PLACEHOLDER_RE, accountHolder)
|
|
}
|
|
|
|
export function getDepositGuideVarsFromSettings(): DepositGuideVars {
|
|
const customerName = getCustomerNameFromUser()
|
|
try {
|
|
const store = useSettingsStore()
|
|
return {
|
|
accountHolder: store.getSettingByKey('deposit_account_holder') as string | null,
|
|
cardNumber: store.getSettingByKey('deposit_card_number') as string | null,
|
|
sheba: store.getSettingByKey('deposit_sheba') as string | null,
|
|
customerName,
|
|
}
|
|
} catch {
|
|
return { customerName }
|
|
}
|
|
}
|
|
|
|
export function getDepositGuideTemplate(): string {
|
|
try {
|
|
const fromSettings = String(useSettingsStore().getSettingByKey('support_messenger_text') ?? '').trim()
|
|
if (fromSettings) return fromSettings
|
|
} catch {
|
|
// settings store may not be ready yet
|
|
}
|
|
return DEFAULT_DEPOSIT_GUIDE_TEXT
|
|
}
|
|
|
|
export function getDepositSupportMessageTemplate(): string {
|
|
try {
|
|
const fromSettings = String(
|
|
useSettingsStore().getSettingByKey('deposit_support_message_text') ?? '',
|
|
).trim()
|
|
if (fromSettings) return fromSettings
|
|
} catch {
|
|
// settings store may not be ready yet
|
|
}
|
|
return DEFAULT_DEPOSIT_SUPPORT_MESSAGE_TEXT
|
|
}
|
|
|
|
export function getRenderedDepositGuideText(vars?: DepositGuideVars): string {
|
|
return renderDepositGuideText(getDepositGuideTemplate(), vars ?? getDepositGuideVarsFromSettings())
|
|
}
|
|
|
|
export function getRenderedDepositSupportMessage(vars?: DepositGuideVars): string {
|
|
return renderDepositSupportMessageText(
|
|
getDepositSupportMessageTemplate(),
|
|
vars ?? getDepositGuideVarsFromSettings(),
|
|
)
|
|
}
|
|
|
|
export function formatDepositCardNumber(value?: string | null) {
|
|
const digits = String(value ?? '').replace(/\D/g, '')
|
|
if (!digits) return ''
|
|
|
|
return digits.replace(/(\d{4})(?=\d)/g, '$1 ').trim()
|
|
}
|
|
|
|
export function formatDepositSheba(value?: string | null) {
|
|
const normalized = String(value ?? '').replace(/\s/g, '').toUpperCase()
|
|
if (!normalized) return ''
|
|
|
|
const digits = normalized.replace(/^IR/i, '').replace(/\D/g, '')
|
|
if (!digits) return normalized
|
|
|
|
const full = `IR${digits}`
|
|
return full.replace(/(.{4})/g, '$1 ').trim()
|
|
}
|
|
|
|
export function hasDepositFieldValue(value?: string | null) {
|
|
return Boolean(String(value ?? '').trim())
|
|
}
|
|
|
|
export function displayDepositCardNumber(value?: string | null) {
|
|
return formatDepositCardNumber(value) || '**** **** **** ****'
|
|
}
|
|
|
|
export function displayDepositSheba(value?: string | null) {
|
|
return formatDepositSheba(value) || 'IR** **** **** **** **** **** **'
|
|
}
|
|
|
|
export function getDepositSettingLabel(key: string, fallback: string) {
|
|
try {
|
|
const item = useSettingsStore().settings.find(
|
|
(setting) => String(setting?.key ?? '') === key,
|
|
)
|
|
return item?.title_fa?.trim() || fallback
|
|
} catch {
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
function normalizeDepositCopyValue(value: string, kind: 'card' | 'sheba') {
|
|
const normalized = value.replace(/\s/g, '')
|
|
if (!normalized) return ''
|
|
|
|
if (kind === 'sheba') {
|
|
const upper = normalized.toUpperCase()
|
|
if (upper.startsWith('IR')) return upper
|
|
const digits = upper.replace(/\D/g, '')
|
|
return digits ? `IR${digits}` : upper
|
|
}
|
|
|
|
return normalized.replace(/\D/g, '')
|
|
}
|
|
|
|
export async function copyDepositFieldValue(
|
|
value: string | null | undefined,
|
|
kind: 'card' | 'sheba',
|
|
emptyMessage: string,
|
|
) {
|
|
const raw = String(value ?? '').trim()
|
|
const normalized = normalizeDepositCopyValue(raw, kind)
|
|
|
|
if (!normalized) {
|
|
message.warning(emptyMessage)
|
|
return
|
|
}
|
|
|
|
try {
|
|
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
|
await navigator.clipboard.writeText(normalized)
|
|
} else if (typeof document !== 'undefined') {
|
|
const textarea = document.createElement('textarea')
|
|
textarea.value = normalized
|
|
textarea.setAttribute('readonly', 'true')
|
|
textarea.style.position = 'fixed'
|
|
textarea.style.left = '-9999px'
|
|
document.body.appendChild(textarea)
|
|
textarea.select()
|
|
document.execCommand('copy')
|
|
document.body.removeChild(textarea)
|
|
} else {
|
|
throw new Error('clipboard unavailable')
|
|
}
|
|
|
|
message.info('کپی شد')
|
|
} catch {
|
|
message.error('کپی انجام نشد')
|
|
}
|
|
}
|