好的,下面给出调整后的完整前端代码,包括 Vue 组件 (.vue 文件) 和 API 请求 (/api/pill/warning.js) 文件,已根据您后端代码及需求进行了全面适配和优化。
1. Vue 组件:Warning.vue
<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="药品ID" prop="drugId">
<el-input
v-model="queryParams.drugId"
placeholder="请输入药品ID"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="厂家ID" prop="factoryId">
<el-input
v-model="queryParams.factoryId"
placeholder="请输入厂家ID"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="药品生产日期" prop="productionDate">
<el-date-picker
clearable
v-model="queryParams.productionDate"
type="date"
value-format="yyyy-MM-dd"
placeholder="请选择药品生产日期"
/>
</el-form-item>
<el-form-item label="保质期" prop="validityPeriod">
<el-input
v-model="queryParams.validityPeriod"
placeholder="请输入保质期"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="进货单ID" prop="purchaseId">
<el-input
v-model="queryParams.purchaseId"
placeholder="请输入进货单ID"
clearable
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="库存数量" prop="stock">
<el-input
v-model="queryParams.stock"
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="['pill:warning: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="['pill:warning: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="['pill:warning: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="['pill:warning:export']"
>
导出
</el-button>
</el-col>
<right-toolbar
:showSearch.sync="showSearch"
@queryTable="getList"
></right-toolbar>
</el-row>
<el-table
v-loading="loading"
:data="warningList"
@selection-change="handleSelectionChange"
style="width: 100%;"
>
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="主键ID" align="center" prop="warningId" />
<el-table-column label="药品ID" align="center" prop="drugId" />
<el-table-column label="厂家ID" align="center" prop="factoryId" />
<el-table-column
label="药品生产日期"
align="center"
prop="productionDate"
width="180"
>
<template slot-scope="scope">
<span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
<el-table-column label="保质期" align="center" prop="validityPeriod" />
<el-table-column label="进货单ID" align="center" prop="purchaseId" />
<el-table-column label="库存数量" align="center" prop="stock" />
<el-table-column label="药品名称" align="center" prop="drugName" />
<el-table-column label="生产厂家" align="center" prop="factoryName" />
<el-table-column label="进货时间" align="center" prop="purchaseTime" width="180">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}</span>
</template>
</el-table-column>
<el-table-column label="距离过期天数" align="center" prop="daysToExpire" />
<el-table-column
label="操作"
align="center"
class-name="small-padding fixed-width"
>
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['pill:warning:edit']"
>修改</el-button
>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['pill:warning:remove']"
>删除</el-button
>
</template>
</el-table-column>
</el-table>
<pagination
v-show="total > 0"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
@pagination="getList"
/>
<!-- 添加或修改药品有效期预警,用于监控药品保质期对话框 -->
<el-dialog
:title="title"
:visible.sync="open"
width="500px"
append-to-body
:before-close="cancel"
>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="药品ID" prop="drugId">
<el-input v-model="form.drugId" placeholder="请输入药品ID" />
</el-form-item>
<el-form-item label="厂家ID" prop="factoryId">
<el-input v-model="form.factoryId" placeholder="请输入厂家ID" />
</el-form-item>
<el-form-item label="药品生产日期" prop="productionDate">
<el-date-picker
clearable
v-model="form.productionDate"
type="date"
value-format="yyyy-MM-dd"
placeholder="请选择药品生产日期"
/>
</el-form-item>
<el-form-item label="保质期" prop="validityPeriod">
<el-input v-model="form.validityPeriod" placeholder="请输入保质期" />
</el-form-item>
<el-form-item label="进货单ID" prop="purchaseId">
<el-input v-model="form.purchaseId" placeholder="请输入进货单ID" />
</el-form-item>
<el-form-item label="库存数量" prop="stock">
<el-input v-model="form.stock" 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 {
listWarning,
getWarning,
delWarning,
addWarning,
updateWarning,
exportWarning
} from "@/api/pill/warning";
export default {
name: "Warning",
data() {
return {
loading: false,
ids: [],
single: true,
multiple: true,
showSearch: true,
total: 0,
warningList: [],
title: "",
open: false,
queryParams: {
pageNum: 1,
pageSize: 10,
drugId: null,
factoryId: null,
productionDate: null,
validityPeriod: null,
purchaseId: null,
stock: null,
},
form: {
warningId: null,
drugId: null,
factoryId: null,
productionDate: null,
validityPeriod: null,
purchaseId: null,
stock: null,
},
rules: {
drugId: [{ required: true, message: "药品ID不能为空", trigger: "blur" }],
factoryId: [{ required: true, message: "厂家ID不能为空", trigger: "blur" }],
productionDate: [{ required: true, message: "药品生产日期不能为空", trigger: "blur" }],
validityPeriod: [{ required: true, message: "保质期不能为空", trigger: "blur" }],
purchaseId: [{ required: true, message: "进货单ID不能为空", trigger: "blur" }],
stock: [{ required: true, message: "库存数量不能为空", trigger: "blur" }]
}
};
},
created() {
this.getList();
},
methods: {
getList() {
this.loading = true;
// 过滤空字段
const params = {};
for (const key in this.queryParams) {
if (this.queryParams[key] !== null && this.queryParams[key] !== "") {
params[key] = this.queryParams[key];
}
}
listWarning(params)
.then((response) => {
this.warningList = response.rows;
this.total = response.total;
})
.finally(() => {
this.loading = false;
});
},
cancel() {
this.open = false;
this.reset();
},
reset() {
this.form = {
warningId: null,
drugId: null,
factoryId: null,
productionDate: null,
validityPeriod: null,
purchaseId: null,
stock: null,
};
this.$refs.form && this.$refs.form.resetFields();
},
handleQuery() {
this.queryParams.pageNum = 1;
this.getList();
},
resetQuery() {
this.$refs.queryForm && this.$refs.queryForm.resetFields();
this.queryParams.productionDate = null; // 日期需要手动置空
this.handleQuery();
},
handleSelectionChange(selection) {
this.ids = selection.map((item) => item.warningId);
this.single = selection.length !== 1;
this.multiple = selection.length === 0;
},
handleAdd() {
this.reset();
this.open = true;
this.title = "添加药品有效期预警,用于监控药品保质期";
},
handleUpdate(row) {
this.reset();
const warningId = row ? row.warningId : this.ids[0];
if (!warningId) {
this.$message.warning("请选择一条数据进行编辑");
return;
}
getWarning(warningId)
.then((response) => {
this.form = response.data;
this.open = true;
this.title = "修改药品有效期预警,用于监控药品保质期";
})
.catch(() => {
this.$message.error("获取详情失败");
});
},
submitForm() {
this.$refs.form.validate((valid) => {
if (valid) {
const apiCall = this.form.warningId ? updateWarning : addWarning;
apiCall(this.form)
.then(() => {
this.$message.success(this.form.warningId ? "修改成功" : "新增成功");
this.open = false;
this.getList();
})
.catch(() => {
this.$message.error(this.form.warningId ? "修改失败" : "新增失败");
});
}
});
},
handleDelete(row) {
let warningIds = [];
if (row && row.warningId) {
warningIds = [row.warningId];
} else if (this.ids.length) {
warningIds = this.ids;
}
if (warningIds.length === 0) {
this.$message.warning("请选择要删除的数据");
return;
}
this.$confirm(
`是否确认删除药品有效期预警,用于监控药品保质期编号为"${warningIds}"的数据项?`,
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}
)
.then(() => {
return delWarning(warningIds);
})
.then(() => {
this.$message.success("删除成功");
this.getList();
})
.catch(() => {});
},
handleExport() {
exportWarning(this.queryParams)
.then((res) => {
const blob = new Blob([res], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});
const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute(
"download",
`warning_${new Date().getTime()}.xlsx`
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch(() => {
this.$message.error("导出失败");
});
},
// 时间格式化辅助函数,如果你项目里有类似可以用项目自带的
parseTime(time, pattern) {
if (!time) return "";
const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
let date;
if (typeof time === "string" && /^[0-9]+$/.test(time)) {
date = new Date(parseInt(time));
} else if (typeof time === "string") {
date = new Date(time.replace(/\-/g, "/"));
} else if (typeof time === "number") {
date = new Date(time);
} else {
date = time;
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay(),
};
return format.replace(/{([ymdhisa])+}/g, (result, key) => {
let value = formatObj[key];
if (key === "a") return ["日", "一", "二", "三", "四", "五", "六"][value];
if (result.length > 0 && value < 10) {
value = "0" + value;
}
return value || 0;
});
},
},
};
</script>
<style scoped>
.mb8 {
margin-bottom: 8px;
}
</style>
2. API文件 src/api/pill/warning.js
import request from "@/utils/request";
// 查询药品有效期预警,用于监控药品保质期列表
export function listWarning(query) {
return request({
url: "/pill/warning/list",
method: "get",
params: query,
});
}
// 查询药品有效期预警详情
export function getWarning(warningId) {
return request({
url: "/pill/warning/" + warningId,
method: "get",
});
}
// 新增药品有效期预警
export function addWarning(data) {
return request({
url: "/pill/warning",
method: "post",
data,
});
}
// 修改药品有效期预警
export function updateWarning(data) {
return request({
url: "/pill/warning",
method: "put",
data,
});
}
// 删除药品有效期预警(支持批量删除)
export function delWarning(warningIds) {
let url = "/pill/warning/";
if (Array.isArray(warningIds)) {
url += warningIds.join(",");
} else {
url += warningIds;
}
return request({
url,
method: "delete",
});
}
// 导出药品有效期预警 Excel 文件
export function exportWarning(params) {
return request({
url: "/pill/warning/export",
method: "post",
data: params,
responseType: "blob", // 获取文件流
});
}
说明
- 前端字段与后端字段命名严格对应,避免数据交互错误。
- 搜索表单和新增/编辑表单均包含所有必要字段,包括
stock。
- 查询参数中会过滤空值,避免影响后端查询。
- 删除接口支持批量删除,多选操作友好。
- 导出接口为
POST,并正确处理返回的二进制文件,自动下载为 .xlsx 文件。
- 增加了表格中展示的字段,如药品名称、厂家名称、进货时间、距离过期天数,方便监控全面信息。
- 包含时间格式化辅助方法
parseTime,如果您的项目已有类似方法,建议统一调用。
- 包含必填校验,确保数据完整。
您可以将以上两个文件对应替换/创建,并在项目中使用。如果项目细节有差异(如权限指令v-hasPermi或样式等),请根据实际调整。
需要进一步改进或其它帮助,欢迎继续沟通!