CheckInList.vue
9.69 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
<template>
<!-- 列表主体 -->
<div :class="wrapper_class" :style="scroll_style">
<button
v-for="item in items"
:key="item.id"
class="CheckInListItem flex flex-col items-center p-2 rounded-lg border transition-colors bg-white/70 border-gray-100 hover:bg-white"
:class="{ 'is-active': selected_item?.id === item.id }"
@click="handle_select(item)"
>
<div class="Icon w-12 h-12 rounded-full flex items-center justify-center mb-1 transition-colors bg-gray-100"
:class="{ 'is-active': selected_item?.id === item.id }"
>
<van-icon v-if="item.task_type === 'checkin'" name="edit" size="1.5rem" :color="item.is_gray ? 'gray' : ''" />
<van-icon v-if="item.task_type === 'upload'" name="tosend" size="1.5rem" :color="item.is_gray ? 'gray' : ''" />
</div>
<span :class="['text-xs', item.is_gray ? 'text-gray-500' : '']">{{ item.name }}</span>
</button>
</div>
<!-- 提交按钮 -->
<div v-if="selected_item" class="mt-3">
<button
class="SubmitBtn mt-2 w-full bg-gradient-to-r from-green-500 to-green-600 text-white py-2 rounded-lg flex items-center justify-center"
@click="handle_submit"
:disabled="submitting"
>
<template v-if="submitting">
<div class="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2"></div>
提交中...
</template>
<template v-else>提交打卡</template>
</button>
</div>
<!-- 二级弹框(课程列表,使用mock数据) -->
<van-popup
:show="inner_popup_show"
@update:show="(v) => inner_popup_show = v"
round
position="bottom"
teleport="body"
:close-on-click-overlay="false"
:style="{ minHeight: '40%', maxHeight: '80%', width: '100%' }"
>
<div class="p-4">
<div class="flex justify-between items-center mb-3">
<h3 class="font-medium">课程列表</h3>
<van-icon name="cross" @click="close_inner_popup" />
</div>
<div class="grid grid-cols-2 gap-4">
<div v-for="course in inner_courses" :key="course.id" class="rounded-xl overflow-hidden bg-white/80">
<div class="h-24 relative">
<img
:src="format_cdn_image(course.imageUrl)"
:alt="course.title"
class="w-full h-full object-cover"
/>
<div v-if="course.isPurchased" class="absolute top-0 left-0 bg-amber-500 text-white text-xs px-2 py-1 rounded-br-lg font-medium" style="background-color: rgba(249, 115, 22, 0.85)">
已购
</div>
</div>
<div class="p-2">
<h4 class="text-sm font-medium line-clamp-1">{{ course.title }}</h4>
<p class="text-xs text-gray-500 line-clamp-1">{{ course.subtitle }}</p>
<div class="flex justify-between items-center mt-2">
<span class="text-xs text-green-600">¥{{ course.price }}</span>
<button class="text-xs px-2 py-1 bg-green-600 text-white rounded" @click="select_inner_course(course)">选择</button>
</div>
</div>
</div>
</div>
</div>
</van-popup>
</template>
<script setup>
import { ref, computed, inject } from 'vue'
import { useRouter } from 'vue-router'
import { checkinTaskAPI } from '@/api/checkin'
import { showToast } from 'vant'
import { courses as mock_courses } from '@/utils/mockData'
/**
* @typedef {Object} CheckInItem
* @property {number|string} id - 任务ID。
* @property {string} name - 任务名称。
* @property {string} task_type - 任务类型,`checkin` 或 `upload`。
* @property {boolean} [is_gray] - 是否置灰,表示今日已完成。
*/
/**
* @function props
* @description 组件接收的属性定义。
*/
const props = defineProps({
items: { type: Array, default: () => [] },
dense: { type: Boolean, default: false },
scroll: { type: Boolean, default: false },
plain: { type: Boolean, default: false },
})
/**
* @function emits
* @description 组件对外抛出的事件。
*/
const emit = defineEmits(['submit-success'])
const router = useRouter()
const selected_item = ref(null)
const submitting = ref(false)
// 父弹框联动(仅弹框模式下有效)
const parent_popup = inject('parent_popup_control', null)
// 二级弹框与数据
const inner_popup_show = ref(false)
const inner_courses = ref([])
/**
* @function wrapper_class
* @description 计算列表容器类名。
* @returns {string[]}
*/
const wrapper_class = computed(() => [
'CheckInListWrapper',
props.dense ? 'grid grid-cols-2 gap-2 py-2' : 'grid grid-cols-2 gap-4 py-2',
])
/**
* @function scroll_style
* @description 当 `scroll` 为真时启用滚动区域样式。
* @returns {Object}
*/
const scroll_style = computed(() => {
if (!props.scroll) return {}
return { maxHeight: '13rem', overflow: 'auto' }
})
/**
* @function handle_select
* @description 处理打卡类型选择:已完成提示;上传型跳转;否则选中。
* @param {CheckInItem} item - 当前点击的打卡项。
* @returns {void}
*/
const handle_select = (item) => {
// TODO: 想要判断是否有二级菜单
const has_submenu = item.children && item.children.length > 0;
// 如果有二级菜单需要特殊处理
if (has_submenu) {
// 不同模式下弹框的显示逻辑是不一样的
if (props.plain) {
// 普通模式:直接弹出本组件的popup
open_inner_popup()
} else {
// 弹框模式:先隐藏父级弹框,再弹出本组件的popup,关闭后重新打开父级弹框
if (parent_popup && typeof parent_popup.hideParent === 'function') {
parent_popup.hideParent()
}
// 略微延迟以确保父弹框状态切换完成
setTimeout(() => open_inner_popup(), 50)
}
return
}
// 点击已完成打卡项提示
if (item.is_gray && item.task_type === 'checkin') {
showToast('您已经完成了今天的打卡')
return
}
// 点击上传项跳转上传页
if (item.task_type === 'upload') {
router.push({
path: '/checkin/index',
query: { id: item.id },
})
return
}
selected_item.value = item
}
/**
* @function handle_submit
* @description 提交打卡调用接口,成功后抛出事件并复位。
* @returns {Promise<void>}
*/
const handle_submit = async () => {
if (!selected_item.value) {
showToast('请选择打卡项目')
return
}
submitting.value = true
try {
const { code } = await checkinTaskAPI({ task_id: selected_item.value.id })
if (code) {
emit('submit-success')
showToast('打卡成功')
selected_item.value = null
}
} catch (e) {
// showToast('打卡失败,请重试')
} finally {
submitting.value = false
}
}
/**
* @function open_inner_popup
* @description 打开二级弹框并填充课程列表(mock 数据)。
* @returns {void}
*/
const open_inner_popup = () => {
inner_courses.value = build_course_list()
inner_popup_show.value = true
}
/**
* @function close_inner_popup
* @description 关闭二级弹框;若处于弹框模式则重新打开父级弹框。
* @returns {void}
*/
const close_inner_popup = () => {
inner_popup_show.value = false
if (!props.plain && parent_popup && typeof parent_popup.reopenParent === 'function') {
// 略微延迟,避免与二级弹框关闭动画冲突
setTimeout(() => parent_popup.reopenParent(), 150)
}
}
/**
* @function build_course_list
* @description 构造课程列表(来源于 mock 数据)。
* @returns {Array}
*/
const build_course_list = () => {
return (mock_courses || []).map(c => ({
id: c.id,
title: c.title,
subtitle: c.subtitle,
imageUrl: c.imageUrl,
price: c.price,
isPurchased: !!c.isPurchased
}))
}
/**
* @function select_inner_course
* @description 选择二级弹框中的课程(占位行为:提示并关闭二级弹框)。
* @param {Object} course - 课程对象。
* @returns {void}
*/
const select_inner_course = (course) => {
showToast(`已选择课程:${course.title}`)
close_inner_popup()
}
/**
* @function format_cdn_image
* @description 若图片来自 cdn.ipadbiz.cn,则追加压缩参数;否则原样返回。
* @param {string} url - 图片地址。
* @returns {string}
*/
const format_cdn_image = (url) => {
if (!url) return ''
const host = 'cdn.ipadbiz.cn'
if (url.includes(host)) {
return `${url}?imageMogr2/thumbnail/200x/strip/quality/70`
}
return url
}
</script>
<style lang="less" scoped>
.CheckInListWrapper {
// 列表项样式
.CheckInListItem {
// 选中态样式
&.is-active {
border-color: #bbf7d0; // 绿色边框
background-color: rgba(16, 185, 129, 0.1); // 轻微绿色背景
}
// 图标样式
.Icon {
&.is-active {
background-color: #10b981; // 绿色激活背景
color: #ffffff; // 白色图标
}
}
}
// 提交按钮样式
.SubmitBtn {
&:disabled {
opacity: 0.7; // 禁用态透明度
}
}
}
</style>