Skip to content

Commit

Permalink
Merge pull request #87 from wonder-game/develop
Browse files Browse the repository at this point in the history
整合通知组件
  • Loading branch information
Joyboo authored Feb 19, 2024
2 parents 0829f4a + 36fd603 commit 4052186
Show file tree
Hide file tree
Showing 32 changed files with 1,102 additions and 25 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
"easyswoole/i18n": "^1.0",
"easyswoole/policy": "^1.0",
"easyswoole/file-watcher": "^1.0",
"wonder-game/es-notify": "1.1.*",
"easyswoole/tracker": "^1.1",
"easyswoole/swoole-ide-helper": "^1.3",
"easyswoole/words-match": "^1.1",
"easyswoole/wechat": "^2.0",
"ext-json": "*",
"ext-iconv": "*",
"ext-mbstring": "*"
"ext-mbstring": "*",
"easyswoole/http-client": "^1.5"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Classes/ShardTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function checkPartition($day)
if ($warning) {
$msg = "检测到以下表分区不足{$day}天:" . implode('', $warning);
trace($msg, 'info', 'worker');
dingtalk_text($msg);
notice($msg);
wechat_notice($msg);
}
}
Expand Down Expand Up @@ -202,7 +202,7 @@ private function _reMsg($msg = '', $code = 0)
{
if ($code > 0) {
$title = '执行分区错误: ';
dingtalk_text("$title $msg", true);
notice("$title $msg");
wechat_notice("$title $msg");
}
trace($msg, $code ? 'error' : 'info', 'crontab');
Expand Down
7 changes: 2 additions & 5 deletions src/Crontab/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ public function onException(\Throwable $throwable, int $taskId, int $workerIndex
public function throwable($row, string $message)
{
trace($message, 'error');
// Trigger::getInstance()->throwable(new \Exception($message));

$title = 'Crontab异常';
$text = implode(" \n\n ", [
'### **' . $title . '**',
'### **Crontab异常**',
'- 服务器: ' . config('SERVNAME'),
'- 项 目:' . config('SERVER_NAME'),
"- id: {$row['id']}",
"- name: {$row['name']}",
"- 详 情:$message",
]);
dingtalk_markdown($title, $text);
notice($text);
}

public function run(int $taskId, int $workerIndex)
Expand Down
11 changes: 3 additions & 8 deletions src/EventMainServerCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use EasySwoole\Socket\AbstractInterface\ParserInterface;
use EasySwoole\Spl\SplBean;
use EasySwoole\WordsMatch\WMServer;
use WonderGame\EsNotify\EsNotify;
use WonderGame\EsUtility\Notify\EsNotify;

class EventMainServerCreate extends SplBean
{
Expand Down Expand Up @@ -50,11 +50,6 @@ class EventMainServerCreate extends SplBean
'reload_after' => null, // callback worker process reload 后
];

/**
* @var null ['key' => new EsNotify/Config([])]
*/
//protected $notifyConfig = null;

protected $consumerJobs = null;

protected function initialize(): void
Expand Down Expand Up @@ -265,8 +260,8 @@ static public function registerNotify()
if ( ! is_array($val)) {
continue;
}
/* @var \WonderGame\EsNotify\DingTalk\Config|\WonderGame\EsNotify\WeChat\Config $cls */
$cls = '\\WonderGame\\EsNotify\\' . ucfirst($key) . '\\Config';
/* @var \WonderGame\EsUtility\Notify\DingTalk\Config|\WonderGame\EsUtility\Notify\WeChat\Config|\WonderGame\EsUtility\Notify\Feishu\Config $cls */
$cls = '\\WonderGame\\EsUtility\\Notify\\' . ucfirst($key) . '\\Config';

foreach ($val as $k => $v) {
EsNotify::getInstance()->register(new $cls($v, true), $key, $k);
Expand Down
63 changes: 63 additions & 0 deletions src/Notify/DingTalk/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk;

use WonderGame\EsUtility\Notify\Interfaces\ConfigInterface;
use WonderGame\EsUtility\Notify\Interfaces\NotifyInterface;
use EasySwoole\Spl\SplBean;

class Config extends SplBean implements ConfigInterface
{
/**
* WebHook
* @var string
*/
protected $url = '';

/**
* 密钥
* @var string
*/
protected $signKey = '';

/**
* 要@哪些人(手机号), true-所有人,false-谁也不鸟
* @var array|bool
*/
protected $at = false;

public function setUrl($url)
{
$this->url = $url;
}

public function getUrl()
{
return $this->url;
}

public function setSignKey($signKey)
{
$this->signKey = $signKey;
}

public function getSignKey()
{
return $this->signKey;
}

public function setAt($at)
{
$this->at = $at;
}

public function getAt()
{
return $this->at;
}

public function getNotifyClass(): NotifyInterface
{
return new Notify($this);
}
}
11 changes: 11 additions & 0 deletions src/Notify/DingTalk/Message/ActionCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

class ActionCard extends Base
{
public function fullData()
{

}
}
42 changes: 42 additions & 0 deletions src/Notify/DingTalk/Message/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

use EasySwoole\Spl\SplBean;
use WonderGame\EsUtility\Notify\Interfaces\MessageInterface;

abstract class Base extends SplBean implements MessageInterface
{
/**
* 手机号
* @var array
*/
protected $atMobiles = [];

/**
* Userid
* @var array
*/
protected $atUserIds = [];

protected $isAtAll = false;

public function getAtText($text = '')
{
foreach (['atMobiles', 'atUserIds'] as $item) {
foreach ($this->{$item} as $tel) {
$text .= ' @' . $tel;
}
}
return $text;
}

public function getServerText($text = '')
{
return $text . PHP_EOL . implode(PHP_EOL, [
'系统:' . APP_MODULE,
'服务器:' . config('SERVNAME'),
'时间:' . date('Y年m月d日 H:i:s')
]);
}
}
11 changes: 11 additions & 0 deletions src/Notify/DingTalk/Message/FeedCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

class FeedCard extends Base
{
public function fullData()
{

}
}
11 changes: 11 additions & 0 deletions src/Notify/DingTalk/Message/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

class Link extends Base
{
public function fullData()
{

}
}
26 changes: 26 additions & 0 deletions src/Notify/DingTalk/Message/Markdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

class Markdown extends Base
{
protected $title = '';

protected $text = '';

public function fullData()
{
return [
'msgtype' => 'markdown',
'markdown' => [
'title' => $this->title,
'text' => $this->getAtText($this->text)
],
'at' => [
'atMobiles' => $this->atMobiles,
'atUserIds' => $this->atUserIds,
'isAtAll' => $this->isAtAll
]
];
}
}
23 changes: 23 additions & 0 deletions src/Notify/DingTalk/Message/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk\Message;

class Text extends Base
{
protected $content = '';

public function fullData()
{
return [
'msgtype' => 'text',
'text' => [
'content' => $this->getServerText($this->getAtText($this->content))
],
'at' => [
'atMobiles' => $this->atMobiles,
'atUserIds' => $this->atUserIds,
'isAtAll' => $this->isAtAll
]
];
}
}
54 changes: 54 additions & 0 deletions src/Notify/DingTalk/Notify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace WonderGame\EsUtility\Notify\DingTalk;

use EasySwoole\HttpClient\HttpClient;
use WonderGame\EsUtility\Notify\Interfaces\ConfigInterface;
use WonderGame\EsUtility\Notify\Interfaces\MessageInterface;
use WonderGame\EsUtility\Notify\Interfaces\NotifyInterface;

class Notify implements NotifyInterface
{
/**
* @var Config
*/
protected $Config = null;

public function __construct(ConfigInterface $Config)
{
$this->Config = $Config;
}

/**
* @document https://open.dingtalk.com/document/group/custom-robot-access
* 每个机器人每分钟最多发送20条消息到群里,如果超过20条,会限流10分钟
* @param MessageInterface $message
* @return void
*/
public function does(MessageInterface $message)
{
$data = $message->fullData();

$url = $this->Config->getUrl();
$secret = $this->Config->getSignKey();

// 签名 &timestamp=XXX&sign=XXX
$timestamp = round(microtime(true), 3) * 1000;

$sign = utf8_encode(urlencode(base64_encode(hash_hmac('sha256', $timestamp . "\n" . $secret, $secret, true))));

$url .= "&timestamp={$timestamp}&sign={$sign}";

$client = new HttpClient($url);

// 支持文本 (text)、链接 (link)、markdown(markdown)、ActionCard、FeedCard消息类型

$response = $client->postJson(json_encode($data));
$json = json_decode($response->getBody(), true);

if ($json['errcode'] !== 0)
{
// todo 异常处理
}
}
}
46 changes: 46 additions & 0 deletions src/Notify/EsNotify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace WonderGame\EsUtility\Notify;

use EasySwoole\Component\Singleton;
use WonderGame\EsUtility\Notify\Interfaces\ConfigInterface;
use WonderGame\EsUtility\Notify\Interfaces\MessageInterface;
use WonderGame\EsUtility\Notify\Interfaces\NotifyInterface;

class EsNotify
{
use Singleton;

protected $container = [];

protected function getContainer(string $type, string $name = 'default'): ?NotifyInterface
{
return $this->container[$type][$name] ?? null;
}

public function register(ConfigInterface $Config, string $type, string $name = 'default')
{
if (isset($this->container[$type][$name])) {
throw new \Exception("EsUtility name already exists: $type.$name");
}

$this->container[$type][$name] = $Config->getNotifyClass();
}

/**
* 执行某一个
* @param string $type 类型:dingtalk、wechat
* @param array $params
* @param string $name
* @return void
*/
public function doesOne(string $type, MessageInterface $message, string $name = 'default')
{
if ($Notify = $this->getContainer($type, $name)) {
$Notify->does($message);
}
}

// 钉钉和微信的 Config Message 不能混用,需识别类型
// public function doesAll()
}
Loading

0 comments on commit 4052186

Please sign in to comment.