123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace App\Http\Controllers\Deed;
- use App\Http\Resources\Invite\QuestionCollection;
- use App\Http\Resources\Invite\QuestionResource;
- use App\Http\Resources\Invite\QuestionTagCollection;
- use App\Models\Invite\QuestionTemplateModel;
- use App\Models\Invite\QuestionTagModel;
- use App\Models\User\InviteConfigModel;
- use App\Models\User\LikeInviteQuestionModel;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class QuestionController extends Controller
- {
- // 问题列表
- public function index(Request $request)
- {
- $type = $request->get('type', 'text');
- $questions = QuestionTemplateModel::where('type', $type)
- ->display(true)
- ->get();
- foreach ($questions as $question) {
- $question->tag;
- switch ($question->type) {
- case "music":
- $question->question = json_decode($question->question, true);
- break;
- }
- }
- return new QuestionCollection($questions);
- }
- /**
- * 获取指定问题
- * @param $id
- * @return QuestionResource
- */
- public function show($id)
- {
- $question = QuestionTemplateModel::findOrFail($id);
- $question->tag;
- switch ($question->type) {
- case "music":
- $question->question = json_decode($question->question, true);
- break;
- }
- return new QuestionResource($question);
- }
- /**
- * 获取随机一个问题
- * @param Request $request
- * @param $id
- * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
- */
- public function random(Request $request, $id)
- {
- $type = $request->get('type', 'text');
- $question = QuestionTemplateModel::where('type', $type)
- ->display(true)
- ->where('id', '<>', $id)
- ->inRandomOrder()
- ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
- $question->tag;
- switch ($question->type) {
- case "music":
- $question->question = json_decode($question->question, true);
- break;
- }
- return new QuestionResource($question);
- }
- /**
- * 获取随机一个问题
- * @param Request $request
- * @param $id
- * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
- */
- public function randomBatch(Request $request)
- {
- $type = $request->get('type', 'text');
- $question_ids = QuestionTemplateModel::where('type', $type)
- ->display(true)
- ->when($request->filled('filter'), function ($query) use ($request) {
- return $query->whereNotIn('id', explode(',', $request->get('filter')));
- })->pluck('id')->toArray();
- $random_ids = array_random($question_ids, 10);
- $questions = QuestionTemplateModel::find($random_ids);
- foreach ($questions as $question) {
- switch ($question->type) {
- case 'music':
- $question->question = json_decode($question->question, true);
- break;
- }
- }
- return new QuestionCollection($questions);
- }
- /**
- * 获取随机一个问题
- * @param Request $request
- * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
- */
- public function filter(Request $request)
- {
- $type = $request->get('type', 'text');
- $question = QuestionTemplateModel::where('type', $type)
- ->display(true)
- ->when($request->filled('filter'), function ($query) use ($request) {
- return $query->whereNotIn('id', explode(',', $request->get('filter')));
- })->inRandomOrder()
- ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
- $question->tag;
- switch ($question->type) {
- case "music":
- $question->question = json_decode($question->question, true);
- break;
- }
- return new QuestionResource($question);
- }
- /**
- * 标签列表
- * @param Request $request
- * @return QuestionTagCollection
- */
- public function tags(Request $request)
- {
- $type = $request->get('type', 'text');
- $tags = QuestionTagModel::where('type', $type)->display(true)->get();
- return new QuestionTagCollection($tags);
- }
- /**
- * 标签下的问题
- * @param Request $request
- * @param $id
- * @return QuestionCollection
- */
- public function tagQuestions(Request $request, $id)
- {
- $tag = QuestionTagModel::findOrFail($id);
- $questions = QuestionTemplateModel::where('tag_id', $tag->id)
- ->display(true)
- ->when($request->get('leader'), function ($query) use ($request) {
- return $query->where('select_count', '>', 0);
- })
- ->paginate($request->input('pre_page', 20));
- foreach ($questions as $question) {
- $question->tag;
- switch ($question->type) {
- case "music":
- case "song":
- $question->question = json_decode($question->question, true);
- break;
- }
- }
- return new QuestionCollection($questions);
- }
- /**
- * 获取领唱信息
- * @param Request $request
- * @param $template_id
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function showSingerByTemplate(Request $request, $template_id)
- {
- $signer = LikeInviteQuestionModel::where('question_type', 1)
- ->where('select', 1)
- ->where('template_id', $template_id)
- ->inRandomOrder()
- ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'author', 'voice_src', 'template_id']);
- $signer->user;
- $signer->tag;
- $question = QuestionTemplateModel::find($signer->template_id);
- $question->question = json_decode($question->question, true);
- $signer->question = $question;
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $signer
- ]);
- }
- /**
- * 获取随机领唱
- * @param Request $request
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function randomLeadSinger(Request $request)
- {
- if (
- $request->filled('uid')
- & $like_questions = InviteConfigModel::where('uid', $request->input('uid'))->value('task_sing_data')
- ) {
- // 用户设置的问题中过滤filter后,随机选择一体题
- $template_id = array_random(array_diff(
- array_column($like_questions, 'id'),
- explode(',', $request->get('filter'))
- ));
- $signer = LikeInviteQuestionModel::where('question_type', 1)
- ->where('select', 1)
- ->where('template_id', $template_id)
- ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
- } else {
- $signer = LikeInviteQuestionModel::where('question_type', 1)
- ->where('select', 1)
- ->when($request->get('filter'), function ($query) use ($request) {
- return $query->whereNotIn('template_id', explode(',', $request->get('filter')));
- })->inRandomOrder()
- ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
- }
- $signer->user;
- $signer->tag;
- $question = QuestionTemplateModel::findOrFail($signer->template_id);
- /** @var QuestionTemplateModel $question */
- $question->question = json_decode($question->question, true);
- $signer->question = $question;
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $signer
- ]);
- }
- /**
- * 获取指定领唱
- * @param Request $request
- * @param $id
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function getLeadSinger(Request $request, $id)
- {
- $signer = LikeInviteQuestionModel::where('question_type', 1)
- ->where('select', 1)
- ->findOrFail($id, ['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
- $signer->user;
- $signer->tag;
- $question = QuestionTemplateModel::find($signer->template_id);
- $question->question = json_decode($question->question, true);
- $signer->question = $question;
- return response([
- 'code' => 200,
- 'message' => 'OK',
- 'data' => $signer
- ]);
- }
- }
|