lrf402788946 4 년 전
부모
커밋
92c4420c60
2개의 변경된 파일22개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      app/controller/.answer.js
  2. 21 0
      app/service/answer.js

+ 1 - 1
app/controller/.answer.js

@@ -1,6 +1,6 @@
 module.exports = {
 module.exports = {
   create: {
   create: {
-    requestBody: ["questionnaire_id", "answer", "user_id"],
+    requestBody: ["questionnaire_id", "answer", "user_id", 'phone'],
   },
   },
   destroy: {
   destroy: {
     params: ["!id"],
     params: ["!id"],

+ 21 - 0
app/service/answer.js

@@ -11,6 +11,27 @@ class AnswerService extends CrudService {
     this.model = this.ctx.model.Answer;
     this.model = this.ctx.model.Answer;
   }
   }
 
 
+  async create(body) {
+    const { phone, user_id } = body;
+    if (!user_id && phone) {
+      // 没有用户id,但是有电话,需要注册
+      // 查询用户
+      let user = await this.ctx.service.util.httpUtil.cpost('/spm', 'live', { model: 'personal', method: 'findOne', query: { phone } }, { method: 'useModel' });
+      if (user) {
+        body.user_id = user._id;
+        delete body.phone;
+      } else {
+        user = await this.ctx.service.util.httpUtil.cpost('/spm', 'live', { service: 'users.personal', method: 'create', body: { phone, name: '个人', code: 'WJDCXTJG', password: '123456', status: '1' } }, { method: 'useService' });
+        if (user) {
+          body.user_id = user._id;
+          delete body.phone;
+        } else throw new BusinessError(ErrorCode.SERVICE_FAULT, '创建用户失败');
+      }
+    }
+    const res = await this.model.create(body);
+    return res;
+  }
+
   async findOne({ questionnaire_id, user_id }) {
   async findOne({ questionnaire_id, user_id }) {
     assert(questionnaire_id, '缺少问卷信息');
     assert(questionnaire_id, '缺少问卷信息');
     assert(user_id, '缺少填写人信息');
     assert(user_id, '缺少填写人信息');