handle huge things!

This commit is contained in:
2026-06-03 22:04:45 +03:30
parent 198f0a164b
commit f82a0c3873
29 changed files with 778 additions and 445 deletions
+3 -3
View File
@@ -31,7 +31,7 @@ export function inputLenRules(len: number, title?: string) {
import {computed, ref} from 'vue'
import type {Rule} from 'ant-design-vue/es/form'
import {useWalletStore} from "@/store/walletStore";
import { isCashLikeDealMethod, getInsufficientCashWalletMessage, getInsufficientGoldWalletMessage } from '@/utils/productDealMethod'
import { isCashLikeDealMethod, isMainMetalWalletDealMethod, getInsufficientCashWalletMessage, getInsufficientGoldWalletMessage } from '@/utils/productDealMethod'
export const mobileValidation = (title?: string) => {
return {len: 11, message: `شماره همراه باید 11 رقم باشد`}
@@ -150,12 +150,12 @@ if (isBuyP){
if (!walletCash?.walletInfo?.cash && isCashLikeDealMethod(methodP)){
return Promise.reject(getInsufficientCashWalletMessage(groupTypeP))
}
if (!walletCash?.walletInfo?.gold && methodP == 'gold'){
if (!walletCash?.walletInfo?.gold && isMainMetalWalletDealMethod(methodP)){
return Promise.reject(getInsufficientGoldWalletMessage(groupTypeP))
}
if (priceP * value > walletCash?.walletInfo?.cash && isCashLikeDealMethod(methodP))
return Promise.reject(getInsufficientCashWalletMessage(groupTypeP))
if (amountP * value > walletCash?.walletInfo?.gold && methodP == 'gold')
if (amountP * value > walletCash?.walletInfo?.gold && isMainMetalWalletDealMethod(methodP))
return Promise.reject(getInsufficientGoldWalletMessage(groupTypeP))
else
return Promise.resolve();
+9
View File
@@ -0,0 +1,9 @@
export function resolveMetalTypeLabel(settingValue?: string | number | null): string {
const value = settingValue?.toString().trim()
return value ? value : 'طلا'
}
export function resolveMeltedGoldLabel(settingValue?: string | number | null): string {
const value = settingValue?.toString().trim()
return value ? value : 'آبشده'
}
+80 -65
View File
@@ -1,87 +1,102 @@
import {loading} from '@/utils/index'
import {getLastPrice, getPrice} from '@/api/home'
import {ref} from 'vue'
import type {Price} from '@/models/home'
import { getLastPrice, getPrice } from '@/api/home'
import type { Price, PriceUp } from '@/models/home'
import { ref } from 'vue'
let interval: any
let interval: ReturnType<typeof setInterval> | undefined
/** @deprecated از lastPrice استفاده کنید — برای سازگاری با کد قدیمی sync می‌شود */
export const price = ref<Price>()
export const lastPrice = ref<Price>()
export const olderPrice = ref<number| undefined>(0)
export const olderPrice = ref<number | undefined>(0)
/** buy/sell معامله: اول up (مثل endpoint قدیمی /price/up) سپس fallback */
export function getTradeBuyPrice(p?: Price | null) {
return p?.up?.buy ?? p?.buy ?? 0
}
export function getTradeSellPrice(p?: Price | null) {
return p?.up?.sell ?? p?.sell ?? 0
}
/** یکسان‌سازی پاسخ last با رفتار قدیمی price/up */
export function normalizePriceResponse(data: Price): Price {
if (!data?.up) {
return data
}
return {
...data,
buy: data.up.buy,
sell: data.up.sell,
}
}
export async function setIntervalGetAllPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
try {
interval = setInterval(async () => {
await getLastPrices()
}, 10000)
} catch (e) {
console.log(e)
}
}
export async function setIntervalGetLastPrice() {
// console.log('salam')
try {
interval = setInterval(async () => {
await getLastPrices()
// await getUpPrice()
}, 10000)
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
return setIntervalGetAllPrice()
}
/** @deprecated فقط getLastPrices را صدا بزنید */
export async function getUpPrice() {
try {
// loading.value = true
const data = await getPrice()
// const dataLast = await getLastPrice()
// if (lastPrice.value?.price_last != dataLast.price_last) {
// olderPrice.value = lastPrice.value?.price_last
// }
price.value = data
// lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
await getLastPrices()
}
async function fetchUpPrices(): Promise<PriceUp | undefined> {
try {
const up = await getPrice()
if (up?.buy != null && up?.sell != null) {
return { buy: up.buy, sell: up.sell }
}
} catch (e) {
console.log(e)
}
}
export async function getLastPrices() {
try {
// loading.value = true
// const data = await getPrice()
const dataLast = await getLastPrice()
if (lastPrice.value?.price_last != dataLast.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
// price.value = data
lastPrice.value = dataLast
} catch (e) {
console.log(e)
} finally {
// loading.value = false
try {
let dataLast = await getLastPrice()
if (!dataLast?.up) {
const up = await fetchUpPrices()
if (up) {
dataLast = { ...dataLast, up }
}
}
const normalized = normalizePriceResponse(dataLast)
if (lastPrice.value?.price_last != normalized.price_last) {
olderPrice.value = lastPrice.value?.price_last
}
lastPrice.value = normalized
price.value = {
...normalized,
buy: getTradeBuyPrice(normalized),
sell: getTradeSellPrice(normalized),
}
} catch (e) {
console.log(e)
}
}
export function stopPriceInterval() {
// console.log('stop')
clearInterval(interval);
if (interval) {
clearInterval(interval)
interval = undefined
}
}
export function percentPriceChanged() {
// console.log(olderPrice.value)
// console.log(lastPrice.value?.price_last)
if (!olderPrice.value)
return 0
return ((lastPrice.value?.price_last-olderPrice.value) *100)/olderPrice.value
}
if (!olderPrice.value) {
return 0
}
return ((lastPrice.value?.price_last! - olderPrice.value) * 100) / olderPrice.value
}
+200 -36
View File
@@ -1,31 +1,13 @@
import type { SelectItem } from '@/models/SelectItem'
import { isBoursecuProject } from '@/utils/config'
import { useSettingsStore } from '@/store/settingsStore'
/** شناسه group_type محصول در دیتابیس — نه برچسب نمایشی */
export const BOURSECU_GROUP_MAFTOL = 'مفتول مس'
export const BOURSECU_GROUP_KATOD = 'کاتد مس'
export const BOURSECU_GROUP_ABSHODE = 'آبشده'
export const BOURSECU_ASSET_LABEL_KATOD = 'دارایی کاتد مس'
export const BOURSECU_ASSET_LABEL_MAFTOL = 'دارایی مفتول مس'
export const BOURSECU_ASSET_LABEL_GOLD = 'دارایی طلایی'
const DEFAULT_TYPE_DEAL: SelectItem[] = [
{ value: 'نقدی', key: 'cash' },
{ value: 'طلایی', key: 'gold' },
]
const MAFTOL_TYPE_DEAL: SelectItem[] = [
{ value: BOURSECU_GROUP_KATOD, key: 'katod' },
{ value: 'نقدی', key: 'cash' },
]
const CASH_ONLY_TYPE_DEAL: SelectItem[] = [
{ value: 'نقدی', key: 'cash' },
]
const GOLD_ONLY_TYPE_DEAL: SelectItem[] = [
{ value: 'طلایی', key: 'gold' },
]
export const BOURSECU_WALLET_LABEL_GENERIC = 'موجودی کیف'
export function normalizeProductGroupType(groupType?: string | null) {
return (groupType ?? '').trim()
@@ -54,24 +36,150 @@ export function isGoldOnlyDealGroup(groupType?: string | null) {
return normalizeProductGroupType(groupType) === BOURSECU_GROUP_ABSHODE
}
export interface ProductUnitSource {
unit?: string | null
direct_price?: number | null
group_type?: string | null
amount?: number | null
}
const COUNT_BASED_GROUP_TYPES = ['سکه', 'گوی', 'مکعب', 'شمش', 'سکه پارسیان']
/** مطابق ادمین: وزن «دارد» یعنی amount مقدار واقعی است؛ «ندارد» یعنی amount = 1 */
export function hasProductUnitWeight(product?: ProductUnitSource | null) {
const amount = Number(product?.amount ?? 0)
return amount > 0 && amount !== 1
}
export function getProductWeightUnit(product?: ProductUnitSource | null) {
if (isBoursecuProject()) {
const group = normalizeProductGroupType(product?.group_type)
if (group === BOURSECU_GROUP_MAFTOL || group === BOURSECU_GROUP_KATOD || group === BOURSECU_GROUP_ABSHODE) {
return getWeightUnit()
}
}
if (!isCountBasedProduct(product) || isGoldOnlyDealGroup(product?.group_type)) {
return getWeightUnit()
}
return 'گرم'
}
export function formatProductWeightAmount(amount?: number | null, product?: ProductUnitSource | null) {
const value = Number(amount ?? 0)
const weightUnit = getProductWeightUnit(product)
if (weightUnit === 'گرم' && value > 0 && value < 1) {
return `${value * 1000} سوت`
}
return `${value} ${weightUnit}`
}
export function getProductUnitWeightLabel(product?: ProductUnitSource | null) {
if (!hasProductUnitWeight(product)) {
return null
}
return formatProductWeightAmount(product?.amount, product)
}
export function getProductTotalWeightLabel(count?: number | null, product?: ProductUnitSource | null) {
if (!hasProductUnitWeight(product)) {
return null
}
const quantity = Number(count ?? 0)
return formatProductWeightAmount(quantity * Number(product?.amount ?? 0), product)
}
export function shouldShowProductTotalWeight(product?: ProductUnitSource | null) {
if (!product) {
return false
}
if (!isCountBasedProduct(product)) {
return true
}
return hasProductUnitWeight(product)
}
export function formatProductInventoryBalance(product?: (ProductUnitSource & { wallet?: { gold?: number | null } }) | null) {
const count = product?.wallet?.gold ?? 0
const quantityPart = `${count} ${getProductQuantityUnit(product)}`
const unitWeight = getProductUnitWeightLabel(product)
if (!unitWeight) {
return quantityPart
}
return `${quantityPart} · هر عدد ${unitWeight}`
}
/** محصول تعدادی: موجودی wallet.gold = تعداد قلم، نه وزن */
export function isCountBasedProduct(product?: ProductUnitSource | null) {
if (!product) {
return false
}
if (product.direct_price != null && product.direct_price !== 0) {
return true
}
const group = normalizeProductGroupType(product.group_type)
if (group === BOURSECU_GROUP_MAFTOL || group === BOURSECU_GROUP_KATOD) {
return true
}
return COUNT_BASED_GROUP_TYPES.includes(group)
}
export function getProductQuantityUnit(product?: ProductUnitSource | null) {
if (isCountBasedProduct(product)) {
return 'عدد'
}
const unit = product?.unit?.trim()
if (unit) {
return unit
}
return getWeightUnit()
}
function getDefaultTypeDealOptions(): SelectItem[] {
const settingsStore = useSettingsStore()
return [
{ value: 'نقدی', key: 'cash' },
{ value: settingsStore.metalTypeLabel, key: 'gold' },
]
}
export function getProductDealMethodOptions(groupType?: string | null): SelectItem[] {
const settingsStore = useSettingsStore()
if (!isBoursecuProject()) {
return DEFAULT_TYPE_DEAL
return getDefaultTypeDealOptions()
}
if (isMaftolMesGroup(groupType)) {
return MAFTOL_TYPE_DEAL
return [
{ value: settingsStore.metalTypeLabel, key: 'katod' },
{ value: 'نقدی', key: 'cash' },
]
}
if (isKatodMesGroup(groupType)) {
return CASH_ONLY_TYPE_DEAL
return [{ value: 'نقدی', key: 'cash' }]
}
if (isGoldOnlyDealGroup(groupType)) {
return GOLD_ONLY_TYPE_DEAL
return [{ value: settingsStore.meltedGoldLabel, key: 'gold' }]
}
return DEFAULT_TYPE_DEAL
return getDefaultTypeDealOptions()
}
export function getDefaultProductDealMethod(groupType?: string | null) {
@@ -79,30 +187,66 @@ export function getDefaultProductDealMethod(groupType?: string | null) {
}
export function isCashLikeDealMethod(method?: string | null) {
return method === 'cash' || method === 'katod'
return method === 'cash'
}
export const BOURSECU_WALLET_LABEL_GENERIC = 'موجودی کیف'
export function isMainMetalWalletDealMethod(method?: string | null) {
return method === 'gold' || method === 'katod'
}
/** برچسب کیف فلز اصلی (main wallet gold) — از setting metal_type */
export function getMainMetalWalletLabel() {
return `موجودی کیف ${useSettingsStore().metalTypeLabel}`
}
/** برچسب کیف آبشده — از setting metal_type */
export function getMeltedGoldWalletLabel() {
return `موجودی کیف ${useSettingsStore().meltedGoldLabel}`
}
export function getGoldWalletLabel(groupType?: string | null) {
if (!isBoursecuProject()) return 'موجودی کیف طلا'
const settingsStore = useSettingsStore()
if (!isBoursecuProject()) {
return getMainMetalWalletLabel()
}
const group = normalizeProductGroupType(groupType)
if (group === BOURSECU_GROUP_KATOD) return `موجودی کیف ${BOURSECU_GROUP_KATOD}`
if (group === BOURSECU_GROUP_MAFTOL) return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
if (group === BOURSECU_GROUP_ABSHODE) return 'موجودی کیف طلا'
if (group === BOURSECU_GROUP_MAFTOL) {
return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
}
if (group === BOURSECU_GROUP_KATOD || group === BOURSECU_GROUP_ABSHODE || !group) {
return group === BOURSECU_GROUP_ABSHODE
? getMeltedGoldWalletLabel()
: getMainMetalWalletLabel()
}
return BOURSECU_WALLET_LABEL_GENERIC
}
export function getCashWalletLabel(groupType?: string | null) {
if (!isBoursecuProject()) return 'موجودی کیف پول'
if (!isBoursecuProject()) {
return 'موجودی کیف پول'
}
const group = normalizeProductGroupType(groupType)
if (group === BOURSECU_GROUP_KATOD) return `موجودی کیف ${BOURSECU_GROUP_KATOD}`
if (group === BOURSECU_GROUP_MAFTOL) return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
if (group === BOURSECU_GROUP_MAFTOL) {
return `موجودی کیف ${BOURSECU_GROUP_MAFTOL}`
}
return BOURSECU_WALLET_LABEL_GENERIC
return 'موجودی کیف پول'
}
export function getMetalAssetLabel() {
return `دارایی ${useSettingsStore().metalTypeLabel}`
}
export function getMeltedGoldAssetLabel() {
return `دارایی ${useSettingsStore().meltedGoldLabel}`
}
export function getMaftolAssetLabel() {
return `دارایی ${BOURSECU_GROUP_MAFTOL}`
}
export function getWeightUnit() {
@@ -113,6 +257,10 @@ export function formatBoursecuAssetAmount(amount?: number | null) {
return `${amount ?? 0}${getWeightUnit()}`
}
export function formatMainMetalWalletBalanceText(gold: number) {
return `${getMainMetalWalletLabel()}: ${gold} ${getWeightUnit()}`
}
export function formatGoldWalletBalanceText(gold: number, groupType?: string | null) {
return `${getGoldWalletLabel(groupType)}: ${gold} ${getWeightUnit()}`
}
@@ -128,3 +276,19 @@ export function getInsufficientGoldWalletMessage(groupType?: string | null) {
export function getInsufficientCashWalletMessage(groupType?: string | null) {
return `${getCashWalletLabel(groupType)} ناکافی است`
}
export function getInsufficientMainMetalWalletMessage() {
return `${getMainMetalWalletLabel()} ناکافی است`
}
export function getMeltedGoldTradeLabel() {
return useSettingsStore().meltedGoldLabel
}
export function getMetalTypeTradeLabel() {
return useSettingsStore().metalTypeLabel
}
export function getTradePriceUnitLabel() {
return useSettingsStore().metalTypeLabel
}
+2 -2
View File
@@ -1,10 +1,10 @@
import type { Product } from '@/models/products'
import { lastPrice } from '@/utils/price'
import { getTradeBuyPrice, lastPrice } from '@/utils/price'
/** قیمت خرید نمایشی محصول (همان مقداری که در لیست و اکشن‌شیت نشان داده می‌شود) */
export function getProductBuyPrice(product: Product): number {
if (product.group_type === 'آبشده') {
return lastPrice.value?.buy ?? 0
return getTradeBuyPrice(lastPrice.value)
}
if (['مس', 'نقره'].includes(product.group_type)) {