sync wechat data
This commit is contained in:
54
Server/application/command/SyncWechatDataToCkbTask.php
Normal file
54
Server/application/command/SyncWechatDataToCkbTask.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use think\facade\Log;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\console\Command;
|
||||
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
|
||||
{
|
||||
protected $lockFile = RUNTIME_PATH . 'sync_wechat_to_ckb.lock';
|
||||
|
||||
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 {
|
||||
$ChuKeBaoAdapter = new ChuKeBaoAdapter();
|
||||
$this->syncWechatAccount($ChuKeBaoAdapter);
|
||||
$this->syncWechatFriend($ChuKeBaoAdapter);
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user