WxLive.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\common\model\plus\live;
  3. use app\common\exception\BaseException;
  4. use app\common\library\easywechat\AppWx;
  5. use app\common\library\easywechat\wx\LiveRoom as WxLiveRoom;
  6. use app\common\model\BaseModel;
  7. /**
  8. * 微信直播模型
  9. */
  10. class WxLive extends BaseModel
  11. {
  12. protected $name = 'app_wx_live';
  13. protected $pk = 'live_id';
  14. //附加字段
  15. protected $append = ['start_time_text', 'end_time_text'];
  16. /**
  17. * 有效期-开始时间
  18. */
  19. public function getStartTimeTextAttr($value, $data)
  20. {
  21. return date('Y-m-d H:i:s', $data['start_time']);
  22. }
  23. /**
  24. * 有效期-开始时间
  25. * @param $value
  26. * @return array
  27. */
  28. public function getEndTimeTextAttr($value, $data)
  29. {
  30. return date('Y-m-d H:i:s', $data['end_time']);
  31. }
  32. /**
  33. * 详情
  34. */
  35. public static function detail($live_id)
  36. {
  37. return (new static())->find($live_id);
  38. }
  39. /**
  40. * 同步直播间
  41. */
  42. public function syn($app_id = null)
  43. {
  44. // 小程序配置信息
  45. $app = AppWx::getApp($app_id);
  46. // 请求api数据
  47. $live_room = new WxLiveRoom($app);
  48. $response = $live_room->syn();
  49. $isEmpty = false;
  50. if ($response === false) {
  51. if ($live_room->getError() == 'empty') {
  52. $isEmpty = true;
  53. } else {
  54. throw new BaseException(['msg' => '获取直播房间列表请求失败:' . $live_room->getError()]);
  55. }
  56. }
  57. // 格式化返回的列表数据
  58. $roomList = [];
  59. if (!$isEmpty) {
  60. foreach ($response['room_info'] as $item) {
  61. $roomList[$item['roomid']] = $item;
  62. }
  63. }
  64. $roomIds = array_column($roomList, 'roomid');
  65. // 本地历史数据
  66. $hasRoomIds = $this->getRoomIds();
  67. // 新增数据库没有的
  68. $this->addRoom($hasRoomIds, $roomIds, $roomList);
  69. // 删除微信小程序已删除的房间
  70. $this->deleteRoom($hasRoomIds, $roomIds);
  71. // 更新本地直播间
  72. $this->updateRoom($hasRoomIds, $roomIds, $roomList);
  73. return true;
  74. }
  75. /**
  76. * 获取本地直播间
  77. */
  78. private function getRoomIds()
  79. {
  80. return $this->where('is_delete', '=', 0)->column('roomid', 'live_id');
  81. }
  82. /**
  83. * 同步新增直播间
  84. */
  85. private function addRoom($hasRoomIds, $roomIds, $roomList)
  86. {
  87. // 需要新增的直播间ID
  88. $ids = array_values(array_diff($roomIds, $hasRoomIds));
  89. if (empty($ids)) return true;
  90. // 整理新增数据
  91. $saveData = [];
  92. foreach ($ids as $roomId) {
  93. $item = $roomList[$roomId];
  94. $saveData[] = [
  95. 'roomid' => $roomId,
  96. 'name' => $item['name'],
  97. 'cover_img' => $item['cover_img'],
  98. 'share_img' => $item['share_img'],
  99. 'anchor_name' => $item['anchor_name'],
  100. 'start_time' => $item['start_time'],
  101. 'end_time' => $item['end_time'],
  102. 'live_status' => $item['live_status'],
  103. 'app_id' => self::$app_id,
  104. ];
  105. }
  106. // 批量新增直播间
  107. return $this->saveAll($saveData);
  108. }
  109. /**
  110. * 同步删除直播间
  111. */
  112. private function deleteRoom($hasRoomIds, $roomIds)
  113. {
  114. // 需要删除的直播间ID
  115. $ids = array_values(array_diff($hasRoomIds, $roomIds));
  116. if (empty($ids)) return true;
  117. // 批量删除直播间
  118. return self::where('roomid', 'in', $ids)->delete();
  119. }
  120. /**
  121. * 修改本地直播间
  122. */
  123. private function updateRoom($hasRoomIds, $roomIds, $roomList)
  124. {
  125. // 需要新增的直播间ID
  126. $ids = array_values(array_intersect($roomIds, $hasRoomIds));
  127. if (empty($ids)) return true;
  128. // 整理新增数据
  129. $saveData = [];
  130. foreach ($ids as $roomId) {
  131. $item = $roomList[$roomId];
  132. $saveData[] = [
  133. 'live_id' => array_search($roomId, $hasRoomIds),
  134. 'roomid' => $roomId,
  135. 'name' => $item['name'],
  136. 'cover_img' => $item['cover_img'],
  137. 'share_img' => $item['share_img'],
  138. 'anchor_name' => $item['anchor_name'],
  139. 'start_time' => $item['start_time'],
  140. 'end_time' => $item['end_time'],
  141. 'live_status' => $item['live_status'],
  142. 'app_id' => self::$app_id,
  143. ];
  144. }
  145. // 批量新增直播间
  146. return $this->saveAll($saveData);
  147. }
  148. /**
  149. * 获取直播回放
  150. * @param $app_id
  151. * @return bool
  152. * @throws BaseException
  153. */
  154. public function getReplay($roomId, $app_id = null)
  155. {
  156. if (empty($roomId)) {
  157. throw new BaseException(['msg' => '缺少必要参数:直播房间ID']);
  158. }
  159. // 小程序配置信息
  160. $app = AppWx::getApp($app_id);
  161. // 请求api数据
  162. $live_room = new WxLiveRoom($app);
  163. $response = $live_room->getReplay($roomId);
  164. if ($response === false) {
  165. if ($live_room->getError() == 'empty') {
  166. return [];
  167. } else {
  168. throw new BaseException(['msg' => '获取直播房间列表请求失败:' . $live_room->getError()]);
  169. }
  170. }
  171. array_shift($response['live_replay']);
  172. array_pop($response['live_replay']);
  173. foreach ($response['live_replay'] as $k => $item) {
  174. $response['live_replay'][$k]['id'] = $k + 1;
  175. $response['live_replay'][$k]['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
  176. }
  177. return $response['live_replay'];
  178. }
  179. }