lrf 2 years ago
parent
commit
71c7623911

+ 3 - 2
app/controller/config/.user.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['!openid', 'name', 'gender', 'phone', 'email', 'type', 'icon'],
+    requestBody: ['!openid', 'name', 'gender', 'phone', 'email', 'type', 'icon', 'card'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['openid', 'name', 'gender', 'phone', 'email', 'type', 'icon'],
+    requestBody: ['openid', 'name', 'gender', 'phone', 'email', 'type', 'icon', 'card'],
   },
   show: {
     parameters: {
@@ -25,6 +25,7 @@ module.exports = {
         gender: 'gender',
         phone: 'phone',
         type: 'type',
+        card: 'card',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 8 - 0
app/middleware/UserCreateCheck.js

@@ -0,0 +1,8 @@
+'use strict';
+const _ = require('lodash');
+module.exports = options => {
+  return async function userCreateCheck(ctx, next) {
+    await ctx.service.user.createCheck();
+    await next();
+  };
+};

+ 2 - 0
app/model/user.js

@@ -7,6 +7,7 @@ const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');
 // 用户表
 const user = {
   openid: { type: String, required: false, zh: 'openid' }, // openid
+  card: { type: String, required: false, zh: '身份证号' }, // openid
   name: { type: String, required: false, zh: '用户名' }, // 用户名
   gender: { type: String, required: false, zh: '性别' }, // 性别
   phone: { type: String, required: false, zh: '手机号' }, // 手机号
@@ -18,6 +19,7 @@ const schema = new Schema(user, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.index({ name: 1 });
+schema.index({ card: 1 });
 schema.index({ gender: 1 });
 schema.index({ phone: 1 });
 schema.index({ type: 1 });

+ 2 - 1
app/public/routerRegister.js

@@ -7,6 +7,7 @@
  * @param {String} rkey 路由主位置的标识
  * @param {String} ckey 路由对应的controller的标识
  */
+const _ = require('lodash');
 module.exports = (app, routes, keyZh, rkey, ckey) => {
   const { router, config } = app;
   const mwares = app.middleware;
@@ -19,7 +20,7 @@ module.exports = (app, routes, keyZh, rkey, ckey) => {
     const allPath = `${config.routePrefix}/${path}`;
     if (process.env.NODE_ENV === 'development') console.log(`     ${zh}: ${allPath}`);
     // 处理中间件
-    if (middleware.length > 0) middleware = middleware.map(i => mwares[i]({ enable: true, model: ckey || rkey }));
+    if (middleware.length > 0) middleware = middleware.map(i => _.get(mwares, i)({ enable: true, model: ckey || rkey }));
     // 注册路由
     router[method](zh, allPath, ...middleware, ctl);
   }

+ 12 - 1
app/service/user.js

@@ -9,6 +9,18 @@ class UserService extends CrudService {
     this.model = this.ctx.model.User;
   }
 
+  async createCheck() {
+    const data = this.ctx.request.body;
+    const { card, phone, openid } = data;
+    let num = await this.model.count({ card });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该身份证号已被注册');
+    num = await this.model.count({ phone });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该手机号码已被注册');
+    num = await this.model.count({ openid });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该微信号码已被注册');
+    return true;
+  }
+
   /**
    * 微信小程序登录
    * @param {String} openid 微信小程序的openid
@@ -21,7 +33,6 @@ class UserService extends CrudService {
     // const newUser = await this.model.create({ openid });
     // return newUser;
   }
-
 }
 
 module.exports = UserService;

+ 1 - 1
app/z_router/user.js

@@ -8,7 +8,7 @@ const routes = [
   { method: 'post', path: `${rkey}/wxAppLogin`, controller: `${ckey}.wxAppLogin`, name: `${ckey}WxAppLogin`, 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}`, controller: `${ckey}.create`, middleware: [ 'password', 'userCreateCheck' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
   { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
   { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
 ];