docs: 文档添加

This commit is contained in:
2025-06-20 11:16:55 +08:00
parent f8876964b9
commit 4b4e596b0c
8 changed files with 52 additions and 49 deletions

View File

@@ -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": [],

View File

@@ -1 +1,14 @@
import './index.ts';
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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 })
await TicketModel.sync({ alter: true });