123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865 |
- 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<Menus>;
- @InjectEntityModel(Role)
- Role: Repository<Role>;
- @InjectEntityModel(UserMenus)
- UserMenus: Repository<UserMenus>;
- @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}"));`);
- }
- }
|