|
@@ -0,0 +1,106 @@
|
|
|
+'use strict';
|
|
|
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const _ = require('lodash');
|
|
|
+const assert = require('assert');
|
|
|
+const path = require('path');
|
|
|
+
|
|
|
+//
|
|
|
+class InstallService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'install');
|
|
|
+ this.dataIndex = path.resolve('app', 'public', 'defaultData');
|
|
|
+ }
|
|
|
+
|
|
|
+ async init() {
|
|
|
+ // await this.initDict();
|
|
|
+ // await this.initAdmin();
|
|
|
+ // await this.initSelfShop();
|
|
|
+ // await this.initTestCustomer();
|
|
|
+ // await this.initGoodsTags();
|
|
|
+ await this.initConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+ async initConfig() {
|
|
|
+ const model = this.ctx.model.System.Config;
|
|
|
+ const num = await model.count({});
|
|
|
+ if (num > 0) return;
|
|
|
+ const p = path.resolve(this.dataIndex, 'config.js');
|
|
|
+ const data = require(p);
|
|
|
+ await model.create(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ async initGoodsTags() {
|
|
|
+ const model = this.ctx.model.System.GoodsTags;
|
|
|
+ const p = path.resolve(this.dataIndex, 'goodsTags.js');
|
|
|
+ const data = require(p);
|
|
|
+ const loop = async (list, pid, level = 1) => {
|
|
|
+ for (const i of list) {
|
|
|
+ const { children = [], label, code } = i;
|
|
|
+ let pdata;
|
|
|
+ pdata = await model.findOne({ code });
|
|
|
+ if (!pdata) pdata = await model.create({ label, code, pid, level });
|
|
|
+ if (children.length > 0) loop(children, pdata._id, level + 1);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ await loop(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化测试顾客
|
|
|
+ */
|
|
|
+ async initTestCustomer() {
|
|
|
+ const model = this.ctx.model.User.User;
|
|
|
+ await model.deleteMany({ name: '测试用户', phone: '11111111111' });
|
|
|
+ // const num = await model.count({ code: 'self' });
|
|
|
+ // if (num > 0) return;
|
|
|
+ // const p = path.resolve(this.dataIndex, 'user.js');
|
|
|
+ // const data = require(p);
|
|
|
+ // await model.create(data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化自营店
|
|
|
+ */
|
|
|
+ async initSelfShop() {
|
|
|
+ const model = this.ctx.model.Shop.Shop;
|
|
|
+ const num = await model.count({ code: 'self' });
|
|
|
+ if (num > 0) return;
|
|
|
+ const p = path.resolve(this.dataIndex, 'selfShop.js');
|
|
|
+ const data = require(p);
|
|
|
+ await model.create(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化超级管理员
|
|
|
+ */
|
|
|
+ async initAdmin() {
|
|
|
+ const model = this.ctx.model.User.Admin;
|
|
|
+ const num = await model.count();
|
|
|
+ if (num > 0) return;
|
|
|
+ const p = path.resolve(this.dataIndex, 'admin.js');
|
|
|
+ const data = require(p);
|
|
|
+ await model.create(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化字典
|
|
|
+ */
|
|
|
+ async initDict() {
|
|
|
+ const indexModel = this.ctx.model.Dev.DictIndex;
|
|
|
+ const dataModel = this.ctx.model.Dev.DictData;
|
|
|
+ const p = path.resolve(this.dataIndex, 'dict.js');
|
|
|
+ const data = require(p);
|
|
|
+ for (const i of data) {
|
|
|
+ const { children, ...others } = i;
|
|
|
+ const num = await indexModel.count({ code: others.code });
|
|
|
+ if (num > 0) continue;
|
|
|
+ await indexModel.create(others);
|
|
|
+ const newChildren = children.map(i => ({ ...i, code: others.code }));
|
|
|
+ await dataModel.insertMany(newChildren);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = InstallService;
|