import { Provide } from '@midwayjs/core'; import { InjectDataSource, InjectEntityModel } from '@midwayjs/typeorm'; import { DataSource, Repository } from 'typeorm'; import { Menus } from '../../entity/system/menus.entity'; import { cloneDeep, get } from 'lodash'; import { Role } from '../../entity/system/role.entity'; import { UserMenus } from '../../entity/system/userMenus.entity'; @Provide() export class InitSystemDataService { @InjectEntityModel(Menus) Menus: Repository; @InjectEntityModel(Role) Role: Repository; @InjectEntityModel(UserMenus) UserMenus: Repository; @InjectDataSource('default') defaultDataSource: DataSource; /**初始化角色的菜单(不包含管理员) */ async initRoleMenus() { const builder = this.Role.createQueryBuilder().update(Role); /**共有: 基础信息,认证信息,行业动态 */ const common = ['center_basic', 'center_attestation', 'center_notice', 'center_collection', 'center_password', 'center_sign']; // 只用通用菜单的角色 const justCommonRole = ['User', 'Association', 'Investment', 'State']; await builder.set({ menu: common }).where('code IN (:...value)', { value: justCommonRole }).execute(); // 第一类菜单 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 builder.set({ menu: menus1 }).where('code IN (:...value)', { value: roleUseMenus1 }).execute(); // 创业大赛用户: 需要去掉报名管理,加入赛事管理 const MatchRole = common.filter(f => f !== 'center_sign'); MatchRole.push('center_match'); await builder .set({ menu: MatchRole }) .where('code IN (:...value)', { value: ['Competition'] }) .execute(); } /**初始化角色数据 */ async initRoleData() { await this.clearTable(get(this.Role, 'metadata.givenTableName')); const datas: any = [ { code: 'Admin', name: '管理员', menu: ['home'], is_use: '0', is_default: '0', is_admin_role: '1', }, { code: 'User', name: '普通用户', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Expert', name: '专家', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Company', name: '企业', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Unit', name: '科研机构', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Incubator', name: '孵化基地', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Competition', name: '创业大赛', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Association', name: '商协会', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'School', name: '院校', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'State', name: '政府部门', is_use: '0', is_default: '0', is_admin_role: '0', }, { code: 'Investment', name: '投资人', is_use: '0', is_default: '0', is_admin_role: '0', }, ]; for (let i = 0; i < datas.length; i++) { const e = datas[i]; e.id = i + 1; } await this.Role.insert(datas); } /**初始化用户目录 */ async initUserMenus() { await this.clearTable(get(this.UserMenus, 'metadata.givenTableName')); 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 initSystemMenus() { await this.clearTable(get(this.Menus, 'metadata.givenTableName')); const datas: any = [ { name: '首页', path: '/', type: '1', is_default: '0', is_use: '0', route_name: 'home', }, { name: '系统管理', path: '/system/index', type: '1', is_default: '0', is_use: '0', route_name: 'system', children: [ { name: '管理员目录', component: 'admin-menus', type: '2', is_default: '0', is_use: '0', route_name: 'system_admin_menus', }, { name: '用户目录', component: 'user-menus', type: '2', is_default: '0', is_use: '0', route_name: 'system_user_menus', }, { name: '字典管理', component: 'dict', type: '2', is_default: '0', is_use: '0', route_name: 'system_dict', }, { name: '地区管理', component: 'region', type: '2', is_default: '0', is_use: '0', route_name: 'system_region', }, { name: '系统功能重置', component: 'system-func', type: '2', is_default: '0', is_use: '0', route_name: 'system_func', }, ], }, { name: '平台设置', path: '/platform/index', type: '1', is_default: '0', is_use: '0', route_name: 'platform', children: [ { name: '基础设置', component: 'basic', type: '2', is_default: '0', is_use: '0', route_name: 'platform_basic', }, { name: '部门管理', component: 'dept', type: '2', is_default: '0', is_use: '0', route_name: 'platform_dept', }, { name: '角色管理', component: 'role', type: '2', is_default: '0', is_use: '0', route_name: 'platform_role', }, { name: '标签管理', component: 'tags', type: '2', is_default: '0', is_use: '0', route_name: 'platform_tags', }, { name: '产业管理', component: 'sector', type: '2', is_default: '0', is_use: '0', route_name: 'platform_sector', }, { name: '合作伙伴', component: 'friend', type: '2', is_default: '0', is_use: '0', route_name: 'platform_friend', }, { name: '导入数据', component: 'import', type: '2', is_default: '0', is_use: '0', route_name: 'platform_import', }, ], }, { name: '信息管理', path: '/information/index', type: '1', is_default: '0', is_use: '0', route_name: 'information', children: [ { name: '平台数据', component: 'platform', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform', children: [ { name: '供给信息', component: 'supply', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_supply', }, { name: '需求信息', component: 'demand', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_demand', }, { name: '技术成果', component: 'achievement', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_achievement', }, { name: '双创活动', component: 'match', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_match', }, { name: '项目信息', component: 'project', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_project', }, { name: '中试信息', component: 'footplate', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_footplate', }, { name: '服务支撑', component: 'support', type: '2', is_default: '0', is_use: '0', route_name: 'information_platform_support', }, ], }, { name: '角色数据', component: 'role', type: '2', is_default: '0', is_use: '0', route_name: 'information_role', children: [ { name: '创业大赛', component: 'competition', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_competition', }, { name: '投资人', component: 'investment', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_investment', }, { name: '商协会', component: 'association', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_association', }, { name: '政府部门', component: 'state', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_state', }, { name: '院校', component: 'school', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_school', }, { name: '企业', component: 'company', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_company', }, { name: '专家', component: 'expert', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_expert', }, { name: '科研机构', component: 'unit', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_unit', }, { name: '孵化基地', component: 'incubator', type: '2', is_default: '0', is_use: '0', route_name: 'information_role_incubator', }, ], }, { name: '平台新闻', component: 'news', type: '2', is_default: '0', is_use: '0', route_name: 'information_news', children: [ { name: '政策信息', component: 'policy', type: '2', is_default: '0', is_use: '0', route_name: 'information_news_policy', }, { name: '新闻通知', component: 'news', type: '2', is_default: '0', is_use: '0', route_name: 'information_news_news', }, { name: '行业动态', component: 'trends', type: '2', is_default: '0', is_use: '0', route_name: 'information_news_trends', }, ], }, ], }, { name: '行研产研', path: '/journal/index', type: '1', is_default: '0', is_use: '0', route_name: 'journal', }, { name: '用户管理', path: '/user/index', type: '1', is_default: '0', is_use: '0', route_name: 'user', children: [ { name: '管理员用户', component: 'admin', type: '2', is_default: '0', is_use: '0', route_name: 'user_admin', }, { name: '平台用户', component: 'user', type: '2', is_default: '0', is_use: '0', route_name: 'user_user', }, ], }, { name: '审核管理', path: '/exam/index', type: '1', is_default: '0', is_use: '0', route_name: 'exam', children: [ { name: '审核设置', component: 'setting', type: '2', is_default: '0', is_use: '0', route_name: 'exam_setting', }, { name: '联系申请', component: 'contact', type: '2', is_default: '0', is_use: '0', route_name: 'exam_contact_apply', }, { name: '企业认领', component: 'company', type: '2', is_default: '0', is_use: '0', route_name: 'exam_company', }, ], }, { name: '数据看板', path: '/board/index', type: '1', is_default: '0', is_use: '0', route_name: 'board', children: [ { name: '企业', component: 'company', type: '2', is_default: '0', is_use: '0', route_name: 'board_company', }, { name: '专家', component: 'expert', type: '2', is_default: '0', is_use: '0', route_name: 'board_expert', }, { name: '需求', component: 'demand', type: '2', is_default: '0', is_use: '0', route_name: 'board_demand', }, { name: '供给', component: 'supply', type: '2', is_default: '0', is_use: '0', route_name: 'board_supply', }, { name: '成果', component: 'achievement', type: '2', is_default: '0', is_use: '0', route_name: 'board_achievement', }, { name: '项目', component: 'project', type: '2', is_default: '0', is_use: '0', route_name: 'board_project', }, { name: '孵化基地', component: 'incubate', type: '2', is_default: '0', is_use: '0', route_name: 'board_incubate', }, { name: '用户数据', component: 'user', type: '2', is_default: '0', is_use: '0', route_name: 'board_user', }, ], }, { name: '站内消息', order_num: 998, path: '/message/index', type: '1', is_default: '0', is_use: '0', route_name: 'message', }, { name: '日志管理', order_num: 999, path: '/log/index', type: '1', is_default: '0', is_use: '0', route_name: 'log', }, ]; // 声明每个数据id.降维并带上父级id // 递归处理 let id = 1; const deepDeal = (list, parent_id?) => { const result = []; for (let i = 0; i < list.length; i++) { const d = list[i]; // 补充id和order_num和parent_id d.id = cloneDeep(id); id = id + 1; if (!get(d, 'order_num')) d.order_num = i + 1; if (parent_id) { // 有上级id,则一定是子页面 d.parent_id = parent_id; d.type = '2'; } let children = get(d, 'children'); const cd = cloneDeep(d); delete cd.children; result.push(cd); if (!children) continue; children = deepDeal(children, d.id); result.push(...children); } return result; }; const odDatas = deepDeal(datas); await this.Menus.insert(odDatas); } /** * 清空表与id序列 * @param tableName 表名 小驼峰 */ private async clearTable(tableName: string) { await this.defaultDataSource.query(`TRUNCATE TABLE "public"."${tableName}" RESTART IDENTITY RESTRICT;`); await this.defaultDataSource.query(`SELECT setval('"${tableName}_id_seq"', (SELECT max(id) FROM "${tableName}"));`); } }