install.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. await this.initGoodsTags();
  19. }
  20. async initGoodsTags() {
  21. const model = this.ctx.model.System.GoodsTags;
  22. const p = path.resolve(this.dataIndex, 'goodsTags.js');
  23. const data = require(p);
  24. const loop = async (list, pid, level = 1) => {
  25. for (const i of list) {
  26. const { children = [], label, code } = i;
  27. let pdata;
  28. pdata = await model.findOne({ code });
  29. if (!pdata) pdata = await model.create({ label, code, pid, level });
  30. if (children.length > 0) loop(children, pdata._id, level + 1);
  31. }
  32. };
  33. await loop(data);
  34. }
  35. /**
  36. * 初始化测试顾客
  37. */
  38. async initTestCustomer() {
  39. const model = this.ctx.model.User.User;
  40. const num = await model.count({ code: 'self' });
  41. if (num > 0) return;
  42. const p = path.resolve(this.dataIndex, 'user.js');
  43. const data = require(p);
  44. await model.create(data);
  45. }
  46. /**
  47. * 初始化自营店
  48. */
  49. async initSelfShop() {
  50. const model = this.ctx.model.Shop.Shop;
  51. const num = await model.count({ code: 'self' });
  52. if (num > 0) return;
  53. const p = path.resolve(this.dataIndex, 'selfShop.js');
  54. const data = require(p);
  55. await model.create(data);
  56. }
  57. /**
  58. * 初始化超级管理员
  59. */
  60. async initAdmin() {
  61. const model = this.ctx.model.User.Admin;
  62. const num = await model.count();
  63. if (num > 0) return;
  64. const p = path.resolve(this.dataIndex, 'admin.js');
  65. const data = require(p);
  66. await model.create(data);
  67. }
  68. /**
  69. * 初始化字典
  70. */
  71. async initDict() {
  72. const indexModel = this.ctx.model.Dev.DictIndex;
  73. const dataModel = this.ctx.model.Dev.DictData;
  74. const p = path.resolve(this.dataIndex, 'dict.js');
  75. const data = require(p);
  76. for (const i of data) {
  77. const { children, ...others } = i;
  78. const num = await indexModel.count({ code: others.code });
  79. if (num > 0) continue;
  80. await indexModel.create(others);
  81. const newChildren = children.map(i => ({ ...i, code: others.code }));
  82. await dataModel.insertMany(newChildren);
  83. }
  84. }
  85. }
  86. module.exports = InstallService;