Skip to content

Commit a6b1e16

Browse files
committed
修复操作日志username读取不到问题
1 parent 69ef8ca commit a6b1e16

File tree

8 files changed

+27
-14
lines changed

8 files changed

+27
-14
lines changed

app/Controller/IndexController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Foundation\Facades\Log;
88
use App\Foundation\Utils\Mail;
9+
use App\Job\EmailNotificationJob;
910
use App\Model\Laboratory\Bilibili\UpUser;
1011
use App\Model\Laboratory\Bilibili\UpUserReport;
1112
use App\Service\Laboratory\Bilibili\VideoService;
@@ -31,8 +32,11 @@ public function __construct()
3132
*/
3233
public function index()
3334
{
34-
$bvid = $this->request->input('bvid');
35-
return curl_get('https://api.bilibili.com/x/web-interface/view?bvid=' . $bvid);
35+
//分发队列
36+
$this->queue->push(new EmailNotificationJob([
37+
'title' => $params['title'],
38+
'content' => $params['content'],
39+
]));
3640

3741
// $url = 'https://m.bilibili.com/video/BV1Q624y1q7sj';
3842

app/Controller/Laboratory/UploadController.php

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function uploadPicByBase64()
4444
];
4545
$this->verifyParams($params, $rules, $message);
4646

47+
base64DecImg($params['file']);
4748
$uploadResult = UploadService::getInstance()->uploadSinglePicByBase64($params['file'], $params['savePath']);
4849
return $this->success($uploadResult);
4950
}

app/Exception/Handler/AppExceptionHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ public function handle(Throwable $throwable, ResponseInterface $response)
5656
if ($throwable instanceof TokenValidException) {
5757
// 阻止异常冒泡
5858
$this->stopPropagation();
59-
return $this->error($throwable->getCode(), $throwable->getMessage());
59+
return $this->error($throwable->getCode(), $throwable->getMessage(), false);
6060
}
6161

6262
if ($throwable instanceof JWTException) {
6363
// 阻止异常冒泡
6464
$this->stopPropagation();
65-
return $this->error($throwable->getCode(), $throwable->getMessage());
65+
return $this->error($throwable->getCode(), $throwable->getMessage(), false);
6666
}
6767

6868
// 判断是否由业务异常类抛出的异常

app/Foundation/Facades/MessageParser.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Hyperf\Utils\Codec\Json;
77

88
/**
9-
* json格式化
9+
* 消息格式化
1010
* Class MessageParser
1111
* @Author YiYuan-Lin
1212
* @Date: 2021/3/12
@@ -15,7 +15,6 @@ class MessageParser
1515
{
1616
/**
1717
* @param string $data
18-
*
1918
* @return array
2019
*/
2120
public static function decode(string $data) : array
@@ -25,11 +24,20 @@ public static function decode(string $data) : array
2524

2625
/**
2726
* @param array $data
28-
*
2927
* @return string
3028
*/
3129
public static function encode(array $data) : string
3230
{
3331
return Json::encode($data);
3432
}
33+
34+
/**
35+
* 异常消息格式化
36+
* @param \Exception $e
37+
* @return string
38+
*/
39+
public static function expMessageParser(\Exception $e) : string
40+
{
41+
return '错误码为:' . $e->getCode() . '错误信息: ' . $e->getMessage() . ':: 错误信息文件位置:' . $e->getFile() . ':: 错误信息行数: ' . $e->getLine();
42+
}
3543
}

app/Foundation/Traits/ApiTrait.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ protected function successByMessage(string $message = '')
5959
* 错误响应
6060
* @param int $statusCode
6161
* @param string|null $message
62+
* @param bool $isRecordLog
6263
* @return \Psr\Http\Message\ResponseInterface
6364
*/
64-
protected function error(int $statusCode = StatusCode::ERR_EXCEPTION, string $message = null)
65+
protected function error(int $statusCode = StatusCode::ERR_EXCEPTION, string $message = null, bool $isRecordLog = true)
6566
{
6667
$message = $message ?? StatusCode::ERR_EXCEPTION;
6768

@@ -73,17 +74,16 @@ protected function error(int $statusCode = StatusCode::ERR_EXCEPTION, string $me
7374
$loginLogData['response_code'] = $statusCode;
7475
$loginLogData['response_result'] = $message;
7576
LoginLog::add($loginLogData);
76-
return $this->response->json($this->formatResponse([], $message, $statusCode));
77-
}else {
77+
}else if ($isRecordLog) {
7878
//记录操作异常日志
7979
$logData = OperateLogService::getInstance()->collectLogInfo();
8080
if(!empty($logData)) {
8181
$logData['response_result'] = $message;
8282
$logData['response_code'] = $statusCode;
8383
if (!empty($logData['action'])) OperateLog::add($logData);
8484
}
85-
return $this->response->json($this->formatResponse([], $message, $statusCode));
8685
}
86+
return $this->response->json($this->formatResponse([], $message, $statusCode));
8787
}
8888

8989
/**

app/Job/EmailNotificationJob.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Job;
66

77
use App\Foundation\Facades\Log;
8+
use App\Foundation\Facades\MessageParser;
89
use App\Foundation\Utils\Mail;
910
use App\Model\Auth\User;
1011
use Hyperf\AsyncQueue\Job;
@@ -48,7 +49,7 @@ public function handle()
4849
->send();
4950
}
5051
} catch (\Exception $e) {
51-
Log::jobLog()->error($e->getMessage());
52+
Log::jobLog()->error(MessageParser::expMessageParser($e));
5253
}
5354
}
5455

app/Service/System/LoginLogService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function collectLoginLogInfo() : array
3636
$loginTime = date('Y-m-d H:i:s');
3737

3838
return [
39-
'username' => $requireParams['username'],
39+
'username' => $requireParams['username'] ?? '',
4040
'login_ip' => $loginIp,
4141
'login_address' => $loginAddress,
4242
'login_browser' => $browser,

bin/hyperf.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env php
22
<?php
3-
43
ini_set('display_errors', 'on');
54
ini_set('display_startup_errors', 'on');
65

0 commit comments

Comments
 (0)