Skip to content

Commit b9937ba

Browse files
committed
serverChan优化
1 parent 4ee830f commit b9937ba

4 files changed

Lines changed: 54 additions & 28 deletions

File tree

‎app/Components/ServerChan.php‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,83 @@
22

33
namespace App\Components;
44

5+
use App\Http\Models\Config;
6+
use App\Http\Models\EmailLog;
57
use GuzzleHttp\Client;
68
use GuzzleHttp\Psr7;
79
use GuzzleHttp\Exception\RequestException;
810
use Log;
911

1012
class ServerChan
1113
{
14+
protected static $config;
15+
16+
function __construct()
17+
{
18+
self::$config = $this->systemConfig();
19+
}
20+
1221
/**
1322
* @param string $title 消息标题
1423
* @param string $content 消息内容
15-
* @param string $key ServerChan上申请的SCKEY
1624
* @return string
1725
*/
18-
public function send($title, $content, $key)
26+
public function send($title, $content)
1927
{
2028
$client = new Client();
2129

2230
try {
23-
$response = $client->request('GET', 'https://sc.ftqq.com/' . $key . '.send', [
31+
$response = $client->request('GET', 'https://sc.ftqq.com/' . self::$config['server_chan_key'] . '.send', [
2432
'query' => [
2533
'text' => $title,
2634
'desp' => $content
2735
]
2836
]);
2937

30-
return json_decode($response->getBody());
38+
$result = json_decode($response->getBody());
39+
if ($result->errno > 0) {
40+
$this->sendEmailLog(1, '[ServerChan]' . $title, $content);
41+
} else {
42+
$this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
43+
}
3144
} catch (RequestException $e) {
3245
Log::error(Psr7\str($e->getRequest()));
3346
if ($e->hasResponse()) {
3447
Log::error(Psr7\str($e->getResponse()));
3548
}
3649
}
3750
}
51+
52+
/**
53+
* 写入邮件发送日志
54+
*
55+
* @param int $user_id 用户ID
56+
* @param string $title 标题
57+
* @param string $content 内容
58+
* @param int $status 投递状态
59+
* @param string $error 投递失败时记录的异常信息
60+
*/
61+
private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
62+
{
63+
$emailLogObj = new EmailLog();
64+
$emailLogObj->user_id = $user_id;
65+
$emailLogObj->title = $title;
66+
$emailLogObj->content = $content;
67+
$emailLogObj->status = $status;
68+
$emailLogObj->error = $error;
69+
$emailLogObj->created_at = date('Y-m-d H:i:s');
70+
$emailLogObj->save();
71+
}
72+
73+
// 系统配置
74+
private function systemConfig()
75+
{
76+
$config = Config::query()->get();
77+
$data = [];
78+
foreach ($config as $vo) {
79+
$data[$vo->name] = $vo->value;
80+
}
81+
82+
return $data;
83+
}
3884
}

‎app/Console/Commands/AutoCheckNodeStatusJob.php‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ public function handle()
5555
// 通过ServerChan发微信消息提醒管理员
5656
if ($config['is_server_chan'] && $config['server_chan_key']) {
5757
$serverChan = new ServerChan();
58-
$result = $serverChan->send($title, $content, $config['server_chan_key']);
59-
if ($result->errno > 0) {
60-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content);
61-
} else {
62-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
63-
}
58+
$serverChan->send($title, $content);
6459
}
6560

6661
// 写入发信缓存

‎app/Http/Controllers/TicketController.php‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ public function replyTicket(Request $request)
8282
// 通过ServerChan发微信消息提醒管理员
8383
if (!$user['is_admin'] && self::$config['is_server_chan'] && self::$config['server_chan_key']) {
8484
$serverChan = new ServerChan();
85-
$result = $serverChan->send($title, $content, self::$config['server_chan_key']);
86-
if ($result->errno > 0) {
87-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content);
88-
} else {
89-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
90-
}
85+
$serverChan->send($title, $content);
9186
}
9287

9388
return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);

‎app/Http/Controllers/UserController.php‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,7 @@ public function addTicket(Request $request)
370370
// 通过ServerChan发微信消息提醒管理员
371371
if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
372372
$serverChan = new ServerChan();
373-
$result = $serverChan->send($emailTitle, $content, self::$config['server_chan_key']);
374-
if ($result->errno > 0) {
375-
$this->sendEmailLog(1, '[ServerChan]' . $emailTitle, $content);
376-
} else {
377-
$this->sendEmailLog(1, '[ServerChan]' . $emailTitle, $content, 0, $result->errmsg);
378-
}
373+
$serverChan->send($emailTitle, $content);
379374
}
380375

381376
return Response::json(['status' => 'success', 'data' => '', 'message' => '提交成功']);
@@ -420,12 +415,7 @@ public function replyTicket(Request $request)
420415
// 通过ServerChan发微信消息提醒管理员
421416
if (self::$config['is_server_chan'] && self::$config['server_chan_key']) {
422417
$serverChan = new ServerChan();
423-
$result = $serverChan->send($title, $content, self::$config['server_chan_key']);
424-
if ($result->errno > 0) {
425-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content);
426-
} else {
427-
$this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
428-
}
418+
$serverChan->send($title, $content);
429419
}
430420

431421
return Response::json(['status' => 'success', 'data' => '', 'message' => '回复成功']);

0 commit comments

Comments
 (0)