install.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const path = require('path');
  7. //
  8. class InstallService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'install');
  11. this.dataIndex = path.resolve('app', 'public', 'defaultData');
  12. }
  13. async init() {
  14. await this.initDict();
  15. // await this.initSelfShop();
  16. // await this.initTestCustomer();
  17. // await this.initTestGoods();
  18. // await this.initServiceContact();
  19. }
  20. /**
  21. * 初始化字典
  22. */
  23. async initDict() {
  24. const indexModel = this.ctx.model.Dev.DictIndex;
  25. const dataModel = this.ctx.model.Dev.DictData;
  26. const p = path.resolve(this.dataIndex, 'dict.js');
  27. const data = require(p);
  28. for (const i of data) {
  29. const { children, ...others } = i;
  30. const num = await indexModel.count({ code: others.code });
  31. if (num > 0) continue;
  32. await indexModel.create(others);
  33. const newChildren = children.map(i => ({ ...i, code: others.code }));
  34. await dataModel.insertMany(newChildren);
  35. }
  36. }
  37. }
  38. module.exports = InstallService;