Browse Source

系统功能,日志和标签处理添加trycatch,角色数据审核接口

lrf 7 months ago
parent
commit
f9548353d5

+ 54 - 0
src/controller/systemFunc.controller.ts

@@ -0,0 +1,54 @@
+import { Controller, Get, Inject } from '@midwayjs/core';
+import { InitSystemDataService } from '../service/initData/initSystemData.service';
+import { DataDealService } from '../service/initData/dataDeal.service';
+import { NeedAdminMiddleware } from '../middleware/needAdmin.middleware';
+import { ESService } from '../service/elasticsearch/es.service';
+
+/**所有的操作都不需要await.目前是 下达一个无监控的任务进程 */
+@Controller('/system/func', { middleware: [NeedAdminMiddleware] })
+export class SystemFuncController {
+  @Inject()
+  InitSystemData: InitSystemDataService;
+  @Inject()
+  DataDeal: DataDealService;
+  @Inject()
+  es: ESService;
+
+  @Get('/')
+  async index() {
+    console.log('in');
+  }
+
+  @Get('/initES')
+  async initES() {
+    this.es.initES();
+  }
+
+  /**修正导入的数据为 richText的数据 */
+  @Get('/cid')
+  async correctImportData() {
+    this.DataDeal.correctImportData();
+  }
+
+  /**初始化后台目录 */
+  @Get('/ism')
+  async initSystemMenus() {
+    this.InitSystemData.initSystemMenus();
+  }
+
+  /**初始化用户目录 */
+  @Get('/ium')
+  async initUserMenus() {
+    this.InitSystemData.initUserMenus();
+  }
+  /**初始化角色数据 */
+  @Get('/ir')
+  async initRoleData() {
+    this.InitSystemData.initRoleData();
+  }
+  /**初始化角色的菜单(不包含管理员) */
+  @Get('/irm')
+  async initRoleMenus() {
+    this.InitSystemData.initRoleMenus();
+  }
+}

+ 9 - 0
src/controller/users/association.controller.ts

@@ -17,6 +17,15 @@ export class AssociationController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/company.controller.ts

@@ -21,6 +21,15 @@ export class CompanyController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/competition.controller.ts

@@ -16,6 +16,15 @@ export class CompetitionController implements BaseController {
 
   @Inject()
   serviceUtil: ServiceUtilService;
+  
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
 
   @Get('/')
   @ApiTags('列表查询')

+ 9 - 0
src/controller/users/expert.controller.ts

@@ -23,6 +23,15 @@ export class ExpertController implements BaseController {
   @Inject()
   utilService: UtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/incubator.controller.ts

@@ -20,6 +20,15 @@ export class IncubatorController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/investment.controller.ts

@@ -20,6 +20,15 @@ export class InvestmentController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/school.controller.ts

@@ -17,6 +17,15 @@ export class schoolController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/state.controller.ts

@@ -20,6 +20,15 @@ export class StateController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 9 - 0
src/controller/users/unit.controller.ts

@@ -20,6 +20,15 @@ export class UnitController implements BaseController {
   @Inject()
   serviceUtil: ServiceUtilService;
 
+  @Post('/examine', { routerName: `审核${namePrefix}` })
+  @ApiTags('信息审核')
+  @ApiQuery({ name: 'examine' })
+  async examine(@Body('id') id: number, @Body('status') status: string) {
+    if (!id || !status) throw new ServiceError(ErrorCode.BODY_ERROR);
+    const result = await this.service.update({ id }, { status });
+    return result;
+  }
+
   @Get('/')
   @ApiTags('列表查询')
   @ApiQuery({ name: 'query' })

+ 1 - 1
src/entity/system/admin.entity.ts

@@ -22,7 +22,7 @@ export class Admin extends BaseModel {
       },
     },
   })
-  password: string; //TODO:需要重新加密
+  password: string;
   @Column({ type: 'jsonb', nullable: true, comment: '角色id数组', default: [] })
   role: string[];
   @Column({ type: 'integer', nullable: true, comment: '部门id' })

+ 1 - 1
src/entity/system/user.entity.ts

@@ -26,7 +26,7 @@ export class User extends BaseModel {
       },
     },
   })
-  password: string; //TODO:需要重新加密
+  password: string;
   @Column({ type: 'character varying', comment: '性别' })
   gender: string;
   @Column({ type: 'character varying', nullable: true, comment: '手机号' })

+ 3 - 1
src/entityLogs/opera.entity.ts

