lrf402788946 4 năm trước cách đây
mục cha
commit
c9675b979c
4 tập tin đã thay đổi với 95 bổ sung0 xóa
  1. 21 0
      app/controller/user.js
  2. 23 0
      app/model/user.js
  3. 2 0
      app/router.js
  4. 49 0
      app/service/user.js

+ 21 - 0
app/controller/user.js

@@ -0,0 +1,21 @@
+'use strict';
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 管理员
+class UserController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.user;
+  }
+
+  async init() {
+    await this.service.init();
+    this.ctx.ok();
+  }
+  async login() {
+    const data = await this.service.login(this.ctx.request.body);
+    this.ctx.ok({ data });
+  }
+}
+module.exports = CrudController(UserController, {});

+ 23 - 0
app/model/user.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
+// 管理员表
+const user = {
+  name: { type: String }, // 名称
+  login_id: { type: String }, // 登陆口令
+  password: { type: Secret, select: false }, // 注册密码
+  openid: { type: String }, // 微信openid
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment(new Date()).format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(user, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('User', schema, 'user');
+};

+ 2 - 0
app/router.js

@@ -7,5 +7,7 @@ module.exports = app => {
   const { router, controller } = app;
   const profix = '/api/article/';
   router.get('/', controller.home.index);
+  router.post('user', `${profix}login`, controller.user.login);
+  router.post('user', `${profix}init`, controller.user.init);
   require('./router/refute')(app); // 文章/辟谣
 };

+ 49 - 0
app/service/user.js

@@ -0,0 +1,49 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const { ObjectId } = require('mongoose').Types;
+const _ = require('lodash');
+const assert = require('assert');
+const jwt = require('jsonwebtoken');
+
+// 管理员
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.model = this.ctx.model.User;
+  }
+
+  async init() {
+    const count = await this.model.count({ login_id: 'admin' });
+    if (count <= 0) {
+      const data = {
+        name: '管理员',
+        login_id: 'admin',
+        password: { secret: '123456' },
+      };
+      await this.model.create(data);
+    }
+
+  }
+
+  /**
+   * 管理员登陆
+   * @param {Object} params 登陆信息
+   * @property login_id code或者是phone
+   * @property password 密码
+   */
+  async login({ login_id, password }) {
+    const object = await this.model.findOne({ login_id }, '+password');
+    if (!object) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户的信息');
+    const { password: op } = object;
+    const { secret } = op;
+    if (secret !== password) throw new BusinessError(ErrorCode.BAD_PASSWORD, '密码错误');
+    const data = _.omit(JSON.parse(JSON.stringify(object)), [ 'meta', 'password', '__v' ]);
+    const { secret: secrets } = this.config.jwt;
+    const token = jwt.sign(data, secrets);
+    return token;
+  }
+
+}
+
+module.exports = UserService;