Commit ddcf983b authored by 王源's avatar 王源 🎧

添加连接消息中心文件

parent aa87ce2b
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
"hyperf/db-connection": "~1.1.0", "hyperf/db-connection": "~1.1.0",
"hyperf/validation": "~1.1.0", "hyperf/validation": "~1.1.0",
"hyperf/command": "~1.1.0", "hyperf/command": "~1.1.0",
"hyperf/redis": "~1.1.0" "hyperf/redis": "~1.1.0",
"hyperf/guzzle": "^1.1@dev"
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"autoload": { "autoload": {
......
<?php
declare(strict_types=1);
return [
'center' => [
'ip' => env('MESSAGE_CENTER_IP', '127.0.0.1'),
'domain' => env('MESSAGE_CENTER_DOMAIN', 'http://localhost'),
'proxy' => env('MESSAGE_CENTER_PROXY'),
'proxy_port' => env('MESSAGE_CENTER_PROXY_PORT', 80),
]
];
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
namespace Meibuyu\Micro; namespace Meibuyu\Micro;
class ConfigProvider class ConfigProvider
{ {
public function __invoke(): array public function __invoke(): array
...@@ -33,7 +32,14 @@ class ConfigProvider ...@@ -33,7 +32,14 @@ class ConfigProvider
], ],
], ],
], ],
'publish' => [
[
'id' => 'message',
'description' => 'message',
'source' => __DIR__ . '/../publish/message.php',
'destination' => BASE_PATH . '/config/autoload/message.php',
],
],
]; ];
} }
} }
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/4/15
* Time: 9:19
*/
namespace Meibuyu\Micro\Message;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Guzzle\HandlerStackFactory;
class MessageCenter
{
/**
* @var Client
*/
protected $client;
/**
* @var ConfigInterface
*/
private $config;
/**
* @var HandlerStackFactory
*/
protected $stackFactory;
public function __construct(ConfigInterface $config, HandlerStackFactory $stackFactory)
{
$this->config = $config;
$this->stackFactory = $stackFactory;
$this->initClient();
}
public function initClient()
{
$options = [
'base_uri' => $this->config->get('message.center.domain') . '/api/',
'handler' => HandlerStack::create(new CoroutineHandler()),
'timeout' => 5,
];
$proxy = $this->config->get('message.center.proxy');
if ($proxy) {
$options = array_merge($options, [
'proxy' => $proxy,
'swoole' => [
'http_proxy_port' => $this->config->get('message.center.proxy_port'),
]
]);
}
$this->client = make(Client::class, ['config' => $options]);
}
public function request($type, $uri, $data = [])
{
return $this->client->request($type, $uri, [
'body' => json_encode($data),
'headers' => ['content-type' => 'application/json']
]);
}
public function syncDingUser()
{
$response = $this->client->request('GET', 'synchronizationDingUser');
return json_decode($response->getBody()->getContents(), true);
}
public function sendDing($userId, $message)
{
$response = $this->client->request('POST', 'sendDing', [
'body' => json_encode(['userId' => $userId, 'message' => $message]),
'headers' => ['content-type' => 'application/json']
]);
return json_decode($response->getBody()->getContents(), true);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment