Live.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use app\expand\controller\BaseAuth;
  5. use think\Cache;
  6. use think\Validate;
  7. /**
  8. * Author: luzheng.liu
  9. * Time: 2020/10/13 14:03
  10. */
  11. class Live extends BaseAuth {
  12. public function __construct() {
  13. parent::__construct();
  14. }
  15. /**
  16. * 获取直播列表
  17. */
  18. public function getLiveList() {
  19. $params = $this->_params;
  20. $rule = [
  21. 'start|开始房间号' => 'require',
  22. 'limit|数量' => 'require',
  23. ];
  24. $validate = new Validate($rule);
  25. if (!$validate->check($params)) {
  26. HelperService::returnJson(['code' => '400', 'msg' => $validate->getError(), 'data' => []]);
  27. }
  28. Cache::rm('access_token_' . $this->_apiCode);
  29. $accessToken = Cache::get('access_token_' . $this->_apiCode);
  30. if (empty($accessToken)) {
  31. $wx = new WechatJs();
  32. $accessToken = $wx->getWxToken();
  33. Cache::set('access_token_' . $this->_apiCode, $accessToken, 300);
  34. }
  35. $data = [
  36. "start" => $params['start'] ?: 0,
  37. 'limit' => $params['limit'] ?: 10,
  38. ];
  39. $roomList = HelperService::httpPost('https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=' . $accessToken, json_encode($data), false);
  40. $roomList = json_decode($roomList, true);
  41. if ($roomList['errcode'] === 1) {
  42. HelperService::returnJson(['code' => '200', 'msg' => '未创建直播间', 'data' => []]);
  43. }
  44. $room = [
  45. 'roomList' => $roomList['room_info'],
  46. 'total' => $roomList['total']
  47. ];
  48. HelperService::returnJson(['code' => '200', 'msg' => 'success', 'data' => $room]);
  49. }
  50. }