123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\command;
-
-
- use app\common\enum\LiveRoomEnum;
- use app\common\model\live\LiveRoom;
- use app\common\server\WxMnpLiveServer;
- use think\console\Command;
- use think\console\Output;
- use think\console\Input;
- use think\facade\Log;
-
-
- class WechatLiveRoom extends Command
- {
-
- protected $roomData = [];
-
-
- protected $requestlimit = 5;
-
-
- protected function configure()
- {
- $this->setName('wechat_live_room')
- ->setDescription('微信小程序直播状态同步');
- }
-
-
- protected function execute(Input $input, Output $output)
- {
- try {
- $liveStatus = [
- LiveRoomEnum::LIVE_STATUS_WAIT,
- LiveRoomEnum::LIVE_STATUS_ING,
- LiveRoomEnum::LIVE_STATUS_STOP,
- LiveRoomEnum::LIVE_STATUS_ERROR,
- ];
- $localRooms = LiveRoom::where('wx_room_id', '>', 0)
- ->where(['audit_status' => LiveRoomEnum::AUDIT_STATUS_SUCCESS, 'del' => 0])
- ->whereIn('live_status', $liveStatus)
- ->select()->toArray();
-
- if (empty($localRooms)) {
- return true;
- }
-
- $wxRooms = $this->getRooms();
- if (empty($wxRooms)) {
- return true;
- }
-
- $updateData = [];
- $wxRooms = array_column($wxRooms, null, 'roomid');
- foreach ($localRooms as $localRoom) {
- $localRoomId = $localRoom['wx_room_id'];
- if (!isset($wxRooms[$localRoomId])) {
- continue;
- }
- $wxRoomData = $wxRooms[$localRoomId];
- $updateData[] = [
- 'id' => $localRoom['id'],
- 'goods_num' => count($wxRoomData['goods']),
- 'live_status' => $wxRoomData['live_status'],
- ];
- }
-
- if (!empty($updateData)) {
- (new LiveRoom())->saveAll($updateData);
- }
-
- return true;
- } catch (\Exception $e) {
- Log::write('更新直播间信息失败:' . $e->getMessage());
- return false;
- }
- }
-
-
-
-
- protected function getRooms($start = 0, $limit = 100)
- {
- $result = (new WxMnpLiveServer())->handle('getRooms', [
- 'start' => $start,
- 'limit' => $limit,
- ]);
-
- if (0 != $result['errcode']) {
- return [];
- }
-
- $this->requestlimit -= 1;
- $this->roomData = array_merge($result['room_info'], $this->roomData);
-
- if ($result['total'] == $limit && $this->requestlimit > 0) {
- return $this->getRooms($limit + 1, $limit);
- }
-
- return $this->roomData;
- }
-
-
- }
|