FormController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers\Fpdx;
  3. use App\Models\Fpdx\FormAnswerModel;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Controllers\Core\Auth;
  7. use App\Models\Fpdx\FormModel;
  8. use App\Models\Fpdx\FormQuestionModel;
  9. class FormController extends Controller
  10. {
  11. //
  12. public function show(Request $request)
  13. {
  14. $uid = Auth::auth();
  15. $form = FormModel::when($request->filled('id'), function ($query) use ($request) {
  16. return $query->where('id', $request->id);
  17. }, function ($query) use ($request) {
  18. return $query->where('stage_id', $request->stage_id)->where('activity_type', '72h');
  19. })->firstOrFail();
  20. if ($form->showed_at > time()) {
  21. return response([
  22. 'code' => 400,
  23. 'message' => '表单未到开放时间'
  24. ]);
  25. }
  26. // 获取问题列表
  27. $questions = FormQuestionModel::where('form_id', $form->id)->where('deleted_at', 0)->orderBy(
  28. 'sort',
  29. 'asc'
  30. )->get();
  31. foreach ($questions as $question) {
  32. switch ($question->type) {
  33. case "checkbox":
  34. case "radio":
  35. $question->options = json_decode($question->options, true);
  36. break;
  37. case "rate":
  38. $question->options = (int)$question->options;
  39. break;
  40. }
  41. }
  42. $form->questions = $questions;
  43. if (FormAnswerModel::where('form_id', $form->id)->where('uid', $uid)->first()) {
  44. $form->submit = 1;
  45. } else {
  46. $form->submit = 0;
  47. }
  48. return response([
  49. 'code' => 200,
  50. 'message' => 'OK',
  51. 'data' => $form
  52. ]);
  53. }
  54. public function submit(Request $request)
  55. {
  56. $this->validate($request, [
  57. 'form_id' => 'required|integer',
  58. 'answers' => 'required|array',
  59. ]);
  60. $uid = Auth::auth();
  61. if (FormAnswerModel::where('form_id', $request->form_id)->where('uid', $uid)->first()) {
  62. return response([
  63. 'code' => 410,
  64. 'message' => "您已提交过,不能重复提交"
  65. ]);
  66. }
  67. $answers = $request->input('answers');
  68. // 提交答案
  69. \DB::beginTransaction();
  70. $questions = FormQuestionModel::where('form_id', $request->form_id)->where('deleted_at', 0)->get();
  71. foreach ($questions as $question) {
  72. // 检测是否必填
  73. if (!isset($answers[$question->sort])) {
  74. if ($question->required) {
  75. \DB::rollBack();
  76. return ['code' => 40002, 'message' => "问题{$question->sort}"];
  77. } else {
  78. continue;
  79. }
  80. }
  81. // 答案类型
  82. switch ($question->type) {
  83. case "rate":
  84. case "input":
  85. case "textarea":
  86. case "radio":
  87. $answer = $answers[$question->sort];
  88. break;
  89. case "checkbox":
  90. $answer = json_encode($answers[$question->sort], JSON_UNESCAPED_UNICODE);
  91. break;
  92. }
  93. FormAnswerModel::create([
  94. 'uid' => $uid,
  95. 'form_id' => $request->form_id,
  96. 'question_id' => $question->id,
  97. 'type' => $question->type,
  98. 'answer' => $answer
  99. ]);
  100. }
  101. \DB::commit();
  102. return ['code' => 200, 'message' => 'OK'];
  103. }
  104. }