QuestionTemplateController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers\Admin\Invite;
  3. use App\Http\Resources\Invite\QuestionTagCollection;
  4. use App\Models\Invite\QuestionTemplateModel;
  5. use App\Models\Invite\QuestionTagModel;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. class QuestionTemplateController extends Controller
  9. {
  10. // 创建问题
  11. public function store(Request $request)
  12. {
  13. $this->validate($request, [
  14. 'type' => 'required|in:text,music',
  15. 'question' => 'required',
  16. 'tag_id' => 'integer',
  17. 'author' => 'max:8'
  18. ]);
  19. $attribute = $request->only('type', 'question', 'tag_id', 'author');
  20. switch ($request->post('type', 'text')) {
  21. case "music":
  22. $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE);
  23. break;
  24. }
  25. QuestionTemplateModel::create($attribute);
  26. return response([
  27. 'code' => 200,
  28. 'message' => 'OK'
  29. ]);
  30. }
  31. // 修改问题
  32. public function update(Request $request, $id)
  33. {
  34. $this->validate($request, [
  35. 'type' => 'in:text,music',
  36. 'question' => '',
  37. 'tag_id' => 'integer',
  38. 'author' => 'max:8',
  39. 'hide' => '',
  40. ]);
  41. $attribute = $request->only('type', 'question', 'tag_id', 'author');
  42. switch ($request->post('type', 'text')) {
  43. case "music":
  44. $attribute['question'] = json_encode($request->post('question'), JSON_UNESCAPED_UNICODE);
  45. break;
  46. }
  47. $question = QuestionTemplateModel::findOrFail($id);
  48. $question->update($attribute);
  49. return response([
  50. 'code' => 200,
  51. 'message' => 'OK'
  52. ]);
  53. }
  54. // 标签列表
  55. public function index(Request $request)
  56. {
  57. $tags = QuestionTagModel::where('type', $request->get('type', 'text'))->get();
  58. foreach ($tags as $tag) {
  59. $questions = QuestionTemplateModel::where('tag_id', $tag->id)->get();
  60. foreach ($questions as $question) {
  61. switch ($question->type) {
  62. case "music":
  63. $question->question = json_decode($question->question, true);
  64. break;
  65. }
  66. }
  67. $tag->questions = $questions;
  68. }
  69. return new QuestionTagCollection($tags);
  70. }
  71. // 创建标签
  72. public function createTag(Request $request)
  73. {
  74. $this->validate($request, [
  75. 'type' => 'required|in:text,music',
  76. 'tag' => 'required|max:8'
  77. ]);
  78. QuestionTagModel::create($request->all());
  79. return response([
  80. 'code' => 200,
  81. 'message' => 'OK'
  82. ]);
  83. }
  84. // 修改标签
  85. public function updateTag(Request $request, $id)
  86. {
  87. $this->validate($request, [
  88. 'type' => 'in:text,music',
  89. 'tag' => 'max:8'
  90. ]);
  91. $questionTag = QuestionTagModel::findOrFail($id);
  92. $questionTag->update($request->all());
  93. return response([
  94. 'code' => 200,
  95. 'message' => 'OK'
  96. ]);
  97. }
  98. }