From 1d6f342ff1033e8b24128dc4820add787ec8b8c4 Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Wed, 7 Jan 2026 14:23:36 +0330 Subject: [PATCH] solve bug of numbers --- src/utils/inputRules.ts | 22 ++++++------ src/utils/useAxios.ts | 2 +- src/views/base/gold/index.vue | 57 +++++++++++++++++++++++++++++-- src/views/base/products/index.vue | 8 ++--- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/src/utils/inputRules.ts b/src/utils/inputRules.ts index e851e12..d03fbef 100644 --- a/src/utils/inputRules.ts +++ b/src/utils/inputRules.ts @@ -148,18 +148,18 @@ const checkNumber = async (_rule: Rule, value: number) => { if (isBuyP) { // Skip wallet balance check if gateway is enabled if (!gatewayP) { - if (!walletCash?.walletInfo?.cash && methodP == 'cash') { - return Promise.reject('موجودی کیف پول ناکافی است') - } - if (!walletCash?.walletInfo?.gold && methodP == 'gold') { - return Promise.reject('موجودی کیف طلا ناکافی است') - } - if (priceP * value > walletCash?.walletInfo?.cash && methodP == 'cash') - return Promise.reject('موجودی کیف پول ناکافی است') - if (amountP * value > walletCash?.walletInfo?.gold && methodP == 'gold') - return Promise.reject('موجودی کیف طلا ناکافی است') + if (!walletCash?.walletInfo?.cash && methodP == 'cash') { + return Promise.reject('موجودی کیف پول ناکافی است') } - return Promise.resolve() + if (!walletCash?.walletInfo?.gold && methodP == 'gold') { + return Promise.reject('موجودی کیف طلا ناکافی است') + } + if (priceP * value > walletCash?.walletInfo?.cash && methodP == 'cash') + return Promise.reject('موجودی کیف پول ناکافی است') + if (amountP * value > walletCash?.walletInfo?.gold && methodP == 'gold') + return Promise.reject('موجودی کیف طلا ناکافی است') + } + return Promise.resolve() } else { if (value > NumP) return Promise.reject('موجودی محصول ناکافی است') diff --git a/src/utils/useAxios.ts b/src/utils/useAxios.ts index 9cc9cb5..88254ec 100644 --- a/src/utils/useAxios.ts +++ b/src/utils/useAxios.ts @@ -67,7 +67,7 @@ _axios.interceptors.response.use( removeToken() // Don't redirect to login if user is on buyProduct page if (router.currentRoute.value.name !== 'buyProduct') { - await router.push({name: 'login'}) + await router.push({name: 'login'}) } } else if (error.response && error.response.status == 403) { message.error('شما دسترسی لازم برای انجام این عملیات را ندارید') diff --git a/src/views/base/gold/index.vue b/src/views/base/gold/index.vue index 668cc7a..acf2b9a 100644 --- a/src/views/base/gold/index.vue +++ b/src/views/base/gold/index.vue @@ -397,16 +397,69 @@ const form = ref({ }) const allowedAsciiCodeFloat = [46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] const allowedAsciiCodeInt = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57] + +// Function to convert Persian/Farsi numbers to English +function convertPersianToEnglish(str: string): string { + const persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'] + const englishNumbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + const persianDecimal = '٫' + const englishDecimal = '.' + + let result = str + // Replace Persian numbers with English numbers + for (let i = 0; i < persianNumbers.length; i++) { + result = result.replace(new RegExp(persianNumbers[i], 'g'), englishNumbers[i]) + } + // Replace Persian decimal separator with English decimal separator + result = result.replace(new RegExp(persianDecimal, 'g'), englishDecimal) + + return result +} + +// Handler for Persian number input +function handlePersianNumberInput(event: Event) { + const target = event.target as HTMLInputElement + if (target && target.value) { + const convertedValue = convertPersianToEnglish(target.value) + if (convertedValue !== target.value) { + form.value.goldValue = convertedValue as any + } + } +} watch(form.value, () => { if (form.value.goldValue) { - const lastCharGoldStr: string = form.value.goldValue.toString() + const goldValueStr = form.value.goldValue.toString() + // Convert Persian numbers to English + const convertedValue = convertPersianToEnglish(goldValueStr) + if (convertedValue !== goldValueStr) { + const numericValue = parseFloat(convertedValue) + form.value.goldValue = isNaN(numericValue) ? null : numericValue + if (!isNaN(numericValue)) { + calculateCash(numericValue) + } + return + } + + const lastCharGoldStr: string = goldValueStr if (!allowedAsciiCodeFloat.includes(lastCharGoldStr.slice(-1).charCodeAt(0))) { form.value.goldValue = isNaN(parseFloat(lastCharGoldStr.slice(0, -1))) ? null : parseFloat(lastCharGoldStr.slice(0, -1)) calculateCash(form.value.goldValue) } } if (form.value.goldCash) { - const lastCharCashStr: string = form.value.goldCash.toString() + const goldCashStr = form.value.goldCash.toString() + // Convert Persian numbers to English + const convertedValue = convertPersianToEnglish(goldCashStr) + if (convertedValue !== goldCashStr) { + const numericValue = parseFloat(convertedValue.replaceAll(',', '')) + form.value.goldCash = isNaN(numericValue) ? null : numericValue + if (!isNaN(numericValue)) { + calculateGold(numericValue) + } + return + } + + const lastCharCashStr: string = goldCashStr if (!allowedAsciiCodeInt.includes(lastCharCashStr.slice(-1).charCodeAt(0))) { form.value.goldCash = isNaN(parseFloat(lastCharCashStr.slice(0, -1))) ? null : parseFloat(lastCharCashStr.slice(0, -1)) calculateGold(form.value.goldCash) diff --git a/src/views/base/products/index.vue b/src/views/base/products/index.vue index e834577..4211e15 100644 --- a/src/views/base/products/index.vue +++ b/src/views/base/products/index.vue @@ -59,16 +59,16 @@ const showFilter = ref() const selectedProduct = ref() function clickProduct(item: Product) { - if(item?.show){ + if(item?.show){ // Check if user is logged in if (!getToken()) { message.warning('برای خرید یا فروش محصولات، لطفا ابتدا وارد حساب کاربری خود شوید') router.push({ name: 'login' }) return } - showFilter.value = true - selectedProduct.value = cloneDeep(item) - } + showFilter.value = true + selectedProduct.value = cloneDeep(item) + } } function changeTab() {