diff --git a/src/Notify/Feishu/Message/CardError.php b/src/Notify/Feishu/Message/CardError.php new file mode 100644 index 0000000..ff8cafe --- /dev/null +++ b/src/Notify/Feishu/Message/CardError.php @@ -0,0 +1,252 @@ + 'div', + 'text' => [ + 'tag' => 'lark_md', + 'content' => $this->getAtText(), + ], + ]; + $data = [ + "i18n_elements" => [ + "zh_cn" => [ + [ + "tag" => "column_set", + "flex_mode" => "none", + "background_style" => "default", + "columns" => [ + [ + "tag" => "column", + "width" => "weighted", + "vertical_align" => "top", + "elements" => [ + [ + "tag" => "markdown", + "content" => "** 服务器:**\n{$this->servername}", + "text_align" => "left", + "text_size" => "normal" + ] + ], + "weight" => 1 + ], + [ + "tag" => "column", + "width" => "weighted", + "vertical_align" => "top", + "elements" => [ + [ + "tag" => "markdown", + "content" => "** 项目:**\n{$this->project}", + "text_align" => "left", + "text_size" => "normal" + ] + ], + "weight" => 1 + ] + ], + "margin" => "16px 0px 0px 0px" + ], + [ + "tag" => "column_set", + "flex_mode" => "none", + "background_style" => "default", + "columns" => [ + [ + "tag" => "column", + "width" => "weighted", + "vertical_align" => "top", + "elements" => [ + [ + "tag" => "markdown", + "content" => "**时间:**\n{$this->datetime}", + "text_align" => "left", + "text_size" => "normal" + ] + ], + "weight" => 1 + ], + [ + "tag" => "column", + "width" => "weighted", + "vertical_align" => "top", + "elements" => [ + [ + "tag" => "markdown", + "content" => "** 触发方式:**\n{$this->trigger}", + "text_align" => "left", + "text_size" => "normal" + ] + ], + "weight" => 1 + ] + ], + "margin" => "16px 0px 0px 0px" + ], + [ + "tag" => "hr" + ], + [ + "tag" => "column_set", + "flex_mode" => "none", + "background_style" => "default", + "columns" => [ + [ + "tag" => "column", + "width" => "weighted", + "vertical_align" => "top", + "elements" => [ + [ + "tag" => "column_set", + "flex_mode" => "none", + "horizontal_spacing" => "default", + "background_style" => "default", + "columns" => [ + [ + "tag" => "column", + "elements" => [ + [ + "tag" => "div", + "text" => [ + "tag" => "plain_text", + "content" => "文件: {$this->filename}", + "text_size" => "normal", + "text_align" => "left", + "text_color" => "default" + ], + "icon" => [ + "tag" => "standard_icon", + "token" => "file-link-word_outlined", + "color" => "grey" + ] + ] + ], + "width" => "weighted", + "weight" => 1 + ] + ] + ] + ], + "weight" => 1 + ] + ], + "margin" => "16px 0px 0px 0px" + ], + [ + "tag" => "column_set", + "flex_mode" => "none", + "horizontal_spacing" => "default", + "background_style" => "default", + "columns" => [ + [ + "tag" => "column", + "elements" => [ + [ + "tag" => "div", + "text" => [ + "tag" => "plain_text", + "content" => "详情: {$this->content}", + "text_size" => "normal", + "text_align" => "left", + "text_color" => "default" + ], + "icon" => [ + "tag" => "standard_icon", + "token" => "tab-more_outlined", + "color" => "grey" + ] + ] + ], + "width" => "weighted", + "weight" => 1 + ] + ] + ], + ...($this->isAtAll ? [$atAllText] : []) + ] + ], + "i18n_header" => [ + "zh_cn" => [ + "title" => [ + "tag" => "plain_text", + "content" => $this->title + ], + "subtitle" => [ + "tag" => "plain_text", + "content" => $this->subTitle + ], + "template" => $this->titleColor + ] + ] + ]; + + return [ + 'msg_type' => 'interactive', + 'card' => $data, + ]; + } +} diff --git a/src/Task/Error.php b/src/Task/Error.php index b74ca9d..09d5739 100644 --- a/src/Task/Error.php +++ b/src/Task/Error.php @@ -4,6 +4,10 @@ use EasySwoole\Task\AbstractInterface\TaskInterface; use EasySwoole\Utility\File; +use WonderGame\EsUtility\Common\Classes\DateUtils; +use WonderGame\EsUtility\Notify\DingTalk\Message\Markdown; +use WonderGame\EsUtility\Notify\EsNotify; +use WonderGame\EsUtility\Notify\Feishu\Message\CardError; /** * 程序异常 @@ -29,21 +33,51 @@ public function run(int $taskId, int $workerIndex) $servname = config('SERVNAME'); $servername = config('SERVER_NAME'); - $data = [ - '### **' . $title . '**', - '- 服务器: ' . $servname, - '- 项 目:' . $servername, - "- 文 件:{$this->data['file']} 第 {$this->data['line']} 行", - "- 详 情:" . $this->data['message'] ?? '', - '- 触发方式: ' . $this->data['trigger'] ?? '', - ]; - - foreach ($this->merge as $key => $value) { - $data[] = "- " . (is_int($key) ? $value : "$key => $value"); + $driver = config('ES_NOTIFY.driver') ?: 'dingTalk'; + + switch ($driver) { + case 'feishu': // 飞书 card + + $message = $this->data['message'] ?? ''; + foreach ($this->merge as $key => $value) { + $message = "\n" . (is_int($key) ? $value : "$key: $value"); + } + + $Message = new CardError([ + 'servername' => $servname, + 'project' => $servername, + 'datetime' => date(DateUtils::FULL), + 'trigger' => $this->data['trigger'] ?? '', + 'filename' => "{$this->data['file']} 第 {$this->data['line']} 行", + 'content' => $message, + 'isAtAll' => true + ]); + + break; + default: // 钉钉 markdown + $data = [ + '### **' . $title . '**', + '- 服务器: ' . $servname, + '- 项 目:' . $servername, + "- 文 件:{$this->data['file']} 第 {$this->data['line']} 行", + "- 详 情:" . $this->data['message'] ?? '', + '- 触发方式: ' . $this->data['trigger'] ?? '', + ]; + + foreach ($this->merge as $key => $value) { + $data[] = "- " . (is_int($key) ? $value : "$key: $value"); + } + + $message = implode($this->warp, $data); + $Message = new Markdown([ + 'title' => $title, + 'text' => $message, + 'isAtAll' => true + ]); + break; } - $message = implode($this->warp, $data); - notice($message, $title); + EsNotify::getInstance()->doesOne($driver, $Message); $message = "$title: {$this->data['message']}, 文件 {$this->data['file']} 第 {$this->data['line']} 行"; wechat_warning($message);