@@ -2,9 +2,11 @@ import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
 import * as dayjs from 'dayjs';
 import { BaseModel } from '../frame/BaseModel';
 @Entity('opera', { comment: '操作日志' })
-export class Opera extends BaseModel{
+export class Opera extends BaseModel {
   @Column({ type: 'integer', nullable: true, comment: '操作人id' })
   operator_id: number;
+  @Column({ type: 'character varying', nullable: true, comment: '是否是管理员: 0是;1否' })
+  is_admin: string;
   @Column({ type: 'character varying', nullable: true, comment: '操作人名称' })
   operator_name: string;
   @Column({ type: 'character varying', nullable: true, comment: 'ip' })

+ 18 - 0
src/entityLogs/searchBehavior.entity.ts

@@ -0,0 +1,18 @@
+import { Column, Entity } from 'typeorm';
+import * as dayjs from 'dayjs';
+import { BaseModel } from '../frame/BaseModel';
+@Entity('searchBehavior', { comment: '检索行为日志' })
+export class SearchBehavior extends BaseModel {
+  @Column({ type: 'integer', nullable: true, comment: '操作人id' })
+  operator_id: number;
+  @Column({ type: 'character varying', nullable: true, comment: '操作人名称' })
+  operator_name: string;
+  @Column({ type: 'character varying', nullable: true, comment: 'ip' })
+  ip: string;
+  @Column({ type: 'character varying', nullable: true, comment: '设备' })
+  device: string;
+  @Column({ type: 'timestamp without time zone', nullable: true, comment: '时间', transformer: { from: value => dayjs(value).format('YYYY-MM-DD HH:mm:ss'), to: value => value } })
+  time: Date;
+  @Column({ type: 'character varying', comment: '检索内容' })
+  keyword: string;
+}

+ 1 - 0
src/error/service.error.ts

@@ -21,6 +21,7 @@ export enum ErrorCode {
 
   // 参数
   ID_NOT_FOUND = 'ID_NOT_FOUND',
+  BODY_ERROR = 'BODY_ERROR',
 
   //admin
   ADMIN_BODY_NICK_NAME = 'ADMIN_BODY_NICK_NAME',

+ 0 - 4
src/frame/BaseModel.ts

@@ -15,10 +15,6 @@ export class BaseModel {
   @UpdateDateColumn({ type: 'timestamp without time zone', transformer: { from: value => dayjs(value).format('YYYY-MM-DD HH:mm:ss'), to: value => value } })
   update_time?: Date;
 
-  /**数据创建者 */
-  @Column({ type: 'character varying', nullable: true, comment: '数据创建者' })
-  data_owner?: string;
-
   /**数据版本 */
   @VersionColumn({ type: 'integer', default: 1 })
   __v?: number;

+ 28 - 7
src/frame/BaseServiceV2.ts

@@ -278,10 +278,15 @@ export abstract class BaseServiceV2 {
     const createData = await this.fetch({ id });
     // 没有查出数据,也是有问题
     if (!createData) return;
-    // 处理标签
+    try {
+     // 处理标签
     await this.dealTags(createData);
     // 添加,没有原数据,只有新数据
     await this.dealLog(null, createData);
+    } catch (error) {
+      console.error('log error');
+      console.error(createData);
+    }
     return createData;
   }
 
@@ -311,10 +316,16 @@ export abstract class BaseServiceV2 {
     await this.model.update(query, updateData);
     const new_data = await originDataBuilder.getMany(query);
     if (new_data.length <= 0) return;
-    // 处理标签
-    await this.dealTags(new_data);
-    // 处理日志
-    await this.dealLog(origin_data, new_data);
+    try {
+      // 处理标签
+      await this.dealTags(new_data);
+      // 处理日志
+      await this.dealLog(origin_data, new_data);
+    } catch (error) {
+      console.error('log error');
+      console.error(origin_data);
+      console.error(new_data);
+    }
   }
 
   /**删除,单删多删都行 */
@@ -327,8 +338,13 @@ export abstract class BaseServiceV2 {
     const origin_data = await originDataBuilder.getMany(query);
     if (origin_data.length <= 0) return;
     const result = await this.model.delete(query);
-    // 处理日志
-    await this.dealLog(origin_data, null);
+    try {
+      // 处理日志
+      await this.dealLog(origin_data, null);
+    } catch (error) {
+      console.error('log error');
+      console.error(origin_data);
+    }
     return result;
   }
 
@@ -360,6 +376,10 @@ export abstract class BaseServiceV2 {
     const user = this.ctx.user;
     if (user && user.id) {
       const req = this.ctx.request;
+      let is_admin = '1';
+      const role = get(user, 'role', []);
+      const has_admin = role.find(f => f === 'Admin');
+      if (has_admin) is_admin = '0';
       const logInfo: any = {
         // 操作人
         operator_id: user.id,
@@ -385,6 +405,7 @@ export abstract class BaseServiceV2 {
         // 变动值: 表:[数据] 需要自己组织
         origin_data: origin_data,
         new_data: new_data,
+        is_admin,
       };
       const routeInfo = await this.webRouterService.getMatchedRouterInfo(this.ctx.path, this.ctx.method);
       if (routeInfo) logInfo.opera = get(routeInfo, 'routerName');

+ 20 - 0
src/middleware/needAdmin.middleware.ts

@@ -0,0 +1,20 @@
+import { Context, IMiddleware, Middleware, NextFunction } from '@midwayjs/core';
+import { get } from 'lodash';
+
+@Middleware()
+export class NeedAdminMiddleware implements IMiddleware<Context, NextFunction> {
+  resolve() {
+    return async (ctx: Context, next: NextFunction) => {
+      const user = get(ctx, 'user');
+      if (!user) return;
+      const role = get(user, 'role', []);
+      const has_admin = role.find(f => f === 'Admin');
+      if (!has_admin) return;
+      await next();
+    };
+  }
+
+  static getName(): string {
+    return 'needAdmin';
+  }
+}

+ 1 - 238
src/service/initData/dataDeal.service.ts

@@ -2,7 +2,7 @@ import { Provide } from '@midwayjs/core';
 import { InjectDataSource, InjectEntityModel } from '@midwayjs/typeorm';
 import { DataSource, In, Repository } from 'typeorm';
 import { Menus } from '../../entity/system/menus.entity';
-import { cloneDeep, get, isNull, isNumber, isObject, isUndefined, omit } from 'lodash';
+import { get, isNull, isNumber, isUndefined } from 'lodash';
 import { User } from '../../entity/system/user.entity';
 import { Admin } from '../../entity/system/admin.entity';
 import { Achievement } from '../../entity/platform/achievement.entity';
@@ -122,243 +122,6 @@ export class DataDealService {
     }
   }
 
-  /**初始化用户目录,先去数据库把表清空,顺便重置下id索引 */
-  async initUserMenus() {
-    await this.UserMenus.query('TRUNCATE TABLE "public"."userMenus" RESTART IDENTITY RESTRICT;');
-    await this.defaultDataSource.query(`SELECT setval('"userMenus_id_seq"', (SELECT max(id) FROM "userMenus"));`);
-    const datas = [
-      {
-        order_num: 0,
-        name: '基础信息',
-        route_name: 'center_basic',
-        i18n_code: 'menus.center_basic',
-        path: '/center/basic',
-        component: '/center/basic',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'User',
-      },
-      {
-        order_num: 1,
-        name: '认证入驻',
-        route_name: 'center_attestation',
-        i18n_code: 'menus.center_attestation',
-        path: '/center/attestation',
-        component: '/center/attestation',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Finished',
-      },
-      {
-        order_num: 2,
-        name: '通知管理',
-        route_name: 'center_notice',
-        i18n_code: 'menus.center_notice',
-        path: '/center/notice',
-        component: '/center/notice',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Notebook',
-      },
-      {
-        order_num: 3,
-        name: '行业动态',
-        route_name: 'center_news2',
-        i18n_code: 'menus.center_news2',
-        path: '/center/news2',
-        component: '/center/news2',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Message',
-      },
-      {
-        order_num: 4,
-        name: '需求管理',
-        route_name: 'center_demand',
-        i18n_code: 'menus.center_demand',
-        path: '/center/demand',
-        component: '/center/demand',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'DataBoard',
-      },
-      {
-        order_num: 5,
-        name: '供给管理',
-        route_name: 'center_supply',
-        i18n_code: 'menus.center_supply',
-        path: '/center/supply',
-        component: '/center/supply',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Notification',
-      },
-      {
-        order_num: 6,
-        name: '成果管理',
-        route_name: 'center_achievement',
-        i18n_code: 'menus.center_achievement',
-        path: '/center/achievement',
-        component: '/center/achievement',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Medal',
-      },
-      {
-        order_num: 7,
-        name: '项目管理',
-        route_name: 'center_project',
-        i18n_code: 'menus.center_project',
-        path: '/center/project',
-        component: '/center/project',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Trophy',
-      },
-      {
-        order_num: 8,
-        name: '中试管理',
-        route_name: 'center_footplate',
-        i18n_code: 'menus.center_footplate',
-        path: '/center/footplate',
-        component: '/center/footplate',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'TakeawayBox',
-      },
-      {
-        order_num: 9,
-        name: '服务管理',
-        route_name: 'center_service',
-        i18n_code: 'menus.center_service',
-        path: '/center/service',
-        component: '/center/service',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'TakeawayBox',
-      },
-      {
-        order_num: 10,
-        name: '赛事管理',
-        route_name: 'center_match',
-        i18n_code: 'menus.center_match',
-        path: '/center/match',
-        component: '/center/match',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'CollectionTag',
-      },
-      {
-        order_num: 11,
-        name: '活动管理',
-        route_name: 'center_sign',
-        i18n_code: 'menus.center_sign',
-        path: '/center/sign',
-        component: '/center/sign',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Suitcase',
-      },
-      {
-        order_num: 12,
-        name: '产研行研',
-        route_name: 'center_journal',
-        i18n_code: 'menus.center_journal',
-        path: '/center/journal',
-        component: '/center/journal',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Reading',
-      },
-      {
-        order_num: 13,
-        name: '企业选择',
-        route_name: 'center_company',
-        i18n_code: 'menus.center_company',
-        path: '/center/company',
-        component: '/center/company',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'SwitchFilled',
-      },
-      {
-        order_num: 998,
-        name: '我的收藏',
-        route_name: 'center_collection',
-        i18n_code: 'menus.center_collection',
-        path: '/center/collection',
-        component: '/center/collection',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Collection',
-      },
-      {
-        order_num: 999,
-        name: '修改密码',
-        route_name: 'center_password',
-        i18n_code: 'menus.center_password',
-        path: '/center/password',
-        component: '/center/password',
-        type: '1',
-        config: [],
-        is_default: '0',
-        is_use: '0',
-        icon: 'Lock',
-      },
-    ];
-    await this.UserMenus.insert(datas);
-  }
-
-  async initRoleMenus() {
-    /**
-     * 初始化角色的菜单
-     * 共有: 基础信息,认证信息,行业动态,
-     */
-    const common = ['center_basic', 'center_attestation', 'center_notice', 'center_collection', 'center_password', 'center_sign'];
-    // 只用通用菜单的角色
-    const justCommonRole = ['User', 'Association', 'Investment', 'State'];
-    await this.Role.update({ code: In(justCommonRole) }, { menu: common });
-    // 第一类菜单
-    const menus1 = [...common, 'center_news2', 'center_demand', 'center_supply', 'center_achievement', 'center_project', 'center_footplate', 'center_service'];
-    // 第一类菜单用户: 用户,科研机构,孵化基地,企业,高校
-    const roleUseMenus1 = ['Expert', 'Unit', 'Incubator', 'Company', 'YX'];
-    await this.Role.update({ code: In(roleUseMenus1) }, { menu: menus1 });
-    // 创业大赛用户: 需要去掉报名管理,加入赛事管理
-    const MatchRole = common.filter(f => f != 'center_sign');
-    MatchRole.push('center_match');
-    await this.Role.update({ code: In(['Competition']) }, { menu: MatchRole });
-  }
-
   count = 0;
 
   isNoValue(val) {

+ 8 - 0
src/service/initData/initSystemData.service.ts

@@ -395,6 +395,14 @@ export class InitSystemDataService {
             is_use: '0',
             route_name: 'system_region',
           },
+          {
+            name: '系统功能重置',
+            component: 'system-func',
+            type: '2',
+            is_default: '0',
+            is_use: '0',
+            route_name: 'system_func',
+          },
         ],
       },
       {

+ 1 - 1
src/service/system/user.service.ts

@@ -48,7 +48,7 @@ export class UserService extends BaseServiceV2 {
     const { status } = body;
     if (status === '1' && arr.role_type !== 'User') {
       // const model = GetModel(upperFirst(arr.role_type));
-      // TODO: 需要明确修改的指代内容了:如果不是User,那还得说明是哪个 users下面的所有都是,都得引入进来处理
+      // 需要明确修改的指代内容了:如果不是User,那还得说明是哪个 users下面的所有都是,都得引入进来处理
       // const user = await model.findOne({ where: { user: id } });
       // await model.update({ where: { id: user.id } }, { status: '1' });
     }