feat: 邮件通知

This commit is contained in:
xiaojunnuo
2023-06-25 15:30:18 +08:00
parent 64afebecd4
commit 937e3fac19
32 changed files with 790 additions and 88 deletions
@@ -1,6 +1,15 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/decorator";
import { CrudController } from "../../../basic/crud-controller";
import { SettingsService } from "../service/settings-service";
import {
ALL,
Body,
Controller,
Inject,
Post,
Provide,
Query,
} from '@midwayjs/decorator';
import { CrudController } from '../../../basic/crud-controller';
import { SettingsService } from '../service/settings-service';
import { SettingsEntity } from '../entity/settings';
/**
*/
@@ -50,4 +59,18 @@ export class SettingsController extends CrudController<SettingsService> {
return super.delete(id);
}
@Post('/save')
async save(@Body(ALL) bean: SettingsEntity) {
await this.service.checkUserId(bean.key, this.ctx.user.id, 'userId', 'key');
bean.userId = this.ctx.user.id;
await this.service.save(bean);
return this.ok({});
}
@Post('/get')
async get(@Query('key') key: string) {
await this.service.checkUserId(key, this.ctx.user.id, 'userId', 'key');
const entity = await this.service.getByKey(key, this.ctx.user.id);
return this.ok(entity);
}
}
@@ -8,8 +8,10 @@ export class SettingsEntity {
id: number;
@Column({ name: 'user_id', comment: '用户id' })
userId: number;
@Column({ comment: 'key', length: 100 })
key: string;
@Column({ comment: '名称', length: 100 })
name: string;
title: string;
@Column({ name: 'setting', comment: '设置', length: 1024, nullable: true })
setting: string;
@@ -1,17 +1,15 @@
import { Provide, Scope, ScopeEnum } from "@midwayjs/decorator";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import { BaseService } from "../../../basic/base-service";
import { SettingsEntity } from "../entity/settings";
import { Provide, Scope, ScopeEnum } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService } from '../../../basic/base-service';
import { SettingsEntity } from '../entity/settings';
/**
* 授权
*/
@Provide()
@Scope(ScopeEnum.Singleton)
export class SettingsService
extends BaseService<SettingsEntity>
{
export class SettingsService extends BaseService<SettingsEntity> {
@InjectEntityModel(SettingsEntity)
repository: Repository<SettingsEntity>;
@@ -19,8 +17,11 @@ export class SettingsService
return this.repository;
}
async getById(id: any): Promise<any> {
async getById(id: any): Promise<SettingsEntity | null> {
const entity = await this.info(id);
if (!entity) {
return null;
}
// const access = accessRegistry.get(entity.type);
const setting = JSON.parse(entity.setting);
return {
@@ -29,5 +30,38 @@ export class SettingsService
};
}
async getByKey(key: string, userId: number): Promise<SettingsEntity | 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: SettingsEntity) {
const entity = await this.repository.findOne({
where: {
key: bean.key,
},
});
if (entity) {
entity.setting = bean.setting;
await this.repository.save(entity);
} else {
bean.title = bean.key;
await this.repository.save(bean);
}
}
}