132 lines
3.9 KiB
PHP
132 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\adminapi\logic\qywx;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\QywxExternalContact;
|
|
use app\common\service\wechat\WechatWorkService;
|
|
use think\facade\Log;
|
|
use think\facade\Cache;
|
|
|
|
/**
|
|
* 企业微信客户逻辑层(精简版)
|
|
*/
|
|
class CustomerLogic extends BaseLogic
|
|
{
|
|
/**
|
|
* @notes 获取企业微信token
|
|
*/
|
|
public static function getAccessToken()
|
|
{
|
|
try {
|
|
$service = new WechatWorkService();
|
|
$token = $service->getContactAccessToken();
|
|
return $token;
|
|
} catch (\Throwable $e) {
|
|
Log::error('获取企业微信token失败: ' . $e->getMessage());
|
|
self::$error = '获取token失败: ' . $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 获取外部联系人列表
|
|
* @param string $userId 员工ID
|
|
*/
|
|
public static function getExternalContacts(string $userId = '')
|
|
{
|
|
try {
|
|
$service = new WechatWorkService();
|
|
|
|
if (empty($userId)) {
|
|
// 获取所有员工
|
|
$userList = $service->getDepartmentUserList();
|
|
if (empty($userList)) {
|
|
self::$error = '未获取到员工列表';
|
|
return false;
|
|
}
|
|
|
|
$allContacts = [];
|
|
foreach ($userList as $user) {
|
|
$userContacts = $service->getExternalContactList($user['userid'] ?? '');
|
|
if (!empty($userContacts)) {
|
|
$allContacts[$user['userid']] = [
|
|
'name' => $user['name'] ?? '',
|
|
'contacts' => $userContacts
|
|
];
|
|
}
|
|
}
|
|
return $allContacts;
|
|
} else {
|
|
// 获取指定员工的客户
|
|
$contacts = $service->getExternalContactList($userId);
|
|
return $contacts;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::error('获取外部联系人失败: ' . $e->getMessage());
|
|
self::$error = '获取联系人失败: ' . $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 获取客户详情
|
|
* @param string $externalUserId 外部联系人ID
|
|
*/
|
|
public static function getContactDetail(string $externalUserId)
|
|
{
|
|
try {
|
|
$service = new WechatWorkService();
|
|
$detail = $service->getExternalContactDetail($externalUserId);
|
|
return $detail;
|
|
} catch (\Throwable $e) {
|
|
Log::error('获取客户详情失败: ' . $e->getMessage());
|
|
self::$error = '获取详情失败: ' . $e->getMessage();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @notes 获取统计信息
|
|
*/
|
|
public static function getStats()
|
|
{
|
|
$total = QywxExternalContact::count();
|
|
|
|
$todayStart = date('Y-m-d 00:00:00');
|
|
$today = QywxExternalContact::where('created_at', '>=', $todayStart)
|
|
->count();
|
|
|
|
// 获取同步状态
|
|
$syncStatus = Cache::get('qywx_sync_status', [
|
|
'last_sync_time' => '',
|
|
'status' => 'idle'
|
|
]);
|
|
|
|
return [
|
|
'total' => $total,
|
|
'today' => $today,
|
|
'lastSync' => $syncStatus['last_sync_time'] ?? '',
|
|
'syncStatus' => $syncStatus['status'] ?? 'idle',
|
|
'syncStatusText' => self::getStatusText($syncStatus['status'] ?? 'idle')
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @notes 获取同步状态文本
|
|
*/
|
|
private static function getStatusText($status)
|
|
{
|
|
$statusMap = [
|
|
'idle' => '未同步',
|
|
'running' => '同步中',
|
|
'completed' => '同步成功',
|
|
'failed' => '同步失败'
|
|
];
|
|
|
|
return $statusMap[$status] ?? '未同步';
|
|
}
|
|
}
|