FormController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Admin\Pair;
  3. use App\Exports\FormExport;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Fpdx\FormModel;
  7. use App\Models\Fpdx\FormQuestionModel;
  8. use Maatwebsite\Excel\Facades\Excel;
  9. class FormController extends Controller
  10. {
  11. /**
  12. * 表单信息
  13. * @param Request $request
  14. * @return array
  15. */
  16. public function show(Request $request, $id)
  17. {
  18. $form = FormModel::findOrFail($id);
  19. // 获取问题列表
  20. $questions = FormQuestionModel::where('form_id', $form->id)
  21. ->where('deleted_at', 0)
  22. ->orderBy('sort', 'asc')
  23. ->get();
  24. foreach ($questions as $question) {
  25. switch ($question->type) {
  26. case "checkbox":
  27. case "radio":
  28. $question->options = json_decode($question->options, true);
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. $form->questions = $questions;
  35. return response([
  36. 'code' => 200,
  37. 'message' => 'OK',
  38. 'data' => $form
  39. ]);
  40. }
  41. /**
  42. * 表单列表
  43. * @param Request $request
  44. * @return array
  45. */
  46. public function index(Request $request)
  47. {
  48. $forms = FormModel::where('deleted_at', 0)
  49. ->orderBy('id', 'desc')
  50. ->paginate($request->get('pre_page', 20));
  51. return response([
  52. 'code' => 200,
  53. 'message' => 'OK',
  54. 'data' => $forms
  55. ]);
  56. }
  57. /**
  58. * 创建表单
  59. * @param Request $request
  60. * @return array
  61. */
  62. public function store(Request $request)
  63. {
  64. $this->validate($request, [
  65. 'title' => 'required|max:32',
  66. 'stage_id' => 'required|integer',
  67. 'activity_type' => 'in:72h,qbj',
  68. ]);
  69. $form = new FormModel();
  70. $form->fill($request->all());
  71. $form->save();
  72. return response([
  73. 'code' => 200,
  74. 'message' => 'OK',
  75. 'data' => $form
  76. ]);
  77. }
  78. /**
  79. * 修改表单
  80. * @param Request $request
  81. * @return array
  82. */
  83. public function update(Request $request)
  84. {
  85. $this->validate($request, [
  86. 'id' => 'required|integer',
  87. 'stage_id' => 'integer',
  88. 'activity_type' => 'in:72h,qbj',
  89. 'title' => 'max:32',
  90. 'describe' => 'max:100',
  91. 'deleted_at' => 'integer',
  92. 'showed_at' => 'integer',
  93. ]);
  94. $form = FormModel::findOrFail($request->id);
  95. $form->fill($request->all());
  96. $form->save();
  97. return response([
  98. 'code' => 200,
  99. 'data' => $form
  100. ]);
  101. }
  102. /**
  103. * 创建问题
  104. * @param Request $request
  105. * @return array
  106. */
  107. public function createQuestion(Request $request, $form_id)
  108. {
  109. $this->validate($request, [
  110. 'question' => 'required|max:64',
  111. 'type' => 'required|in:rate,input,textarea,radio,checkbox',
  112. 'required' => 'required|bool',
  113. 'sort' => 'required|integer',
  114. ]);
  115. $attributes = $request->all();
  116. $attributes['form_id'] = $request->form_id;
  117. switch ($attributes['type']) {
  118. case "checkbox":
  119. case "radio":
  120. $attributes['options'] = json_encode($attributes['options'], JSON_UNESCAPED_UNICODE);
  121. break;
  122. case "rate":
  123. $attributes['options'] = (int)$attributes['options'];
  124. break;
  125. default:
  126. break;
  127. }
  128. $question = FormQuestionModel::create($attributes);
  129. return response([
  130. 'code' => 200,
  131. 'data' => $question
  132. ]);
  133. }
  134. /**
  135. * 修改问题
  136. * @param Request $request
  137. * @return array
  138. */
  139. public function updateQuestion(Request $request, $form_id, $id)
  140. {
  141. $this->validate($request, [
  142. 'form_id' => 'integer',
  143. 'question' => 'max:64',
  144. 'type' => 'in:rate,input,textarea,radio,checkbox',
  145. 'required' => 'bool',
  146. 'sort' => 'integer',
  147. ]);
  148. $form = FormModel::findOrFail($form_id);
  149. $question = FormQuestionModel::where('form_id', $form->id)->where('id', $id)->firstOrFail();
  150. $attributes = $request->only('form_id', 'sort', 'question', 'type', 'required', 'options', 'deleted_at');
  151. if ($request->filled('options')) {
  152. switch ($attributes['type']) {
  153. case "checkbox":
  154. case "radio":
  155. $attributes['options'] = json_encode($attributes['options'], JSON_UNESCAPED_UNICODE);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. $question->update($attributes);
  162. return response([
  163. 'code' => 200,
  164. 'data' => $question
  165. ]);
  166. }
  167. public function export(Request $request, $form_id)
  168. {
  169. return Excel::download(new FormExport($form_id), "form_{$form_id}.xlsx");
  170. }
  171. }