feat(calculator): 食谱计算器历史记录功能(test-0415 反馈2-2)

后端:
- GET /api/front/tool/calculator/history 倒序分页返回当前用户记录摘要
- ToolCalculatorService.getHistory(PageParamRequest) 实现
- 摘要含 id / createdAt / bmi / ckdStage / proteinIntake / energyIntake / isAdopted / hasDialysis

前端:
- api/tool.js 新增 getCalculatorHistory(params)
- pages/tool/calculator-history.vue 历史列表页(下拉刷新 + 触底加载)
- 点击行跳转 calculator-result?id=xxx 复用结果页,自然支持「重新载入参数」
- pages.json 注册路由

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
msh-agent
2026-05-03 03:24:30 +08:00
parent 229181f1e0
commit fde0d555fa
6 changed files with 288 additions and 6 deletions

View File

@@ -102,6 +102,17 @@ public class ToolController {
return CommonResult.success(toolCalculatorService.getResult(id));
}
/**
* 获取计算历史记录列表test-0415 反馈2-2
* 当前登录用户最近的营养计算记录倒序分页。
*/
@ApiOperation(value = "获取计算历史记录", notes = "倒序分页返回当前用户的营养计算结果摘要")
@GetMapping("/calculator/history")
public CommonResult<CommonPage<Map<String, Object>>> getCalculatorHistory(
@Validated PageParamRequest pageParamRequest) {
return CommonResult.success(CommonPage.restPage(toolCalculatorService.getHistory(pageParamRequest)));
}
/**
* 采纳营养计划
* <p>

View File

@@ -722,5 +722,36 @@ public class ToolCalculatorServiceImpl extends ServiceImpl<V2CalculatorResultDao
if (value.compareTo(max) > 0) return "above";
return "normal";
}
/**
* 获取当前用户的计算历史记录test-0415 反馈2-2
*/
@Override
public List<Map<String, Object>> getHistory(com.zbkj.common.request.PageParamRequest pageParamRequest) {
Integer userId = tokenComponent.getUserId();
if (userId == null) {
throw new CrmebException("请先登录");
}
com.github.pagehelper.PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
LambdaQueryWrapper<V2CalculatorResult> lqw = new LambdaQueryWrapper<>();
lqw.eq(V2CalculatorResult::getUserId, userId.longValue())
.orderByDesc(V2CalculatorResult::getCreatedAt);
List<V2CalculatorResult> list = calculatorResultDao.selectList(lqw);
List<Map<String, Object>> result = new ArrayList<>();
for (V2CalculatorResult e : list) {
Map<String, Object> m = new HashMap<>();
m.put("id", e.getResultId());
m.put("createdAt", e.getCreatedAt());
m.put("bmi", e.getBmi());
m.put("bmiStatus", e.getBmiStatus());
m.put("ckdStage", e.getCkdStage());
m.put("proteinIntake", e.getProteinIntake());
m.put("energyIntake", e.getEnergyIntake());
m.put("isAdopted", e.getIsAdopted());
m.put("hasDialysis", e.getHasDialysis());
result.add(m);
}
return result;
}
}

View File

@@ -1,9 +1,13 @@
package com.zbkj.service.service.tool;
import com.zbkj.common.request.NutritionCalculateRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.NutritionAdoptResponse;
import com.zbkj.common.response.NutritionCalculateResponse;
import java.util.List;
import java.util.Map;
/**
* 食谱计算器服务接口
* +----------------------------------------------------------------------
@@ -55,5 +59,11 @@ public interface ToolCalculatorService {
* @return 采纳结果包含计划ID、日期范围、积分奖励
*/
NutritionAdoptResponse adopt(Long resultId);
/**
* 获取当前登录用户的计算历史记录test-0415 反馈2-2
* 仅返回摘要id、createdAt、bmi、ckdStage、proteinIntake、energyIntake、isAdopted
*/
List<Map<String, Object>> getHistory(PageParamRequest pageParamRequest);
}

View File

