12345678910111213141516171819202122232425262728293031 |
- import { BaseService } from '../../frame/BaseService';
- import { Provide } from '@midwayjs/core';
- import { get } from 'lodash';
- import { ServiceError } from '../../error/CustomerError.error';
- import { ErrorCode } from '../../error/Codes';
- import { InjectEntityModel } from '@midwayjs/typeorm';
- import { Repository } from 'typeorm';
- import { Admin } from '../../entity/system/admin.entity';
- @Provide()
- export class AdminService extends BaseService {
- @InjectEntityModel(Admin)
- model: Repository<Admin>;
- /**查重 */
- async checkInDB(data: object) {
- // account查重,虽然数据库有unique约束,但是尽量不触发,因为异常捕获不好解析
- const account = get(data, 'account');
- const num = await this.model.count({ where: { account } });
- if (num > 0) throw new ServiceError(ErrorCode.ACCOUNT_IS_EXISTS);
- }
- async initSuperAdmin() {
- const data = {
- account: 'admin',
- password: '1qaz2wsx',
- nick_name: '系统管理员',
- is_super: '0',
- };
- const is_exist = await this.model.count({ where: { is_super: '0' } });
- if (!is_exist) return await this.model.insert(data);
- return;
- }
- }
|