自适应重量单位

This commit is contained in:
2025-11-27 17:52:19 +08:00
parent 9af7e0d005
commit ede3d6b330
3 changed files with 41 additions and 20 deletions

View File

@@ -40,9 +40,9 @@
</template> </template>
<script> <script>
import { ref, reactive, computed, watch } from 'vue'; import {ref, reactive, computed, watch} from 'vue';
import { ElMessage } from 'element-plus'; import {ElMessage} from 'element-plus';
import { InventoryApi } from '../../api/inventory'; import {InventoryApi} from '../../api/inventory';
export default { export default {
name: 'StockAdjustmentDialog', name: 'StockAdjustmentDialog',
@@ -61,7 +61,7 @@ export default {
}, },
}, },
emits: ['update:visible', 'success'], emits: ['update:visible', 'success'],
setup(props, { emit }) { setup(props, {emit}) {
const formRef = ref(null); const formRef = ref(null);
const dialogVisible = computed({ const dialogVisible = computed({
get: () => props.visible, get: () => props.visible,
@@ -94,9 +94,9 @@ export default {
const rules = { const rules = {
change_amount: [ change_amount: [
{ required: true, message: '请输入变动数量', trigger: 'blur' }, {required: true, message: '请输入变动数量', trigger: 'blur'},
{ type: 'number', message: '数量必须为数字', trigger: 'blur' }, {type: 'number', message: '数量必须为数字', trigger: 'blur'},
{ min: 1, message: '数量必须大于0', trigger: 'blur' }, {min: 1, message: '数量必须大于0', trigger: 'blur'},
], ],
}; };

View File

@@ -2,7 +2,11 @@
<div> <div>
<el-table :data="stockList" style="width: 100%" v-loading="loading"> <el-table :data="stockList" style="width: 100%" v-loading="loading">
<el-table-column prop="raw_material_name" label="原料名称"></el-table-column> <el-table-column prop="raw_material_name" label="原料名称"></el-table-column>
<el-table-column prop="stock" label="当前库存 (g)"></el-table-column> <el-table-column prop="stock" label="当前库存">
<template #default="scope">
{{ formatWeight(scope.row.stock) }}
</template>
</el-table-column>
<el-table-column prop="last_updated" label="最后更新时间"> <el-table-column prop="last_updated" label="最后更新时间">
<template #default="scope"> <template #default="scope">
{{ formatRFC3339(scope.row.last_updated) }} {{ formatRFC3339(scope.row.last_updated) }}
@@ -30,7 +34,7 @@
import { ref, onMounted, watch } from 'vue'; import { ref, onMounted, watch } from 'vue';
import { InventoryApi } from '../../api/inventory'; import { InventoryApi } from '../../api/inventory';
import { ElMessage } from 'element-plus'; import { ElMessage } from 'element-plus';
import { formatRFC3339 } from '../../utils/format'; import { formatRFC3339, formatWeight } from '../../utils/format'; // 导入 formatWeight
export default { export default {
name: 'StockListTable', name: 'StockListTable',
@@ -118,6 +122,7 @@ export default {
handleSizeChange, handleSizeChange,
handleCurrentChange, handleCurrentChange,
formatRFC3339, formatRFC3339,
formatWeight, // 暴露给模板
handleAdjust, // 暴露给模板 handleAdjust, // 暴露给模板
}; };
}, },

View File

@@ -18,5 +18,21 @@ export function formatRFC3339(rfc3339String) {
} }
} }
/**
* 将以克为单位的重量转换为更合适的单位(克或千克)并格式化
* @param {number} grams - 以克为单位的重量
* @returns {string} - 格式化后的重量字符串 (例如 "1.5 kg" 或 "500 g")
*/
export function formatWeight(grams) {
if (typeof grams !== 'number' || isNaN(grams)) {
return '--';
}
if (grams >= 1000) {
return (grams / 1000).toFixed(2) + ' kg';
} else {
return grams + ' g';
}
}
// 你未来还可以添加其他全局格式化函数 // 你未来还可以添加其他全局格式化函数
// export function formatCurrency(number) { ... } // export function formatCurrency(number) { ... }