index.vue
8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!--
* @Date: 2026-01-30
* @Description: 产品详情页
-->
<template>
<div class="min-h-screen bg-[#F9FAFB] pb-[calc(160rpx+env(safe-area-inset-bottom))]">
<NavHeader title="产品详情" />
<!-- Loading State -->
<div v-if="loading" class="flex items-center justify-center h-screen">
<div class="flex flex-col items-center">
<div class="w-[64rpx] h-[64rpx] border-4 border-gray-200 border-t-blue-600 rounded-full animate-spin mb-[24rpx]"></div>
<text class="text-gray-600 text-[28rpx]">加载中...</text>
</div>
</div>
<!-- Content -->
<div v-else>
<!-- Banner Image -->
<div class="w-full h-[420rpx] relative">
<img
class="w-full h-full object-cover"
:src="productDetail.cover_image || bannerImage"
mode="aspectFill"
/>
<div class="absolute top-[32rpx] right-[32rpx] flex items-center gap-[16rpx]">
<!-- Hot Tag -->
<div
v-if="productDetail.recommend === 'hot'"
class="bg-red-500 text-white text-[24rpx] px-[20rpx] py-[10rpx] rounded-full shadow-sm backdrop-blur-sm bg-opacity-90"
>
热卖
</div>
</div>
</div>
<!-- Product Header -->
<div class="relative mt-[-40rpx] bg-white rounded-t-[40rpx] px-[40rpx] pt-[48rpx] pb-[40rpx] z-10">
<h1 class="text-[#1F2937] text-[44rpx] font-bold leading-[1.2] mb-[24rpx]">
{{ productDetail.product_name || '产品详情' }}
</h1>
<!-- 动态标签 -->
<div v-if="productDetail.tags && productDetail.tags.length" class="flex flex-wrap gap-[16rpx]">
<div
v-for="tag in productDetail.tags"
:key="tag.id"
class="rounded-[8rpx] px-[16rpx] py-[6rpx]"
:style="{
backgroundColor: tag.bg_color,
color: tag.text_color
}"
>
<span class="text-[24rpx]">{{ tag.name }}</span>
</div>
</div>
</div>
<!-- Product Description (富文本) -->
<div v-if="productDetail.product_description" class="px-[32rpx] mt-[32rpx]">
<div class="bg-white rounded-[32rpx] p-[40rpx]">
<h2 class="text-[#1F2937] text-[32rpx] font-bold mb-[32rpx]">产品描述</h2>
<!-- 使用 rich-text 渲染富文本 -->
<rich-text :nodes="productDetail.product_description" class="text-[#4B5563] text-[28rpx] leading-[1.6]"></rich-text>
</div>
</div>
<!-- Attachments -->
<div v-if="productDetail.documents && productDetail.documents.length" class="px-[32rpx] mt-[32rpx]">
<div class="bg-white rounded-[32rpx] p-[40rpx]">
<h2 class="text-[#1F2937] text-[32rpx] font-bold mb-[16rpx]">相关附件</h2>
<!-- <text class="text-[#9CA3AF] text-[24rpx] mb-[24rpx] block">如需下载,可在预览页点击右上角「...」转发至其他设备</text> -->
<div class="flex flex-col gap-[24rpx]">
<div
v-for="(doc, index) in productDetail.documents"
:key="index"
class="flex flex-col p-[24rpx] bg-gray-50 rounded-[16rpx]"
>
<div class="flex items-center justify-between">
<div class="flex items-center flex-1 mr-[24rpx]">
<image
:src="getDocumentIcon({ extension: doc.extension, fileName: doc.file_name })"
class="w-[58rpx] h-[58rpx] mr-[24rpx]"
mode="aspectFit"
/>
<div class="flex flex-col">
<span class="text-[#1F2937] text-[28rpx] font-medium mb-[4rpx] line-clamp-2">{{ doc.file_name }}</span>
<span class="text-[#9CA3AF] text-[24rpx]">{{ getDocumentLabel({ extension: doc.extension, fileName: doc.file_name }) }} · {{ doc.file_size_formatted }}</span>
</div>
</div>
<IconFont name="eye" size="14" color="#2563EB" @tap="viewDocument(doc)" />
</div>
</div>
</div>
</div>
</div>
</div>
<!-- TabBar -->
<!-- <TabBar /> -->
<!-- 计划书按钮 -->
<div class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200 px-[32rpx] py-[24rpx] flex items-center justify-center">
<nut-button
color="#2563EB"
class="!w-full !h-[88rpx] !rounded-[16rpx] !text-[28rpx] !font-bold"
@tap="openPlanPopup"
>
制作计划书
</nut-button>
</div>
<!-- 计划书表单容器 -->
<!-- 测试数据:后端接口和字段还没有准备好,使用 PlanFormContainer 进行的前端测试 -->
<!-- 使用 v-if 条件渲染,避免 productDetail 为 null 时的 prop 类型检查错误 -->
<view v-if="showPlanPopup && productDetail.id">
<PlanFormContainer
v-model:visible="showPlanPopup"
:product="productDetail"
@close="showPlanPopup = false"
@submit="handlePlanSubmit"
/>
</view>
</div>
</template>
<script setup>
import { ref, nextTick } from 'vue'
import NavHeader from '@/components/navigation/NavHeader.vue'
import IconFont from '@/components/icons/IconFont.vue'
import PlanFormContainer from '@/components/plan/PlanFormContainer.vue'
import { useFileOperation } from '@/composables/useFileOperation'
import Taro, { useLoad } from '@tarojs/taro'
import { getDocumentIcon, getDocumentLabel } from '@/utils/documentIcons'
import { detailAPI } from '@/api/get_product'
const { viewFile } = useFileOperation()
// 计划书弹窗状态
const showPlanPopup = ref(false)
// 接收页面参数
const productId = ref(null)
// 加载状态
const loading = ref(true)
// 产品详情数据
const productDetail = ref({
id: null,
product_name: '',
recommend: '',
product_description: '',
cover_image: '',
tags: [],
documents: []
})
/**
* 获取产品详情
*
* @description 调用 detailAPI 获取产品详情数据
* @param {string} id - 产品ID
*/
const fetchProductDetail = async (id) => {
try {
loading.value = true
const res = await detailAPI({
i: id
})
if (res.code === 1 && res.data) {
productDetail.value = res.data
} else {
Taro.showToast({
title: res.msg || '获取产品详情失败',
icon: 'none',
duration: 2000
})
}
} catch (err) {
console.error('获取产品详情失败:', err)
Taro.showToast({
title: '网络错误,请重试',
icon: 'none',
duration: 2000
})
} finally {
loading.value = false
}
}
/**
* 查看文档
*
* @description 打开文档预览,支持图片、视频、PDF 等多种格式
* @param {Object} doc - 文档对象
* @param {string} doc.file_name - 文档名称
* @param {string} doc.file_url - 文档 URL
*/
const viewDocument = (doc) => {
// 打开文件预览(自动判断文件类型)
viewFile({
fileName: doc.file_name,
downloadUrl: doc.file_url
})
}
/**
* 打开计划书弹窗
*
* @description 打开当前产品的计划书表单
*/
const openPlanPopup = () => {
showPlanPopup.value = true
}
/**
* 处理计划书提交
*
* @description 接收 PlanFormContainer 的提交结果事件
* PlanFormContainer 已经调用过 API 并处理了 toast 提示
* 这里根据成功/失败状态跳转到结果页面
* @param {Object} result - 提交结果
* @param {boolean} result.success - 是否成功
* @param {number} result.order_id - 订单 ID(成功时)
* @param {number} result.product_id - 产品 ID
* @param {string} result.form_sn - 表单标识
*/
const handlePlanSubmit = async (result) => {
console.log('[Product Detail] 计划书提交结果:', result)
// 关闭弹窗
showPlanPopup.value = false
await nextTick()
// 构建结果页面参数
const params = {
success: result.success ? 'true' : 'false'
}
// 失败时传递后端返回的错误信息
if (!result.success && result.message) {
params.message = result.message
}
console.log('[Product Detail] 跳转到结果页面,参数:', params)
Taro.navigateTo({
url: `/pages/plan-submit-result/index?${new URLSearchParams(params).toString()}`
})
}
useLoad((options) => {
console.log('产品详情页参数:', options)
if (options.id) {
productId.value = options.id
console.log('产品ID:', productId.value)
// 获取产品详情数据
fetchProductDetail(options.id)
} else {
console.warn('未接收到产品ID')
loading.value = false
Taro.showToast({
title: '产品ID不存在',
icon: 'none',
duration: 2000
})
}
})
// Random banner image (fallback)
const bannerImage = `https://picsum.photos/seed/${Math.floor(Math.random() * 1000)}/750/420`
</script>