Browse Source

增加用户管理

liuyu 5 years ago
parent
commit
857f158683
9 changed files with 191 additions and 1 deletions
  1. 1 1
      .vscode/settings.json
  2. 47 0
      app/controller/.user.js
  3. 22 0
      app/controller/user.js
  4. 24 0
      app/model/user.js
  5. 4 0
      app/router.js
  6. 59 0
      app/service/login.js
  7. 17 0
      app/service/user.js
  8. 16 0
      config/config.default.js
  9. 1 0
      package.json

+ 1 - 1
.vscode/settings.json

@@ -1,3 +1,3 @@
 {
-    "eggHelper.serverPort": 35684
+    "eggHelper.serverPort": 56637
 }

+ 47 - 0
app/controller/.user.js

@@ -0,0 +1,47 @@
+module.exports = {
+  create: {
+    requestBody: [
+      'name',
+      '!mobile',
+      '!passwd',
+      'openid',
+      'remark'
+    ]
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete'
+  },
+  update: {
+    params: ['!id'],
+    requestBody: [
+      'name',
+      'mobile',
+      'passwd',
+      'openid',
+      'remark'
+    ]
+  },
+  show: {
+    parameters: {
+      params: ['!id']
+    },
+    service: 'fetch'
+  },
+  index: {
+    parameters: {
+      query: {
+        name: 'name',
+        mobile: 'mobile',
+        openid: 'openid'
+      }
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
+      desc: true,
+      count: true
+    }
+  },
+};

+ 22 - 0
app/controller/user.js

@@ -0,0 +1,22 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.user.js');
+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 login() {
+    const res = await this.service.login(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+}
+
+module.exports = CrudController(UserController, meta);

+ 24 - 0
app/model/user.js

@@ -0,0 +1,24 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
+
+// 用户表
+const UserSchema = {
+  name: { type: String, required: false, maxLength: 200 }, // 名称
+  mobile: { type: String, required: true, maxLength: 64 }, // 手机
+  passwd: { type: Secret, select: false }, // 注册密码
+  openid: { type: String, required: false }, // 微信openid
+  remark: { type: String, required: false }, // 备注
+};
+
+
+const schema = new Schema(UserSchema, { toJSON: { virtuals: true } });
+schema.index({ openid: 1 });
+schema.index({ mobile: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('User', schema, 'user');
+};

+ 4 - 0
app/router.js

@@ -15,4 +15,8 @@ module.exports = app => {
   // 友情链接表设置路由
   router.resources('link', '/api/setting/link', controller.link); // index、create、show、destroy
   router.post('link', '/api/setting/link/update/:id', controller.link.update);
+
+  // 用户表设置路由
+  router.resources('user', '/api/setting/user', controller.user); // index、create、show、destroy
+  router.post('user', '/api/setting/user/update/:id', controller.user.update);
 };

+ 59 - 0
app/service/login.js

@@ -0,0 +1,59 @@
+'use strict';
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const jwt = require('jsonwebtoken');
+
+class LoginService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'login');
+    this.uModel = this.ctx.model.User;
+  }
+
+  async login(data) {
+    const { mobile, passwd } = data;
+    assert(mobile, 'mobile不能为空');
+    assert(/^\d{11}$/i.test(mobile), 'mobile无效');
+    assert(passwd, 'passwd不能为空');
+    let res = await this.uModel.findOne({ mobile }, '+passwd');
+    if (!res) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    // 验证密码
+    console.log(res.passwd.secret);
+    console.log(passwd);
+    if (res.passwd.secret !== passwd) {
+      throw new BusinessError(ErrorCode.BAD_PASSWORD);
+    }
+    return await this.createJwt(res);
+  }
+
+  // 创建登录Token
+  async createJwt({ _id, name, mobile, openid }) {
+    const { secret, expiresIn = '1d' } = this.config.jwt;
+    const subject = mobile;
+    const token = await jwt.sign({ userid: _id.toString(), name, openid }, secret, { expiresIn, issuer, subject });
+    return token;
+  }
+
+  async wxlogin(data) {
+    const { openid } = data;
+    assert(openid, 'openid不能为空');
+    let res = await this.uModel.findOne({ openid });
+    let newdata = {};
+    if (!res) {
+      res = await this.uModel.findOne({ openid });
+      if (!res) {
+        throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+      }
+      newdata = { id: res.id, name: res.name, openid: res.openid, type: 'patient' };
+    } else {
+      newdata = { id: res.id, name: res.name, openid: res.openid, type: 'doctor' };
+    }
+    return await newdata;
+  }
+}
+module.exports = LoginService;

+ 17 - 0
app/service/user.js

@@ -0,0 +1,17 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'set');
+    this.model = this.ctx.model.User;
+  }
+}
+
+module.exports = UserService;

+ 16 - 0
config/config.default.js

@@ -2,6 +2,8 @@
 
 'use strict';
 
+const { jwt } = require('./config.secret');
+
 /**
  * @param {Egg.EggAppInfo} appInfo app info
  */
@@ -42,6 +44,20 @@ module.exports = appInfo => {
     },
   };
 
+  // 安全配置
+  config.security = {
+    csrf: {
+      // ignoreJSON: true, // 默认为 false,当设置为 true 时,将会放过所有 content-type 为 `application/json` 的请求
+      enable: false,
+    },
+  };
+  // // JWT config
+  config.jwt = {
+    ...jwt,
+    expiresIn: '1d',
+    issuer: 'jobs',
+  };
+
   return {
     ...config,
     ...userConfig,

+ 1 - 0
package.json

@@ -10,6 +10,7 @@
     "egg": "^2.15.1",
     "egg-scripts": "^2.11.0",
     "lodash": "^4.17.15",
+    "jsonwebtoken": "^8.5.1",
     "silly-datetime": "^0.1.2",
     "naf-framework-mongoose": "^0.6.11"
   },