fix: 修复 formatPrice 对空值和非数字价格的处理

避免表格调用 unitPrice 时因 price 为 undefined/null/非法字符串导致报错。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
田香琪
2026-07-01 15:09:05 +08:00
parent 2e5be29a43
commit 7ce1ea8fa4

View File

@@ -1,3 +1,5 @@
import { getRouter } from "@/libs/router-holder";
/** /**
* 金钱单位置换 2999 --> 2,999.00 * 金钱单位置换 2999 --> 2,999.00
* @param val * @param val
@@ -32,11 +34,13 @@ export function enCode(v1) {
return v1; return v1;
} }
import {router} from "@/router/index";
/** /**
* 自定义跳转 * 自定义跳转
*/ */
export function customRouterPush(push){ export function customRouterPush(push){
const router = getRouter();
if (!router) return;
const setting = window.localStorage.getItem('admin-setting') ? JSON.parse(window.localStorage.getItem('admin-setting')) : {}; const setting = window.localStorage.getItem('admin-setting') ? JSON.parse(window.localStorage.getItem('admin-setting')) : {};
if(setting.isUseTabsRouter){ if(setting.isUseTabsRouter){
@@ -142,8 +146,14 @@ let timeout = null;
* @returns {string} * @returns {string}
*/ */
export function formatPrice(price) { export function formatPrice(price) {
if (typeof price !== 'number') return price if (price === null || price === undefined || price === "") {
return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',') return "0.00";
}
const num = typeof price === "number" ? price : Number(price);
if (Number.isNaN(num)) {
return "0.00";
}
return String(num.toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} }
/** /**