新增
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\common\service\wechat\WechatWorkService;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
|
||||
/**
|
||||
* 检查企业微信API权限
|
||||
*/
|
||||
class QywxCheckPermissions extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('qywx:check-permissions')
|
||||
->setDescription('检查企业微信API权限配置');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始检查企业微信API权限...');
|
||||
$output->writeln('');
|
||||
|
||||
try {
|
||||
$service = new WechatWorkService();
|
||||
|
||||
// 检查应用权限
|
||||
$output->writeln('1. 检查应用配置...');
|
||||
$permissionCheck = $service->checkAppPermissions();
|
||||
|
||||
if ($permissionCheck['success']) {
|
||||
$output->writeln(' ✓ 应用配置正常');
|
||||
$output->writeln(' 应用ID: ' . ($permissionCheck['data']['agentid'] ?? 'N/A'));
|
||||
$output->writeln(' 应用名称: ' . ($permissionCheck['data']['name'] ?? 'N/A'));
|
||||
} else {
|
||||
$output->writeln(' ✗ 应用配置异常: ' . $permissionCheck['message']);
|
||||
}
|
||||
$output->writeln('');
|
||||
|
||||
// 测试获取部门成员
|
||||
$output->writeln('2. 测试获取部门成员...');
|
||||
$userList = $service->getDepartmentUserList(1, false);
|
||||
if (!empty($userList)) {
|
||||
$output->writeln(' ✓ 成功获取 ' . count($userList) . ' 个成员');
|
||||
$testUser = $userList[0] ?? null;
|
||||
if ($testUser) {
|
||||
$output->writeln(' 测试用户: ' . ($testUser['name'] ?? '') . ' (' . ($testUser['userid'] ?? '') . ')');
|
||||
}
|
||||
} else {
|
||||
$output->writeln(' ✗ 未能获取成员列表');
|
||||
}
|
||||
$output->writeln('');
|
||||
|
||||
// 测试获取客户列表(这里会暴露权限问题)
|
||||
if (!empty($userList)) {
|
||||
$output->writeln('3. 测试获取客户列表权限...');
|
||||
$testUser = $userList[0];
|
||||
$customerList = $service->getExternalContactList($testUser['userid']);
|
||||
|
||||
if ($customerList === false) {
|
||||
$output->writeln(' ✗ 客户联系API权限未开启(错误码48002)');
|
||||
$output->writeln('');
|
||||
$output->writeln('解决方案:');
|
||||
$output->writeln('1. 登录企业微信管理后台');
|
||||
$output->writeln('2. 进入"应用管理"');
|
||||
$output->writeln('3. 找到您的应用');
|
||||
$output->writeln('4. 开启以下权限:');
|
||||
$output->writeln(' - 客户联系 - 获取客户列表');
|
||||
$output->writeln(' - 客户联系 - 获取客户详情');
|
||||
$output->writeln('5. 将服务器IP添加到可信IP白名单');
|
||||
} elseif (is_array($customerList)) {
|
||||
$output->writeln(' ✓ 客户联系API权限正常');
|
||||
$output->writeln(' 该成员的客户数量: ' . count($customerList));
|
||||
} else {
|
||||
$output->writeln(' ? 未知响应类型');
|
||||
}
|
||||
}
|
||||
$output->writeln('');
|
||||
$output->writeln('检查完成!');
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln('');
|
||||
$output->writeln('✗ 检查过程出错: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -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