muscles.vue
12.2 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
<!-- 原型设计图的肌肉页面,但是接口文档没有肌肉的接口,选择接口文档的细分锻炼部位 -->
<template>
<div class="page-container">
<!-- 搜索与新增区域 -->
<ContentWrap>
<div class="search-bar">
<el-input v-model="searchForm.name" placeholder="肌肉名称" clearable @keyup.enter="handleSearch" />
<el-button @click="handleSearch">
<el-icon>
<Search />
</el-icon>搜索
</el-button>
<el-button @click="handleReset">
<el-icon>
<Refresh />
</el-icon>重置
</el-button>
<el-button @click="handleAdd">
<el-icon>
<Plus />
</el-icon>新增
</el-button>
</div>
</ContentWrap>
<ContentWrap>
<!-- 表格区域 -->
<el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
<!-- ID列 -->
<el-table-column prop="id" label="ID" sortable align="center" />
<!-- 肌肉名称列(对应接口的name字段) -->
<el-table-column prop="name" label="肌肉名称" align="center" />
<!-- 关联大类ID列 -->
<!-- <el-table-column prop="categoryId" label="关联大类ID" align="center" /> -->
<el-table-column prop="createTime" label="创建时间" align="center" :formatter="dateFormatter" />
<el-table-column prop="updateTime" label="更新时间" align="center" min-width="100" :formatter="dateFormatter" />
<!-- 操作列 -->
<el-table-column label="操作" align="center">
<template #default="scope">
<el-button icon="Edit" size="small" link style="color: #409EFF;" @click="handleEdit(scope.row)">
编辑
</el-button>
<el-button icon="Delete" size="small" link style="color: #F56C6C;" @click="handleDelete(scope.row)">
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 空数据提示 -->
<div v-if="!loading && tableData.length === 0" style=" padding: 50px 0;text-align: center;">
<el-empty description="暂无相关数据" />
</div>
<!-- 分页区域:-->
<div class="pagination-wrapper" style="margin-top: 20px; text-align: right;">
<el-pagination v-model:current-page="pagination.pageNo" v-model:page-size="pagination.pageSize"
:total="pagination.total" :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next, jumper"
style="text-align: left;" @size-change="handleSizeChange" @current-change="handleCurrentChange"
:disabled="pagination.total === 0" />
</div>
</ContentWrap>
<ContentWrap>
<!-- 编辑弹窗 -->
<el-dialog v-model="editDialogVisible" title="编辑肌肉信息" @close="resetEditForm">
<el-form ref="editFormRef" :rules="editRules" :model="editForm" label-width="100px">
<el-form-item label="肌肉名称" prop="name">
<el-input v-model="editForm.name" placeholder="请输入肌肉名称" />
</el-form-item>
<!-- <el-form-item label="关联大类ID" prop="categoryId">
<el-input v-model.number="editForm.categoryId" type="number" placeholder="请输入关联大类ID" />
</el-form-item> -->
</el-form>
<template #footer>
<el-button @click="editDialogVisible = false">取消</el-button>
<el-button type="primary" @click="submitEditForm">确定</el-button>
</template>
</el-dialog>
<!-- 新增弹窗 -->
<el-dialog v-model="addDialogVisible" title="新增肌肉信息" @close="resetAddForm">
<el-form :model="addForm" :rules="addRules" ref="addFormRef" label-width="100px">
<el-form-item label="肌肉名称" prop="name">
<el-input v-model="addForm.name" placeholder="请输入肌肉名称" />
</el-form-item>
<!-- <el-form-item label="关联大类ID" prop="categoryId">
<el-input v-model.number="addForm.categoryId" type="number" placeholder="请输入关联大类ID" />
</el-form-item> -->
</el-form>
<template #footer>
<el-button @click="addDialogVisible = false">取消</el-button>
<el-button type="primary" @click="submitAddForm">确定</el-button>
</template>
</el-dialog>
</ContentWrap>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage, ElMessageBox, ElEmpty, ElForm, ElFormItem, ElInput, ElDialog } from 'element-plus'
// 导入你提供的接口文件
import { MusclesApi, type MusclesVO } from '@/api/store/training/muscle'
import { dateFormatter } from '@/utils/formatTime'
// 表格数据(对应接口返回的列表)
const tableData = ref<MusclesVO[]>([])
// 加载中状态
const loading = ref(false)
// 搜索表单:保持原有结构
const searchForm = reactive({
name: ''
})
// 分页参数
const pagination = reactive({
pageNo: 1, // 替换原currentPage
pageSize: 10, // 替换原pageSize
total: 0 // 替换原total
})
// ---------------------- 编辑功能相关变量 ----------------------
// 编辑弹窗显隐
const editDialogVisible = ref(false)
// 编辑表单Ref(用于表单验证)
const editFormRef = ref<InstanceType<typeof ElForm>>()
// 编辑表单数据
const editForm = reactive<MusclesVO>({
id: 0,
categoryId: 0,
name: ''
})
// 编辑表单校验规则
const editRules = reactive({
name: [
{ required: true, message: '请输入肌肉名称', trigger: 'blur' },
{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
],
categoryId: [
{ required: true, message: '请输入关联大类ID', trigger: 'blur' },
{ type: 'number', min: 1, message: '关联大类ID必须为正整数', trigger: 'blur' }
]
})
// ---------------------- 新增功能相关变量 ----------------------
// 新增弹窗显隐
const addDialogVisible = ref(false)
// 新增表单Ref(用于表单验证)
const addFormRef = ref<InstanceType<typeof ElForm>>()
// 新增表单数据(id无需填写,由后端生成)
const addForm = reactive<Omit<MusclesVO, 'id'>>({
categoryId: 0,
name: ''
})
// 新增表单校验规则(和编辑规则一致)
const addRules = reactive({
name: [
{ required: true, message: '请输入肌肉名称', trigger: 'blur' },
{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
],
categoryId: [
{ required: true, message: '请输入关联大类ID', trigger: 'blur' },
{ type: 'number', min: 1, message: '关联大类ID必须为正整数', trigger: 'blur' }
]
})
// 2. 核心方法:获取表格数据
const getMusclesList = async () => {
try {
loading.value = true
// 构造请求参数:参数转字符串传递
const params = {
pageNo: pagination.pageNo + '', // 转字符串
pageSize: pagination.pageSize + '', // 转字符串
name: searchForm.name // 搜索的肌肉名称
}
// 调用接口获取数据
const res = await MusclesApi.getMusclesPage(params)
// 兼容不同的接口返回格式(优先取res.data,没有则取res本身)
const result = res.data || res
tableData.value = result.list || []
pagination.total = result.total || 0
console.log('肌肉分页接口返回数据:', res)
// 分页边界处理:如果当前页大于总页数,自动切到最后一页
const totalPages = Math.ceil(pagination.total / pagination.pageSize)
if (pagination.pageNo > totalPages && totalPages > 0) {
pagination.pageNo = totalPages
getMusclesList() // 重新请求最后一页数据
}
} catch (error) {
ElMessage.error('获取肌肉列表失败,请重试')
console.error('获取列表失败:', error)
tableData.value = []
pagination.total = 0
} finally {
loading.value = false
}
}
// 3. 页面挂载时自动加载数据
onMounted(() => {
getMusclesList()
})
// 监听总条数变化,处理页码越界问题
watch([() => pagination.total, () => pagination.pageSize], () => {
const totalPages = Math.ceil(pagination.total / pagination.pageSize)
if (pagination.pageNo > totalPages && totalPages > 0) {
pagination.pageNo = totalPages
}
})
// 4. 事件处理函数
// 搜索:保持原有逻辑,仅调整分页参数名
const handleSearch = () => {
pagination.pageNo = 1 // 搜索时重置页码为1
getMusclesList()
}
// 重置:保持原有逻辑,仅调整分页参数名
const handleReset = () => {
searchForm.name = ''
pagination.pageNo = 1
pagination.pageSize = 10
getMusclesList()
}
// ---------------------- 新增功能相关方法 ----------------------
// 打开新增弹窗
const handleAdd = () => {
resetAddForm()
addDialogVisible.value = true
}
// 重置新增表单
const resetAddForm = () => {
addForm.name = ''
addForm.categoryId = 0
// 清空表单验证状态
if (addFormRef.value) {
addFormRef.value.clearValidate()
}
}
// 提交新增表单
const submitAddForm = async () => {
if (!addFormRef.value) return
try {
// 表单验证
await addFormRef.value.validate()
// 调用新增接口(无需传id,由后端生成)
await MusclesApi.createMuscles({
...addForm,
id: 0 // 占位,后端会自动生成
})
ElMessage.success('新增成功!')
// 关闭弹窗
addDialogVisible.value = false
// 重新加载列表数据
getMusclesList()
} catch (error) {
if (error !== 'cancel') { // 排除表单验证取消的情况
ElMessage.error('新增失败,请重试')
console.error('新增失败:', error)
}
}
}
// ---------------------- 编辑功能相关方法 ----------------------
// 打开编辑弹窗并回显数据
const handleEdit = async (row: MusclesVO) => {
try {
// 清空编辑表单
resetEditForm()
// 打开弹窗
editDialogVisible.value = true
// 调用接口获取详情
const res = await MusclesApi.getMuscles(row.id)
const detail = res.data || res
// 回显数据到编辑表单
editForm.id = detail.id
editForm.name = detail.name
editForm.categoryId = detail.categoryId
} catch (error) {
ElMessage.error('获取肌肉详情失败,请重试')
console.error('获取详情失败:', error)
}
}
// 重置编辑表单
const resetEditForm = () => {
editForm.id = 0
editForm.name = ''
editForm.categoryId = 0
// 清空表单验证状态
if (editFormRef.value) {
editFormRef.value.clearValidate()
}
}
// 提交编辑表单
const submitEditForm = async () => {
if (!editFormRef.value) return
try {
// 表单验证
await editFormRef.value.validate()
// 调用更新接口
await MusclesApi.updateMuscles({ ...editForm })
ElMessage.success('编辑成功!')
// 关闭弹窗
editDialogVisible.value = false
// 重新加载列表数据
getMusclesList()
} catch (error) {
if (error !== 'cancel') { // 排除表单验证取消的情况
ElMessage.error('编辑失败,请重试')
console.error('编辑失败:', error)
}
}
}
// 删除
const handleDelete = async (row: MusclesVO) => {
try {
// 确认删除
await ElMessageBox.confirm(
'此操作将永久删除该肌肉信息,是否继续?',
'温馨提示',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}
)
// 调用删除接口
await MusclesApi.deleteMuscles(row.id)
ElMessage.success('删除成功!')
// 重新获取列表
getMusclesList()
} catch (error) {
if (error !== 'cancel') { // 排除用户取消的情况
ElMessage.error('删除失败,请重试')
console.error('删除失败:', error)
}
}
}
// ---------------------- 分页相关方法 ----------------
// 分页:每页条数改变
const handleSizeChange = (val: number) => {
pagination.pageSize = val
getMusclesList()
}
// 分页:当前页码改变
const handleCurrentChange = (val: number) => {
pagination.pageNo = val
getMusclesList()
}
</script>
<style scoped>
.page-container {
/* min-height: 100vh;
background: #fff; */
}
/* 新增分页样式 */
.pagination-wrapper {
display: flex;
padding: 12px 16px 0;
justify-content: flex-end;
}
</style>