guhongwei 2 years ago
parent
commit
3a05e717b9

+ 5 - 3
app/controller/config/.appbasic.js

@@ -1,6 +1,7 @@
+
 module.exports = {
   create: {
-    requestBody: ["name", "logo_url", "foot_menus"],
+    requestBody: ['name','logo_url','foot_menus','account_btn'],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +9,7 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["name", "logo_url", "foot_menus"],
+    requestBody: ['name','logo_url','foot_menus','account_btn'],
   },
   show: {
     parameters: {
@@ -21,7 +22,8 @@ module.exports = {
       query: {
         "meta.createdAt@start": "meta.createdAt@start",
         "meta.createdAt@end": "meta.createdAt@end",
-        name: "name",
+        'name': 'name' ,
+
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 62 - 0
app/controller/config/.user.js

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

+ 13 - 0
app/controller/user.js

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

+ 1 - 0
app/model/appbasic.js

@@ -8,6 +8,7 @@ const appbasic = {
   name: { type: String, required: false, zh: '名称' }, //
   logo_url: { type: Array, required: false, zh: 'logo' }, //
   foot_menus: { type: Array, required: false, zh: '底部菜单' }, //
+  account_btn: { type: Array, required: false, zh: '账号功能按钮' }, //
 };
 const schema = new Schema(appbasic, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });

+ 29 - 0
app/model/user.js

@@ -0,0 +1,29 @@
+'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 user = {
+  account: { type: String, required: false, zh: '账号' }, //
+  password: { type: Secret, required: false, select: false, zh: '密码' }, //
+  phone: { type: String, required: false, zh: '联系电话' }, //
+  nick_name: { type: String, required: false, zh: '昵称' }, //
+  gender: { type: String, required: false, zh: '性别' }, //
+  logo_url: { type: Array, required: false, zh: '头像' }, //
+};
+const schema = new Schema(user, { toJSON: { getters: true, virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.index({ account: 1 });
+schema.index({ phone: 1 });
+schema.index({ nick_name: 1 });
+schema.index({ gender: 1 });
+
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('User', schema, 'user');
+};

+ 1 - 0
app/router.js

@@ -19,4 +19,5 @@ module.exports = app => {
   require('./z_router/leavemess')(app); // 留言
   // app
   require('./z_router/appbasic')(app); // app基本信息
+  require('./z_router/user')(app); // 用户信息
 };

+ 41 - 0
app/service/user.js

@@ -0,0 +1,41 @@
+'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 UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.model = this.ctx.model.User;
+  }
+  async resetPwd({ id }, { password }) {
+    const data = await this.model.findById(id);
+    if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    data.password = { secret: password };
+    await data.save();
+  }
+  /**
+   * 登陆
+   * @param {Object} body 登陆参数
+   * @param body.account
+   * @param body.password
+   */
+  async login({ account, password }) {
+    let user = await this.model.findOne({ account }, '+password');
+    if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    const { password: upwd } = user;
+    // if (status !== '1') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
+    if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
+    user = JSON.parse(JSON.stringify(user));
+    delete user.meta;
+    delete user.__v;
+    delete user.password;
+    const token = this.ctx.service.util.jwt.encrypt(user);
+    return token;
+  }
+}
+
+module.exports = UserService;

+ 30 - 0
app/z_router/user.js

@@ -0,0 +1,30 @@
+'use strict';
+// 路由配置
+const rKey = 'user'; // routerKey,路由前缀变量
+const cKey = 'user'; // controllerKey,controller名
+const zhKey = '用户';
+const routes = [
+  { method: 'get', path: `/${rKey}`, controller: `${cKey}.index`, name: `${cKey}Query`, zh: `${zhKey}列表查询` },
+  { method: 'get', path: `/${rKey}/:id`, controller: `${cKey}.show`, name: `${cKey}Show`, zh: `${zhKey}查询` },
+  { method: 'post', path: `/${rKey}/login`, controller: `${cKey}.login`, name: `${cKey}Login`, zh: `${zhKey}登陆` },
+  { method: 'post', path: `/${rKey}/resetPwd/:id`, controller: `${cKey}.resetPwd`, name: `${cKey}ResetPwd`, zh: `重置密码${zhKey}` },
+  { method: 'post', path: `/${rKey}`, controller: `${cKey}.create`, name: `${cKey}Create`, zh: `创建${zhKey}` },
+  { method: 'post', path: `/${rKey}/:id`, controller: `${cKey}.update`, name: `${cKey}Update`, zh: `修改${zhKey}` },
+  { method: 'delete', path: `/${rKey}/:id`, controller: `${cKey}.destroy`, name: `${cKey}Delete`, zh: `删除${zhKey}` },
+];
+
+module.exports = app => {
+  const { router, config } = app;
+  const mwares = app.middleware;
+  for (const route of routes) {
+    const { method, path, controller: ctl, zh } = route;
+    let { middleware = [] } = route;
+    if (!method || !path || !ctl) continue;
+    // 拼全路径
+    const allPath = `${config.routePrefix}${path}`;
+    // 处理中间件
+    if (middleware.length > 0) middleware = middleware.map(i => mwares[i]());
+    // 注册路由
+    router[method](zh, allPath, ...middleware, ctl);
+  }
+};