| 1 | <?php |
||
| 2 | |||
| 3 | // +---------------------------------------------------------------------- |
||
| 4 | // | ThinkPHP6企业微信群通知 for ThinkLibrary 6.0 |
||
| 5 | // +---------------------------------------------------------------------- |
||
| 6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||
| 7 | // +---------------------------------------------------------------------- |
||
| 8 | // | 官方网站: https://www.dtapp.net |
||
| 9 | // +---------------------------------------------------------------------- |
||
| 10 | // | 开源协议 ( https://mit-license.org ) |
||
| 11 | // +---------------------------------------------------------------------- |
||
| 12 | // | 国内仓库地址 :https://gitee.com/liguangchun/qyweixin-grouprobo |
||
| 13 | // | 国外仓库地址 :https://github.com/GC0202/qyweixin-grouprobo |
||
| 14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/qyweixin-grouprobo |
||
| 15 | // +---------------------------------------------------------------------- |
||
| 16 | |||
| 17 | namespace DtApp\Notice\QyWeiXin\qywechat; |
||
| 18 | |||
| 19 | use DtApp\Notice\QyWeiXin\exception\Exception; |
||
| 20 | use DtApp\ThinkLibrary\Service; |
||
| 21 | use DtApp\ThinkLibrary\service\curl\HttpService; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * 定义当前版本 |
||
| 25 | */ |
||
| 26 | const VERSION = '1.0.5'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * 企业微信机器人扩展 |
||
| 30 | * Class GroupRobotService |
||
| 31 | * @package DtApp\Notice\QyWeiXin\qywechat |
||
| 32 | */ |
||
| 33 | class GroupRobotService extends Service |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Api接口 |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $apiUrl = "https://qyapi.weixin.qq.com/"; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 消息类型 |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $msgType = 'text'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * 链接 |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $_webHook = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * 链接 |
||
| 55 | * @param string $webHook |
||
| 56 | * @return $this |
||
| 57 | */ |
||
| 58 | public function webHook($webHook = '') |
||
| 59 | { |
||
| 60 | $this->_webHook = $webHook; |
||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * key |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $_key = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * key |
||
| 72 | * @param string $key |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function key($key = '') |
||
| 76 | { |
||
| 77 | $this->_key = $key; |
||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * 发送文本消息 |
||
| 83 | * @param string $content 消息内容 |
||
| 84 | * @return bool |
||
| 85 | * @throws Exception |
||
| 86 | */ |
||
| 87 | public function text(string $content = '') |
||
| 88 | { |
||
| 89 | $this->msgType = 'text'; |
||
| 90 | return $this->send([ |
||
| 91 | 'text' => [ |
||
| 92 | 'content' => $content, |
||
| 93 | ], |
||
| 94 | ]); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * 发送markdown消息 |
||
| 99 | * @param string $content 消息内容 |
||
| 100 | * @return bool |
||
| 101 | * @throws Exception |
||
| 102 | */ |
||
| 103 | public function markdown(string $content = '') |
||
| 104 | { |
||
| 105 | $this->msgType = 'markdown'; |
||
| 106 | return $this->send([ |
||
| 107 | 'markdown' => [ |
||
| 108 | 'content' => $content, |
||
| 109 | ], |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 组装发送消息 |
||
| 115 | * @param array $data 消息内容数组 |
||
| 116 | * @return bool |
||
| 117 | * @throws Exception |
||
| 118 | */ |
||
| 119 | private function send(array $data) |
||
| 120 | { |
||
| 121 | if (empty($this->_webHook) && empty($this->_key)) { |
||
| 122 | throw new Exception('企业微信自定义机器人接口未配置,【webhook,key】请配置其中一个'); |
||
| 123 | } |
||
| 124 | if (empty($data['msgtype'])) { |
||
| 125 | $data['msgtype'] = $this->msgType; |
||
| 126 | } |
||
| 127 | if (!empty($this->_webHook)) { |
||
| 128 | return HttpService::instance() |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 129 | ->url($this->_webHook) |
||
| 130 | ->data($data) |
||
| 131 | ->post() |
||
| 132 | ->toArray(); |
||
| 133 | } |
||
| 134 | if (!empty($this->_key)) { |
||
| 135 | return HttpService::instance() |
||
|
0 ignored issues
–
show
|
|||
| 136 | ->url("{$this->apiUrl}cgi-bin/webhook/send?key=" . $this->_key) |
||
| 137 | ->data($data) |
||
| 138 | ->post() |
||
| 139 | ->toArray(); |
||
| 140 | } |
||
| 141 | |||
| 142 | throw new Exception('企业微信自定义机器人接口未配置,【webhook,key】请配置其中一个'); |
||
| 143 | } |
||
| 144 | } |