init.service.ts 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Provide } from '@midwayjs/decorator';
  2. import { InjectEntityModel } from '@midwayjs/typegoose';
  3. import { ReturnModelType } from '@typegoose/typegoose';
  4. import { Admin } from '../entity/system/admin.entity';
  5. import { Role } from '../entity/system/role.entity';
  6. import { Menus } from '../entity/system/menus.entity';
  7. import { DictData } from '../entity/system/dictData.entity';
  8. import { DictType } from '../entity/system/dictType.entity';
  9. import { Types } from 'mongoose';
  10. const ObjectId = Types.ObjectId;
  11. @Provide()
  12. export class InitService {
  13. @InjectEntityModel(Admin)
  14. adminModel: ReturnModelType<typeof Admin>;
  15. @InjectEntityModel(Role)
  16. roleModel: ReturnModelType<typeof Role>;
  17. @InjectEntityModel(Menus)
  18. menusModel: ReturnModelType<typeof Menus>;
  19. @InjectEntityModel(DictType)
  20. dictTypeModel: ReturnModelType<typeof DictType>;
  21. @InjectEntityModel(DictData)
  22. dictDataModel: ReturnModelType<typeof DictData>;
  23. async adminUser() {
  24. const data = {
  25. account: 'admin',
  26. password: '1qaz2wsx',
  27. nick_name: '系统管理员',
  28. is_super: '0',
  29. };
  30. const is_exist = await this.adminModel.count({ is_super: '0' });
  31. if (!is_exist) return await this.adminModel.create(data);
  32. return;
  33. }
  34. async initRole() {
  35. const num = await this.roleModel.count();
  36. if (num > 0) return;
  37. const datas = [{ name: '管理员', code: 'admin' }];
  38. await this.roleModel.insertMany(datas);
  39. }
  40. async initMenus() {
  41. const num = await this.menusModel.count();
  42. if (num > 0) return;
  43. const datas: any = [{ name: '首页', order_num: 1, path: '/homeIndex', component: '/home/index', type: '1' }];
  44. // 系统设置
  45. const smId = new ObjectId();
  46. const systemMenus = [
  47. { _id: smId, name: '系统设置', order_num: 2, type: '0' },
  48. { name: '菜单设置', parent_id: smId.toString(), order_num: 1, path: '/system/menus', component: '/system/menus/index', type: '1' },
  49. { name: '角色管理', parent_id: smId.toString(), order_num: 2, path: '/system/role', component: '/system/role/index', type: '1' },
  50. { name: '字典管理', parent_id: smId.toString(), order_num: 3, path: '/system/dict', component: '/system/dict/index', type: '1' },
  51. { name: '字典数据', parent_id: smId.toString(), order_num: 4, path: '/system/dictData', component: '/system/dictData/index', type: '2' },
  52. { name: '平台设置', parent_id: smId.toString(), order_num: 5, path: '/system/config', component: '/system/config/index', type: '1' },
  53. { name: '模块设置', parent_id: smId.toString(), order_num: 6, path: '/system/module', component: '/system/module/index', type: '1' },
  54. ];
  55. // 用户管理
  56. const umId = new ObjectId();
  57. const userMenus = [
  58. { _id: umId, name: '用户管理', order_num: 3, type: '0' },
  59. { name: '管理员用户', parent_id: umId.toString(), order_num: 1, path: '/user/admin', component: '/user/admin/index', type: '1' },
  60. { name: '平台用户', parent_id: umId.toString(), order_num: 2, path: '/user/user', component: '/user/user/index', type: '1' },
  61. ];
  62. const password = { name: '修改密码', order_num: 999, path: '/acccount/updatepd', component: '/acccount/updatepd/index', type: '1' };
  63. datas.push(...systemMenus, ...userMenus, password);
  64. // 项目业务菜单
  65. const busMenus = [
  66. { name: '公证员管理', order_num: 5, path: '/personnel', component: '/personnel/index', type: '1' },
  67. { name: '申办流程管理', order_num: 6, path: '/path', component: '/path/index', type: '1' },
  68. { name: '业务管理', order_num: 7, path: '/business', component: '/business/index', type: '1' },
  69. { name: '申请记录管理', order_num: 8, path: '/apply', component: '/apply/index', type: '1' },
  70. ];
  71. datas.push(...busMenus);
  72. await this.menusModel.insertMany(datas);
  73. }
  74. async initRoleMenu(admin: Admin) {}
  75. async initDict() {
  76. const isUseType = [{ title: '是否使用', code: 'isUse', is_use: '0' }];
  77. const isUseData = [
  78. { code: 'isUse', label: '使用', value: '0', sort: 1, is_use: '0' },
  79. { code: 'isUse', label: '禁用', value: '1', sort: 2, is_use: '0' },
  80. ];
  81. await this.dictTypeModel.insertMany(isUseType);
  82. await this.dictDataModel.insertMany(isUseData);
  83. }
  84. }