2023-06-25 15:30:18 +08:00
|
|
|
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';
|
2023-06-07 23:36:42 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 授权
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
2023-06-25 15:30:18 +08:00
|
|
|
export class SettingsService extends BaseService<SettingsEntity> {
|
2023-06-07 23:36:42 +08:00
|
|
|
@InjectEntityModel(SettingsEntity)
|
|
|
|
|
repository: Repository<SettingsEntity>;
|
|
|
|
|
|
|
|
|
|
getRepository() {
|
|
|
|
|
return this.repository;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 15:30:18 +08:00
|
|
|
async getById(id: any): Promise<SettingsEntity | null> {
|
2023-06-07 23:36:42 +08:00
|
|
|
const entity = await this.info(id);
|
2023-06-25 15:30:18 +08:00
|
|
|
if (!entity) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-06-07 23:36:42 +08:00
|
|
|
// const access = accessRegistry.get(entity.type);
|
|
|
|
|
const setting = JSON.parse(entity.setting);
|
|
|
|
|
return {
|
|
|
|
|
id: entity.id,
|
|
|
|
|
...setting,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-25 15:30:18 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2023-06-07 23:36:42 +08:00
|
|
|
|
2023-06-25 15:30:18 +08:00
|
|
|
async save(bean: SettingsEntity) {
|
|
|
|
|
const entity = await this.repository.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
key: bean.key,
|
2023-12-12 23:35:41 +08:00
|
|
|
userId: bean.userId,
|
2023-06-25 15:30:18 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (entity) {
|
|
|
|
|
entity.setting = bean.setting;
|
|
|
|
|
await this.repository.save(entity);
|
|
|
|
|
} else {
|
|
|
|
|
bean.title = bean.key;
|
|
|
|
|
await this.repository.save(bean);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 23:36:42 +08:00
|
|
|
}
|