129 lines
4.0 KiB
PHP
129 lines
4.0 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeshop开源商城系统
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||
// | github下载:https://github.com/likeshop-github
|
||
// | 访问官网:https://www.likeshop.cn
|
||
// | 访问社区:https://home.likeshop.cn
|
||
// | 访问手册:http://doc.likeshop.cn
|
||
// | 微信公众号:likeshop技术社区
|
||
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||
// | likeshop团队版权所有并拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeshop.cn.team
|
||
// +----------------------------------------------------------------------
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取直播间
|
||
* @param int $start
|
||
* @param int $limit
|
||
* @return array
|
||
* @throws \Exception
|
||
* @author 段誉
|
||
* @date 2023/2/17 18:45
|
||
*/
|
||
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;
|
||
}
|
||
|
||
|
||
} |