| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace app\common\model\plus\live;
- use app\common\exception\BaseException;
- use app\common\library\easywechat\AppWx;
- use app\common\library\easywechat\wx\LiveRoom as WxLiveRoom;
- use app\common\model\BaseModel;
- /**
- * 微信直播模型
- */
- class WxLive extends BaseModel
- {
- protected $name = 'app_wx_live';
- protected $pk = 'live_id';
- //附加字段
- protected $append = ['start_time_text', 'end_time_text'];
- /**
- * 有效期-开始时间
- */
- public function getStartTimeTextAttr($value, $data)
- {
- return date('Y-m-d H:i:s', $data['start_time']);
- }
- /**
- * 有效期-开始时间
- * @param $value
- * @return array
- */
- public function getEndTimeTextAttr($value, $data)
- {
- return date('Y-m-d H:i:s', $data['end_time']);
- }
- /**
- * 详情
- */
- public static function detail($live_id)
- {
- return (new static())->find($live_id);
- }
- /**
- * 同步直播间
- */
- public function syn($app_id = null)
- {
- // 小程序配置信息
- $app = AppWx::getApp($app_id);
- // 请求api数据
- $live_room = new WxLiveRoom($app);
- $response = $live_room->syn();
- $isEmpty = false;
- if ($response === false) {
- if ($live_room->getError() == 'empty') {
- $isEmpty = true;
- } else {
- throw new BaseException(['msg' => '获取直播房间列表请求失败:' . $live_room->getError()]);
- }
- }
- // 格式化返回的列表数据
- $roomList = [];
- if (!$isEmpty) {
- foreach ($response['room_info'] as $item) {
- $roomList[$item['roomid']] = $item;
- }
- }
- $roomIds = array_column($roomList, 'roomid');
- // 本地历史数据
- $hasRoomIds = $this->getRoomIds();
- // 新增数据库没有的
- $this->addRoom($hasRoomIds, $roomIds, $roomList);
- // 删除微信小程序已删除的房间
- $this->deleteRoom($hasRoomIds, $roomIds);
- // 更新本地直播间
- $this->updateRoom($hasRoomIds, $roomIds, $roomList);
- return true;
- }
- /**
- * 获取本地直播间
- */
- private function getRoomIds()
- {
- return $this->where('is_delete', '=', 0)->column('roomid', 'live_id');
- }
- /**
- * 同步新增直播间
- */
- private function addRoom($hasRoomIds, $roomIds, $roomList)
- {
- // 需要新增的直播间ID
- $ids = array_values(array_diff($roomIds, $hasRoomIds));
- if (empty($ids)) return true;
- // 整理新增数据
- $saveData = [];
- foreach ($ids as $roomId) {
- $item = $roomList[$roomId];
- $saveData[] = [
- 'roomid' => $roomId,
- 'name' => $item['name'],
- 'cover_img' => $item['cover_img'],
- 'share_img' => $item['share_img'],
- 'anchor_name' => $item['anchor_name'],
- 'start_time' => $item['start_time'],
- 'end_time' => $item['end_time'],
- 'live_status' => $item['live_status'],
- 'app_id' => self::$app_id,
- ];
- }
- // 批量新增直播间
- return $this->saveAll($saveData);
- }
- /**
- * 同步删除直播间
- */
- private function deleteRoom($hasRoomIds, $roomIds)
- {
- // 需要删除的直播间ID
- $ids = array_values(array_diff($hasRoomIds, $roomIds));
- if (empty($ids)) return true;
- // 批量删除直播间
- return self::where('roomid', 'in', $ids)->delete();
- }
- /**
- * 修改本地直播间
- */
- private function updateRoom($hasRoomIds, $roomIds, $roomList)
- {
- // 需要新增的直播间ID
- $ids = array_values(array_intersect($roomIds, $hasRoomIds));
- if (empty($ids)) return true;
- // 整理新增数据
- $saveData = [];
- foreach ($ids as $roomId) {
- $item = $roomList[$roomId];
- $saveData[] = [
- 'live_id' => array_search($roomId, $hasRoomIds),
- 'roomid' => $roomId,
- 'name' => $item['name'],
- 'cover_img' => $item['cover_img'],
- 'share_img' => $item['share_img'],
- 'anchor_name' => $item['anchor_name'],
- 'start_time' => $item['start_time'],
- 'end_time' => $item['end_time'],
- 'live_status' => $item['live_status'],
- 'app_id' => self::$app_id,
- ];
- }
- // 批量新增直播间
- return $this->saveAll($saveData);
- }
- /**
- * 获取直播回放
- * @param $app_id
- * @return bool
- * @throws BaseException
- */
- public function getReplay($roomId, $app_id = null)
- {
- if (empty($roomId)) {
- throw new BaseException(['msg' => '缺少必要参数:直播房间ID']);
- }
- // 小程序配置信息
- $app = AppWx::getApp($app_id);
- // 请求api数据
- $live_room = new WxLiveRoom($app);
- $response = $live_room->getReplay($roomId);
- if ($response === false) {
- if ($live_room->getError() == 'empty') {
- return [];
- } else {
- throw new BaseException(['msg' => '获取直播房间列表请求失败:' . $live_room->getError()]);
- }
- }
- array_shift($response['live_replay']);
- array_pop($response['live_replay']);
- foreach ($response['live_replay'] as $k => $item) {
- $response['live_replay'][$k]['id'] = $k + 1;
- $response['live_replay'][$k]['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
- }
- return $response['live_replay'];
- }
- }
|