Compare commits

...

2 Commits

Author SHA1 Message Date
liyang fc9879b847 chore: 日志记录 1 month ago
liyang 91a4ca3c71 fix: 融资申请 1 month ago
  1. 14
      app/ExceptionHandle.php
  2. 10
      app/home/middleware/ApiLogMiddleware.php
  3. 5
      app/home/service/UserService.php

14
app/ExceptionHandle.php

@ -1,4 +1,5 @@
<?php <?php
namespace app; namespace app;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
@ -7,6 +8,7 @@ use think\exception\Handle;
use think\exception\HttpException; use think\exception\HttpException;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\facade\Log;
use think\Response; use think\Response;
use Throwable; use Throwable;
@ -50,8 +52,18 @@ class ExceptionHandle extends Handle
*/ */
public function render($request, Throwable $e): Response public function render($request, Throwable $e): Response
{ {
// 统一写日志
Log::error('System Exception' . json_encode([
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
]));
// return json([
// 'code' => 100500,
// 'message' => 'System error',
// ]);
// 添加自定义异常处理机制 // 添加自定义异常处理机制
// 其他错误交给系统处理 // 其他错误交给系统处理
return parent::render($request, $e); return parent::render($request, $e);
} }

10
app/home/middleware/ApiLogMiddleware.php

@ -40,8 +40,14 @@ class ApiLogMiddleware
if ($request->method(true) === 'OPTIONS') { if ($request->method(true) === 'OPTIONS') {
return response()->send(); return response()->send();
} }
$response = $next($request);
$path = $request->pathinfo(); $path = $request->pathinfo();
$shouldLog = in_array($path, $this->logRoutes); $shouldLog = in_array($path, $this->logRoutes);
$responseData = $response instanceof Response ? $response->getData() : null;
// 如果接口返回错误码,也记录日志
if ($responseData && isset($responseData['code']) && $responseData['code'] != 0) {
$shouldLog = true;
}
$logData = []; $logData = [];
if ($shouldLog) { if ($shouldLog) {
$ip = (new BaseHomeService())->getClientRealIp(); $ip = (new BaseHomeService())->getClientRealIp();
@ -58,11 +64,9 @@ class ApiLogMiddleware
} }
} }
$response = $next($request);
if ($shouldLog && $response instanceof Response) { if ($shouldLog && $response instanceof Response) {
$logData['response'] = $response->getData(); $logData['response'] = $responseData;
} }
//写入redis //写入redis
if (!empty($logData)) { if (!empty($logData)) {

5
app/home/service/UserService.php

@ -1591,7 +1591,10 @@ class UserService extends BaseHomeService
} }
// 参数校验 nation phone sms_code // 参数校验 nation phone sms_code
validate(UserValidate::class)->scene('apply_loan')->check($param); // validate(UserValidate::class)->scene('apply_loan')->check($param);
if (empty($param['num']) || $param['num'] < 0 || empty($param['day']) || $param['day'] < 0) {
return $this->toData('500', lang('parameter_error'));
}
$count = UserLoanModel::where('status', 0)->where('user_id', $userId)->count(); $count = UserLoanModel::where('status', 0)->where('user_id', $userId)->count();
if ($count > 0) { if ($count > 0) {
return $this->toData('500', lang('there_is_an_application_record_please_wait_for_review')); return $this->toData('500', lang('there_is_an_application_record_please_wait_for_review'));

Loading…
Cancel
Save