feat: add login for plugin

This commit is contained in:
2025-03-23 16:48:19 +08:00
parent 74a484718a
commit cb490470c1
18 changed files with 1079 additions and 369 deletions

227
src/routes/mark/list.ts Normal file
View File

@@ -0,0 +1,227 @@
import { app } from '@/app.ts';
import { MarkModel } from './model.ts';
import { MarkServices } from './services/mark.ts';
import dayjs from 'dayjs';
app
.route({
path: 'mark',
key: 'list',
description: 'mark list.',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
ctx.body = await MarkServices.getList({
uid: tokenUser.id,
query: ctx.query,
queryType: 'simple',
});
})
.addTo(app);
app
.route({
path: 'mark',
key: 'getVersion',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query;
if (id) {
const markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
ctx.body = {
version: Number(markModel.version),
updatedAt: markModel.updatedAt,
createdAt: markModel.createdAt,
id: markModel.id,
};
} else {
const [markModel, created] = await MarkModel.findOrCreate({
where: {
uid: tokenUser.id,
puid: tokenUser.uid,
title: dayjs().format('YYYY-MM-DD'),
},
defaults: {
title: dayjs().format('YYYY-MM-DD'),
uid: tokenUser.id,
markType: 'wallnote',
tags: ['daily'],
},
});
ctx.body = {
version: Number(markModel.version),
updatedAt: markModel.updatedAt,
createdAt: markModel.createdAt,
id: markModel.id,
created: created,
};
}
})
.addTo(app);
app
.route({
path: 'mark',
key: 'get',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query;
if (id) {
const markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
ctx.body = markModel;
} else {
// id 不存在获取当天的title为 日期的一条数据
const [markModel, created] = await MarkModel.findOrCreate({
where: {
uid: tokenUser.id,
puid: tokenUser.uid,
title: dayjs().format('YYYY-MM-DD'),
},
defaults: {
title: dayjs().format('YYYY-MM-DD'),
uid: tokenUser.id,
markType: 'wallnote',
tags: ['daily'],
uname: tokenUser.username,
puid: tokenUser.uid,
version: 1,
},
});
ctx.body = markModel;
}
})
.addTo(app);
app
.route({
path: 'mark',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, ...data } = ctx.query.data || {};
let markModel: MarkModel;
if (id) {
markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
const version = Number(markModel.version) + 1;
await markModel.update({ ...markModel.data, ...data, version });
} else {
markModel = await MarkModel.create({
...data,
uname: tokenUser.username,
uid: tokenUser.id,
puid: tokenUser.uid,
});
}
ctx.body = markModel;
})
.addTo(app);
app
.route({
path: 'mark',
key: 'updateNode',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const operate = ctx.query.operate || 'update';
const { id, node } = ctx.query.data || {};
const markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
await MarkModel.updateJsonNode(id, node, { operate });
ctx.body = markModel;
})
.addTo(app);
app
.route({
path: 'mark',
key: 'updateNodes',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, nodeOperateList } = ctx.query.data || {};
const markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
if (!nodeOperateList || !Array.isArray(nodeOperateList) || nodeOperateList.length === 0) {
ctx.throw(400, 'nodeOperateList is required');
}
if (nodeOperateList.some((node) => !node.node)) {
ctx.throw(400, 'nodeOperateList node is required');
}
const newmark = await MarkModel.updateJsonNodes(id, nodeOperateList);
ctx.body = newmark;
})
.addTo(app);
app
.route({
path: 'mark',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query;
const markModel = await MarkModel.findByPk(id);
if (!markModel) {
ctx.throw(404, 'mark not found');
}
if (markModel.uid !== tokenUser.id) {
ctx.throw(403, 'no permission');
}
await markModel.destroy();
ctx.body = markModel;
})
.addTo(app);
app
.route({ path: 'mark', key: 'getMenu', description: '获取菜单', middleware: ['auth'] })
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { rows, count } = await MarkModel.findAndCountAll({
where: {
uid: tokenUser.id,
},
attributes: ['id', 'title', 'summary', 'tags', 'thumbnail', 'link', 'createdAt', 'updatedAt'],
});
ctx.body = {
list: rows,
total: count,
};
})
.addTo(app);