fix some bugs
This commit is contained in:
@@ -603,14 +603,54 @@ export function getTradePriceUnitLabel() {
|
||||
return useSettingsStore().metalTypeLabel
|
||||
}
|
||||
|
||||
/** برچسب قیمت حین معامله — معامله مقداری: نام و واحد همان محصول */
|
||||
export type TransactionTradeProductSource = {
|
||||
product?: (ProductUnitSource & { title?: string | null }) | null
|
||||
wallet?: { product?: (ProductUnitSource & { title?: string | null }) | null } | null
|
||||
title?: string | null
|
||||
}
|
||||
|
||||
/** محصول معاملهشده — نه لزوماً همان کیف پول (مثلاً خرید مفتول با کیف کاتد) */
|
||||
export function resolveTransactionTradeProduct(
|
||||
item?: TransactionTradeProductSource | null,
|
||||
): (ProductUnitSource & { title?: string | null }) | null {
|
||||
if (!item) {
|
||||
return null
|
||||
}
|
||||
|
||||
const fromTransaction = item.product
|
||||
if ((fromTransaction?.title ?? '').trim()) {
|
||||
return fromTransaction ?? null
|
||||
}
|
||||
|
||||
const title = (item.title ?? '').trim()
|
||||
const match = title.match(/^(?:خرید|فروش)\s+(.+)$/u)
|
||||
if (match?.[1]?.trim()) {
|
||||
return {
|
||||
...(item.wallet?.product ?? {}),
|
||||
title: match[1].trim(),
|
||||
}
|
||||
}
|
||||
|
||||
return item.wallet?.product ?? null
|
||||
}
|
||||
|
||||
/** برچسب قیمت حین معامله — نام و واحد همان محصول معاملهشده */
|
||||
export function getTransactionTradePriceLabel(product?: (ProductUnitSource & { title?: string | null }) | null) {
|
||||
if (product && usesQuantityTrade(product)) {
|
||||
if (!product) {
|
||||
return `قیمت هر ${getWeightUnit()} ${getTradePriceUnitLabel()}`
|
||||
}
|
||||
|
||||
const title = (product.title ?? '').trim()
|
||||
|
||||
if (usesQuantityTrade(product)) {
|
||||
const unit = getProductQuantityUnit(product)
|
||||
const title = (product.title ?? '').trim()
|
||||
return title ? `قیمت هر ${unit} ${title}` : `قیمت هر ${unit}`
|
||||
}
|
||||
|
||||
if (title) {
|
||||
return `قیمت هر ${getWeightUnit()} ${title}`
|
||||
}
|
||||
|
||||
return `قیمت هر ${getWeightUnit()} ${getTradePriceUnitLabel()}`
|
||||
}
|
||||
|
||||
|
||||
+72
-33
@@ -108,11 +108,7 @@ _axios.interceptors.response.use(
|
||||
},
|
||||
async function (error) {
|
||||
const response = error.response
|
||||
const config = error.config
|
||||
|
||||
if (response?.data?.message && response.status !== 401) {
|
||||
checkStatus(response.data.message, response.status, config)
|
||||
}
|
||||
const config = error.config as MyAxiosRequestConfig | undefined
|
||||
|
||||
if (response?.status === 401) {
|
||||
const hadAuth = Boolean(config?.headers?.Authorization)
|
||||
@@ -124,42 +120,85 @@ _axios.interceptors.response.use(
|
||||
scheduleSessionVerify()
|
||||
}
|
||||
}
|
||||
} else if (response?.status === 403) {
|
||||
message.error('شما دسترسی لازم برای انجام این عملیات را ندارید')
|
||||
} else if (error.response && error.response.status == 422) {
|
||||
// message.error(error.response.data?.message)
|
||||
} else if (error.response) {
|
||||
message.error('خطا در بارگذاری اطلاعات')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
// Any status codes that falls outside the range of 2xx cause this function to trigger
|
||||
// Do something with response error
|
||||
|
||||
//const { response, code, message, config } = error || {}
|
||||
//const errorMessageMode = config?.requestOptions?.errorMessageMode || 'none'
|
||||
//const msg: string = response?.data?.error?.message ?? ''
|
||||
//checkStatus(error?.response?.status, msg)
|
||||
if (!shouldShowApiError(config)) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
const apiMessage = extractApiErrorMessage(response?.data)
|
||||
|
||||
if (response?.status === 403) {
|
||||
showApiError(apiMessage || 'شما دسترسی لازم برای انجام این عملیات را ندارید', config)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
if (response?.status === 422) {
|
||||
if (apiMessage) {
|
||||
showApiError(apiMessage, config)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
if (response) {
|
||||
showApiError(apiMessage || DEFAULT_LOAD_ERROR, config)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
showApiError(DEFAULT_LOAD_ERROR, config)
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
function checkStatus(msg: string, code: number, config: any) {
|
||||
//const { code, data, message } = data
|
||||
const errorMessage = get(config, 'meta.errorMessage', 'message')
|
||||
/*if (config.meta) {
|
||||
if (config.meta.message) {
|
||||
errorMessage = config.meta.message
|
||||
}
|
||||
}*/
|
||||
console.log('this is config', config)
|
||||
if (errorMessage == 'none') return
|
||||
switch (errorMessage) {
|
||||
case 'message':
|
||||
message.error(msg)
|
||||
break
|
||||
default:
|
||||
message.error(msg)
|
||||
const DEFAULT_LOAD_ERROR = 'خطا در بارگذاری اطلاعات'
|
||||
|
||||
function extractApiErrorMessage(data: unknown): string {
|
||||
if (!data || typeof data !== 'object') {
|
||||
return ''
|
||||
}
|
||||
|
||||
const record = data as Record<string, unknown>
|
||||
const rawMessage = record.message
|
||||
|
||||
if (typeof rawMessage === 'string' && rawMessage.trim()) {
|
||||
return rawMessage.trim()
|
||||
}
|
||||
|
||||
if (typeof rawMessage === 'number') {
|
||||
return String(rawMessage)
|
||||
}
|
||||
|
||||
const rawError = record.error
|
||||
if (typeof rawError === 'string' && rawError.trim()) {
|
||||
return rawError.trim()
|
||||
}
|
||||
|
||||
const errors = record.errors
|
||||
if (errors && typeof errors === 'object') {
|
||||
for (const value of Object.values(errors as Record<string, unknown>)) {
|
||||
if (Array.isArray(value) && typeof value[0] === 'string' && value[0].trim()) {
|
||||
return value[0].trim()
|
||||
}
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
return value.trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
function shouldShowApiError(config: MyAxiosRequestConfig | undefined): boolean {
|
||||
return get(config, 'meta.errorMessage', 'message') !== 'none'
|
||||
}
|
||||
|
||||
function showApiError(msg: string, config: MyAxiosRequestConfig | undefined) {
|
||||
if (!shouldShowApiError(config)) {
|
||||
return
|
||||
}
|
||||
|
||||
message.error(msg)
|
||||
}
|
||||
|
||||
const myAxios = {
|
||||
//AxiosResponse<T>
|
||||
|
||||
Reference in New Issue
Block a user