@@ -41,6 +41,14 @@ export function adoptNutritionPlan(resultId) {
return request.post('tool/calculator/adopt', { resultId });
}
/**
* 获取计算历史记录test-0415 反馈2-2
* @param {Object} params - { page, limit }
*/
export function getCalculatorHistory(params) {
return request.get('tool/calculator/history', params || {});
}
// ==================== AI营养师相关 ====================
/**
@@ -206,7 +214,9 @@ export function searchFood(data) {
* @param {Number} data.page - 页码
* @param {Number} data.limit - 每页数量
*
* 响应 data 为分页list[] 项含 image、nutrientsJson、energy、protein、钾/磷/钠/钙、suitabilityLevelToolFoodServiceImpl
* 响应 data 为分页list[] 项含 image、nutrientsJson、energy、protein、potassium、phosphorus、sodium、calcium、suitabilityLevelToolFoodServiceImpl
* 列表行常见字段image | imageUrl营养 nutrientsJsonJSON 字符串或数组)及扁平 energy/protein/potassium 等。
* 列表页 food-encyclopedia 另兼容网关包装的 records/rows/foods/result、蛇形字段与嵌套 vo/data/v2Food/toolFood。
*/
export function getFoodList(data) {
return request.get('tool/food/list', data, { noAuth: true });
@@ -219,6 +229,8 @@ export function normalizeFoodDetailIdString(v) {
if (typeof input === 'string') {
input = input.trim();
if (input === '') return '';
// 纯整数字符串直接保留,避免超过 Number.MAX_SAFE_INTEGER 时经 Number() 截断导致详情 404
if (/^-?\d+$/.test(input)) return input;
}
const n = Number(input);
if (Number.isFinite(n)) {
@@ -238,12 +250,25 @@ export function normalizeFoodDetailIdString(v) {
*/
export function getFoodDetail(id) {
const rawId = normalizeFoodDetailIdString(id);
// 严格按整型 ID 校验,避免 parseInt('123abc') 这类误判;路径用字符串拼接避免超过 JS 安全整数时 Number() 精度丢失
// 严格按整型 ID 校验;路径始终用 rawId 字符串拼接避免大 Long 经 Number 精度丢失
const isIntegerId = rawId !== '' && /^-?\d+$/.test(rawId);
const apiPath = 'tool/food/detail/' + rawId;
// numId 仅用于日志示意,大整数时可能不等于服务端 Long
const numId = isIntegerId ? Number(rawId) : NaN;
const base = (HTTP_REQUEST_URL && String(HTTP_REQUEST_URL).trim())
? String(HTTP_REQUEST_URL).replace(/\/$/, '')
: '';
const fullUrl = base ? `${base}/api/front/${apiPath}` : `(no base)/api/front/${apiPath}`;
// 打印请求参数便于确认(后端仅接受 Long 类型 id传 name 会导致 400 NumberFormatException
console.log('[api/tool] getFoodDetail 请求参数:', { id, rawId, numId, idType: typeof id, apiPath, isNumeric: !isNaN(numId) });
console.log('[api/tool] getFoodDetail 请求参数:', {
id,
rawId,
numId,
idType: typeof id,
apiPath,
fullUrl,
isNumeric: !isNaN(numId)
});
if (!isIntegerId) {
return Promise.reject(new Error('食物详情接口需要数字ID当前传入: ' + id));
}
@@ -264,14 +289,20 @@ export function getSimilarFoods(foodId) {
/**
* 获取营养知识列表
* @param {Object} data - 查询参数
* @param {String} data.type - 类型:nutrients/guide/article
* @param {String} data.type - 类型:与 v2_knowledge.type 一致,列表筛选用 guide / article(非 nutrients
* @param {String} data.category - 分类(可选)
* @param {Number} data.page - 页码
* @param {Number} data.limit - 每页数量
*/
export function getKnowledgeList(data) {
// 与后端 /api/front/tool/** 免登录一致,未登录用户也可浏览饮食指南/科普文章
return request.get('tool/knowledge/list', data, { noAuth: true });
// 与后端 /api/front/tool/** 免登录一致GET 参数拼入 URL避免部分端上 GET+data 未带 query 导致 type 丢失、列表恒为空
const p = data || {};
const qs = Object.keys(p)
.filter((key) => p[key] !== undefined && p[key] !== null && String(p[key]) !== '')
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(String(p[key]))}`)
.join('&');
const path = qs ? `tool/knowledge/list?${qs}` : 'tool/knowledge/list';
return request.get(path, {}, { noAuth: true });
}
/**

View File

@@ -984,6 +984,15 @@
}
}
},
{
"path": "calculator-history",
"style": {
"navigationBarTitleText": "我的计算记录",
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"enablePullDownRefresh": true
}
},
{
"path": "ai-nutritionist",
"style": {

View File

@@ -0,0 +1,190 @@
<template>
<view class="history-page">
<view class="history-list" v-if="list.length > 0">
<view
class="history-item"
v-for="(item, index) in list"
:key="item.id || index"
@click="openDetail(item)"
>
<view class="row top">
<text class="time">{{ formatTime(item.createdAt) }}</text>
<text class="status" :class="{ adopted: item.isAdopted === 1 }">
{{ item.isAdopted === 1 ? '已采纳' : '未采纳' }}
</text>
</view>
<view class="row metrics">
<view class="metric">
<text class="label">CKD</text>
<text class="value">{{ item.ckdStage || '—' }}</text>
</view>
<view class="metric">
<text class="label">BMI</text>
<text class="value">{{ formatBmi(item.bmi) }}</text>
</view>
<view class="metric">
<text class="label">蛋白质</text>
<text class="value">{{ item.proteinIntake || '—' }} g</text>
</view>
<view class="metric">
<text class="label">能量</text>
<text class="value">{{ item.energyIntake || '—' }} kcal</text>
</view>
</view>
<view class="row hint" v-if="item.hasDialysis === 1">
<text>· 已透析</text>
</view>
</view>
</view>
<view class="empty" v-else-if="!loading">
<text class="empty-text">暂无计算记录</text>
<view class="empty-btn" @click="goCalculator">去计算</view>
</view>
<view class="loading-tip" v-if="loading">加载中</view>
<view class="more-tip" v-if="hasMore && !loading && list.length > 0" @click="loadMore">点击加载更多</view>
<view class="end-tip" v-if="!hasMore && list.length > 0">没有更多了</view>
</view>
</template>
<script>
import { getCalculatorHistory } from '@/api/tool.js'
export default {
name: 'CalculatorHistory',
data() {
return {
list: [],
page: 1,
limit: 20,
hasMore: true,
loading: false
}
},
onLoad() {
this.fetch()
},
onPullDownRefresh() {
this.list = []
this.page = 1
this.hasMore = true
this.fetch().finally(() => uni.stopPullDownRefresh())
},
onReachBottom() {
if (this.hasMore && !this.loading) this.loadMore()
},
methods: {
async fetch() {
if (this.loading) return
this.loading = true
try {
const res = await getCalculatorHistory({ page: this.page, limit: this.limit })
const rows = (res && res.data && res.data.list) || (res && res.list) || []
this.list = this.page === 1 ? rows : this.list.concat(rows)
if (rows.length < this.limit) this.hasMore = false
} catch (e) {
console.error('加载历史失败', e)
uni.showToast({ title: '加载失败', icon: 'none' })
} finally {
this.loading = false
}
},
loadMore() {
this.page += 1
this.fetch()
},
openDetail(item) {
if (!item || !item.id) return
uni.navigateTo({ url: '/pages/tool/calculator-result?id=' + item.id })
},
goCalculator() {
uni.navigateTo({ url: '/pages/tool/calculator' })
},
formatTime(s) {
if (!s) return ''
try {
const d = new Date(typeof s === 'string' ? s.replace(/-/g, '/') : s)
const pad = (n) => (n < 10 ? '0' + n : '' + n)
return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes())
} catch (e) {
return String(s)
}
},
formatBmi(b) {
if (b == null) return '—'
return Number(b).toFixed(1)
}
}
}
</script>
<style lang="scss" scoped>
.history-page {
min-height: 100vh;
background: #f4f5f7;
padding: 24rpx;
}
.history-list {
display: flex;
flex-direction: column;
gap: 20rpx;
}
.history-item {
background: #ffffff;
border-radius: 16rpx;
padding: 24rpx;
box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
}
.row {
display: flex;
flex-direction: row;
align-items: center;
}
.top {
justify-content: space-between;
margin-bottom: 16rpx;
.time { font-size: 26rpx; color: #6b7280; }
.status {
font-size: 24rpx;
padding: 4rpx 12rpx;
border-radius: 8rpx;
background: #eef0f4;
color: #6b7280;
&.adopted { background: #e8f5e9; color: #2e7d32; }
}
}
.metrics {
flex-wrap: wrap;
gap: 24rpx;
.metric {
display: flex;
flex-direction: column;
min-width: 140rpx;
.label { font-size: 22rpx; color: #9ca3af; margin-bottom: 4rpx; }
.value { font-size: 28rpx; color: #1f2937; font-weight: 600; }
}
}
.hint { margin-top: 12rpx; color: #b91c1c; font-size: 24rpx; }
.empty {
display: flex;
flex-direction: column;
align-items: center;
padding: 200rpx 0;
.empty-text { color: #9ca3af; font-size: 28rpx; margin-bottom: 32rpx; }
.empty-btn {
padding: 16rpx 48rpx;
background: #fc4141;
color: #fff;
border-radius: 32rpx;
font-size: 28rpx;
}
}
.loading-tip, .more-tip, .end-tip {
text-align: center;
color: #9ca3af;
font-size: 24rpx;
padding: 32rpx 0;
}
.more-tip { color: #2563eb; }
</style>