Codex Assistant
2 天以前 ba94ceae1315174798ae1967ef62268c6d16cd5b
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
<template>
  <div class="activity-page">
    <div class="page-card">
      <!-- 页面头部 -->
      <div class="page-header">
        <div class="title-section">
          <h1 class="page-title">比赛信息</h1>
          <p class="page-subtitle">管理您的精彩比赛</p>
        </div>
      </div>
 
      <!-- 搜索工具栏 -->
      <div class="search-toolbar">
        <el-input
          v-model="searchForm.name"
          placeholder="请输入比赛名称"
          style="width: 200px"
          clearable
          @keyup.enter="handleSearch"
          @clear="handleClear"
        />
        <el-select
          v-model="searchForm.state"
          placeholder="比赛状态"
          style="width: 160px"
          clearable
          @change="handleStateChange"
        >
          <el-option
            v-for="option in stateOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
        <el-button type="primary" @click="handleSearch">
          <el-icon><Search /></el-icon>
          查询
        </el-button>
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>
          新增比赛
        </el-button>
      </div>
 
      <!-- 比赛列表 -->
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="name" label="比赛名称" min-width="200" />
        <el-table-column prop="playerCount" label="报名人数" width="120" align="center" />
        <el-table-column prop="matchTime" label="比赛时间" width="180" />
        <el-table-column prop="signupDeadline" label="报名截止时间" width="180" />
        <el-table-column prop="stateName" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.stateName)">{{ row.stateName }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center">
          <template #default="{ row }">
            <div class="table-actions">
              <el-button
                text
                :icon="Edit"
                size="small"
                @click="handleEdit(row)"
                class="action-btn edit-btn"
                title="编辑"
              />
              <el-button
                text
                :icon="Delete"
                size="small"
                @click="handleDelete(row)"
                class="action-btn delete-btn"
                title="删除"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
 
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import { reactive, ref, onMounted, onActivated, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { getActivities, updateActivityState } from '@/api/activity'
import { Search, Plus, Edit, Delete } from '@element-plus/icons-vue'
 
console.log('=== activity-list.vue 组件开始加载 ===')
 
const loading = ref(false)
const router = useRouter()
 
// 搜索表单
const searchForm = reactive({
  name: '',
  state: ''
})
 
const stateOptions = [
  { label: '未发布', value: 0 },
  { label: '发布', value: 1 },
  { label: '关闭', value: 2 }
]
 
// 分页信息
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})
 
// 表格数据
const tableData = ref([])
 
// 调试用途:监听表格数据变化
watch(
  tableData,
  newVal => {
    console.log('=== tableData 发生变化 ===', newVal)
    console.log('tableData.value.length:', newVal.length)
  },
  { deep: true }
)
 
// 获取状态标签类型
const getStatusType = (status: string) => {
  const typeMap: Record<string, string> = {
    已发布: 'success',
    发布: 'success',
    进行中: 'success',
    报名中: 'warning',
    待开始: 'info',
    已结束: 'info',
    关闭: 'danger'
  }
  return typeMap[status] || 'info'
}
 
// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadData()
}
 
const handleStateChange = () => {
  pagination.page = 1
  loadData()
}
 
// 清空搜索
const handleClear = () => {
  searchForm.name = ''
  loadData()
}
 
// 新增比赛
const handleAdd = () => {
  router.push('/activity/new')
}
 
// 编辑比赛
const handleEdit = (row: any) => {
  router.push(`/activity/edit/${row.id}`)
}
 
// 删除比赛
const handleDelete = async (row: any) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除比赛 “${row.name}” 吗?`,
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
 
    await updateActivityState(row.id, 2)
    ElMessage.success('删除成功')
    loadData()
  } catch {
    // 用户取消操作
  }
}
 
// 分页大小改变
const handleSizeChange = (size: number) => {
  pagination.size = size
  loadData()
}
 
// 当前页改变
const handleCurrentChange = (page: number) => {
  pagination.page = page
  loadData()
}
 
// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    const keyword = (searchForm.name || '').trim()
    const data = await getActivities(
      pagination.page - 1,
      pagination.size,
      keyword,
      searchForm.state
    )
 
    // 数据映射:将 API 返回字段转换为表格需要的字段
    const mappedData = (data?.content || []).map(item => ({
      ...item,
      playerCount: item.playerCount || 0,
      stateName: item.stateName || ''
    }))
 
    tableData.value = mappedData
    pagination.total = data?.totalElements || 0
  } catch (e: any) {
    console.error('加载数据失败:', e)
    ElMessage.error(e?.message || '加载比赛列表失败')
  } finally {
    loading.value = false
  }
}
 
onMounted(() => {
  loadData()
})
 
// 页面激活时重新加载数据(解决从新增/编辑返回后列表不同步的问题)
onActivated(() => {
  loadData()
})
</script>
 
<style scoped>
.activity-page {
  padding: 20px;
}
 
.page-card {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
 
/* 页面头部样式 */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
  gap: 20px;
}
 
.title-section {
  flex: 1;
}
 
.page-title {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #1a1a1a;
  line-height: 1.2;
}
 
.page-subtitle {
  margin: 0;
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}
 
/* 工具栏样式 */
.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-shrink: 0;
}
 
/* 搜索工具栏样式 */
.search-toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 20px;
}
 
/* 搜索按钮 */
.search-icon {
  color: #999;
}
 
.search-button {
  margin-right: 4px;
}
 
/* 表格操作按钮样式 */
.table-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}
 
.action-btn {
  padding: 4px;
  border: none;
  background: transparent !important;
  transition: all 0.2s ease;
}
 
.edit-btn {
  color: #409eff;
}
 
.edit-btn:hover {
  color: #337ecc;
  transform: scale(1.2);
  background: rgba(64, 158, 255, 0.1) !important;
}
 
.delete-btn {
  color: #f56c6c;
}
 
.delete-btn:hover {
  color: #dd6161;
  transform: scale(1.2);
  background: rgba(245, 108, 108, 0.1) !important;
}
 
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
 
/* 响应式适配 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }
 
  .toolbar {
    flex-wrap: wrap;
    gap: 8px;
  }
 
  .toolbar .el-input {
    width: 100% !important;
    max-width: 280px;
  }
}
</style>