新增
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\adminapi\logic\qywx\CustomerLogic;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\facade\Log;
|
||||
|
||||
/**
|
||||
* 企业微信客户同步定时任务
|
||||
*
|
||||
* 使用方法:
|
||||
* php think qywx:sync-customer
|
||||
*
|
||||
* 配置crontab(每小时执行一次):
|
||||
* 0 * * * * cd /path/to/project && php think qywx:sync-customer >> /dev/null 2>&1
|
||||
*/
|
||||
class QywxSyncCustomer extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('qywx:sync-customer')
|
||||
->addOption('force', 'f', \think\console\input\Option::VALUE_NONE, '强制同步,忽略自动同步设置')
|
||||
->setDescription('同步企业微信客户数据');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始同步企业微信客户...');
|
||||
|
||||
try {
|
||||
$force = $input->getOption('force');
|
||||
|
||||
if (!$force) {
|
||||
// 检查是否开启自动同步
|
||||
$settings = CustomerLogic::getSyncSettings();
|
||||
if (!($settings['auto_sync'] ?? false)) {
|
||||
$output->writeln('自动同步未开启,跳过(使用 --force 强制同步)');
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 检查距离上次同步是否超过间隔时间
|
||||
$lastSyncTime = $settings['last_sync_time'] ?? 0;
|
||||
$interval = $settings['interval'] ?? 3600;
|
||||
$now = time();
|
||||
|
||||
if ($now - $lastSyncTime < $interval) {
|
||||
$output->writeln('距离上次同步时间不足,跳过(使用 --force 强制同步)');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 执行同步
|
||||
$result = CustomerLogic::syncCustomers();
|
||||
if ($result === false) {
|
||||
$error = CustomerLogic::getError();
|
||||
$output->writeln('同步失败: ' . $error);
|
||||
Log::error('企业微信客户同步失败: ' . $error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
$output->writeln('同步成功!');
|
||||
$output->writeln('同步数量: ' . ($result['sync_count'] ?? 0));
|
||||
$output->writeln('新增数量: ' . ($result['new_count'] ?? 0));
|
||||
$output->writeln('更新数量: ' . ($result['update_count'] ?? 0));
|
||||
|
||||
return 0;
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln('同步异常: ' . $e->getMessage());
|
||||
Log::error('企业微信客户同步异常: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user