VoiceController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Http\Controllers\Goodnight;
  3. use App\Http\Controllers\Miniprogram\Auth;
  4. use App\Models\Goodnight\TopicExampleModel;
  5. use App\Models\Goodnight\TopicModel;
  6. use App\Services\Goodnight\VoiceService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use Tymon\JWTAuth\Exceptions\JWTException;
  10. class VoiceController extends Controller
  11. {
  12. /**
  13. * 创建声音
  14. * @param Request $request
  15. * @return array
  16. * @throws \App\Exceptions\DBException
  17. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  18. */
  19. public function store(Request $request)
  20. {
  21. $uid = Auth::auth();
  22. $this->validate($request, [
  23. 'cover' => 'required',
  24. 'voice' => 'required'
  25. ]);
  26. $gvService = new VoiceService();
  27. $data = $request->only(['cover', 'voice', 'topic_id', 'topic_example_id']);
  28. $data['uid'] = $uid;
  29. $id = $gvService->create($data);
  30. return array(
  31. 'code' => 200,
  32. 'message' => 'success',
  33. 'data' => [
  34. 'voice_id' => $id
  35. ]
  36. );
  37. }
  38. /**
  39. * 删除声音
  40. * @param int $voice_id
  41. * @return array
  42. * @throws \App\Exceptions\AlertException
  43. * @throws \App\Exceptions\DBException
  44. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  45. */
  46. public function delete(int $voice_id)
  47. {
  48. $uid = Auth::auth();
  49. $gvService = new VoiceService();
  50. $gvService->delete($uid, $voice_id);
  51. return array(
  52. 'code' => 200,
  53. 'message' => 'success'
  54. );
  55. }
  56. /**
  57. * 点赞声音
  58. * @param int $voice_id
  59. * @return array
  60. * @throws \App\Exceptions\AlertException
  61. * @throws \App\Exceptions\DBException
  62. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  63. */
  64. public function thumb(int $voice_id)
  65. {
  66. $uid = Auth::auth();
  67. $gvService = new VoiceService();
  68. $gvService->thumb($uid, $voice_id);
  69. return array(
  70. 'code' => 200,
  71. 'message' => 'success'
  72. );
  73. }
  74. /**
  75. * 标记声音
  76. * @param int $voice_id
  77. * @param Request $request
  78. * @return array
  79. * @throws \App\Exceptions\AlertException
  80. * @throws \App\Exceptions\DBException
  81. */
  82. public function tag(int $voice_id, Request $request)
  83. {
  84. try {
  85. $uid = Auth::auth();
  86. } catch (JWTException $e) {
  87. $uid = 0;
  88. }
  89. $this->validate($request, [
  90. 'tag' => 'required'
  91. ]);
  92. $gvService = new VoiceService();
  93. $data = array(
  94. 'voice_id' => $voice_id,
  95. 'tag' => $request->post('tag')
  96. );
  97. $data = $gvService->tag($uid, $data);
  98. return array(
  99. 'code' => 200,
  100. 'message' => 'success',
  101. 'data' => [
  102. 'goodnight_coin' => $data
  103. ]
  104. );
  105. }
  106. /**
  107. * 用户端获取声音信息
  108. * @param int $voice_id
  109. * @return array
  110. */
  111. public function get(int $voice_id)
  112. {
  113. try {
  114. $uid = Auth::auth();
  115. } catch (JWTException $e) {
  116. $uid = 0;
  117. }
  118. $gvService = new VoiceService();
  119. $data = $gvService->getVoice($uid, $voice_id);
  120. return array(
  121. 'code' => 200,
  122. 'message' => 'success',
  123. 'data' => $data
  124. );
  125. }
  126. /**
  127. * 我录制的声音
  128. * @param Request $request
  129. * @return array
  130. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  131. */
  132. public function myvoices(Request $request)
  133. {
  134. $uid = Auth::auth();
  135. $page = $request->get('page') ?? 1;
  136. $pre_page = $request->input('pre_page', 20);
  137. $gvService = new VoiceService();
  138. $data = $gvService->myVoice($page, $pre_page, $uid);
  139. return array(
  140. 'code' => 200,
  141. 'message' => 'success',
  142. 'data' => [
  143. 'page' => $page,
  144. 'limit' => $pre_page,
  145. 'total' => $data['total'],
  146. 'list' => $data['list']
  147. ]
  148. );
  149. }
  150. /**
  151. * 用户录制的声音
  152. * @param Request $request
  153. * @param int $uid
  154. * @return array
  155. */
  156. public function uservoices(Request $request, int $uid)
  157. {
  158. $page = $request->get('page') ?? 1;
  159. $gvService = new VoiceService();
  160. $data = $gvService->userVoice($page, 20, $uid);
  161. return response([
  162. 'code' => 200,
  163. 'message' => 'success',
  164. 'data' => [
  165. 'page' => $page,
  166. 'limit' => 20,
  167. 'total' => $data['total'],
  168. 'list' => $data['list']
  169. ]
  170. ]);
  171. }
  172. /**
  173. * 后台-列表-审核声音
  174. * @param Request $request
  175. * @return array
  176. */
  177. public function voicelist(Request $request)
  178. {
  179. $page = $request->get('page') ?? 1;
  180. $search = $request->only('check', 'tag', 'search', 'expedit', 'commit');
  181. $pages = array(
  182. 'page' => $page,
  183. 'limit' => 20
  184. );
  185. $gvService = new VoiceService();
  186. $data = $gvService->checkList($pages, $search);
  187. return response([
  188. 'code' => 200,
  189. 'message' => 'success',
  190. 'data' => [
  191. 'page' => $page,
  192. 'limit' => 20,
  193. 'total' => $data['total'],
  194. 'list' => $data['list']
  195. ]
  196. ]);
  197. }
  198. /**
  199. * 后台-列表-推荐声音
  200. * @param int $voice_id
  201. * @return array
  202. * @throws \App\Exceptions\DBException
  203. */
  204. public function commit(int $voice_id): array
  205. {
  206. $gvService = new VoiceService();
  207. $gvService->commit($voice_id);
  208. return array(
  209. 'code' => 200,
  210. 'message' => 'success'
  211. );
  212. }
  213. /**
  214. * 后台-列表-审核声音
  215. * @param Request $request
  216. * @param int $voice_id
  217. * @return array
  218. * @throws \App\Exceptions\DBException
  219. */
  220. public function check(Request $request, int $voice_id): array
  221. {
  222. $this->validate($request, [
  223. 'check' => 'required|in:-1,1',
  224. ]);
  225. $check = $request->post('check');
  226. $gvService = new VoiceService();
  227. $gvService->check($voice_id, $check);
  228. return array(
  229. 'code' => 200,
  230. 'message' => 'success'
  231. );
  232. }
  233. /**
  234. * 后台-列表-标记声音
  235. * @param int $voice_id
  236. * @return array
  237. * @throws \App\Exceptions\DBException
  238. */
  239. public function admintag(Request $request, int $voice_id): array
  240. {
  241. $this->validate($request, [
  242. 'tag' => 'required'
  243. ]);
  244. $gvService = new VoiceService();
  245. $gvService->admintag($voice_id);
  246. return array(
  247. 'code' => 200,
  248. 'message' => 'success'
  249. );
  250. }
  251. /**
  252. * 加急晚安
  253. * @param int $voice_id
  254. * @return array
  255. * @throws JWTException
  256. * @throws \App\Exceptions\AlertException
  257. */
  258. public function expeditVoice(int $voice_id)
  259. {
  260. $uid = Auth::auth();
  261. $vs = new VoiceService();
  262. $vs->expeditVoice($voice_id, $uid);
  263. return array(
  264. 'code' => 200,
  265. 'message' => 'success'
  266. );
  267. }
  268. /**
  269. *
  270. * @return \Illuminate\Http\JsonResponse
  271. */
  272. public function topics()
  273. {
  274. $topics = TopicModel::select('id', 'topic')->where('show', '1')->orderBy("sort", "desc")->get();
  275. foreach ($topics as $topic) {
  276. $topic->examples = TopicExampleModel::select("id", "content")->where([
  277. 'show' => 1,
  278. 'topic_id' => $topic->id
  279. ])->get();
  280. }
  281. return response()->json([
  282. 'code' => 200,
  283. 'message' => 'OK',
  284. 'data' => $topics
  285. ]);
  286. }
  287. }