-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from wonder-game/develop
整合通知组件
- Loading branch information
Showing
32 changed files
with
1,102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
// 签名 ×tamp=XXX&sign=XXX | ||
$timestamp = round(microtime(true), 3) * 1000; | ||
|
||
$sign = utf8_encode(urlencode(base64_encode(hash_hmac('sha256', $timestamp . "\n" . $secret, $secret, true)))); | ||
|
||
$url .= "×tamp={$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 异常处理 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.