submit cursor rule
This commit is contained in:
98
Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php
Normal file
98
Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace WeChatDeviceApi\Adapters\ChuKeBao;
|
||||
|
||||
use WeChatDeviceApi\Contracts\WeChatServiceInterface;
|
||||
use WeChatDeviceApi\Exceptions\ApiException;
|
||||
//use WeChatDeviceApi\Exceptions\DeviceOfflineException;
|
||||
// 如果有 Client.php
|
||||
// use WeChatDeviceApi\Adapters\VendorA\Client as VendorAApiClient;
|
||||
|
||||
class Adapter implements WeChatServiceInterface
|
||||
{
|
||||
protected $config;
|
||||
// protected $apiClient; // 如果使用 VendorAApiClient
|
||||
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
// $this->apiClient = new VendorAApiClient($config['api_key'], $config['api_secret'], $config['base_url']);
|
||||
// 校验配置等...
|
||||
if (empty($config['api_key']) || empty($config['base_url'])) {
|
||||
throw new \InvalidArgumentException("VendorA API key and base_url are required.");
|
||||
}
|
||||
}
|
||||
|
||||
public function addFriend(string $deviceId, string $targetWxId): bool
|
||||
{
|
||||
// 1. 构建请求参数 (VendorA 特定的格式)
|
||||
$params = [
|
||||
'device_identifier' => $deviceId,
|
||||
'wechat_user_to_add' => $targetWxId,
|
||||
'apiKey' => $this->config['api_key'],
|
||||
// ... 其他 VendorA 特定参数
|
||||
];
|
||||
|
||||
// 2. 调用 VendorA 的 API (例如使用 GuzzleHttp 或 cURL)
|
||||
// $response = $this->apiClient->post('/friend/add', $params);
|
||||
// 伪代码:
|
||||
$url = $this->config['base_url'] . '/friend/add';
|
||||
// $httpClient = new \GuzzleHttp\Client();
|
||||
// $response = $httpClient->request('POST', $url, ['form_params' => $params]);
|
||||
// $responseData = json_decode($response->getBody()->getContents(), true);
|
||||
|
||||
// 模拟API调用
|
||||
echo "VendorA: Adding friend {$targetWxId} using device {$deviceId}\n";
|
||||
$responseData = ['code' => 0, 'message' => 'Success']; // 假设的响应
|
||||
|
||||
// 3. 处理响应,转换为标准结果
|
||||
if (!isset($responseData['code'])) {
|
||||
throw new ApiException("VendorA: Invalid API response for addFriend.");
|
||||
}
|
||||
// if ($responseData['code'] === 1001) { // 假设1001是设备离线
|
||||
// throw new DeviceOfflineException("VendorA: Device {$deviceId} is offline.");
|
||||
// }
|
||||
if ($responseData['code'] !== 0) {
|
||||
throw new ApiException("VendorA: Failed to add friend - " . ($responseData['message'] ?? 'Unknown error'));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function likeMoment(string $deviceId, string $momentId): bool
|
||||
{
|
||||
echo "VendorA: Liking moment {$momentId} using device {$deviceId}\n";
|
||||
// 实现 VendorA 的点赞逻辑
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getGroupList(string $deviceId): array
|
||||
{
|
||||
echo "VendorA: Getting group list for device {$deviceId}\n";
|
||||
// 实现 VendorA 的获取群列表逻辑,并转换数据格式
|
||||
return [
|
||||
['id' => 'group1_va', 'name' => 'VendorA Group 1', 'member_count' => 10],
|
||||
];
|
||||
}
|
||||
|
||||
public function getFriendList(string $deviceId): array
|
||||
{
|
||||
echo "VendorA: Getting friend list for device {$deviceId}\n";
|
||||
return [
|
||||
['id' => 'friend1_va', 'nickname' => 'VendorA Friend 1', 'remark' => 'VA-F1'],
|
||||
];
|
||||
}
|
||||
|
||||
public function getDeviceInfo(string $deviceId): array
|
||||
{
|
||||
echo "VendorA: Getting device info for device {$deviceId}\n";
|
||||
return ['id' => $deviceId, 'status' => 'online_va', 'battery' => '80%'];
|
||||
}
|
||||
|
||||
public function bindDeviceToCompany(string $deviceId, string $companyId): bool
|
||||
{
|
||||
echo "VendorA: Binding device {$deviceId} to company {$companyId}\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
// ... 实现接口中的其他方法
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace WeChatDeviceApi\Contracts;
|
||||
|
||||
interface WeChatServiceInterface
|
||||
{
|
||||
/**
|
||||
* 添加好友
|
||||
* @param string $deviceId 设备ID
|
||||
* @param string $targetWxId 目标微信ID
|
||||
* @return bool 是否成功
|
||||
* @throws \WeChatDeviceApi\Exceptions\ApiException
|
||||
// * @throws \WeChatDeviceApi\Exceptions\DeviceOfflineException
|
||||
*/
|
||||
public function addFriend(string $deviceId, string $targetWxId): bool;
|
||||
|
||||
/**
|
||||
* 朋友圈点赞
|
||||
* @param string $deviceId 设备ID
|
||||
* @param string $momentId 朋友圈ID
|
||||
* @return bool 是否成功
|
||||
*/
|
||||
public function likeMoment(string $deviceId, string $momentId): bool;
|
||||
|
||||
/**
|
||||
* 获取群列表
|
||||
* @param string $deviceId 设备ID
|
||||
* @return array 群信息列表
|
||||
*/
|
||||
public function getGroupList(string $deviceId): array;
|
||||
|
||||
/**
|
||||
* 获取好友列表
|
||||
* @param string $deviceId 设备ID
|
||||
* @return array 好友信息列表
|
||||
*/
|
||||
public function getFriendList(string $deviceId): array;
|
||||
|
||||
/**
|
||||
* 获取设备信息
|
||||
* @param string $deviceId 设备ID
|
||||
* @return array 设备详情
|
||||
*/
|
||||
public function getDeviceInfo(string $deviceId): array;
|
||||
|
||||
/**
|
||||
* 绑定设备到公司
|
||||
* @param string $deviceId 设备ID
|
||||
* @param string $companyId 公司ID
|
||||
* @return bool 是否成功
|
||||
*/
|
||||
public function bindDeviceToCompany(string $deviceId, string $companyId): bool;
|
||||
|
||||
// ... 其他方法定义
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace WeChatDeviceApi\Exceptions;
|
||||
|
||||
class ApiException extends \RuntimeException // 或者 \Exception
|
||||
{
|
||||
// 可以添加更多上下文信息
|
||||
}
|
||||
94
Server/extend/WeChatDeviceApi/Manager.php
Normal file
94
Server/extend/WeChatDeviceApi/Manager.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
namespace WeChatDeviceApi;
|
||||
|
||||
use think\facade\Config;
|
||||
use WeChatDeviceApi\Contracts\WeChatServiceInterface;
|
||||
|
||||
class Manager
|
||||
{
|
||||
/**
|
||||
* @var array 适配器实例缓存
|
||||
*/
|
||||
protected $adapters = [];
|
||||
|
||||
/**
|
||||
* @var array 配置
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function __construct(array $config = null)
|
||||
{
|
||||
$this->config = $config ?: Config::get('wechat_device_api.');
|
||||
if (empty($this->config)) {
|
||||
throw new \InvalidArgumentException("WeChat Device API configuration not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的适配器实例
|
||||
*
|
||||
* @param string|null $name 适配器名称 (例如 'vendor_a', 'vendor_b'),null 则使用默认
|
||||
* @return WeChatServiceInterface
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function adapter(string $name = null): WeChatServiceInterface
|
||||
{
|
||||
$name = $name ?: $this->getDefaultAdapterName();
|
||||
|
||||
if (!isset($this->config['adapters'][$name])) {
|
||||
throw new \InvalidArgumentException("Adapter [{$name}] configuration not found.");
|
||||
}
|
||||
|
||||
if (!isset($this->adapters[$name])) {
|
||||
$this->adapters[$name] = $this->createAdapter($name);
|
||||
}
|
||||
|
||||
return $this->adapters[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建适配器实例
|
||||
*
|
||||
* @param string $name
|
||||
* @return WeChatServiceInterface
|
||||
*/
|
||||
protected function createAdapter(string $name): WeChatServiceInterface
|
||||
{
|
||||
$adapterConfig = $this->config['adapters'][$name];
|
||||
$driverClass = $adapterConfig['driver'] ?? null;
|
||||
|
||||
if (!$driverClass || !class_exists($driverClass)) {
|
||||
throw new \InvalidArgumentException("Driver class for adapter [{$name}] not found or not specified.");
|
||||
}
|
||||
|
||||
$adapterInstance = new $driverClass($adapterConfig);
|
||||
|
||||
if (!$adapterInstance instanceof WeChatServiceInterface) {
|
||||
throw new \LogicException("Driver class [{$driverClass}] must implement WeChatServiceInterface.");
|
||||
}
|
||||
|
||||
return $adapterInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认适配器名称
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultAdapterName(): string
|
||||
{
|
||||
return $this->config['default_adapter'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态调用默认适配器的方法
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $method, array $parameters)
|
||||
{
|
||||
return $this->adapter()->{$method}(...$parameters);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user