Mplive.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace addons\shopro\controller\app;
  3. use addons\shopro\controller\Common;
  4. use app\admin\model\shopro\app\mplive\Room;
  5. use addons\shopro\facade\Wechat;
  6. use addons\shopro\library\mplive\ServiceProvider;
  7. class Mplive extends Common
  8. {
  9. protected $noNeedLogin = ['getRoomList', 'getMpLink'];
  10. protected $noNeedRight = ['*'];
  11. public function getRoomList()
  12. {
  13. // 通过客户端访问触发,每10分钟刷新一次房间状态
  14. $lastUpdateTime = cache('wechatMplive.update');
  15. if ($lastUpdateTime < (time() - 60 * 10)) {
  16. cache('wechatMplive.update', time());
  17. $app = Wechat::miniProgram();
  18. (new ServiceProvider())->register($app);
  19. $res = $app->broadcast->getRooms();
  20. $data = [];
  21. if (isset($res['errcode']) && ($res['errcode'] !== 0 && $res['errcode'] !== 1001)) {
  22. $this->error($res['errmsg'] ?? '');
  23. } else {
  24. // 更新直播间列表
  25. Room::where('roomid', '>', 0)->delete();
  26. foreach ($res['room_info'] as $room) {
  27. $room['status'] = $room['live_status'];
  28. $room['type'] = $room['live_type'];
  29. $data[] = $room;
  30. }
  31. Room::strict(false)->insertAll($data);
  32. }
  33. }
  34. $params = $this->request->param();
  35. $ids = $params['ids'] ?? '';
  36. $list = Room::where('roomid', 'in', $ids)->select();
  37. $this->success('获取成功', $list);
  38. }
  39. public function getMpLink()
  40. {
  41. $wechat = Wechat::miniProgram();
  42. (new ServiceProvider())->register($wechat);
  43. // TODO: 需防止被恶意消耗次数
  44. $res = $wechat->broadcast->urlscheme();
  45. $link = $res['openlink'];
  46. $this->success('获取成功', $link);
  47. }
  48. }