QuestionController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Http\Controllers\Deed;
  3. use App\Http\Resources\Invite\QuestionCollection;
  4. use App\Http\Resources\Invite\QuestionResource;
  5. use App\Http\Resources\Invite\QuestionTagCollection;
  6. use App\Models\Invite\QuestionTemplateModel;
  7. use App\Models\Invite\QuestionTagModel;
  8. use App\Models\User\InviteConfigModel;
  9. use App\Models\User\LikeInviteQuestionModel;
  10. use Illuminate\Http\Request;
  11. use App\Http\Controllers\Controller;
  12. class QuestionController extends Controller
  13. {
  14. // 问题列表
  15. public function index(Request $request)
  16. {
  17. $type = $request->get('type', 'text');
  18. $questions = QuestionTemplateModel::where('type', $type)
  19. ->display(true)
  20. ->get();
  21. foreach ($questions as $question) {
  22. $question->tag;
  23. switch ($question->type) {
  24. case "music":
  25. $question->question = json_decode($question->question, true);
  26. break;
  27. }
  28. }
  29. return new QuestionCollection($questions);
  30. }
  31. /**
  32. * 获取指定问题
  33. * @param $id
  34. * @return QuestionResource
  35. */
  36. public function show($id)
  37. {
  38. $question = QuestionTemplateModel::findOrFail($id);
  39. $question->tag;
  40. switch ($question->type) {
  41. case "music":
  42. $question->question = json_decode($question->question, true);
  43. break;
  44. }
  45. return new QuestionResource($question);
  46. }
  47. /**
  48. * 获取随机一个问题
  49. * @param Request $request
  50. * @param $id
  51. * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
  52. */
  53. public function random(Request $request, $id)
  54. {
  55. $type = $request->get('type', 'text');
  56. $question = QuestionTemplateModel::where('type', $type)
  57. ->display(true)
  58. ->where('id', '<>', $id)
  59. ->inRandomOrder()
  60. ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
  61. $question->tag;
  62. switch ($question->type) {
  63. case "music":
  64. $question->question = json_decode($question->question, true);
  65. break;
  66. }
  67. return new QuestionResource($question);
  68. }
  69. /**
  70. * 获取随机一个问题
  71. * @param Request $request
  72. * @param $id
  73. * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
  74. */
  75. public function randomBatch(Request $request)
  76. {
  77. $type = $request->get('type', 'text');
  78. $question_ids = QuestionTemplateModel::where('type', $type)
  79. ->display(true)
  80. ->when($request->filled('filter'), function ($query) use ($request) {
  81. return $query->whereNotIn('id', explode(',', $request->get('filter')));
  82. })->pluck('id')->toArray();
  83. $random_ids = array_random($question_ids, 10);
  84. $questions = QuestionTemplateModel::find($random_ids);
  85. foreach ($questions as $question) {
  86. switch ($question->type) {
  87. case 'music':
  88. $question->question = json_decode($question->question, true);
  89. break;
  90. }
  91. }
  92. return new QuestionCollection($questions);
  93. }
  94. /**
  95. * 获取随机一个问题
  96. * @param Request $request
  97. * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
  98. */
  99. public function filter(Request $request)
  100. {
  101. $type = $request->get('type', 'text');
  102. $question = QuestionTemplateModel::where('type', $type)
  103. ->display(true)
  104. ->when($request->filled('filter'), function ($query) use ($request) {
  105. return $query->whereNotIn('id', explode(',', $request->get('filter')));
  106. })->inRandomOrder()
  107. ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
  108. $question->tag;
  109. switch ($question->type) {
  110. case "music":
  111. $question->question = json_decode($question->question, true);
  112. break;
  113. }
  114. return new QuestionResource($question);
  115. }
  116. /**
  117. * 标签列表
  118. * @param Request $request
  119. * @return QuestionTagCollection
  120. */
  121. public function tags(Request $request)
  122. {
  123. $type = $request->get('type', 'text');
  124. $tags = QuestionTagModel::where('type', $type)->display(true)->get();
  125. return new QuestionTagCollection($tags);
  126. }
  127. /**
  128. * 标签下的问题
  129. * @param Request $request
  130. * @param $id
  131. * @return QuestionCollection
  132. */
  133. public function tagQuestions(Request $request, $id)
  134. {
  135. $tag = QuestionTagModel::findOrFail($id);
  136. $questions = QuestionTemplateModel::where('tag_id', $tag->id)
  137. ->display(true)
  138. ->when($request->get('leader'), function ($query) use ($request) {
  139. return $query->where('select_count', '>', 0);
  140. })
  141. ->paginate($request->input('pre_page', 20));
  142. foreach ($questions as $question) {
  143. $question->tag;
  144. switch ($question->type) {
  145. case "music":
  146. case "song":
  147. $question->question = json_decode($question->question, true);
  148. break;
  149. }
  150. }
  151. return new QuestionCollection($questions);
  152. }
  153. /**
  154. * 获取领唱信息
  155. * @param Request $request
  156. * @param $template_id
  157. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  158. */
  159. public function showSingerByTemplate(Request $request, $template_id)
  160. {
  161. $signer = LikeInviteQuestionModel::where('question_type', 1)
  162. ->where('select', 1)
  163. ->where('template_id', $template_id)
  164. ->inRandomOrder()
  165. ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'author', 'voice_src', 'template_id']);
  166. $signer->user;
  167. $signer->tag;
  168. $question = QuestionTemplateModel::find($signer->template_id);
  169. $question->question = json_decode($question->question, true);
  170. $signer->question = $question;
  171. return response([
  172. 'code' => 200,
  173. 'message' => 'OK',
  174. 'data' => $signer
  175. ]);
  176. }
  177. /**
  178. * 获取随机领唱
  179. * @param Request $request
  180. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  181. */
  182. public function randomLeadSinger(Request $request)
  183. {
  184. if (
  185. $request->filled('uid')
  186. & $like_questions = InviteConfigModel::where('uid', $request->input('uid'))->value('task_sing_data')
  187. ) {
  188. // 用户设置的问题中过滤filter后,随机选择一体题
  189. $template_id = array_random(array_diff(
  190. array_column($like_questions, 'id'),
  191. explode(',', $request->get('filter'))
  192. ));
  193. $signer = LikeInviteQuestionModel::where('question_type', 1)
  194. ->where('select', 1)
  195. ->where('template_id', $template_id)
  196. ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
  197. } else {
  198. $signer = LikeInviteQuestionModel::where('question_type', 1)
  199. ->where('select', 1)
  200. ->when($request->get('filter'), function ($query) use ($request) {
  201. return $query->whereNotIn('template_id', explode(',', $request->get('filter')));
  202. })->inRandomOrder()
  203. ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
  204. }
  205. $signer->user;
  206. $signer->tag;
  207. $question = QuestionTemplateModel::findOrFail($signer->template_id);
  208. /** @var QuestionTemplateModel $question */
  209. $question->question = json_decode($question->question, true);
  210. $signer->question = $question;
  211. return response([
  212. 'code' => 200,
  213. 'message' => 'OK',
  214. 'data' => $signer
  215. ]);
  216. }
  217. /**
  218. * 获取指定领唱
  219. * @param Request $request
  220. * @param $id
  221. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  222. */
  223. public function getLeadSinger(Request $request, $id)
  224. {
  225. $signer = LikeInviteQuestionModel::where('question_type', 1)
  226. ->where('select', 1)
  227. ->findOrFail($id, ['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
  228. $signer->user;
  229. $signer->tag;
  230. $question = QuestionTemplateModel::find($signer->template_id);
  231. $question->question = json_decode($question->question, true);
  232. $signer->question = $question;
  233. return response([
  234. 'code' => 200,
  235. 'message' => 'OK',
  236. 'data' => $signer
  237. ]);
  238. }
  239. }