LiveController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\webApp;
  4. use App\Http\Validator\LiveValidator;
  5. use App\Services\Api\TaskService;
  6. use App\Services\LiveService;
  7. use App\Services\RedisService;
  8. /**
  9. * 在线直播
  10. * @package App\Http\Controllers\Api\v1
  11. */
  12. class LiveController extends webApp
  13. {
  14. /**
  15. * 列表
  16. * @return array
  17. */
  18. public function index()
  19. {
  20. try {
  21. $params = request()->post();
  22. $pageSize = request()->post('pageSize', 0);
  23. $params['is_recommend'] = 1;
  24. $datas = LiveService::make()->getDataList($params, $pageSize,'', $this->userId);
  25. return showJson(1010, true, $datas);
  26. } catch (\Exception $exception){
  27. RedisService::set("caches:request:error_live_index", ['error'=>$exception->getMessage(),'trace'=>$exception->getTrace()], 7200);
  28. return showJson(1018, false, ['error'=>env('APP_DEBUG')? $exception->getMessage() : '']);
  29. }
  30. }
  31. /**
  32. * 在线用户数据
  33. * @return array
  34. */
  35. public function users()
  36. {
  37. $params =request()->post();
  38. $pageSize = request()->post('pageSize', 15);
  39. $datas = LiveService::make()->getUserList($params, $pageSize, $this->userId);
  40. return showJson(1010, true, $datas);
  41. }
  42. /**
  43. * 礼物列表
  44. * @return array
  45. */
  46. public function giftList()
  47. {
  48. $params =request()->post();
  49. $pageSize = request()->post('pageSize', 15);
  50. $datas = LiveService::make()->getGiftList($params, $pageSize, $this->userId);
  51. return showJson(1010, true, $datas);
  52. }
  53. /**
  54. * 播放与浏览
  55. * @return array
  56. */
  57. public function updatePlay()
  58. {
  59. $id = request()->post('id',0);
  60. if(!$result = LiveService::make()->updatePlay($this->userId, $id)){
  61. return showJson(LiveService::make()->getError(), false);
  62. }else{
  63. return showJson(LiveService::make()->getError(), true, $result);
  64. }
  65. }
  66. /**
  67. * 获取流地址
  68. * @return array
  69. */
  70. public function getUrl()
  71. {
  72. $urls = LiveService::make()->getLiveUrl($this->userId);
  73. return showJson(1010, true, $urls);
  74. }
  75. /**
  76. * 直播详情
  77. * @param LiveValidator $validator
  78. * @return array
  79. */
  80. public function getInfo(LiveValidator $validator)
  81. {
  82. $params = request()->all();
  83. $params = $validator->check($params, 'info');
  84. if (!is_array($params)) {
  85. return showJson($params, false);
  86. }
  87. if(!$info = LiveService::make()->getInfo($params['id'], $this->userId)){
  88. return showJson(LiveService::make()->getError(),false);
  89. }else{
  90. return showJson(1010,true, $info);
  91. }
  92. }
  93. /**
  94. * 更新观看时长任务
  95. * @return array
  96. */
  97. public function updatePlayTask()
  98. {
  99. // 观看直播任务
  100. $id = request()->post('id',0);
  101. $time = request()->post('time',0);
  102. TaskService::make()->updateTask($this->userId,1, $id, $time);
  103. return showJson(1010,true, []);
  104. }
  105. /**
  106. * 状态
  107. * @return array|mixed
  108. */
  109. public function status()
  110. {
  111. if(!$result = LiveService::make()->status()){
  112. return showJson(LiveService::make()->getError(), false);
  113. }else{
  114. return showJson(LiveService::make()->getError(), true, $result);
  115. }
  116. }
  117. /**
  118. * 点赞
  119. * @return array|mixed
  120. */
  121. public function like()
  122. {
  123. $params = request()->post();
  124. if(!$result = LiveService::make()->like($this->userId, $params)){
  125. return showJson(LiveService::make()->getError(), false);
  126. }else{
  127. return showJson(LiveService::make()->getError(), true, $result);
  128. }
  129. }
  130. /**
  131. * 创建直播间(开启直播)
  132. * @return array
  133. */
  134. public function create(LiveValidator $validator)
  135. {
  136. $params = request()->all();
  137. $params = $validator->check($params, 'create');
  138. if (!is_array($params)) {
  139. return showJson($params, false);
  140. }
  141. if(!$result = LiveService::make()->create($this->userId, $params)){
  142. return showJson(LiveService::make()->getError(),false);
  143. }else{
  144. return showJson(LiveService::make()->getError(),true, $result);
  145. }
  146. }
  147. /**
  148. * 礼物打赏
  149. * @return array
  150. */
  151. public function reward()
  152. {
  153. $params = request()->all();
  154. if(!$result = LiveService::make()->reward($this->userId, $params)){
  155. return showJson(LiveService::make()->getError(),false);
  156. }else{
  157. return showJson(LiveService::make()->getError(),true, $result);
  158. }
  159. }
  160. }