diff --git a/README.md b/README.md index 5016217..e472391 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# 启动 + +> 所有的命令,在当前根目录下运行即可 + ## env 安装 node 环境 diff --git a/backend/package.json b/backend/package.json index 3fbc676..3057127 100644 --- a/backend/package.json +++ b/backend/package.json @@ -17,7 +17,7 @@ "build": "rimraf dist && bun run bun.config.mjs", "test": "tsx test/**/*.ts", "clean": "rm -rf dist", - "pub": "npm run build && envision pack -p -u", + "pub": "envision pack -p -u", "cmd": "tsx cmd/index.ts " }, "keywords": [], diff --git a/backend/src/dev.ts b/backend/src/dev.ts index bb3e522..718e7be 100644 --- a/backend/src/dev.ts +++ b/backend/src/dev.ts @@ -1 +1,14 @@ -import './index.ts'; \ No newline at end of file +import { proxyRoute, initProxy } from '@kevisual/local-proxy/proxy.ts'; +initProxy({ + pagesDir: './pages', + watch: true, + home: '/root/tickets', +}); +import { app } from './app.ts'; +import './index.ts'; + +app.listen(3004, () => { + console.log('Server is running on http://localhost:3004'); +}); + +app.onServerRequest(proxyRoute); diff --git a/backend/src/index.ts b/backend/src/index.ts index e9da905..784b410 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,15 +1,2 @@ -import { proxyRoute, initProxy } from '@kevisual/local-proxy/proxy.ts'; -initProxy({ - pagesDir: './pages', - watch: true, - home: '/root/tickets', -}); -import { app } from './app.ts'; - +import './app.ts'; import './routes/ticket/list.ts'; - -app.listen(3004, () => { - console.log('Server is running on http://localhost:3004'); -}); - -app.onServerRequest(proxyRoute); diff --git a/backend/src/routes/ticket/list.ts b/backend/src/routes/ticket/list.ts index 1004c04..0a0bd1c 100644 --- a/backend/src/routes/ticket/list.ts +++ b/backend/src/routes/ticket/list.ts @@ -23,36 +23,22 @@ app key: 'list', }) .define(async (ctx) => { - const searchForm = ctx.query?.data || {} as SearchTicketsParams; - const { - current = 1, - pageSize = 10, - search, - status, - type, - sort = 'createdAt', - order = 'desc', - uid, - startDate, - endDate - } = searchForm; - + const searchForm = ctx.query?.data || ({} as SearchTicketsParams); + const { current = 1, pageSize = 10, search, status, type, sort = 'createdAt', order = 'desc', uid, startDate, endDate } = searchForm; + // 构建查询条件 let where: any = {}; - + // 如果提供了搜索关键词,同时搜索标题和描述 if (search) { - where[Op.or] = [ - { title: { [Op.like]: `%${search}%` } }, - { description: { [Op.like]: `%${search}%` } } - ]; + where[Op.or] = [{ title: { [Op.like]: `%${search}%` } }, { description: { [Op.like]: `%${search}%` } }]; } - + // 添加其他过滤条件 if (status) where.status = status; if (type) where.type = type; if (uid) where.uid = uid; - + // 日期范围过滤 if (startDate || endDate) { const dateFilter: any = {}; @@ -65,14 +51,14 @@ app const validSortFields = ['id', 'title', 'status', 'type', 'price', 'createdAt', 'updatedAt']; const sortField = validSortFields.includes(sort) ? sort : 'createdAt'; const sortOrder = order?.toUpperCase() === 'ASC' ? 'ASC' : 'DESC'; - + const { rows, count } = await TicketModel.findAndCountAll({ where, offset: (current - 1) * pageSize, limit: pageSize, order: [[sortField, sortOrder]], }); - + ctx.body = { list: rows, pagination: { @@ -93,14 +79,24 @@ app .define(async (ctx) => { const data = ctx.query?.data || {}; const { id, ...updateData } = data; - if (!id) { - ctx.throw(400, 'ID is required for update'); + + let ticket: TicketModel; + let isNew = false; + if (id) { + ticket = await TicketModel.findByPk(id); + if (!ticket) { + ctx.throw(404, 'Ticket not found'); + } + } else { + isNew = true; + ticket = await TicketModel.create({ ...updateData }); } - const ticket = await TicketModel.findByPk(id); if (!ticket) { ctx.throw(404, 'Ticket not found'); } - await ticket.update(updateData); + if (!isNew) { + await ticket.update(updateData); + } ctx.body = ticket; }) .addTo(app); diff --git a/backend/src/routes/ticket/model.ts b/backend/src/routes/ticket/model.ts index dab40c7..49bcbc1 100644 --- a/backend/src/routes/ticket/model.ts +++ b/backend/src/routes/ticket/model.ts @@ -39,10 +39,12 @@ TicketModel.init( type: { type: DataTypes.STRING, allowNull: false, + defaultValue: 'rule', }, title: { type: DataTypes.STRING, allowNull: false, + defaultValue: '未命名票据', }, description: { type: DataTypes.TEXT, @@ -56,10 +58,11 @@ TicketModel.init( price: { type: DataTypes.STRING, allowNull: false, + defaultValue: '0', }, uid: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, }, createdAt: { type: DataTypes.DATE, @@ -77,4 +80,4 @@ TicketModel.init( }, ); -await TicketModel.sync({ alter: true }) \ No newline at end of file +await TicketModel.sync({ alter: true }); diff --git a/index.html b/index.html index 7fe3403..865871a 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@ -