ActivityApplyHistoryPopup.vue
9.5 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
<template>
<van-popup
:show="show"
@update:show="(v) => emit('update:show', v)"
position="bottom"
:style="{ width: '100%', height: '100%' }"
:lock-scroll="true"
>
<div class="h-full w-full flex flex-col bg-[#F7F8FA]">
<div class="bg-white px-4 py-3 flex items-center justify-between shadow-sm">
<div class="flex items-center gap-3">
<!-- <van-icon name="arrow-left" class="text-[#333]" @click="emit('update:show', false)" /> -->
<div class="text-[#333] font-bold text-base">遗失的星球活动</div>
</div>
<van-icon name="cross" class="text-[#999]" @click="emit('update:show', false)" />
</div>
<div class="bg-white px-4 py-3 flex justify-between items-center sticky top-0 z-20 shadow-sm">
<span class="text-[#333] font-medium text-sm">已补充</span>
<div class="flex items-center text-[#666] text-sm">
<span>{{ apply_records.length }}条</span>
</div>
</div>
<div class="flex-1 overflow-y-auto px-4 py-4 space-y-4">
<div
v-for="item in apply_records"
:key="item.id"
class="bg-white rounded-xl py-4 pr-4 pl-0 shadow-sm relative overflow-hidden flex"
>
<div class="w-1 h-4 bg-[#0052D9] rounded-r-sm mt-1 shrink-0 self-start"></div>
<div class="pl-3 flex-1">
<h3 class="text-[#333] font-bold text-base leading-snug">
{{ item.name }}
</h3>
<div class="border-t border-dashed border-gray-200 my-3"></div>
<div class="flex justify-between items-center text-xs text-[#666] mb-2">
<span>活动年份: {{ item.year }}</span>
<span class="text-[#FF3B30] font-bold">实付金额: {{ format_paid_amount(item.paid_amount) }}</span>
</div>
<div class="flex justify-between items-center text-xs text-[#666] mb-4">
<span>活动地点: {{ item.location }}</span>
</div>
<div class="flex justify-end gap-2">
<van-button
size="small"
plain
color="#0052D9"
class="!rounded-full !px-4 !h-[28px] !text-xs"
@click="open_edit(item)"
>
修改
</van-button>
<van-button
size="small"
plain
color="#FF3B30"
class="!rounded-full !px-4 !h-[28px] !text-xs"
@click="confirm_delete(item)"
>
删除
</van-button>
</div>
</div>
</div>
<div v-if="!apply_records.length" class="py-10 text-center text-sm text-[#999]">
暂无申请记录
</div>
</div>
<div class="bg-white/60 backdrop-blur-md p-4 pb-8 shadow-[0_-2px_10px_rgba(0,0,0,0.05)]">
<van-button
block
color="#0052D9"
class="!rounded-lg !h-[44px] !text-base !font-bold"
@click="open_add"
>
新增
</van-button>
</div>
</div>
<van-popup
v-model:show="show_form_popup"
round
position="bottom"
:style="{ height: '70%' }"
class="flex flex-col"
>
<div class="p-4 flex-1 flex flex-col">
<div class="flex items-center justify-between mb-5">
<h3 class="text-center font-bold text-lg text-[#333]">{{ form_mode === 'add' ? '新增' : '编辑' }}</h3>
<van-icon name="cross" class="text-[#999]" @click="close_form_popup" />
</div>
<div class="bg-white rounded-xl overflow-hidden">
<van-field
v-model="form.name"
label="活动名称"
placeholder="例如: 2025年北京市海淀区活动"
class="!py-3"
type="textarea"
/>
<van-field
v-model="form.year"
label="活动年份"
placeholder="例如: 2025"
class="!py-3"
/>
<van-field
v-model="form.location"
label="活动地点"
placeholder="例如: 北京市海淀区"
class="!py-3"
/>
<van-field
v-model="form.paid_amount"
label="实付金额"
placeholder="例如: 1000"
class="!py-3"
/>
</div>
<div class="mt-auto pt-6 flex gap-3">
<van-button
block
plain
color="#0052D9"
class="!rounded-lg !h-[44px] !text-base !font-medium flex-1"
@click="close_form_popup"
>
取消
</van-button>
<van-button
block
color="#0052D9"
class="!rounded-lg !h-[44px] !text-base !font-bold flex-1"
@click="submit_form"
>
保存
</van-button>
</div>
</div>
</van-popup>
</van-popup>
</template>
<!--
* @component ActivityApplyHistoryPopup
* @description 活动申请历史记录弹窗,支持增删改查
-->
<script setup>
import { ref, onMounted } from 'vue'
import { showToast, showConfirmDialog } from 'vant'
import { getSupplementListAPI, supplementAddAPI, supplementEditAPI, supplementDelAPI } from '@/api/recall_users'
const props = defineProps({
/** 是否显示弹窗 */
show: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['update:show'])
/** @type {import('vue').Ref<Array>} 申请记录列表 */
const apply_records = ref([])
const show_form_popup = ref(false)
const form_mode = ref('add')
const editing_id = ref(null)
const form = ref({
name: '',
year: '',
location: '',
paid_amount: ''
})
/**
* @description 重置表单数据
*/
const reset_form = () => {
form.value = {
name: '',
year: '',
location: '',
paid_amount: ''
}
editing_id.value = null
}
/**
* @description 获取申请记录列表
* 调用 getSupplementListAPI 接口并映射数据字段
* @returns {Promise<void>}
*/
const fetch_records = async () => {
const res = await getSupplementListAPI()
if (res && res.code) {
const list = Array.isArray(res.data) ? res.data : []
apply_records.value = list.map((item) => ({
id: item.id,
name: item.title || '',
year: item.year || '',
location: item.address || '',
paid_amount: item.amount ?? ''
}))
} else if (res && res.msg) {
showToast(res.msg)
}
}
/**
* @description 打开新增表单
*/
const open_add = () => {
form_mode.value = 'add'
reset_form()
show_form_popup.value = true
}
/**
* @description 打开编辑表单
* @param {Object} item 记录对象
*/
const open_edit = (item) => {
form_mode.value = 'edit'
editing_id.value = item.id
form.value = {
name: item.name || '',
year: item.year || '',
location: item.location || '',
paid_amount: item.paid_amount || ''
}
show_form_popup.value = true
}
/**
* @description 关闭表单弹窗
*/
const close_form_popup = () => {
show_form_popup.value = false
}
/**
* @description 提交表单(新增或编辑)
* 1. 验证必填项
* 2. 构造参数
* 3. 根据模式调用 add 或 edit 接口
* 4. 成功后刷新列表
* @returns {Promise<void>}
*/
const submit_form = async () => {
if (!String(form.value.name || '').trim()) {
showToast('请输入活动名称')
return
}
if (!String(form.value.year || '').trim()) {
showToast('请输入活动年份')
return
}
if (!String(form.value.location || '').trim()) {
showToast('请输入活动地点')
return
}
if (!String(form.value.paid_amount || '').trim()) {
showToast('请输入实付金额')
return
}
const params = {
title: form.value.name,
year: form.value.year,
address: form.value.location,
amount: form.value.paid_amount
}
if (form_mode.value === 'add') {
const res = await supplementAddAPI(params)
if (res && res.code) {
showToast('新增成功')
show_form_popup.value = false
fetch_records()
} else if (res && res.msg) {
showToast(res.msg)
} else {
showToast('新增失败,请稍后重试')
}
} else {
const res = await supplementEditAPI({
id: editing_id.value,
...params
})
if (res && res.code) {
showToast('修改成功')
show_form_popup.value = false
fetch_records()
} else if (res && res.msg) {
showToast(res.msg)
} else {
showToast('修改失败,请稍后重试')
}
}
}
/**
* @description 确认删除记录
* @param {Object} item 记录对象
* @returns {Promise<void>}
*/
const confirm_delete = async (item) => {
try {
await showConfirmDialog({
title: '温馨提示',
message: `确定要删除“${item.name || '该记录'}”吗?`,
confirmButtonColor: '#FF3B30'
})
} catch (e) {
return
}
const res = await supplementDelAPI({ id: item.id })
if (res && res.code) {
showToast('删除成功')
fetch_records()
} else if (res && res.msg) {
showToast(res.msg)
} else {
showToast('删除失败,请稍后重试')
}
}
/**
* @description 格式化实付金额
* @param {string|number} val 金额
* @returns {string} 格式化后的金额字符串
*/
const format_paid_amount = (val) => {
const str = String(val ?? '').trim()
if (!str) return '¥0.00'
const pure = str.replace(/^¥/, '').trim()
const num = Number(pure)
if (Number.isFinite(num) && pure !== '') {
return `¥${num.toFixed(2)}`
}
return str.startsWith('¥') ? str : `¥${str}`
}
onMounted(() => {
fetch_records()
})
</script>