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] ?? '未同步'; } }