install.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.initAdmin();
  16. await this.initSelfShop();
  17. await this.initTestCustomer();
  18. }
  19. /**
  20. * 初始化测试顾客
  21. */
  22. async initTestCustomer() {
  23. const model = this.ctx.model.User.User;
  24. const num = await model.count({ code: 'self' });
  25. if (num > 0) return;
  26. const p = path.resolve(this.dataIndex, 'selfShop.js');
  27. const data = require(p);
  28. await model.create(data);
  29. }
  30. /**
  31. * 初始化自营店
  32. */
  33. async initSelfShop() {
  34. const model = this.ctx.model.Shop.Shop;
  35. const num = await model.count({ code: 'self' });
  36. if (num > 0) return;
  37. const p = path.resolve(this.dataIndex, 'selfShop.js');
  38. const data = require(p);
  39. await model.create(data);
  40. }
  41. /**
  42. * 初始化超级管理员
  43. */
  44. async initAdmin() {
  45. const model = this.ctx.model.User.Admin;
  46. const num = await model.count();
  47. if (num > 0) return;
  48. const p = path.resolve(this.dataIndex, 'admin.js');
  49. const data = require(p);
  50. await model.create(data);
  51. }
  52. /**
  53. * 初始化字典
  54. */
  55. async initDict() {
  56. const indexModel = this.ctx.model.Dev.DictIndex;
  57. const dataModel = this.ctx.model.Dev.DictData;
  58. const p = path.resolve(this.dataIndex, 'dict.js');
  59. const data = require(p);
  60. for (const i of data) {
  61. const { children, ...others } = i;
  62. const num = await indexModel.count({ code: others.code });
  63. if (num > 0) continue;
  64. await indexModel.create(others);
  65. const newChildren = children.map(i => ({ ...i, code: others.code }));
  66. await dataModel.insertMany(newChildren);
  67. }
  68. }
  69. }
  70. module.exports = InstallService;