lrf пре 2 година
родитељ
комит
6070e90a5c

+ 13 - 0
app/controller/user/admin.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./config/.admin.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class AdminController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.user.admin;
+  }
+}
+module.exports = CrudController(AdminController, meta);

+ 41 - 0
app/controller/user/config/.admin.js

@@ -0,0 +1,41 @@
+module.exports = {
+  create: {
+    requestBody: ['account', 'password', 'name'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['account', 'password', 'name'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+        account: 'account',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+  login: {
+    requestBody: ['!account', '!password'],
+  },
+};

+ 1 - 0
app/model/shop/shop.js

@@ -4,6 +4,7 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 
 // 商店信息
 const shop = {
+  user: { type: String, required: false, ref: 'User.User', zh: '用户' }, //
   name: { type: String, required: false, zh: '商店名称' }, //
   code: { type: String, required: false, zh: '店铺编号' }, // 自增,中间件处理
   person: { type: String, required: false, zh: '店主' }, //

+ 23 - 0
app/model/user/admin.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+
+const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');
+
+// 管理员
+const admin = {
+  account: { type: String, required: false, zh: '账号' }, //
+  password: { type: Secret, required: false, select: false, zh: '密码' }, //
+  name: { type: String, required: false, zh: '名称' }, //
+};
+const schema = new Schema(admin, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ account: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Admin', schema, 'admin');
+};

+ 5 - 0
app/public/defaultData/admin.js

@@ -0,0 +1,5 @@
+module.exports = {
+  account: 'sadmin',
+  password: { secret: '1qaz2wsx' },
+  name: '超级管理员',
+};

+ 6 - 0
app/public/defaultData/selfShop.js

@@ -0,0 +1,6 @@
+module.exports = {
+  name: '平台自营店',
+  code: 'self',
+  person: '平台',
+  status: '1',
+};

+ 5 - 0
app/public/defaultData/user.js

@@ -0,0 +1,5 @@
+module.exports = {
+  name: '测试用户',
+  phone: '11111111111',
+  password: { secret: '1qaz2wsx' },
+};

+ 36 - 0
app/service/user/admin.js

@@ -0,0 +1,36 @@
+'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');
+
+//
+class AdminService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'admin');
+    this.model = this.ctx.model.User.Admin;
+  }
+  /**
+   * 登陆
+   * @param {Object} body 登陆参数
+   * @param body.account 账户
+   * @param body.password 密码
+   */
+  async login({ account, password }) {
+    const { populate } = this.getRefMods();
+    const user = await this.model.findOne({ account }, '+password').populate(populate);
+    if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    const { password: upwd, status } = user;
+    if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
+    if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
+    // // 使用redis存储,后续的任何操作进行token的校验
+    // await this.setUserInRedis(user);
+    delete user.password;
+    delete user.meta;
+    delete user.__v;
+    const token = this.ctx.service.util.jwt.encrypt(user);
+    return token;
+  }
+}
+
+module.exports = AdminService;

+ 41 - 4
app/service/util/install.js

@@ -14,11 +14,48 @@ class InstallService extends CrudService {
 
   async init() {
     await this.initDict();
-  //   await this.initSelfShop();
-  //   await this.initTestCustomer();
-  //   await this.initTestGoods();
-  //   await this.initServiceContact();
+    await this.initAdmin();
+    await this.initSelfShop();
+    await this.initTestCustomer();
   }
+
+  /**
+   * 初始化测试顾客
+   */
+  async initTestCustomer() {
+    const model = this.ctx.model.User.User;
+    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 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);
+  }
+
   /**
    * 初始化字典
    */

+ 20 - 0
app/z_router/user/admin.js

@@ -0,0 +1,20 @@
+'use strict';
+// 路由配置
+const path = require('path');
+const regPath = path.resolve('app', 'public', 'routerRegister');
+const routerRegister = require(regPath);
+const rkey = 'admin';
+const ckey = 'user.admin';
+const keyZh = '管理员';
+const routes = [
+  { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}Login`, zh: `${keyZh}登陆` },
+  { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
+  { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, middleware: [ 'password' ], name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+];
+
+module.exports = app => {
+  routerRegister(app, routes, keyZh, rkey, ckey);
+};

+ 1 - 0
app/z_router/user/index.js

@@ -2,5 +2,6 @@
  * @param {Egg.Application} app - egg application
  */
 module.exports = app => {
+  require('./admin')(app); // 管理员
   require('./user')(app); // 用户
 };