| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <template>
- <view class="recipe-detail">
- <view v-if="recipe" class="detail-content">
- <!-- 头部信息 -->
- <view class="header">
- <view v-if="recipe.coverImage" class="cover-wrap">
- <image class="cover" :src="getImageUrl(recipe.coverImage)" mode="aspectFill" />
- </view>
- <text class="title">{{ recipe.name }}</text>
- <view v-if="recipe.mealType" class="tag-row">
- <text class="tag">{{ recipe.mealType }}</text>
- <text v-if="recipe.difficulty" class="tag tag-sec">{{ recipe.difficulty }}</text>
- <text v-if="recipe.cookTime" class="tag tag-sec">{{ recipe.cookTime }}分钟</text>
- </view>
- </view>
- <!-- 营养摘要 -->
- <view class="section">
- <text class="section-title">营养信息</text>
- <view class="nutrition-row">
- <view class="nutrition-item">
- <text class="nutrition-val">{{ recipe.totalCalories || 0 }}</text>
- <text class="nutrition-label">热量(kcal)</text>
- </view>
- <view class="nutrition-item">
- <text class="nutrition-val">{{ recipe.totalProtein || 0 }}</text>
- <text class="nutrition-label">蛋白(g)</text>
- </view>
- <view class="nutrition-item">
- <text class="nutrition-val">{{ recipe.totalFat || 0 }}</text>
- <text class="nutrition-label">脂肪(g)</text>
- </view>
- <view class="nutrition-item">
- <text class="nutrition-val">{{ recipe.totalCarbs || 0 }}</text>
- <text class="nutrition-label">碳水(g)</text>
- </view>
- </view>
- </view>
- <!-- 食材列表 -->
- <view v-if="ingredientList.length > 0" class="section">
- <text class="section-title">食材</text>
- <view class="ingredient-item" v-for="(item, idx) in ingredientList" :key="idx">
- <text class="ingredient-text">{{ item }}</text>
- </view>
- </view>
- <!-- 烹饪步骤 -->
- <view v-if="stepList.length > 0" class="section">
- <text class="section-title">步骤</text>
- <view class="step-item" v-for="(step, idx) in stepList" :key="idx">
- <view class="step-num-wrap">
- <text class="step-num">{{ idx + 1 }}</text>
- </view>
- <text class="step-text">{{ step }}</text>
- </view>
- </view>
- <!-- 描述 -->
- <view v-if="recipe.description" class="section">
- <text class="section-title">简介</text>
- <text class="desc-text">{{ recipe.description }}</text>
- </view>
- </view>
- <!-- 加载中 -->
- <view v-if="!recipe && loading" class="empty-state">
- <text class="loading-text">加载中...</text>
- </view>
- <!-- 空状态 -->
- <view v-if="!recipe && !loading" class="empty-state">
- <text class="empty-icon">🍳</text>
- <text class="empty-text">食谱信息不存在</text>
- </view>
- </view>
- </template>
- <script>
- import { getMealRecommend } from '@/utils/api'
- import config from '@/config.js'
- export default {
- name: 'RecipeDetail',
- data() {
- return {
- recipeId: '',
- recipe: null,
- ingredientList: [],
- stepList: [],
- loading: false
- }
- },
- onLoad(options) {
- this.recipeId = options.id || ''
- if (this.recipeId) {
- this.loadDetail()
- }
- },
- methods: {
- loadDetail() {
- this.loading = true
- // 从缓存中尝试读取推荐结果中的食谱
- try {
- var cached = uni.getStorageSync('recommend_result')
- if (cached) {
- var data = typeof cached === 'string' ? JSON.parse(cached) : cached
- var recipes = data.candidateRecipes || []
- for (var i = 0; i < recipes.length; i++) {
- if (String(recipes[i].id) === String(this.recipeId)) {
- this.recipe = recipes[i]
- this.parseIngredients(this.recipe.ingredients)
- this.parseSteps(this.recipe.steps)
- this.loading = false
- return
- }
- }
- }
- } catch (e) {
- // ignore
- }
- // 缓存中没有,使用ID占位
- this.recipe = { id: this.recipeId, name: '食谱详情' }
- this.loading = false
- },
- parseIngredients(str) {
- if (!str) {
- this.ingredientList = []
- return
- }
- try {
- var parsed = typeof str === 'string' ? JSON.parse(str) : str
- this.ingredientList = Array.isArray(parsed) ? parsed : [String(parsed)]
- } catch (e) {
- this.ingredientList = str.split(/[;;,,\n]/).filter(function(s) { return s.trim() })
- }
- },
- parseSteps(str) {
- if (!str) {
- this.stepList = []
- return
- }
- try {
- var parsed = typeof str === 'string' ? JSON.parse(str) : str
- this.stepList = Array.isArray(parsed) ? parsed : [String(parsed)]
- } catch (e) {
- this.stepList = str.split(/[;;\n]/).filter(function(s) { return s.trim() })
- }
- },
- getImageUrl: function(path) {
- return config.API_BASE_URL + path
- }
- }
- }
- </script>
- <style scoped>
- .recipe-detail {
- min-height: 100vh;
- background: var(--bg, #FFF7ED);
- padding: 20rpx 30rpx;
- }
- .header {
- background: #fff;
- border-radius: 16rpx;
- padding: 30rpx;
- box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
- margin-bottom: 20rpx;
- }
- .cover-wrap {
- margin-bottom: 20rpx;
- }
- .cover {
- width: 100%;
- height: 360rpx;
- border-radius: 12rpx;
- }
- .title {
- font-size: 36rpx;
- font-weight: bold;
- color: #333;
- display: block;
- margin-bottom: 16rpx;
- }
- .tag-row {
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- gap: 12rpx;
- }
- .tag {
- font-size: 22rpx;
- background: var(--color-primary, #F97316);
- color: #fff;
- padding: 6rpx 16rpx;
- border-radius: 6rpx;
- }
- .tag-sec {
- background: #FFF0E0;
- color: var(--color-primary, #F97316);
- }
- .section {
- background: #fff;
- border-radius: 16rpx;
- padding: 30rpx;
- box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
- margin-bottom: 20rpx;
- }
- .section-title {
- font-size: 30rpx;
- font-weight: 600;
- color: #333;
- margin-bottom: 16rpx;
- padding-left: 10rpx;
- border-left: 6rpx solid var(--color-primary, #F97316);
- display: block;
- }
- .nutrition-row {
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- gap: 16rpx;
- }
- .nutrition-item {
- flex: 1;
- min-width: 120rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- background: #FFF7ED;
- border-radius: 12rpx;
- padding: 16rpx 10rpx;
- }
- .nutrition-val {
- font-size: 32rpx;
- color: var(--color-primary, #F97316);
- font-weight: 600;
- }
- .nutrition-label {
- font-size: 22rpx;
- color: #999;
- margin-top: 6rpx;
- }
- .ingredient-item {
- padding: 12rpx 0;
- border-bottom: 1rpx solid #f5f5f5;
- }
- .ingredient-item:last-child {
- border-bottom: none;
- }
- .ingredient-text {
- font-size: 28rpx;
- color: #333;
- }
- .step-item {
- display: flex;
- flex-direction: row;
- margin-bottom: 20rpx;
- }
- .step-num-wrap {
- flex-shrink: 0;
- width: 44rpx;
- height: 44rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- margin-right: 16rpx;
- }
- .step-num {
- width: 40rpx;
- height: 40rpx;
- line-height: 40rpx;
- text-align: center;
- background: var(--color-primary, #F97316);
- color: #fff;
- border-radius: 50%;
- font-size: 24rpx;
- }
- .step-text {
- font-size: 28rpx;
- color: #333;
- line-height: 1.6;
- flex: 1;
- }
- .desc-text {
- font-size: 28rpx;
- color: #666;
- line-height: 1.6;
- }
- .empty-state {
- display: flex;
- flex-direction: column;
- align-items: center;
- padding: 120rpx 0;
- }
- .empty-icon {
- font-size: 80rpx;
- }
- .empty-text {
- font-size: 28rpx;
- color: #999;
- margin-top: 20rpx;
- }
- .loading-text {
- font-size: 28rpx;
- color: #999;
- }
- </style>
|