gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
<template> <div class="app-container"> <el-form :model="queryParams" ref="queryForm"…
- Usercode
- Assistant
- User
不要省略,给我完整代码
- Assistant
不要省略,给我完整代码
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="primary"
plain
icon="el-icon-plus"
size="mini"
@click="handleAdd"
v-hasPermi="['commSys:comm:add']"
>新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="success"
plain
icon="el-icon-edit"
size="mini"
:disabled="single"
@click="handleUpdate"
v-hasPermi="['commSys:comm:edit']"
>修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="danger"
plain
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['commSys:comm:remove']"
>删除</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="warning"
plain
icon="el-icon-download"
size="mini"
@click="handleExport"
v-hasPermi="['commSys:comm:export']"
>导出</el-button>
</el-col>
<el-col :span="1.5">
<el-button
:type="filterExpired ? 'danger' : 'danger'"
:plain="!filterExpired"
icon="el-icon-warning"
size="mini"
@click="toggleExpiredFilter"
v-hasPermi="['commSys:comm:filter']"
>{{ filterExpired ? '取消筛选' : '筛选过期' }}</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<div v-if="filterExpired" class="filter-notice">
当前显示过期商品共 {{ displayedCommList.length }} 条
</div>
<el-table v-loading="loading" :data="displayedCommList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="主键" align="center" prop="commId" />
<el-table-column label="商品名称" align="center" prop="commName" />
<el-table-column label="商品图片" align="center" width="120">
<template slot-scope="scope">
<img
:src="getImageUrl(scope.row.commImage)"
alt="商品图片"
class="comm-image"
@error="handleImageError(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column label="规格(净含量)" align="center" prop="unit" />
<el-table-column label="商品价格" align="center" width="120">
<template slot-scope="scope">
<span class="price-display">¥{{ parseFloat(scope.row.price).toFixed(2) }}</span>
</template>
</el-table-column>
<el-table-column label="保质期" align="center" width="110">
<template slot-scope="scope">
<div class="expiration-display">
<span class="days">{{ scope.row.exp }}</span>
<span class="unit">个月</span>
</div>
</template>
</el-table-column>
<el-table-column label="商品类型" align="center" width="150">
<template slot-scope="scope">
<el-tag
:type="getClassTagType(scope.row.classId)"
effect="plain"
class="type-tag"
>
{{ getClassName(scope.row.classId) }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="生产日期" align="center" prop="pd" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.pd, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<div class="operation-buttons">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['commSys:comm:edit']"
>修改</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['commSys:comm:remove']"
>删除</el-button>
</div>
</template>
</el-table-column>
</el-table>
<pagination
v-show="total>0 && !filterExpired"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
@pagination="getList"
/>
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
<el-form-item label="商品名称" prop="commName">
<el-input v-model="form.commName" placeholder="请输入商品名称" />
</el-form-item>
<el-form-item label="商品图片" prop="commImage">
<div v-if="form.commImage">
<img
:src="getImageUrl(form.commImage)"
alt="预览图"
class="preview-image"
@error="handleImageError(-1)"
/>
<el-button size="small" type="danger" @click="clearImage">移除图片</el-button>
</div>
<image-upload v-model="form.commImage" />
</el-form-item>
<el-form-item label="规格" prop="unit">
<el-input v-model="form.unit" placeholder="请输入规格" />
</el-form-item>
<el-form-item label="商品价格" prop="price">
<el-input
v-model.number="form.price"
placeholder="0.00"
style="width: 200px"
>
<template slot="prefix">¥</template>
</el-input>
</el-form-item>
<el-form-item label="保质期" prop="exp">
<el-input
v-model.number="form.exp"
placeholder="请输入月数"
style="width: 200px"
>
<template slot="append">个月</template>
</el-input>
</el-form-item>
<el-form-item label="商品类型" prop="classId">
<el-select
v-model="form.classId"
placeholder="请选择类型"
style="width: 100%"
>
<el-option
v-for="item in classList"
:key="item.classId"
:label="item.className"
:value="item.classId"
:style="{
color: getClassColor(item.classId),
fontWeight: 500
}"
/>
</el-select>
</el-form-item>
<el-form-item label="生产日期" prop="pd">
<el-date-picker
v-model="form.pd"
type="date"
value-format="yyyy-MM-dd"
placeholder="选择日期"
/>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
你的代码里价格输入用的是el-input,并且v-model.number绑定到了form.price,显示时用了parseFloat(...).toFixed(2)格式化为两位小数。如果你希望输入时能接受小数,显示时也能显示整数或者小数(小数部分有几位就显示几位,没有小数点则显示整数),且做最小幅度修改,建议修改显示价格那部分的处理逻辑即可。
v-model.number保持不变,el-input已经支持小数输入。toFixed(2)会强制显示两位),改为根据实际数字格式化显示。定位到模板中显示价格的地方:
<el-table-column label="商品价格" align="center" width="120">
<template slot-scope="scope">
<span class="price-display">¥{{ parseFloat(scope.row.price).toFixed(2) }}</span>
</template>
</el-table-column>
改为:
<el-table-column label="商品价格" align="center" width="120">
<template slot-scope="scope">
<span class="price-display">¥{{ formatPrice(scope.row.price) }}</span>
</template>
</el-table-column>
formatPricemethods: {
// ...你其他的方法
formatPrice(value) {
if (value == null || value === '') return '';
const num = parseFloat(value);
if (Number.isInteger(num)) {
return num.toString();
} else {
// 保留最多两位小数,末尾0去掉
return num.toFixed(2).replace(/\.?0+$/, '');
}
},
// 其它已有方法...
}
这样显示既支持整数,也支持小数,且小数最多两位,末尾无意义0会被去掉。
el-input的v-model.number能允许输入小数:<el-form-item label="商品价格" prop="price">
<el-input
v-model.number="form.price"
placeholder="0.00"
style="width: 200px"
>
<template slot="prefix">¥</template>
</el-input>
</el-form-item>
<template>
<!-- ...省略其他代码 -->
<el-table-column label="商品价格" align="center" width="120">
<template slot-scope="scope">
<span class="price-display">¥{{ formatPrice(scope.row.price) }}</span>
</template>
</el-table-column>
<!-- ...省略其他代码 -->
<el-form-item label="商品价格" prop="price">
<el-input
v-model.number="form.price"
placeholder="0.00"
style="width: 200px"
>
<template slot="prefix">¥</template>
</el-input>
</el-form-item>
</template>
<script>
export default {
// ...省略其他代码
methods: {
// 价格格式化,显示整数或最多两位小数,不显示末尾无意义0
formatPrice(value) {
if (value == null || value === '') return '';
const num = parseFloat(value);
if (Number.isInteger(num)) {
return num.toString();
} else {
return num.toFixed(2).replace(/\.?0+$/, '');
}
},
// 其他已有的方法保持不变
}
}
</script>
rules里加正则或数值判断,但你需求中未提及额外校验。如果需要,我可以帮你生成修改后的完整文件代码,你告诉我即可。
明白了,下面给你完整代码,已完成以下改动:
formatPrice方法格式化,显示整数或最多两位小数且末尾去0。v-model.number接收小数。代码中只作此必要修改,其他保持你原来代码不变。
完整代码如下:
<template>
<div class="app-container">
<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="商品名称" prop="commName">
<el-input
v-model="queryParams.commName"
placeholder="请输入商品名称"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="primary"
plain
icon="el-icon-plus"
size="mini"
@click="handleAdd"
v-hasPermi="['commSys:comm:add']"
>新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="success"
plain
icon="el-icon-edit"
size="mini"
:disabled="single"
@click="handleUpdate"
v-hasPermi="['commSys:comm:edit']"
>修改</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="danger"
plain
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['commSys:comm:remove']"
>删除</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="warning"
plain
icon="el-icon-download"
size="mini"
@click="handleExport"
v-hasPermi="['commSys:comm:export']"
>导出</el-button>
</el-col>
<el-col :span="1.5">
<el-button
:type="filterExpired ? 'danger' : 'danger'"
:plain="!filterExpired"
icon="el-icon-warning"
size="mini"
@click="toggleExpiredFilter"
v-hasPermi="['commSys:comm:filter']"
>{{ filterExpired ? '取消筛选' : '筛选过期' }}</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<div v-if="filterExpired" class="filter-notice">
当前显示过期商品共 {{ displayedCommList.length }} 条
</div>
<el-table v-loading="loading" :data="displayedCommList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="主键" align="center" prop="commId" />
<el-table-column label="商品名称" align="center" prop="commName" />
<el-table-column label="商品图片" align="center" width="120">
<template slot-scope="scope">
<img
:src="getImageUrl(scope.row.commImage)"
alt="商品图片"
class="comm-image"
@error="handleImageError(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column label="规格(净含量)" align="center" prop="unit" />
<el-table-column label="商品价格" align="center" width="120">
<template slot-scope="scope">
<span class="price-display">¥{{ formatPrice(scope.row.price) }}</span>
</template>
</el-table-column>
<el-table-column label="保质期" align="center" width="110">
<template slot-scope="scope">
<div class="expiration-display">
<span class="days">{{ scope.row.exp }}</span>
<span class="unit">个月</span>
</div>
</template>
</el-table-column>
<el-table-column label="商品类型" align="center" width="150">
<template slot-scope="scope">
<el-tag
:type="getClassTagType(scope.row.classId)"
effect="plain"
class="type-tag"
>
{{ getClassName(scope.row.classId) }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="生产日期" align="center" prop="pd" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.pd, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<div class="operation-buttons">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['commSys:comm:edit']"
>修改</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['commSys:comm:remove']"
>删除</el-button>
</div>
</template>
</el-table-column>
</el-table>
<pagination
v-show="total>0 && !filterExpired"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
@pagination="getList"
/>
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="100px">
<el-form-item label="商品名称" prop="commName">
<el-input v-model="form.commName" placeholder="请输入商品名称" />
</el-form-item>
<el-form-item label="商品图片" prop="commImage">
<div v-if="form.commImage">
<img
:src="getImageUrl(form.commImage)"
alt="预览图"
class="preview-image"
@error="handleImageError(-1)"
/>
<el-button size="small" type="danger" @click="clearImage">移除图片</el-button>
</div>
<image-upload v-model="form.commImage" />
</el-form-item>
<el-form-item label="规格" prop="unit">
<el-input v-model="form.unit" placeholder="请输入规格" />
</el-form-item>
<el-form-item label="商品价格" prop="price">
<el-input
v-model.number="form.price"
placeholder="0.00"
style="width: 200px"
>
<template slot="prefix">¥</template>
</el-input>
</el-form-item>
<el-form-item label="保质期" prop="exp">
<el-input
v-model.number="form.exp"
placeholder="请输入月数"
style="width: 200px"
>
<template slot="append">个月</template>
</el-input>
</el-form-item>
<el-form-item label="商品类型" prop="classId">
<el-select
v-model="form.classId"
placeholder="请选择类型"
style="width: 100%"
>
<el-option
v-for="item in classList"
:key="item.classId"
:label="item.className"
:value="item.classId"
:style="{
color: getClassColor(item.classId),
fontWeight: 500
}"
/>
</el-select>
</el-form-item>
<el-form-item label="生产日期" prop="pd">
<el-date-picker
v-model="form.pd"
type="date"
value-format="yyyy-MM-dd"
placeholder="选择日期"
/>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { listComm, getComm, delComm, addComm, updateComm } from "@/api/commSys/comm";
import { listClass } from "@/api/commSys/class";
export default {
name: "Comm",
data() {
return {
queryParams: {
pageNum: 1,
pageSize: 10,
commName: null
},
commList: [],
total: 0,
loading: false,
showSearch: true,
open: false,
title: "",
form: {
commId: null,
commName: null,
commImage: null,
unit: null,
price: null,
pd: null,
exp: null,
classId: null,
createTime: null,
updateTime: null
},
rules: {
commName: [
{ required: true, message: "请输入商品名称", trigger: "blur" }
],
price: [
{ required: true, message: "请输入商品价格", trigger: "blur" },
{ type: "number", message: "价格必须为数字值" }
],
classId: [
{ required: true, message: "请选择商品类型", trigger: "change" }
]
},
ids: [],
single: true,
multiple: true,
classList: [],
classColorMap: {
1: "#FF4757",
2: "#2ED573",
3: "#1E90FF",
4: "#FFA502"
},
imageBaseUrl: process.env.NODE_ENV === "development"
? "http://localhost:8080"
: "https://your-production-domain.com",
filterExpired: false
};
},
computed: {
displayedCommList() {
if (this.filterExpired) {
return this.commList.filter(item => this.isExpired(item));
}
return this.commList;
}
},
created() {
this.getList();
this.getClassList();
},
methods: {
getList() {
this.loading = true;
listComm(this.queryParams).then(response => {
this.commList = response.rows;
this.total = response.total;
this.loading = false;
});
},
getClassList() {
listClass().then(response => {
this.classList = response.rows;
});
},
cancel() {
this.open = false;
this.reset();
},
reset() {
this.form = {
commId: null,
commName: null,
commImage: null,
unit: null,
price: null,
pd: null,
exp: null,
classId: null,
createTime: null,
updateTime: null
};
this.resetForm("form");
},
handleQuery() {
this.queryParams.pageNum = 1;
this.getList();
},
resetQuery() {
this.resetForm("queryForm");
this.handleQuery();
},
handleSelectionChange(selection) {
this.ids = selection.map(item => item.commId);
this.single = selection.length !== 1;
this.multiple = !selection.length;
},
handleAdd() {
this.reset();
this.open = true;
this.title = "添加商品";
},
handleUpdate(row) {
this.reset();
const commId = row.commId || this.ids;
getComm(commId).then(response => {
this.form = response.data;
this.open = true;
this.title = "修改商品";
});
},
submitForm() {
this.$refs["form"].validate(valid => {
if (valid) {
if (this.form.commId != null) {
updateComm(this.form).then(() => {
this.$modal.msgSuccess("修改成功");
this.open = false;
this.getList();
});
} else {
addComm(this.form).then(() => {
this.$modal.msgSuccess("新增成功");
this.open = false;
this.getList();
});
}
}
});
},
handleDelete(row) {
const commIds = row.commId || this.ids;
this.$modal.confirm(`是否确认删除商品编号为"${commIds}"的数据项?`).then(() => {
return delComm(commIds);
}).then(() => {
this.getList();
this.$modal.msgSuccess("删除成功");
}).catch(() => {});
},
handleExport() {
this.download("commSys/comm/export", {
...this.queryParams
}, `comm_${new Date().getTime()}.xlsx`);
},
getClassName(classId) {
const item = this.classList.find(item => item.classId === classId);
return item ? item.className : "";
},
getImageUrl(path) {
if (!path) return "https://picsum.photos/120/120?random=1";
if (path.startsWith("http")) return path;
return `${this.imageBaseUrl}${path}`;
},
handleImageError(index) {
console.error(`图片加载失败,索引: ${index}`);
},
clearImage() {
this.form.commImage = null;
},
getClassTagType(classId) {
const types = ["", "danger", "success", "info", "warning"];
return types[classId % 4 + 1] || "primary";
},
getClassColor(classId) {
return this.classColorMap[classId % 4 + 1] || "#57606f";
},
toggleExpiredFilter() {
this.filterExpired = !this.filterExpired;
this.handleSelectionChange([]);
},
isExpired(item) {
if (!item.pd || !item.exp) return false;
try {
const productionDate = new Date(item.pd);
if (isNaN(productionDate)) return false;
const expiryDate = new Date(productionDate);
expiryDate.setMonth(expiryDate.getMonth() + parseInt(item.exp));
return expiryDate < new Date();
} catch (e) {
console.error('日期计算错误:', e);
return false;
}
},
formatPrice(value) {
if (value == null || value === '') return '';
const num = parseFloat(value);
if (Number.isInteger(num)) {
return num.toString();
} else {
return num.toFixed(2).replace(/\.?0+$/, '');
}
}
}
};
</script>
<style scoped>
.app-container {
padding: 20px;
}
.comm-image {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 4px;
border: 1px solid #ebeef5;
}
.preview-image {
max-width: 150px;
max-height: 150px;
margin-bottom: 10px;
display: block;
border-radius: 4px;
border: 1px solid #ebeef5;
}
.operation-buttons {
display: flex;
gap: 4px;
justify-content: center;
align-items: center;
white-space: nowrap;
}
.operation-buttons .el-button {
margin: 0;
padding: 0 4px;
}
.price-display {
color: #ff4500;
font-weight: 600;
font-size: 14px;
letter-spacing: 0.5px;
}
.expiration-display {
display: flex;
align-items: baseline;
justify-content: center;
}
.days {
font-size: 16px;
color: #2f3542;
margin-right: 3px;
}
.unit {
font-size: 12px;
color: #747d8c;
}
.type-tag {
min-width: 70px;
text-align: center;
letter-spacing: 0.5px;
border-radius: 12px;
transition: all 0.3s ease;
}
.type-tag:hover {
transform: scale(1.05);
}
.el-input__prefix {
display: flex;
align-items: center;
padding-left: 8px;
color: #606266;
}
.filter-notice {
margin: 10px 0;
padding: 10px;
background-color: #fffbe6;
border: 1px solid #ffe58f;
border-radius: 4px;
color: #faad14;
font-size: 14px;
}
</style>
formatPrice方法统一管控价格如何显示,兼容整数和小数(小数最多2位,末尾0去除)。v-model.number绑定,允许输入整数和小数。如果有其他小需求欢迎随时告诉我。
Not yet included in a published release.