install.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. await model.deleteMany({ name: '测试用户', phone: '11111111111' });
  41. // const num = await model.count({ code: 'self' });
  42. // if (num > 0) return;
  43. // const p = path.resolve(this.dataIndex, 'user.js');
  44. // const data = require(p);
  45. // await model.create(data);
  46. }
  47. /**
  48. * 初始化自营店
  49. */
  50. async initSelfShop() {
  51. const model = this.ctx.model.Shop.Shop;
  52. const num = await model.count({ code: 'self' });
  53. if (num > 0) return;
  54. const p = path.resolve(this.dataIndex, 'selfShop.js');
  55. const data = require(p);
  56. await model.create(data);
  57. }
  58. /**
  59. * 初始化超级管理员
  60. */
  61. async initAdmin() {
  62. const model = this.ctx.model.User.Admin;
  63. const num = await model.count();
  64. if (num > 0) return;
  65. const p = path.resolve(this.dataIndex, 'admin.js');
  66. const data = require(p);
  67. await model.create(data);
  68. }
  69. /**
  70. * 初始化字典
  71. */
  72. async initDict() {
  73. const indexModel = this.ctx.model.Dev.DictIndex;
  74. const dataModel = this.ctx.model.Dev.DictData;
  75. const p = path.resolve(this.dataIndex, 'dict.js');
  76. const data = require(p);
  77. for (const i of data) {
  78. const { children, ...others } = i;
  79. const num = await indexModel.count({ code: others.code });
  80. if (num > 0) continue;
  81. await indexModel.create(others);
  82. const newChildren = children.map(i => ({ ...i, code: others.code }));
  83. await dataModel.insertMany(newChildren);
  84. }
  85. }
  86. }
  87. module.exports = InstallService;