275 lines
6.2 KiB
Vue

<template>
<view class="synthesis-wrap">
<view class="page-title">碎片合成</view>
<view v-if="loading" class="status-text">加载中...</view>
<view v-else-if="recipes.length === 0" class="status-text">暂无可用的合成配方</view>
<view v-for="recipe in recipes" :key="recipe.id" class="recipe-card">
<!-- 目标商品 -->
<view class="target-section">
<image v-if="recipe.target_product" :src="getFirstImage(recipe.target_product.images_json)" mode="aspectFill" class="target-image" />
<view class="target-info">
<text class="target-name">{{ recipe.target_product?.name || '目标商品' }}</text>
<text class="recipe-name">{{ recipe.name }}</text>
<text class="recipe-desc" v-if="recipe.description">{{ recipe.description }}</text>
</view>
</view>
<!-- 材料列表 -->
<view class="materials-section">
<text class="section-label">所需材料</text>
<view class="materials-grid">
<view v-for="(mat, idx) in recipe.materials" :key="idx" class="material-item">
<image :src="mat.image || '/static/placeholder.png'" mode="aspectFill" class="material-image" />
<text class="material-name">{{ mat.name }}</text>
<view class="material-count" :class="{ enough: mat.owned_count >= mat.required_count }">
<text>{{ mat.owned_count }}</text>
<text class="count-sep">/</text>
<text>{{ mat.required_count }}</text>
</view>
</view>
</view>
</view>
<!-- 合成按钮 -->
<button
class="synthesis-btn"
:class="{ disabled: !recipe.can_synthesize }"
:disabled="!recipe.can_synthesize || synthesizing"
@tap="onSynthesize(recipe)"
>
{{ recipe.can_synthesize ? '立即合成' : '材料不足' }}
</button>
</view>
</view>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { getSynthesisRecipes, doSynthesis } from '../../api/synthesis.js'
const loading = ref(true)
const synthesizing = ref(false)
const recipes = ref([])
function getFirstImage(imagesJson) {
if (!imagesJson) return '/static/placeholder.png'
try {
const imgs = JSON.parse(imagesJson)
return imgs && imgs.length > 0 ? imgs[0] : '/static/placeholder.png'
} catch {
return imagesJson
}
}
async function loadRecipes() {
loading.value = true
const userId = uni.getStorageSync('user_id')
if (!userId) {
loading.value = false
return
}
try {
const res = await getSynthesisRecipes(userId)
recipes.value = res?.list || []
} catch (e) {
console.error('loadRecipes error', e)
} finally {
loading.value = false
}
}
async function onSynthesize(recipe) {
if (synthesizing.value || !recipe.can_synthesize) return
try {
await new Promise((resolve, reject) => {
uni.showModal({
title: '确认合成',
content: `确定要合成「${recipe.target_product?.name || '目标商品'}」吗?合成后碎片将被消耗。`,
success: (res) => res.confirm ? resolve() : reject('cancel'),
fail: reject
})
})
} catch {
return
}
synthesizing.value = true
const userId = uni.getStorageSync('user_id')
try {
await doSynthesis(userId, recipe.id)
uni.showToast({ title: '合成成功!', icon: 'success' })
await loadRecipes()
} catch (e) {
uni.showToast({ title: e?.message || '合成失败', icon: 'none' })
} finally {
synthesizing.value = false
}
}
onMounted(() => {
loadRecipes()
})
// 支持从其他页面返回时刷新
const onShow = () => {
if (!loading.value) loadRecipes()
}
defineExpose({ onShow })
</script>
<style lang="scss" scoped>
.synthesis-wrap {
min-height: 100vh;
padding: 24rpx;
background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
}
.page-title {
font-size: 36rpx;
font-weight: bold;
color: #fff;
text-align: center;
padding: 20rpx 0 30rpx;
}
.status-text {
text-align: center;
color: rgba(255, 255, 255, 0.5);
padding: 100rpx 0;
font-size: 28rpx;
}
.recipe-card {
background: rgba(255, 255, 255, 0.08);
border-radius: 24rpx;
padding: 30rpx;
margin-bottom: 24rpx;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.target-section {
display: flex;
align-items: center;
gap: 20rpx;
margin-bottom: 24rpx;
}
.target-image {
width: 140rpx;
height: 140rpx;
border-radius: 16rpx;
background: rgba(255, 255, 255, 0.1);
}
.target-info {
flex: 1;
display: flex;
flex-direction: column;
gap: 6rpx;
}
.target-name {
font-size: 32rpx;
font-weight: bold;
color: #fff;
}
.recipe-name {
font-size: 24rpx;
color: rgba(255, 255, 255, 0.7);
}
.recipe-desc {
font-size: 22rpx;
color: rgba(255, 255, 255, 0.5);
}
.materials-section {
margin-bottom: 24rpx;
}
.section-label {
font-size: 24rpx;
color: rgba(255, 255, 255, 0.6);
margin-bottom: 16rpx;
display: block;
}
.materials-grid {
display: flex;
flex-wrap: wrap;
gap: 16rpx;
}
.material-item {
display: flex;
flex-direction: column;
align-items: center;
width: 160rpx;
padding: 16rpx;
background: rgba(255, 255, 255, 0.05);
border-radius: 16rpx;
border: 1px solid rgba(255, 255, 255, 0.08);
}
.material-image {
width: 100rpx;
height: 100rpx;
border-radius: 12rpx;
margin-bottom: 8rpx;
background: rgba(255, 255, 255, 0.1);
}
.material-name {
font-size: 22rpx;
color: rgba(255, 255, 255, 0.8);
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100%;
}
.material-count {
font-size: 24rpx;
color: #ff5252;
font-weight: bold;
margin-top: 4rpx;
&.enough {
color: #4caf50;
}
.count-sep {
margin: 0 2rpx;
color: rgba(255, 255, 255, 0.4);
}
}
.synthesis-btn {
width: 100%;
height: 80rpx;
border-radius: 40rpx;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
font-size: 30rpx;
font-weight: bold;
border: none;
display: flex;
align-items: center;
justify-content: center;
&.disabled {
background: rgba(255, 255, 255, 0.15);
color: rgba(255, 255, 255, 0.4);
}
&:active:not(.disabled) {
opacity: 0.85;
}
}
</style>