InterlocutionController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Http\Controllers\User;
  3. use App\Events\AuditMedia;
  4. use App\Http\Controllers\Miniprogram\Auth;
  5. use App\Models\Interlocution\AnswerModel;
  6. use App\Models\Interlocution\QuestionModel;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. class InterlocutionController extends Controller
  10. {
  11. //
  12. public function questions(Request $request)
  13. {
  14. $questions = QuestionModel::get();
  15. return response([
  16. 'code' => 200,
  17. 'message' => 'OK',
  18. 'data' => $questions
  19. ]);
  20. }
  21. public function answers(Request $request, $uid)
  22. {
  23. $answers = AnswerModel::where('uid', $uid)->get();
  24. foreach ($answers as $answer) {
  25. $answer->question;
  26. }
  27. return response([
  28. 'code' => 200,
  29. 'data' => $answers
  30. ]);
  31. }
  32. /**
  33. * @param Request $request
  34. * @param $question_id
  35. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  36. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  37. * @deprecated UserInterlocution/CreateAnswer
  38. */
  39. public function createAnswer(Request $request, $question_id)
  40. {
  41. $this->validate($request, [
  42. 'content' => 'required|max:100',
  43. ]);
  44. $uid = Auth::auth();
  45. $answer = AnswerModel::updateOrCreate([
  46. 'uid' => $uid,
  47. 'question_id' => $question_id
  48. ], $request->all());
  49. // 文本检测
  50. event(new AuditMedia($uid, 'text', $request->input('content'), 'question', "id={$answer->id}"));
  51. return response([
  52. 'code' => 200,
  53. 'data' => $answer
  54. ]);
  55. }
  56. public function getAnswer(Request $request, $question_id)
  57. {
  58. $uid = Auth::auth();
  59. $question = QuestionModel::findOrFail($question_id);
  60. $question->answer = AnswerModel::where(['uid' => $uid, 'question_id' => $question_id])->first();
  61. return response([
  62. 'code' => 200,
  63. 'data' => $question
  64. ]);
  65. }
  66. }