import { Hono } from 'hono' import type { Variables, WithAuth } from "./middlewares.ts"; export function createXunlianCodesRoutes(withAuth: WithAuth) { const xunlianCodesRoutes = new Hono<{ Variables: Variables }>() // 获取训练码列表 xunlianCodesRoutes.get('/', withAuth, async (c) => { try { const apiClient = c.get('apiClient') const page = Number(c.req.query('page')) || 1 const pageSize = Number(c.req.query('pageSize')) || 10 const offset = (page - 1) * pageSize const search = c.req.query('search') || '' let query = apiClient.database.table('xunlian_codes') .orderBy('id', 'desc') if (search) { query = query.where('code', 'like', `%${search}%`) } const total = await query.clone().count() const codes = await query.select('id', 'code', 'status', 'created_at', 'updated_at') .limit(pageSize).offset(offset) return c.json({ data: codes, pagination: { total: Number(total), current: page, pageSize } }) } catch (error) { console.error('获取训练码列表失败:', error) return c.json({ error: '获取训练码列表失败' }, 500) } }) // 获取单个训练码详情 xunlianCodesRoutes.get('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') const code = await apiClient.database.table('xunlian_codes') .where('id', id) .select('id', 'code', 'status', 'created_at', 'updated_at') .first() if (!code) { return c.json({ error: '训练码不存在' }, 404) } return c.json({ data: code, message: '获取训练码详情成功' }) } catch (error) { console.error('获取训练码详情失败:', error) return c.json({ error: '获取训练码详情失败' }, 500) } }) // 创建训练码 xunlianCodesRoutes.post('/', withAuth, async (c) => { try { const apiClient = c.get('apiClient') const body = await c.req.json() // 验证必填字段 const { code } = body if (!code) { return c.json({ error: '缺少训练码' }, 400) } // 检查训练码是否已存在 const existingCode = await apiClient.database.table('xunlian_codes') .where('code', code) .first() if (existingCode) { return c.json({ error: '训练码已存在' }, 400) } // 创建训练码 const [id] = await apiClient.database.table('xunlian_codes').insert({ code, status: 'active', // 默认激活状态 created_at: new Date(), updated_at: new Date() }) const newCode = await apiClient.database.table('xunlian_codes') .where('id', id) .select('id', 'code', 'status', 'created_at') .first() return c.json({ data: newCode, message: '创建训练码成功' }) } catch (error) { console.error('创建训练码失败:', error) return c.json({ error: '创建训练码失败' }, 500) } }) // 更新训练码 xunlianCodesRoutes.put('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') const body = await c.req.json() // 验证必填字段 const { code } = body if (!code) { return c.json({ error: '缺少训练码' }, 400) } // 检查训练码是否存在 const existingCode = await apiClient.database.table('xunlian_codes') .where('id', id) .first() if (!existingCode) { return c.json({ error: '训练码不存在' }, 404) } // 如果修改了训练码,检查新训练码是否已被使用 if (code !== existingCode.code) { const codeWithSameName = await apiClient.database.table('xunlian_codes') .where('code', code) .whereNot('id', id.toString()) .first() if (codeWithSameName) { return c.json({ error: '训练码已存在' }, 400) } } // 更新训练码信息 const updateData = { code, updated_at: new Date() } await apiClient.database.table('xunlian_codes') .where('id', id) .update(updateData) const updatedCode = await apiClient.database.table('xunlian_codes') .where('id', id) .select('id', 'code', 'status', 'created_at') .first() return c.json({ data: updatedCode, message: '更新训练码成功' }) } catch (error) { console.error('更新训练码失败:', error) return c.json({ error: '更新训练码失败' }, 500) } }) // 删除训练码 xunlianCodesRoutes.delete('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') // 检查训练码是否存在 const existingCode = await apiClient.database.table('xunlian_codes') .where('id', id) .first() if (!existingCode) { return c.json({ error: '训练码不存在' }, 404) } // 删除训练码 await apiClient.database.table('xunlian_codes') .where('id', id) .delete() return c.json({ message: '删除训练码成功', id }) } catch (error) { console.error('删除训练码失败:', error) return c.json({ error: '删除训练码失败' }, 500) } }) // 批量生成训练码 xunlianCodesRoutes.post('/batch-generate', withAuth, async (c) => { try { const apiClient = c.get('apiClient') const body = await c.req.json() const { count, prefix } = body if (!count || count <= 0) { return c.json({ error: '无效的生成数量' }, 400) } const codes = [] const now = new Date() for (let i = 0; i < count; i++) { const code = prefix ? `${prefix}-${generateRandomCode(8)}` : generateRandomCode(10) codes.push({ code, status: 'active', created_at: now, updated_at: now }) } // 批量插入 await apiClient.database.table('xunlian_codes').insert(codes) return c.json({ message: `成功生成${count}个训练码`, count }) } catch (error) { console.error('批量生成训练码失败:', error) return c.json({ error: '批量生成训练码失败' }, 500) } }) // 更新训练码状态 xunlianCodesRoutes.put('/:id/status', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') const body = await c.req.json() const { status } = body if (!status || !['active', 'inactive', 'used'].includes(status)) { return c.json({ error: '无效的状态值' }, 400) } // 检查训练码是否存在 const existingCode = await apiClient.database.table('xunlian_codes') .where('id', id) .first() if (!existingCode) { return c.json({ error: '训练码不存在' }, 404) } // 更新状态 await apiClient.database.table('xunlian_codes') .where('id', id) .update({ status, updated_at: new Date() }) return c.json({ message: '更新训练码状态成功', id, status }) } catch (error) { console.error('更新训练码状态失败:', error) return c.json({ error: '更新训练码状态失败' }, 500) } }) return xunlianCodesRoutes } // 生成随机训练码 function generateRandomCode(length: number) { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' let result = '' for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)) } return result }