trainingPlanTable.vue
6.12 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
<template>
<div class="page-container">
<!-- 搜索栏 -->
<ContentWrap>
<el-form :inline="true" :model="queryParams" class="search-form">
<el-form-item label="计划名称">
<el-input
v-model="queryParams.name"
placeholder="请输入计划名称"
clearable
class="!w-220px"
@keyup.enter="handleSearch"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleSearch">
<Icon icon="ep:search" class="mr-5px" />搜索
</el-button>
<el-button @click="handleReset">
<Icon icon="ep:refresh" class="mr-5px" />重置
</el-button>
<el-button type="primary" @click="openAddDialog">
<Icon icon="ep:plus" class="mr-5px" />新增
</el-button>
</el-form-item>
</el-form>
</ContentWrap>
<!-- 表格区域 -->
<ContentWrap>
<el-table v-loading="loading" :data="tableData" stripe>
<el-table-column prop="name" label="计划名称" align="center" />
<el-table-column label="计划周期" align="center" :formatter="formatDurationWeeks" />
<el-table-column label="场景" align="center" :formatter="formatTrainingScene" />
<el-table-column label="训练频率" align="center" :formatter="formatFrequency" />
<el-table-column label="难度" align="center" :formatter="formatDifficulty" />
<el-table-column label="适合人群" align="center" :formatter="formatTargetPeople" />
<el-table-column prop="createTime" label="创建时间" align="center" :formatter="dateFormatter" />
<el-table-column prop="updateTime" label="更新时间" align="center" :formatter="dateFormatter" />
<el-table-column label="操作" align="center" width="150">
<template #default="scope">
<el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
<el-button link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<Pagination
:total="queryParams.total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</ContentWrap>
<!-- 弹窗 -->
<TrainingPlanDialog
v-model:visible="dialogVisible"
:form-data="formData"
:title="dialogTitle"
@submit="getList"
/>
</div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { trainingPlanApi } from '@/api/store/training/trainingPlan'
import TrainingPlanDialog from './trainingPlanDialog.vue'
import { dateFormatter } from '@/utils/formatTime'
// ==================== 常量映射 ====================
const DIFFICULTY_MAP = { 1: '初阶', 2: '中阶', 3: '高阶' }
const TARGET_PEOPLE_MAP = { 1: '不限', 2: '青少年', 3: '成年人', 4: '中年人', 5: '运动损伤' }
const TRAINING_SCENE_MAP = { 1: '不限', 2: '健身房', 3: '仅哑铃', 4: '仅哑铃 + 杠铃' }
// ==================== 查询参数 ====================
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
total: 0,
name: ''
})
const loading = ref(false)
const tableData = ref([])
// ==================== 弹窗状态 ====================
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formData = ref({
templateIds: [{ templateId: undefined, sortOrder: 1 }]
})
// ==================== 格式化函数 ====================
const formatDifficulty = (row) => DIFFICULTY_MAP[row.difficultyLevel] || '未知'
const formatTargetPeople = (row) => TARGET_PEOPLE_MAP[row.targetPeople] || '未知'
const formatTrainingScene = (row) => TRAINING_SCENE_MAP[row.trainingScene] || '未知'
const formatDurationWeeks = (row) => row.durationWeeks ? row.durationWeeks + '周' : '0周'
const formatFrequency = (row) => row.frequencyPerWeek ? row.frequencyPerWeek + '练/周' : '0练/周'
// ==================== 数据加载 ====================
const getList = async () => {
loading.value = true
try {
const res = await trainingPlanApi.getTrainingPlan({
pageNo: queryParams.pageNo + '',
pageSize: queryParams.pageSize + '',
name: queryParams.name
})
tableData.value = res.list || []
queryParams.total = res.total || 0
} catch (err) {
ElMessage.error('请求失败,请检查网络或接口')
} finally {
loading.value = false
}
}
// ==================== 搜索与重置 ====================
const handleSearch = () => {
queryParams.pageNo = 1
getList()
}
const handleReset = () => {
Object.assign(queryParams, { pageNo: 1, pageSize: 10, total: 0, name: '' })
getList()
}
// ==================== 新增 ====================
const openAddDialog = () => {
dialogTitle.value = '新增训练计划'
formData.value = {
id: undefined,
categoryId: 0,
name: '',
urlCover: '',
introduction: '',
durationWeeks: 1,
frequencyPerWeek: 1,
difficultyLevel: 1,
targetPeople: 1,
trainingScene: 1,
equipmentsSummary: '',
templateIds: [{ templateId: undefined, sortOrder: 1 }]
}
dialogVisible.value = true
}
// ==================== 编辑 ====================
const handleEdit = (row) => {
dialogTitle.value = '编辑训练计划'
formData.value = { ...formData.value, ...row }
dialogVisible.value = true
}
// ==================== 删除 ====================
const handleDelete = async (id) => {
try {
await ElMessageBox.confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
await trainingPlanApi.deleteTrainingPlan(id)
ElMessage.success('删除成功')
getList()
} catch (err) {
if (err !== 'cancel') {
ElMessage.error('删除失败')
}
}
}
// ==================== 初始化 ====================
onMounted(() => {
getList()
})
</script>
<style scoped>
.page-container {
padding: 20px;
min-height: 100vh;
background-color: #f5f7fa;
box-sizing: border-box;
}
.search-form :deep(.el-form-item) {
margin-bottom: 0;
}
</style>