Voice.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\VoiceModel;
  4. use Illuminate\Http\Request;
  5. class Voice extends Controller
  6. {
  7. public function store(Request $request)
  8. {
  9. $this->validate($request, [
  10. 'content' => 'required',
  11. 'type' => 'required'
  12. ]);
  13. $model = new VoiceModel();
  14. $voice = $model->fill([
  15. 'content' => $request->input('content'),
  16. 'type' => $request->input('type')
  17. ]);
  18. if ($voice->save()) {
  19. return array(
  20. 'code' => 200,
  21. 'message' => 'success',
  22. 'data' => [
  23. 'voice_id' => $voice->id
  24. ],
  25. );
  26. } else {
  27. return array(
  28. 'code' => 505,
  29. 'message' => '数据库错误'
  30. );
  31. }
  32. }
  33. /**
  34. * @param Request $request
  35. * @param int $voice_id
  36. * @return array
  37. * @api {Post} /api/voice/:voice_id/update 更新语音独白
  38. * @apiName UpdateVoiceRead
  39. * @apiGroup Voice
  40. *
  41. * @apiParam {String} content 独白内容
  42. *
  43. * @apiSucsess {json} Success-Response:
  44. * HTTP/1.1 200 OK
  45. * {
  46. * "code": 200,
  47. * "message": "success"
  48. * }
  49. */
  50. public function update(Request $request, int $voice_id)
  51. {
  52. $model = new VoiceModel();
  53. $read = $model->find($voice_id);
  54. if (collect($read)->isEmpty()) {
  55. return array(
  56. 'code' => 101,
  57. 'message' => '参数错误'
  58. );
  59. }
  60. $read->content = $request->input('content');
  61. if ($read->save()) {
  62. return array(
  63. 'code' => 200,
  64. 'message' => 'success',
  65. 'data' => [
  66. 'id' => $read->id
  67. ],
  68. );
  69. } else {
  70. return array(
  71. 'code' => 505,
  72. 'message' => '数据库错误'
  73. );
  74. }
  75. }
  76. /**
  77. * @param string $type
  78. * @return array
  79. * @api {Get} /api/voice/:type 获取某个类型的独白
  80. * @apiName GetVoicesByType
  81. * @apiGroup Voice
  82. *
  83. * @apiParam {String} type 类型
  84. *
  85. * @apiSucsess {json} Success-Response:
  86. * HTTP/1.1 200 OK
  87. * {
  88. * "code": 200,
  89. * "message": "success"
  90. * }
  91. */
  92. public function get(string $type)
  93. {
  94. $model = new VoiceModel();
  95. $reads = $model->where('type', $type)->get();
  96. return array(
  97. 'code' => 200,
  98. 'message' => 'success',
  99. 'data' => $reads
  100. );
  101. }
  102. }