LiveRoom.php 6.8 KB

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