install.js 3.0 KB

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