LiveRoom.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\store\model\wxapp;
  3. use app\common\exception\BaseException;
  4. use app\store\model\Wxapp as WxappModel;
  5. use app\common\model\wxapp\LiveRoom as LiveRoomModel;
  6. use app\common\library\wechat\live\Room as LiveRoomApi;
  7. /**
  8. * 微信小程序直播间模型
  9. * Class LiveRoom
  10. * @package app\store\model\wxapp
  11. */
  12. class LiveRoom extends LiveRoomModel
  13. {
  14. /**
  15. * 获取直播间列表
  16. * @param string $search 检索词
  17. * @return \think\Paginator
  18. * @throws \think\exception\DbException
  19. */
  20. public function getList($search = '')
  21. {
  22. !empty($search) && $this->where('room_name|anchor_name', 'like', "%{$search}%");
  23. return $this->where('is_delete', '=', 0)
  24. ->order([
  25. 'is_top' => 'desc',
  26. 'live_status' => 'asc',
  27. 'create_time' => 'desc'
  28. ])
  29. ->paginate(15, false, [
  30. 'query' => \request()->request()
  31. ]);
  32. }
  33. /**
  34. * 设置直播间置顶状态
  35. * @param $isTop
  36. * @return false|int
  37. */
  38. public function setIsTop($isTop)
  39. {
  40. return $this->save(['is_top' => (int)$isTop]);
  41. }
  42. /**
  43. * 刷新直播间列表(同步微信api)
  44. * 每次拉取上限100条数据
  45. * @throws BaseException
  46. * @throws \think\exception\DbException
  47. * @throws \Exception
  48. */
  49. public function refreshLiveList()
  50. {
  51. // 获取微信api最新直播间列表信息
  52. $originRoomList = $this->getOriginRoomList();
  53. // 获取微信直播间的房间id集
  54. $originRoomIds = $this->getOriginRoomIds($originRoomList);
  55. // 已存储的所有房间id集
  56. $localRoomIds = $this->getLocalRoomIds();
  57. // 同步新增直播间
  58. $this->refreshLiveNew($localRoomIds, $originRoomIds, $originRoomList);
  59. // 同步删除直播间
  60. $this->refreshLiveRemove($localRoomIds, $originRoomIds);
  61. // 同步更新直播间
  62. $this->refreshLiveUpdate($localRoomIds, $originRoomIds, $originRoomList);
  63. return true;
  64. }
  65. /**
  66. * 获取微信api最新直播间列表信息
  67. * @return array
  68. * @throws BaseException
  69. * @throws \think\Exception
  70. * @throws \think\exception\DbException
  71. */
  72. private function getOriginRoomList()
  73. {
  74. // 小程序配置信息
  75. $wxConfig = WxappModel::getWxappCache();
  76. // 请求api数据
  77. $LiveRoomApi = new LiveRoomApi($wxConfig['app_id'], $wxConfig['app_secret']);
  78. $response = $LiveRoomApi->getLiveRoomList();
  79. if ($response === false) {
  80. throw new BaseException(['msg' => '直播房间列表api请求失败:' . $LiveRoomApi->getError()]);
  81. }
  82. // 格式化返回的列表数据
  83. $originRoomList = [];
  84. foreach ($response['room_info'] as $item) {
  85. $originRoomList[$item['roomid']] = $item;
  86. }
  87. return $originRoomList;
  88. }
  89. /**
  90. * 获取微信直播间的房间id集
  91. * @param $originRoomList
  92. * @return array
  93. */
  94. private function getOriginRoomIds($originRoomList)
  95. {
  96. $originRoomIds = [];
  97. foreach ($originRoomList as $item) {
  98. $originRoomIds[] = $item['roomid'];
  99. }
  100. return $originRoomIds;
  101. }
  102. /**
  103. * 获取数据库中已存在的roomid
  104. * @return array
  105. */
  106. private function getLocalRoomIds()
  107. {
  108. return $this->where('is_delete', '=', 0)->column('room_id', 'id');
  109. }
  110. /**
  111. * 同步新增直播间
  112. * @param array $localRoomIds 本地直播间id集
  113. * @param array $originRoomIds 最新直播间id集
  114. * @param array $originRoomList 最新直播间列表
  115. * @return array|bool|false
  116. * @throws \Exception
  117. */
  118. private function refreshLiveNew($localRoomIds, $originRoomIds, $originRoomList)
  119. {
  120. // 需要新增的直播间ID
  121. $newLiveRoomIds = array_values(array_diff($originRoomIds, $localRoomIds));
  122. if (empty($newLiveRoomIds)) return true;
  123. // 整理新增数据
  124. $saveData = [];
  125. foreach ($newLiveRoomIds as $roomId) {
  126. $item = $originRoomList[$roomId];
  127. $saveData[] = [
  128. 'room_id' => $roomId,
  129. 'room_name' => $item['name'],
  130. 'cover_img' => $item['cover_img'],
  131. 'share_img' => $item['share_img'],
  132. 'anchor_name' => $item['anchor_name'],
  133. 'start_time' => $item['start_time'],
  134. 'end_time' => $item['end_time'],
  135. 'live_status' => $item['live_status'],
  136. 'wxapp_id' => self::$wxapp_id,
  137. ];
  138. }
  139. // 批量新增直播间
  140. return $this->isUpdate(false)->saveAll($saveData, false);
  141. }
  142. /**
  143. * 同步更新直播间
  144. * @param array $localRoomIds 本地直播间id集
  145. * @param array $originRoomIds 最新直播间id集
  146. * @param array $originRoomList 最新直播间列表
  147. * @return array|bool|false
  148. * @throws \Exception
  149. */
  150. private function refreshLiveUpdate($localRoomIds, $originRoomIds, $originRoomList)
  151. {
  152. // 需要新增的直播间ID
  153. $updatedLiveRoomIds = array_values(array_intersect($originRoomIds, $localRoomIds));
  154. if (empty($updatedLiveRoomIds)) return true;
  155. // 根据直播间id获取主键id
  156. $idArr = $this->getLocalIdsByRoomIds($localRoomIds);
  157. // 整理新增数据
  158. $saveData = [];
  159. foreach ($updatedLiveRoomIds as $roomId) {
  160. $item = $originRoomList[$roomId];
  161. $saveData[] = [
  162. 'id' => $idArr[$roomId],
  163. 'room_id' => $roomId,
  164. 'room_name' => $item['name'],
  165. 'cover_img' => $item['cover_img'],
  166. 'share_img' => $item['share_img'],
  167. 'anchor_name' => $item['anchor_name'],
  168. 'start_time' => $item['start_time'],
  169. 'end_time' => $item['end_time'],
  170. 'live_status' => $item['live_status'],
  171. 'wxapp_id' => self::$wxapp_id,
  172. ];
  173. }
  174. // 批量新增直播间
  175. return $this->isUpdate(true)->saveAll($saveData);
  176. }
  177. /**
  178. * 同步删除直播间
  179. * @param array $localRoomIds 本地直播间id集
  180. * @param array $originRoomIds 最新直播间id集
  181. * @return array|bool|false
  182. * @throws \Exception
  183. */
  184. private function refreshLiveRemove($localRoomIds, $originRoomIds)
  185. {
  186. // 需要删除的直播间ID
  187. $removedLiveRoomIds = array_values(array_diff($localRoomIds, $originRoomIds));
  188. if (empty($removedLiveRoomIds)) return true;
  189. // 根据直播间id获取主键id
  190. $removedIds = $this->getLocalIdsByRoomIds($localRoomIds, $removedLiveRoomIds);
  191. // 批量删除直播间
  192. return self::destroy(array_values($removedIds));
  193. }
  194. /**
  195. * 根据直播间id获取主键id
  196. * @param array $localRoomIds
  197. * @param array $searchRoomIds
  198. * @return array
  199. */
  200. private function getLocalIdsByRoomIds($localRoomIds, $searchRoomIds = [])
  201. {
  202. $data = [];
  203. foreach ($localRoomIds as $id => $roomId) {
  204. if (empty($searchRoomIds) || in_array($roomId, $searchRoomIds)) {
  205. $data[$roomId] = $id;
  206. }
  207. }
  208. return $data;
  209. }
  210. }