1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Components;
- use Exception;
- use Log;
- use stdClass;
- class PushNotification
- {
- public static function send($title, $content)
- {
- switch(Helpers::systemConfig()['is_notification']){
- case 1:
- return self::ServerChan($title, $content);
- break;
- case 2:
- return self::Bark($title, $content);
- break;
- default:
- }
- }
- /**
- * ServerChan推送消息
- *
- * @param string $title 消息标题
- * @param string $content 消息内容
- *
- * @return mixed
- */
- private static function ServerChan($title, $content)
- {
- try{
- // TODO:一天仅可发送不超过500条
- $url = 'https://sc.ftqq.com/'.Helpers::systemConfig()['server_chan_key'].'.send?text='.$title.'&desp='.urlencode($content);
- $result = json_decode(Curl::send($url));
- if(empty(Helpers::systemConfig()['server_chan_key'])){
- $result = new stdClass();
- $result->errno = TRUE;
- $result->errmsg = "未正确配置ServerChan";
- }
- if($result != NULL && !$result->errno){
- Helpers::addNotificationLog($title, $content, 2);
- }else{
- Helpers::addNotificationLog($title, $content, 2, 'admin', 1, $result? $result->errmsg : '未知');
- }
- } catch(Exception $e){
- Log::error('ServerChan消息推送异常:'.$e);
- }
- return $result;
- }
- /**
- * Bark推送消息
- *
- * @param string $title 消息标题
- * @param string $content 消息内容
- *
- * @return mixed
- */
- private static function Bark($title, $content)
- {
- try{
- $url = 'https://api.day.app/'.Helpers::systemConfig()['bark_key'].'/'.$title.'/'.$content;
- $result = json_decode(Curl::send($url));
- if($result->code == 200){
- Helpers::addNotificationLog($title, $content, 3);
- }else{
- Helpers::addNotificationLog($title, $content, 3, 'admin', $result->message);
- }
- } catch(Exception $e){
- Log::error('Bark消息推送异常:'.$e);
- }
- return $result;
- }
- }
|