Selaa lähdekoodia

初始化数据

lrf 5 kuukautta sitten
vanhempi
commit
b2473295f5
1 muutettua tiedostoa jossa 85 lisäystä ja 11 poistoa
  1. 85 11
      src/service/frame/Init.service.ts

+ 85 - 11
src/service/frame/Init.service.ts

@@ -1,4 +1,4 @@
-import { Provide } from '@midwayjs/core';
+import { Autoload, Init, Scope, ScopeEnum } from '@midwayjs/core';
 import { InjectEntityModel } from '@midwayjs/typegoose';
 import { ReturnModelType } from '@typegoose/typegoose';
 import { Types } from 'mongoose';
@@ -8,7 +8,8 @@ import { DictType } from '../../entity/system/dictType.entity';
 import { Menus } from '../../entity/system/menus.entity';
 import { Role } from '../../entity/system/role.entity';
 const ObjectId = Types.ObjectId;
-@Provide()
+@Autoload()
+@Scope(ScopeEnum.Singleton)
 export class InitService {
   @InjectEntityModel(Admin)
   adminModel: ReturnModelType<typeof Admin>;
@@ -20,7 +21,14 @@ export class InitService {
   dictTypeModel: ReturnModelType<typeof DictType>;
   @InjectEntityModel(DictData)
   dictDataModel: ReturnModelType<typeof DictData>;
-
+  @Init()
+  async init() {
+    console.log('to init');
+    this.adminUser();
+    this.initRole();
+    this.initMenus();
+    this.initDict();
+  }
   async adminUser() {
     const data = {
       account: 'admin',
@@ -42,11 +50,28 @@ export class InitService {
   async initMenus() {
     const num = await this.menusModel.count();
     if (num > 0) return;
-    const datas: any = [{ name: '首页', order_num: 1, path: '/', component: '/home/index', type: '1', route_name: 'home' }];
+    const datas: any = [
+      {
+        name: '首页',
+        order_num: 1,
+        path: '/',
+        component: '/home/index',
+        type: '1',
+        route_name: 'home',
+      },
+    ];
     // 系统设置
     const smId = new ObjectId();
     const systemMenus = [
-      { _id: smId, name: '系统设置', path: '/system', order_num: 2, type: '0', route_name: 'system', is_default: '0' },
+      {
+        _id: smId,
+        name: '系统设置',
+        path: '/system',
+        order_num: 2,
+        type: '0',
+        route_name: 'system',
+        is_default: '0',
+      },
       {
         name: '菜单设置',
         parent_id: smId.toString(),
@@ -107,17 +132,66 @@ export class InitService {
           },
         ],
       },
-      { name: '字典管理', parent_id: smId.toString(), order_num: 3, path: '/system/dict', component: '/system/dict/index', type: '1', route_name: 'system_dict', is_default: '0' },
-      { name: '字典数据', parent_id: smId.toString(), order_num: 4, path: '/system/dictData', component: '/system/dictData/index', type: '2', route_name: 'system_dict_data', is_default: '0' },
+      {
+        name: '字典管理',
+        parent_id: smId.toString(),
+        order_num: 3,
+        path: '/system/dict',
+        component: '/system/dict/index',
+        type: '1',
+        route_name: 'system_dict',
+        is_default: '0',
+      },
+      {
+        name: '字典数据',
+        parent_id: smId.toString(),
+        order_num: 4,
+        path: '/system/dictData',
+        component: '/system/dictData/index',
+        type: '2',
+        route_name: 'system_dict_data',
+        is_default: '0',
+      },
     ];
     // 用户管理
     const umId = new ObjectId();
     const userMenus = [
-      { _id: umId, name: '用户管理', order_num: 3, path: '/user', type: '0', route_name: 'user', is_default: '1' },
-      { name: '管理员用户', parent_id: umId.toString(), order_num: 1, path: '/user/admin', component: '/user/admin/index', type: '1', route_name: 'user_admin' },
-      { name: '平台用户', parent_id: umId.toString(), order_num: 2, path: '/user/user', component: '/user/user/index', type: '1', route_name: 'user_user' },
+      {
+        _id: umId,
+        name: '用户管理',
+        order_num: 3,
+        path: '/user',
+        type: '0',
+        route_name: 'user',
+        is_default: '1',
+      },
+      {
+        name: '管理员用户',
+        parent_id: umId.toString(),
+        order_num: 1,
+        path: '/user/admin',
+        component: '/user/admin/index',
+        type: '1',
+        route_name: 'user_admin',
+      },
+      {
+        name: '平台用户',
+        parent_id: umId.toString(),
+        order_num: 2,
+        path: '/user/user',
+        component: '/user/user/index',
+        type: '1',
+        route_name: 'user_user',
+      },
     ];
-    const password = { name: '修改密码', order_num: 999, path: '/acccount/updatepd', component: '/acccount/updatepd/index', type: '1' , route_name: 'password'};
+    const password = {
+      name: '修改密码',
+      order_num: 999,
+      path: '/acccount/updatepd',
+      component: '/acccount/updatepd/index',
+      type: '1',
+      route_name: 'password',
+    };
 
     datas.push(...systemMenus, ...userMenus, password);
     // 项目业务菜单