lrf 8 miesięcy temu
rodzic
commit
11b0fe9fd2

+ 2 - 2
README.md

@@ -30,6 +30,6 @@
 ### 2.1 带id导入数据
 带id导入数据后,需要执行数据库命令,使id的序列从导入数据的最后一条数据开始,否则会出现id已存在问题:
 ``` 
-  SELECT setval('company_id_seq', (SELECT max(id) FROM "company"));
-  SELECT setval('表名_唯一约束的字段_seq', (SELECT max(唯一约束的字段) FROM 表名));
+  SELECT setval('"company_id_seq"', (SELECT max(id) FROM "company"));
+  SELECT setval('"表名_唯一约束的字段_seq"', (SELECT max(唯一约束的字段) FROM 表名));
 ```

+ 45 - 28
src/service/initData/dataDeal.service.ts

@@ -1,6 +1,6 @@
 import { Provide } from '@midwayjs/core';
-import { InjectEntityModel } from '@midwayjs/typeorm';
-import { In, Repository } from 'typeorm';
+import { InjectDataSource, InjectEntityModel } from '@midwayjs/typeorm';
+import { DataSource, In, Repository } from 'typeorm';
 import { Menus } from '../../entity/system/menus.entity';
 import { get, isNull, isUndefined, omit } from 'lodash';
 import { User } from '../../entity/system/user.entity';
