PaperController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Services\Exam\PaperService;
  4. /**
  5. * 试卷管理-控制器
  6. */
  7. class PaperController extends Backend
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->service = PaperService::make();
  13. }
  14. public function index()
  15. {
  16. $pageSize = request()->get('limit', 10);
  17. $list = $this->service->customList(request()->all(), $pageSize);
  18. $message = array(
  19. "msg" => '操作成功',
  20. "code" => 0,
  21. "data" => isset($list['list']) ? $list['list'] : [],
  22. "count" => isset($list['total']) ? $list['total'] : 0,
  23. );
  24. return $message;
  25. }
  26. public function import()
  27. {
  28. $formData = request()->input('formData', []);
  29. $topicsData = request()->input('topicsData', []);
  30. // 基本校验
  31. if (empty($formData['name'])) {
  32. return response()->json(['code' => 1, 'msg' => '试卷名称不能为空']);
  33. }
  34. if (empty($formData['subject_id'])) {
  35. return response()->json(['code' => 1, 'msg' => '题目分类不能为空']);
  36. }
  37. if (!is_array($topicsData) || count($topicsData) === 0) {
  38. return response()->json(['code' => 1, 'msg' => '题目数据不能为空']);
  39. }
  40. // 题目数据合法性检查
  41. foreach ($topicsData as $index => $topic) {
  42. if (empty($topic['topic_name'])) {
  43. return response()->json(['code' => 1, 'msg' => "第" . ($index + 1) . "题目内容不能为空"]);
  44. }
  45. if (empty($topic['score'])) {
  46. return response()->json(['code' => 1, 'msg' => "第" . ($index + 1) . "题目分数不能为空"]);
  47. }
  48. if (empty($topic['topic_type'])) {
  49. return response()->json(['code' => 1, 'msg' => "第" . ($index + 1) . "题目类型不能为空"]);
  50. }
  51. }
  52. return $this->service->createPaperWithTopics($formData, $topicsData);
  53. }
  54. }