Skip to content

Commit

Permalink
Merge pull request #117 from wonder-game/develop
Browse files Browse the repository at this point in the history
优化飞书Error消息模版
  • Loading branch information
Joyboo authored Jun 24, 2024
2 parents 1a04111 + c080fa5 commit 0f7946a
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 13 deletions.
252 changes: 252 additions & 0 deletions src/Notify/Feishu/Message/CardError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
<?php

namespace WonderGame\EsUtility\Notify\Feishu\Message;

/**
* Class CardError
* @document https://open.feishu.cn/cardkit/editor?cardId=AAqHDSfEv3SKm&cardLocale=zh_cn
* @package WonderGame\EsUtility\Notify\Feishu\Message
*/
class CardError extends Base
{
/**
* 标题栏颜色
* @var string
*/
protected $titleColor = 'red';

/**
* 主标题
* @var string
*/
protected $title = '程序异常';

/**
* 副标题,可不设置
* @var string
*/
protected $subTitle = '';

/**
* 运行的服务器
* @var string
*/
protected $servername = '';

/**
* 运行的项目
* @var string
*/
protected $project = '';

/**
* 发生的时间
* @var string
*/
protected $datetime = '';

/**
* 触发的方式 trigger|error
* @var string
*/
protected $trigger = '';

/**
* 文件
* @var string
*/
protected $filename = '';

/**
* 内容
* @var string
*/
protected $content = '';

public function fullData()
{
$atAllText = [
'tag' => '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,
];
}
}
60 changes: 47 additions & 13 deletions src/Task/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 程序异常
Expand All @@ -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);
Expand Down

0 comments on commit 0f7946a

Please sign in to comment.