@@ -59,46 +59,52 @@ export class DataDealService {
   @InjectEntityModel(Role)
   Role: Repository<Role>;
 
+  @InjectDataSource('default')
+  defaultDataSource: DataSource;
+
   /**初始化用户目录,先去数据库把表清空,顺便重置下id索引 */
   async initUserMenus() {
-    console.log(' in 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_base',
-        i18n_code: 'menus.center_base',
-        path: '/center/base',
-        component: '/center/base/index',
+        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_role',
-        i18n_code: 'menus.center_role',
-        path: '/center/role',
-        component: '/center/role/index',
+        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: '消息通知',
+        name: '通知管理',
         route_name: 'center_notice',
         i18n_code: 'menus.center_notice',
         path: '/center/notice',
-        component: '/center/notice/index',
+        component: '/center/notice',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Notebook',
       },
       {
         order_num: 3,
@@ -106,11 +112,12 @@ export class DataDealService {
         route_name: 'center_news2',
         i18n_code: 'menus.center_news2',
         path: '/center/news2',
-        component: '/center/news2/index',
+        component: '/center/news2',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Message',
       },
       {
         order_num: 4,
@@ -118,11 +125,12 @@ export class DataDealService {
         route_name: 'center_demand',
         i18n_code: 'menus.center_demand',
         path: '/center/demand',
-        component: '/center/demand/index',
+        component: '/center/demand',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'DataBoard',
       },
       {
         order_num: 5,
@@ -130,11 +138,12 @@ export class DataDealService {
         route_name: 'center_supply',
         i18n_code: 'menus.center_supply',
         path: '/center/supply',
-        component: '/center/supply/index',
+        component: '/center/supply',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Notification',
       },
       {
         order_num: 6,
@@ -142,11 +151,12 @@ export class DataDealService {
         route_name: 'center_achievement',
         i18n_code: 'menus.center_achievement',
         path: '/center/achievement',
-        component: '/center/achievement/index',
+        component: '/center/achievement',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Medal',
       },
       {
         order_num: 7,
@@ -154,11 +164,12 @@ export class DataDealService {
         route_name: 'center_project',
         i18n_code: 'menus.center_project',
         path: '/center/project',
-        component: '/center/project/index',
+        component: '/center/project',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Trophy',
       },
       {
         order_num: 8,
@@ -166,11 +177,12 @@ export class DataDealService {
         route_name: 'center_footplate',
         i18n_code: 'menus.center_footplate',
         path: '/center/footplate',
-        component: '/center/footplate/index',
+        component: '/center/footplate',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'TakeawayBox',
       },
       {
         order_num: 9,
@@ -178,23 +190,25 @@ export class DataDealService {
         route_name: 'center_match',
         i18n_code: 'menus.center_match',
         path: '/center/match',
-        component: '/center/match/index',
+        component: '/center/match',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'CollectionTag',
       },
       {
         order_num: 10,
-        name: '报名管理',
+        name: '活动管理',
         route_name: 'center_sign',
         i18n_code: 'menus.center_sign',
         path: '/center/sign',
-        component: '/center/sign/index',
+        component: '/center/sign',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Suitcase',
       },
       {
         order_num: 11,
@@ -202,23 +216,25 @@ export class DataDealService {
         route_name: 'center_journal',
         i18n_code: 'menus.center_journal',
         path: '/center/journal',
-        component: '/center/journal/index',
+        component: '/center/journal',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Reading',
       },
       {
         order_num: 998,
-        name: '收藏管理',
+        name: '我的收藏',
         route_name: 'center_collection',
         i18n_code: 'menus.center_collection',
         path: '/center/collection',
-        component: '/center/collection/index',
+        component: '/center/collection',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Collection',
       },
       {
         order_num: 999,
@@ -226,11 +242,12 @@ export class DataDealService {
         route_name: 'center_password',
         i18n_code: 'menus.center_password',
         path: '/center/password',
-        component: '/center/password/index',
+        component: '/center/password',
         type: '1',
         config: [],
         is_default: '0',
         is_use: '0',
+        icon: 'Lock',
       },
     ];
     await this.UserMenus.insert(datas);
@@ -241,7 +258,7 @@ export class DataDealService {
      * 初始化角色的菜单
      * 共有: 基础信息,认证信息,行业动态,
      */
-    const common = ['center_base', 'center_role', 'center_notice', 'center_collection', 'center_password', 'center_sign'];
+    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 });

+ 27 - 2
src/service/system/role.service.ts

@@ -8,6 +8,7 @@ import { compact, flattenDeep, get, isArray, last, lowerFirst, uniq, upperFirst
 import { DeptService } from './dept.service';
 import { MenusService } from './menus.service';
 import { BaseServiceV2 } from '../../frame/BaseServiceV2';
+import { UserMenusService } from './userMenus.service';
 @Provide()
 export class RoleService extends BaseServiceV2 {
   @InjectEntityModel(Role)
@@ -21,6 +22,10 @@ export class RoleService extends BaseServiceV2 {
 
   @Inject()
   menusService: MenusService;
+
+  @Inject()
+  userMenusService: UserMenusService;
+
   @Inject()
   ctx: Context;
   getQueryColumnsOpera(): object {
@@ -77,9 +82,24 @@ export class RoleService extends BaseServiceV2 {
     const apiCodes = getControllerCode(menuList);
     return apiCodes;
   }
-
+  /**
+   * 获取用户菜单
+   * @param needCode 是否只需要编码
+   * @returns 
+   */
   async getUserMenus(needCode = false) {
     const user = this.ctx.user;
+    let roles = get(user, 'role');
+    if (!roles) roles = [];
+    const userRoles = await this.model.createQueryBuilder().where(`"code" IN (:...val)`, { val: user.role }).getMany();
+    if (!userRoles || userRoles.length <= 0) return;
+    const hasAdmin = userRoles.filter(f => f.is_admin_role === '1');
+    if (hasAdmin.length <= 0) {
+      const mids = userRoles.map(i => i.menu);
+      const menusCodes = uniq(flattenDeep(mids));
+      const roleMenus = await this.getRoleUserMenus(menusCodes);
+      return { menus: roleMenus, role_code: menusCodes };
+    }
     // 平台的超级管理员有所有权限
     if (await this.isSuperAdmin()) {
       const menus = await this.menusService.queryMenu({ is_use: '0' });
@@ -88,7 +108,6 @@ export class RoleService extends BaseServiceV2 {
     // 其他用户需要把 部门 和 角色的权限叠加在一起
     // 部门权限
     const deptCodes = await this.getDeptCodes(get(user, 'dept'));
-
     // 角色权限
     const roleCodes = await this.getRoleCodes(get(user, 'role'));
     let allCodes = [...deptCodes, ...roleCodes];
@@ -104,6 +123,12 @@ export class RoleService extends BaseServiceV2 {
     treeMenu = this.menusService.treeMenu(menuList, treeMenu);
     return { menus: treeMenu, role_code: allCodes };
   }
+
+  async getRoleUserMenus(roles) {
+    const { data } = await this.userMenusService.query({ is_use: '0', route_name: roles }, { order: { order_num: 'ASC' } }, { route_name: this.Opera.In });
+    return data;
+  }
+
   /**
    * 整理出角色目录的编码,主要是与route_name编码对应
    * @param allCodes 本用户的角色+部门的完整权限编码