This commit is contained in:
Your Name
2026-04-18 15:54:14 +08:00
parent fa3da15228
commit 4097fe0cc6
271 changed files with 1257 additions and 369 deletions
+35 -2
View File
@@ -15,7 +15,11 @@ use think\facade\Log;
*
* 使用方法:
* php think qywx:sync-customer
*
* php think qywx:sync-customer --force --today # 仅落库「今日首次加为外部联系人」的客户(app.default_timezone
* php think qywx:sync-customer --force --today --today-any-follow # 任一条跟进在今天即落库(旧逻辑,命中多)
*
* 说明:企微「客户列表」API 不支持按时间筛选;--today 只减少写库条数。拉取侧默认用 batch/get_by_user 批量详情(project.qywx_sync_use_batch_detail)加速。
*
* 配置crontab(每小时执行一次):
* 0 * * * * cd /path/to/project && php think qywx:sync-customer >> /dev/null 2>&1
*/
@@ -25,6 +29,18 @@ class QywxSyncCustomer extends Command
{
$this->setName('qywx:sync-customer')
->addOption('force', 'f', \think\console\input\Option::VALUE_NONE, '强制同步,忽略自动同步设置')
->addOption(
'today',
't',
\think\console\input\Option::VALUE_NONE,
'仅落库「企微首次添加时间」在今日的客户(min(createtime),时区见 app.default_timezone);仍会拉全量列表与详情'
)
->addOption(
'today-any-follow',
null,
\think\console\input\Option::VALUE_NONE,
'需与 --today 同时使用:任一条跟进 createtime 在今日即落库(旧行为,容易大量命中)'
)
->setDescription('同步企业微信客户数据');
}
@@ -55,7 +71,20 @@ class QywxSyncCustomer extends Command
}
// 执行同步
$result = CustomerLogic::syncCustomers();
$syncOptions = [];
if ($input->getOption('today')) {
[$syncOptions['follow_createtime_from'], $syncOptions['follow_createtime_to']] = CustomerLogic::todayCreatetimeWindowBounds();
$syncOptions['follow_createtime_mode'] = $input->getOption('today-any-follow') ? 'any' : 'first';
$tz = (string) config('app.default_timezone', 'Asia/Shanghai');
$output->writeln(sprintf(
'今日窗口(%s): %s ~ %s | 模式: %s',
$tz,
date('Y-m-d H:i:s', $syncOptions['follow_createtime_from']),
date('Y-m-d H:i:s', $syncOptions['follow_createtime_to']),
$syncOptions['follow_createtime_mode'] === 'any' ? '任一条跟进在今天' : '仅首次添加在今天(今日新客)'
));
}
$result = CustomerLogic::syncCustomers($syncOptions);
if ($result === false) {
$error = CustomerLogic::getError();
$output->writeln('同步失败: ' . $error);
@@ -67,6 +96,10 @@ class QywxSyncCustomer extends Command
$output->writeln('同步数量: ' . ($result['sync_count'] ?? 0));
$output->writeln('新增数量: ' . ($result['new_count'] ?? 0));
$output->writeln('更新数量: ' . ($result['update_count'] ?? 0));
$skipped = (int) ($result['skipped_count'] ?? 0);
if ($skipped > 0) {
$output->writeln('跳过数量(非指定日期内新建跟进): ' . $skipped);
}
return 0;
} catch (\Throwable $e) {