perf: 增加系统设置,可以关闭自助注册功能

This commit is contained in:
xiaojunnuo
2024-06-16 00:20:02 +08:00
parent 575bf2b73b
commit 20feacea12
19 changed files with 522 additions and 40 deletions
@@ -0,0 +1,67 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/decorator";
import { CrudController } from "../../../basic/crud-controller";
import { Constants } from "../../../basic/constants";
import { UserSettingsService } from "../service/user-settings-service";
import { UserSettingsEntity } from "../entity/user-settings";
/**
*/
@Provide()
@Controller('/api/user/settings')
export class UserSettingsController extends CrudController<UserSettingsService> {
@Inject()
service: UserSettingsService;
getService() {
return this.service;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body) {
body.query = body.query ?? {};
body.query.userId = this.ctx.user.id;
return super.page(body);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body) {
body.userId = this.ctx.user.id;
return super.list(body);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean) {
bean.userId = this.ctx.user.id;
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.ctx.user.id);
return super.update(bean);
}
@Post('/info', { summary: Constants.per.authOnly })
async info(@Query('id') id) {
await this.service.checkUserId(id, this.ctx.user.id);
return super.info(id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id) {
await this.service.checkUserId(id, this.ctx.user.id);
return super.delete(id);
}
@Post('/save', { summary: Constants.per.authOnly })
async save(@Body(ALL) bean: UserSettingsEntity) {
bean.userId = this.ctx.user.id;
await this.service.save(bean);
return this.ok({});
}
@Post('/get', { summary: Constants.per.authOnly })
async get(@Query('key') key: string) {
const entity = await this.service.getByKey(key, this.ctx.user.id);
return this.ok(entity);
}
}
@@ -0,0 +1,30 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
/**
*/
@Entity('user_settings')
export class UserSettingsEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: 'user_id', comment: '用户id' })
userId: number;
@Column({ comment: 'key', length: 100 })
key: string;
@Column({ comment: '名称', length: 100 })
title: string;
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
setting: string;
@Column({
name: 'create_time',
comment: '创建时间',
default: () => 'CURRENT_TIMESTAMP',
})
createTime: Date;
@Column({
name: 'update_time',
comment: '修改时间',
default: () => 'CURRENT_TIMESTAMP',
})
updateTime: Date;
}
@@ -0,0 +1,68 @@
import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService } from '../../../basic/base-service';
import { UserSettingsEntity } from '../entity/user-settings';
/**
* 授权
*/
@Provide()
@Scope(ScopeEnum.Singleton)
export class UserSettingsService extends BaseService<UserSettingsEntity> {
@InjectEntityModel(UserSettingsEntity)
repository: Repository<UserSettingsEntity>;
getRepository() {
return this.repository;
}
async getById(id: any): Promise<UserSettingsEntity | null> {
const entity = await this.info(id);
if (!entity) {
return null;
}
// const access = accessRegistry.get(entity.type);
const setting = JSON.parse(entity.setting);
return {
id: entity.id,
...setting,
};
}
async getByKey(key: string, userId: number): Promise<UserSettingsEntity | null> {
if (!key || !userId) {
return null;
}
return await this.repository.findOne({
where: {
key,
userId,
},
});
}
async getSettingByKey(key: string, userId: number): Promise<any | null> {
const entity = await this.getByKey(key, userId);
if (!entity) {
return null;
}
return JSON.parse(entity.setting);
}
async save(bean: UserSettingsEntity) {
const entity = await this.repository.findOne({
where: {
key: bean.key,
userId: bean.userId,
},
});
if (entity) {
entity.setting = bean.setting;
await this.repository.save(entity);
} else {
bean.title = bean.key;
await this.repository.save(bean);
}
}
}