index.vue
12.6 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!--
* @Date: 2026-02-06
* @Description: 我的计划书 - 支持滚动加载更多(参考 week-hot-material)
-->
<template>
<view class="h-screen bg-gray-50 flex flex-col">
<!-- NavHeader -->
<NavHeader title="我的计划书" />
<!-- Search Bar -->
<view class="px-[24rpx] py-[16rpx] bg-white">
<SearchBar
v-model="searchValue"
placeholder="搜索计划书名称、申请人..."
:show-clear="true"
variant="rounded"
@search="onSearch"
@clear="onClear"
/>
</view>
<!-- Tabs Container -->
<view class="bg-white mt-[2rpx]">
<nut-tabs v-model="activeTabId">
<template #titles>
<view class="filter-tabs-wrapper">
<view
v-for="item in tabsData"
:key="item.id"
:class="[
'filter-tab-item',
activeTabId === item.id ? 'filter-tab-active' : 'filter-tab-inactive'
]"
@tap="onTabClick(item.id)"
>
<text class="filter-tab-text">{{ item.name }}</text>
</view>
</view>
</template>
</nut-tabs>
</view>
<!-- 可滚动列表区域(支持触底加载更多) -->
<scroll-view
v-if="listVisible"
:key="listRenderKey"
:scroll-y="true"
class="flex-1 min-h-0 w-full"
style="height: 100%;"
@scrolltolower="onScrollToLower"
>
<view class="px-[24rpx] py-[24rpx] pb-[calc(env(safe-area-inset-bottom))] box-border w-full">
<!-- 加载状态 -->
<view v-if="loading && currentList.length === 0" class="flex items-center justify-center py-[60rpx]">
<view class="loading-spinner"></view>
<text class="ml-[16rpx] text-[#9CA3AF] text-[28rpx]">加载中...</text>
</view>
<view v-else class="flex flex-col gap-[24rpx] w-full box-border">
<!-- Plan List -->
<view
v-for="(item, index) in currentList"
:key="item.id"
class="bg-white rounded-[24rpx] p-[24rpx] shadow-sm plan-item w-full box-border"
:style="{ animationDelay: `${index * 50}ms` }"
>
<!-- Header -->
<view class="flex justify-between items-start mb-[16rpx]">
<view class="flex-1">
<view class="text-[30rpx] font-bold text-gray-900 leading-normal mb-[8rpx]">{{ item.title }}</view>
<view class="flex items-center gap-[12rpx]">
<view class="bg-blue-50 text-blue-600 text-[22rpx] px-[12rpx] py-[4rpx] rounded-[8rpx]" v-if="item.tag">
{{ item.tag }}
</view>
</view>
</view>
<view class="ml-[24rpx]">
<!-- Status Badge or Icon could go here -->
</view>
</view>
<!-- Info -->
<view class="flex justify-between items-center text-gray-500 text-[24rpx] mb-[24rpx]">
<text>{{ item.client }}</text>
<text>{{ item.date }}</text>
</view>
<!-- Divider -->
<view class="h-[1rpx] bg-gray-100 my-[20rpx]"></view>
<!-- Actions -->
<ListItemActions
:viewable="true"
:deletable="true"
@view="onView(item)"
@delete="onDelete(item)"
/>
</view>
<!-- 空状态 -->
<view v-if="currentList.length === 0 && !loading && !loadingMore">
<nut-empty description="暂无相关计划书" image="empty" />
</view>
<!-- 加载更多提示 -->
<view v-if="currentList.length > 0" class="flex items-center justify-center py-[40rpx]">
<view v-if="loadingMore" class="flex items-center">
<view class="loading-spinner-small"></view>
<text class="ml-[12rpx] text-[#9CA3AF] text-[24rpx]">加载中...</text>
</view>
<view v-else-if="!hasMore" class="text-[#9CA3AF] text-[24rpx]">
没有更多了
</view>
</view>
</view>
</view>
</scroll-view>
<!-- TabBar -->
<!-- <TabBar current="" /> -->
</view>
</template>
<script setup>
import { ref, nextTick } from 'vue'
import Taro, { useLoad, useReachBottom } from '@tarojs/taro'
import { useFileOperation } from '@/composables/useFileOperation'
import { listAPI } from '@/api/plan'
import NavHeader from '@/components/navigation/NavHeader.vue'
import ListItemActions from '@/components/list/ListItemActions/index.vue'
import SearchBar from '@/components/forms/SearchBar.vue'
const { viewFile } = useFileOperation()
const searchValue = ref('')
const activeTabId = ref('')
const listVisible = ref(true)
const listRenderKey = ref(0)
const loading = ref(false)
const loadingMore = ref(false)
const hasMore = ref(true)
const currentList = ref([])
const currentPage = ref(0) // 当前页码(从0开始)
const pageSize = 20 // 每页数量
/**
* Tab 数据源
* @description 包含分类信息和对应的计划书列表
*/
const tabsData = ref([
{ id: '', name: '全部', list: [] },
{ id: 'processing', name: '生成中', list: [] },
{ id: 'generated', name: '已生成', list: [] },
])
/**
* 从 API 数据转换为组件数据格式
* @description 将 API 返回的数据结构转换为组件使用的格式
* @param {Object} apiItem - API 返回的计划书对象
* @returns {Object} 组件使用的计划书对象
*/
const transformApiItem = (apiItem) => {
// 获取第一个文件(如果有)
const firstFile = apiItem.proposal_files && apiItem.proposal_files[0]
return {
id: apiItem.id,
title: apiItem.product_name || '未命名计划书',
client: `客户:${apiItem.customer_name || '未知'}`,
date: apiItem.created_time || '',
tag: apiItem.categories && apiItem.categories.length > 0 ? apiItem.categories[0] : '',
status: apiItem.order_status === 'processing' ? 'processing' : 'generated',
fileName: firstFile?.file_name || '',
downloadUrl: firstFile?.file_url || '',
// 保存完整的原始数据
_raw: apiItem
}
}
/**
* 加载计划书列表(调用真实 API)
* @param {number} page - 页码(从1开始,API 要求)
* @param {number} limit - 每页数量
* @param {boolean} isLoadMore - 是否为加载更多
*/
const fetchPlanList = async (page = 1, limit = pageSize, isLoadMore = false) => {
try {
// 如果是加载更多,使用 loadingMore 状态,否则使用 loading 状态
if (isLoadMore) {
loadingMore.value = true
} else {
loading.value = true
}
// 构建请求参数
const params = {
page: page,
limit: limit
}
// 根据 activeTabId 添加状态过滤
if (activeTabId.value !== '') {
params.order_status = activeTabId.value
}
// 搜索关键字
if (searchValue.value) {
params.keyword = searchValue.value
}
// 调用 API
const res = await listAPI(params)
if (res.code === 1 && res.data) {
const apiList = res.data.list || []
const transformedList = apiList.map(transformApiItem)
if (isLoadMore) {
// 加载更多:追加数据
currentList.value = [...currentList.value, ...transformedList]
} else {
// 首次加载或刷新:替换数据
currentList.value = transformedList
}
// 判断是否还有更多数据
// 如果返回的数据量少于请求的量,说明没有更多了
hasMore.value = transformedList.length >= limit
} else {
Taro.showToast({
title: res.msg || '加载失败',
icon: 'none',
duration: 2000
})
}
} catch (error) {
console.error('加载计划书列表失败:', error)
Taro.showToast({
title: '网络异常,请重试',
icon: 'none',
duration: 2000
})
} finally {
if (isLoadMore) {
loadingMore.value = false
} else {
loading.value = false
}
}
}
/**
* Tab 点击处理
*/
const onTabClick = (id) => {
if (activeTabId.value === id) {
return
}
activeTabId.value = id
listVisible.value = false
// 重置分页状态(从第 1 页开始)
currentPage.value = 1
hasMore.value = true
nextTick(() => {
listRenderKey.value += 1
listVisible.value = true
// 重新加载数据
fetchPlanList(1, pageSize, false)
})
}
/**
* 搜索处理
*/
const onSearch = () => {
// 重置分页状态(从第 1 页开始)
currentPage.value = 1
hasMore.value = true
listRenderKey.value += 1
// 重新加载数据
fetchPlanList(1, pageSize, false)
}
/**
* 清空搜索
*/
const onClear = () => {
console.log('[Plan Page] onClear 被调用')
// 清空搜索值
searchValue.value = ''
// 重置分页状态(从第 1 页开始)
currentPage.value = 1
hasMore.value = true
listRenderKey.value += 1
// 重新加载数据
fetchPlanList(1, pageSize, false)
console.log('[Plan Page] onClear 完成,列表已刷新')
}
/**
* 页面加载时初始化数据
*/
useLoad(() => {
// 加载第一页数据
fetchPlanList(1, pageSize, false)
})
/**
* 触底加载更多
* @description 使用防抖避免频繁触发
*/
let loadMoreTimer = null
const triggerLoadMore = () => {
if (loading.value || loadingMore.value || !hasMore.value) {
return
}
if (loadMoreTimer) {
clearTimeout(loadMoreTimer)
}
loadMoreTimer = setTimeout(async () => {
const newPage = currentPage.value + 1
currentPage.value = newPage
await fetchPlanList(newPage, pageSize, true)
}, 300)
}
const onScrollToLower = () => {
triggerLoadMore()
}
useReachBottom(() => {
triggerLoadMore()
})
/**
* 查看计划书文档
*
* @description 使用 useFileOperation 的 viewFile 功能查看 PDF 文档
* @param {Object} item - 计划书对象
*/
const onView = async (item) => {
// 检查是否已生成
if (item.status === 'processing') {
Taro.showToast({
title: '计划书生成中,请稍后',
icon: 'none'
})
return
}
// 使用 useFileOperation 查看文档
await viewFile({
downloadUrl: item.downloadUrl,
fileName: item.fileName
})
}
/**
* 删除计划书
* @description 注意:当前后端可能未提供删除接口,暂时只做前端提示
*/
const onDelete = (item) => {
Taro.showModal({
title: '提示',
content: '删除功能需要后端支持,是否继续?',
success: (res) => {
if (res.confirm) {
// TODO: 调用删除 API
// const res = await deletePlanAPI({ id: item.id })
// if (res.code === 1) {
// Taro.showToast({ title: '已删除', icon: 'success' })
// // 重新加载列表
// currentPage.value = 1
// fetchPlanList(1, pageSize, false)
// }
Taro.showToast({
title: '删除功能待实现',
icon: 'none',
duration: 2000
})
}
}
})
}
</script>
<style lang="less">
/* 列表项进入动画 */
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20rpx);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.plan-item {
animation: slideIn 0.5s cubic-bezier(0.2, 0.8, 0.2, 1) backwards;
}
/* 加载动画 */
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.loading-spinner {
width: 40rpx;
height: 40rpx;
border: 4rpx solid #E5E7EB;
border-top-color: #4CAF50;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
.loading-spinner-small {
width: 32rpx;
height: 32rpx;
border: 3rpx solid #E5E7EB;
border-top-color: #4CAF50;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
// FilterTabs 风格的标签栏
.filter-tabs-wrapper {
display: flex;
overflow-x: auto;
padding: 24rpx 24rpx;
gap: 24rpx;
transition: all 0.3s ease;
background-color: #fff;
width: 100%;
// 隐藏滚动条
&::-webkit-scrollbar {
display: none;
width: 0;
height: 0;
}
-ms-overflow-style: none;
scrollbar-width: none;
}
.filter-tab-item {
display: flex;
align-items: center;
justify-content: center;
padding: 0 32rpx;
border-radius: 9999rpx;
white-space: nowrap;
transition: all 0.3s ease;
flex-shrink: 0;
}
.filter-tab-active {
background-color: #2563EB; // 蓝色背景
color: #fff;
}
.filter-tab-inactive {
background-color: #F3F4F6; // 灰色背景
color: #6B7280;
}
.filter-tab-text {
font-size: 28rpx;
font-weight: 500;
}
// 覆盖 NutUI Tabs 默认样式,隐藏原有的头部和内容(因为我们使用自定义头部和外部列表)
:deep(.nut-tabs__titles) {
display: none;
}
:deep(.nut-tabs__content) {
display: none;
}
</style>