添加用户
This commit is contained in:
@@ -10,6 +10,9 @@ export type ContainerPublish = {
|
||||
};
|
||||
export type Container = Partial<InstanceType<typeof ContainerModel>>;
|
||||
|
||||
/**
|
||||
* 用户代码容器
|
||||
*/
|
||||
export class ContainerModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
|
||||
@@ -7,3 +7,5 @@ import './resource/index.ts';
|
||||
import './prompt-graph/index.ts';
|
||||
|
||||
import './agent/index.ts';
|
||||
|
||||
import './user/index.ts';
|
||||
|
||||
@@ -18,18 +18,23 @@ type PageNodeData = {
|
||||
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
export interface PageData {
|
||||
edges: any[];
|
||||
nodes: PageNodeData[];
|
||||
viewport: any;
|
||||
[key: string]: any;
|
||||
}
|
||||
/**
|
||||
* 页面数据
|
||||
*/
|
||||
export class PageModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
declare description: string;
|
||||
declare type: string;
|
||||
declare data: PageData;
|
||||
declare uid: string;
|
||||
}
|
||||
PageModel.init(
|
||||
{
|
||||
|
||||
@@ -28,6 +28,9 @@ export type Resource = {
|
||||
uid?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* 资源管理
|
||||
*/
|
||||
export class ResourceModel extends Model {
|
||||
declare id: string;
|
||||
declare name: string;
|
||||
|
||||
1
src/routes/user/index.ts
Normal file
1
src/routes/user/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts'
|
||||
119
src/routes/user/list.ts
Normal file
119
src/routes/user/list.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { CustomError } from '@abearxiong/router';
|
||||
|
||||
app
|
||||
.route('user', 'list')
|
||||
.define(async (ctx) => {
|
||||
const users = await User.findAll({
|
||||
attributes: ['id', 'username', 'description', 'needChangePassword'],
|
||||
order: [['updatedAt', 'DESC']],
|
||||
logging: false,
|
||||
});
|
||||
ctx.body = users;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route('user', 'login')
|
||||
.define(async (ctx) => {
|
||||
const { username, password } = ctx.query;
|
||||
const user = await User.findOne({ where: { username } });
|
||||
if (!user) {
|
||||
new CustomError(401, 'User not found');
|
||||
}
|
||||
if (user.password !== password) {
|
||||
new CustomError(401, 'Password error');
|
||||
}
|
||||
const token = await user.createToken();
|
||||
ctx.body = token;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route('user', 'auth')
|
||||
.define(async (ctx) => {
|
||||
const { checkToken: token } = ctx.query;
|
||||
try {
|
||||
const result = await User.verifyToken(token);
|
||||
ctx.body = result?.payload || {};
|
||||
} catch (e) {
|
||||
new CustomError(401, 'Token InValid ');
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route('user', 'updateSelf', {
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { username, password, description } = ctx.query;
|
||||
const state = ctx.state?.tokenUser || {};
|
||||
const { id } = state;
|
||||
const user = await User.findByPk(id);
|
||||
if (!user) {
|
||||
throw new CustomError(500, 'user not found');
|
||||
}
|
||||
if (username) {
|
||||
user.username = username;
|
||||
}
|
||||
if (password) {
|
||||
user.createPassword(password);
|
||||
}
|
||||
if (description) {
|
||||
user.description = description;
|
||||
}
|
||||
await user.save();
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
description: user.description,
|
||||
needChangePassword: user.needChangePassword,
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
app
|
||||
.route('user', 'update', {
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { id, username, password, description } = ctx.query;
|
||||
const user = await User.findByPk(id);
|
||||
if (!user) {
|
||||
throw new CustomError(500, 'user not found');
|
||||
}
|
||||
if (username) {
|
||||
user.username = username;
|
||||
}
|
||||
if (password) {
|
||||
user.createPassword(password);
|
||||
}
|
||||
if (description) {
|
||||
user.description = description;
|
||||
}
|
||||
await user.save();
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
description: user.description,
|
||||
needChangePassword: user.needChangePassword,
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app.route('user', 'add').define(async (ctx) => {
|
||||
const { username, password, description } = ctx.query;
|
||||
if (!username) {
|
||||
throw new CustomError(400, 'username is required');
|
||||
}
|
||||
const user = await User.createUser(username, password, description);
|
||||
const token = await user.createToken();
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
description: user.description,
|
||||
needChangePassword: user.needChangePassword,
|
||||
token,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user