Files
cunkebao_v3/Server/application/command/SyncWechatDataToCkbTask.php

93 lines
2.8 KiB
PHP
Raw Normal View History

2025-05-12 21:18:19 +08:00
<?php
namespace app\command;
use think\facade\Log;
use think\console\Input;
use think\console\Output;
use think\console\Command;
2025-05-13 14:38:51 +08:00
use think\facade\App;
2025-05-12 21:18:19 +08:00
use WeChatDeviceApi\Adapters\ChuKeBao\Adapter as ChuKeBaoAdapter;
// */7 * * * * cd /www/wwwroot/mckb_quwanzhi_com/Server && php think sync:wechatData >> /www/wwwroot/mckb_quwanzhi_com/Server/runtime/log/sync_wechat_data.log 2>&1
class SyncWechatDataToCkbTask extends Command
{
2025-05-13 14:38:51 +08:00
protected $lockFile;
public function __construct()
{
parent::__construct();
$this->lockFile = App::getRuntimePath() . 'sync_wechat_to_ckb.lock';
}
protected function configure()
{
$this->setName('sync:wechatData')
->setDescription('同步微信数据到存客宝');
}
2025-05-12 21:18:19 +08:00
protected function execute(Input $input, Output $output)
{
// 检查锁文件
if (file_exists($this->lockFile)) {
$lockTime = filectime($this->lockFile);
if (time() - $lockTime < 3600) {
Log::info('微信好友同步任务已在运行中,跳过本次执行');
return false;
}
unlink($this->lockFile);
}
file_put_contents($this->lockFile, time());
try {
2025-05-14 11:19:19 +08:00
$output->writeln("同步任务 sync_wechat_to_ckb 开始");
2025-05-12 21:18:19 +08:00
$ChuKeBaoAdapter = new ChuKeBaoAdapter();
$this->syncWechatAccount($ChuKeBaoAdapter);
$this->syncWechatFriend($ChuKeBaoAdapter);
2025-05-13 14:38:51 +08:00
$this->syncWechatDeviceLoginLog($ChuKeBaoAdapter);
2025-05-19 16:16:58 +08:00
$this->syncWechatDevice($ChuKeBaoAdapter);
$this->syncWechatCustomer($ChuKeBaoAdapter);
2025-05-14 11:19:19 +08:00
$output->writeln("同步任务 sync_wechat_to_ckb 已结束");
2025-05-12 21:18:19 +08:00
return true;
} catch (\Exception $e) {
Log::error('微信好友同步任务异常:' . $e->getMessage());
return false;
} finally {
if (file_exists($this->lockFile)) {
unlink($this->lockFile);
}
}
}
protected function syncWechatFriend(ChuKeBaoAdapter $ChuKeBaoAdapter)
{
return $ChuKeBaoAdapter->syncFriendship();
}
protected function syncWechatAccount(ChuKeBaoAdapter $ChuKeBaoAdapter)
{
return $ChuKeBaoAdapter->syncWechatAccount();
}
2025-05-13 14:38:51 +08:00
protected function syncWechatDeviceLoginLog(ChuKeBaoAdapter $ChuKeBaoAdapter)
{
return $ChuKeBaoAdapter->syncWechatDeviceLoginLog();
}
2025-05-19 16:16:58 +08:00
// syncDevice
protected function syncWechatDevice(ChuKeBaoAdapter $ChuKeBaoAdapter)
{
return $ChuKeBaoAdapter->syncDevice();
}
// syncWechatCustomer
protected function syncWechatCustomer(ChuKeBaoAdapter $ChuKeBaoAdapter)
{
return $ChuKeBaoAdapter->syncWechatCustomer();
}
2025-05-12 21:18:19 +08:00
}