1 changed files with 83 additions and 0 deletions
@ -0,0 +1,83 @@ |
|||
import { getConfigKey } from "@/api/system/config"; |
|||
|
|||
const QUERY_DAY_LIMIT_CONFIG_KEY = "b_side_query_day_limit_config"; |
|||
|
|||
let cachedLimitDays = null; |
|||
let loadingPromise = null; |
|||
|
|||
function hasConfigValue(value) { |
|||
if (value === undefined || value === null) { |
|||
return false; |
|||
} |
|||
if (typeof value === "string" && !value.trim()) { |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
function parseLimitDays(configValue) { |
|||
if (configValue === undefined || configValue === null || configValue === "") { |
|||
return null; |
|||
} |
|||
|
|||
let parsedValue = configValue; |
|||
// Handle nested JSON strings, e.g. "\"{\\\"limitDays\\\":5}\""
|
|||
for (let i = 0; i < 2; i += 1) { |
|||
if (typeof parsedValue !== "string") { |
|||
break; |
|||
} |
|||
const text = parsedValue.trim(); |
|||
if (!text) { |
|||
return null; |
|||
} |
|||
try { |
|||
parsedValue = JSON.parse(text); |
|||
} catch (error) { |
|||
// Plain numeric string is also supported.
|
|||
const numericDays = Number(text); |
|||
if (Number.isFinite(numericDays)) { |
|||
return numericDays > 0 ? Math.floor(numericDays) : null; |
|||
} |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
if (parsedValue && typeof parsedValue === "object" && "configValue" in parsedValue) { |
|||
return parseLimitDays(parsedValue.configValue); |
|||
} |
|||
|
|||
const days = Number(parsedValue && parsedValue.limitDays); |
|||
if (!Number.isFinite(days) || days <= 0) { |
|||
return null; |
|||
} |
|||
return Math.floor(days); |
|||
} |
|||
|
|||
export async function getQueryDayLimitDays() { |
|||
if (cachedLimitDays !== null) { |
|||
return cachedLimitDays; |
|||
} |
|||
if (loadingPromise) { |
|||
return loadingPromise; |
|||
} |
|||
|
|||
loadingPromise = getConfigKey(QUERY_DAY_LIMIT_CONFIG_KEY) |
|||
.then((response) => { |
|||
const configValue = hasConfigValue(response && response.data) |
|||
? response.data |
|||
: response && response.msg; |
|||
const limitDays = parseLimitDays(configValue); |
|||
if (Number.isFinite(limitDays) && limitDays > 0) { |
|||
cachedLimitDays = limitDays; |
|||
} |
|||
return limitDays; |
|||
}) |
|||
.catch(() => { |
|||
return null; |
|||
}) |
|||
.finally(() => { |
|||
loadingPromise = null; |
|||
}); |
|||
|
|||
return loadingPromise; |
|||
} |
|||
Loading…
Reference in new issue