增加搜索框

This commit is contained in:
2025-11-21 17:27:40 +08:00
parent 4f5135a619
commit 2d864f7f7c
3 changed files with 56 additions and 48 deletions

View File

@@ -1658,7 +1658,7 @@
"parameters": [
{
"type": "string",
"description": "按名称模糊查询",
"description": "按营养名称模糊查询",
"name": "name",
"in": "query"
},
@@ -1679,6 +1679,12 @@
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "按原料名称模糊查询",
"name": "raw_material_name",
"in": "query"
}
],
"responses": {
@@ -2660,10 +2666,16 @@
"parameters": [
{
"type": "string",
"description": "按名称模糊查询",
"description": "按原料名称模糊查询",
"name": "name",
"in": "query"
},
{
"type": "string",
"description": "按营养名称模糊查询",
"name": "nutrient_name",
"in": "query"
},
{
"type": "string",
"description": "排序字段,例如 \"id DESC\"",
@@ -3069,7 +3081,6 @@
},
{
"enum": [
7,
-1,
0,
1,
@@ -3079,12 +3090,12 @@
5,
-1,
5,
6
6,
7
],
"type": "integer",
"format": "int32",
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -3094,7 +3105,8 @@
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
"InvalidLevel",
"_numLevels"
],
"name": "level",
"in": "query"
@@ -9641,7 +9653,6 @@
"type": "integer",
"format": "int32",
"enum": [
7,
-1,
0,
1,
@@ -9651,10 +9662,10 @@
5,
-1,
5,
6
6,
7
],
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
@@ -9664,7 +9675,8 @@
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
"InvalidLevel",
"_numLevels"
]
}
},

View File

@@ -30,7 +30,8 @@ import {PaginationDTO, Response} from '../enums';
/**
* @typedef {object} NutrientsParams
* @property {string} [name] - 按名称模糊查询
* @property {string} [name] - 按营养名称模糊查询
* @property {string} [raw_material_name] - 按原料名称模糊查询
* @property {string} [order_by] - 排序字段,例如 "id DESC"
* @property {number} [page]
* @property {number} [page_size]
@@ -239,7 +240,8 @@ import {PaginationDTO, Response} from '../enums';
/**
* @typedef {object} RawMaterialsParams
* @property {string} [name] - 按名称模糊查询
* @property {string} [name] - 按原料名称模糊查询
* @property {string} [nutrient_name] - 按营养名称模糊查询
* @property {string} [order_by] - 排序字段,例如 "id DESC"
* @property {number} [page]
* @property {number} [page_size]

View File

@@ -1,5 +1,21 @@
<template>
<div>
<!-- 搜索区域 -->
<div style="margin-bottom: 20px; display: flex; align-items: center; gap: 10px;">
<el-select v-model="searchType" placeholder="请选择搜索类型" style="width: 150px;">
<el-option label="按原料名称" value="name"></el-option>
<el-option label="按营养名称" value="nutrient_name"></el-option>
</el-select>
<el-input
v-model="searchKeyword"
placeholder="请输入关键词"
clearable
style="width: 300px;"
@keyup.enter="handleSearch"
></el-input>
<el-button type="primary" @click="handleSearch">搜索</el-button>
</div>
<!-- 原料列表 -->
<el-table :data="tableData" style="width: 100%" v-loading="loading" row-key="id"
:expand-row-keys="expandRowKeys" @expand-change="handleExpandChange">
@@ -48,7 +64,7 @@ export default {
const tableData = ref([]);
const loading = ref(false);
const searchKeyword = ref('');
const searchType = ref('rawMaterial');
const searchType = ref('name'); // 默认按原料名称搜索
const expandRowKeys = ref([]);
const pagination = ref({
@@ -60,48 +76,24 @@ export default {
const fetchRawMaterials = async () => {
loading.value = true;
try {
let response;
if (searchType.value === 'rawMaterial') {
response = await FeedApi.getRawMaterials({
name: searchKeyword.value,
const params = {
page: pagination.value.page,
page_size: pagination.value.page_size,
});
} else {
const nutrientResponse = await FeedApi.getNutrients({
name: searchKeyword.value,
page: pagination.value.page,
page_size: pagination.value.page_size,
});
// 从营养数据中提取原料
const rawMaterials = [];
if (nutrientResponse.data && nutrientResponse.data.list) {
nutrientResponse.data.list.forEach(nutrient => {
if (nutrient.raw_materials) {
nutrient.raw_materials.forEach(rm => {
// 避免重复添加
if (!rawMaterials.some(item => item.id === rm.id)) {
rawMaterials.push({
id: rm.id,
name: rm.name,
// 模拟其他字段
description: '通过营养搜索',
raw_material_nutrients: []
});
}
});
}
});
}
response = { data: { list: rawMaterials, pagination: { total: rawMaterials.length } } };
};
if (searchKeyword.value) {
params[searchType.value] = searchKeyword.value;
}
const response = await FeedApi.getRawMaterials(params);
if (response.data) {
tableData.value = response.data.list;
pagination.value.total = response.data.pagination.total;
}
} catch (error) {
console.error('获取原料列表失败:', error);
ElMessage.error('获取原料列表失败');
} finally {
loading.value = false;
}
@@ -124,7 +116,8 @@ export default {
const handleExpandChange = async (row, expandedRows) => {
const isExpanded = expandedRows.some(r => r.id === row.id);
if (isExpanded && row.raw_material_nutrients.length === 0) {
// 优化:仅在展开时且数据未加载时才请求
if (isExpanded && (!row.raw_material_nutrients || row.raw_material_nutrients.length === 0)) {
try {
const response = await FeedApi.getRawMaterialById(row.id);
if (response.data) {
@@ -135,6 +128,7 @@ export default {
}
} catch (error) {
console.error('获取原料详情失败:', error);
ElMessage.error('获取原料营养成分失败');
}
}
};