1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const path = require('path');
- //
- class InstallService extends CrudService {
- constructor(ctx) {
- super(ctx, 'install');
- this.dataIndex = path.resolve('app', 'public', 'defaultData');
- }
- async init() {
- await this.initRole();
- await this.initAdmin();
- await this.initTestBasic();
- }
- /**
- * 初始化角色数据
- */
- async initRole() {
- const model = this.ctx.model.System.Role;
- const num = await model.count();
- if (num > 0) return;
- const dataPath = path.resolve(this.dataIndex, 'role.js');
- const list = require(dataPath);
- await model.insertMany(list);
- }
- /**
- * 初始化管理员
- */
- async initAdmin() {
- const model = this.ctx.model.System.User;
- const roleModel = this.ctx.model.System.Role;
- const num = await model.count();
- if (num > 0) return;
- // 需要将role中menu的mode为all的数据取出来,这是超级管理员的角色信息
- const roleData = await roleModel.findOne({ code: 'sadmin' });
- if (!roleData) return;
- const { _id: role } = roleData;
- const dataPath = path.resolve(this.dataIndex, 'user.js');
- let list = require(dataPath);
- list = list.map(i => ({ ...i, role }));
- await model.insertMany(list);
- }
- /**
- * 初始化测试用户
- */
- async initTestBasic() {
- const model = this.ctx.model.Basic;
- const roleModel = this.ctx.model.System.Role;
- const userModel = this.ctx.model.System.User;
- const num = await model.count();
- if (num > 0) return;
- const roleData = await roleModel.findOne({ code: 'user' });
- if (!roleData) return;
- const { _id: role } = roleData;
- const dataPath = path.resolve(this.dataIndex, 'basic.js');
- const list = require(dataPath);
- const basicList = await model.insertMany(list);
- const users = basicList.map(i => {
- const { _id: basic, name, phone } = i;
- const obj = { role, basic, phone, name, account: name, password: { secret: '111111' } };
- return obj;
- });
- await userModel.insertMany(users);
- }
- }
- module.exports = InstallService;
|