Files
zyt/server/app/common/service/assistantservice/WecomArchiveHttp.php
T
2026-11-12 09:07:07 +08:00

102 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\service\assistantservice;
use RuntimeException;
/** 线上企微归档。本地库不存在时,工作台和预警都走这组令牌接口。 */
final class WecomArchiveHttp
{
public static function configured(): bool
{
return self::base() !== '' && self::token() !== '';
}
/** @return list<array{external_id:string,name:string,conversations:int}> */
public static function accounts(): array
{
$json = self::get('/api/v2/archive/staff-feed/accounts');
$accounts = [];
foreach (is_array($json['accounts'] ?? null) ? $json['accounts'] : [] as $row) {
if (!is_array($row)) { continue; }
$accounts[] = ['external_id' => (string) ($row['external_id'] ?? ''), 'name' => trim((string) ($row['name'] ?? '')),
'conversations' => (int) ($row['conversations'] ?? 0)];
}
return $accounts;
}
/** @return list<array{id:string,name:string,conversation_type:string,last_message_at:string,message_count:int,preview:string}> */
public static function conversations(string $externalId): array
{
if ($externalId === '') { return []; }
$json = self::get('/api/v2/archive/staff-feed/conversations', ['account' => $externalId]);
$items = [];
foreach (is_array($json['conversations'] ?? null) ? $json['conversations'] : [] as $row) {
if (!is_array($row)) { continue; }
$items[] = ['id' => (string) ($row['id'] ?? ''), 'name' => trim((string) ($row['name'] ?? '')),
'conversation_type' => (string) ($row['conversation_type'] ?? ''), 'last_message_at' => (string) ($row['last_message_at'] ?? ''),
'message_count' => (int) ($row['message_count'] ?? 0), 'preview' => trim((string) ($row['preview'] ?? ''))];
}
return $items;
}
/** @return array{account:string,messages:list<array{id:string,sent_at:string,direction:string,message_type:string,sender_name:string,content:string}>} */
public static function messages(string $conversationId): array
{
if ($conversationId === '') { return ['account' => '', 'messages' => []]; }
$json = self::get('/api/v2/archive/staff-feed/messages', ['conversation_id' => $conversationId]);
$messages = [];
foreach (is_array($json['messages'] ?? null) ? $json['messages'] : [] as $row) {
if (!is_array($row)) { continue; }
$messages[] = ['id' => (string) ($row['id'] ?? ''), 'sent_at' => (string) ($row['sent_at'] ?? ''),
'direction' => (string) ($row['direction'] ?? ''), 'message_type' => (string) ($row['message_type'] ?? ''),
'sender_name' => trim((string) ($row['sender_name'] ?? '')), 'content' => trim((string) ($row['content'] ?? ''))];
}
return ['account' => (string) ($json['account'] ?? ''), 'messages' => $messages];
}
/** @return array{configured:bool,items:array<int,array<string,mixed>>,next_cursor:string,has_more:bool} */
public static function page(string $cursor, int $limit): array
{
$empty = ['configured' => false, 'items' => [], 'next_cursor' => $cursor, 'has_more' => false];
if (!self::configured()) { return $empty; }
$limit = max(1, min(200, $limit));
$json = self::get('/api/v2/archive/staff-feed/outbound', ['cursor' => $cursor, 'limit' => (string) $limit]);
$rows = is_array($json['items'] ?? null) ? $json['items'] : [];
return ['configured' => true, 'items' => WecomArchiveSqlite::identify($rows),
'next_cursor' => (string) ($json['next_cursor'] ?? $cursor), 'has_more' => (bool) ($json['has_more'] ?? false)];
}
/** @param array<string,string> $query @return array<string,mixed> */
private static function get(string $path, array $query = []): array
{
$base = self::base();
$token = self::token();
if ($base === '' || $token === '') { throw new RuntimeException('WECOM_ARCHIVE'); }
$url = $base . $path;
if ($query) { $url .= '?' . http_build_query($query); }
$handle = curl_init($url);
if ($handle === false) { throw new RuntimeException('WECOM_ARCHIVE'); }
curl_setopt_array($handle, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER => ['Accept: application/json', 'X-Archive-Feed-Token: ' . $token]]);
$body = curl_exec($handle);
$code = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
$json = is_string($body) ? json_decode($body, true) : null;
if ($code !== 200 || !is_array($json)) { throw new RuntimeException('WECOM_ARCHIVE'); }
return $json;
}
private static function base(): string
{
return rtrim((string) config('assistant_service.wecom_archive_base_url', ''), '/');
}
private static function token(): string
{
return trim((string) config('assistant_service.wecom_archive_token', ''));
}
}