diff --git a/app/Constants/v3/LogLabel.php b/app/Constants/v3/LogLabel.php index a5c5bf0..7163136 100644 --- a/app/Constants/v3/LogLabel.php +++ b/app/Constants/v3/LogLabel.php @@ -17,6 +17,11 @@ class LogLabel extends AbstractConstants */ const ERROR_CODE_EXCEPTION = 'error_code_exception_log'; + /** + * @Message("ErrorCode异常抛出") + */ + const ERROR_CODE_EXCEPTION_LOG_DATA = 'error_code_exception_log_data'; + /** * @Message("线上订单") */ diff --git a/app/Controller/v3/CommunityController.php b/app/Controller/v3/CommunityController.php index 37607cc..c4e9f93 100644 --- a/app/Controller/v3/CommunityController.php +++ b/app/Controller/v3/CommunityController.php @@ -19,9 +19,11 @@ class CommunityController extends BaseController */ protected $userCommunityService; - public function bind(CommunityBindRequest $request) + // public function bind(CommunityBindRequest $request) + public function bind() { - $data = $request->validated(); + // $data = $request->validated(); + $data = $this->request->all(); $jsonData = $data['json_data'] ?? json_encode([]); $res = $this->userCommunityService->bind(UserRelationBind::BIND_TYPE_COMMUNITY, $data['source_id'], $data['user_id'], $jsonData); return $this->success(['id' => $res->id]); diff --git a/app/Controller/v3/OrderOfflineController.php b/app/Controller/v3/OrderOfflineController.php index 52d1003..9d52f3a 100644 --- a/app/Controller/v3/OrderOfflineController.php +++ b/app/Controller/v3/OrderOfflineController.php @@ -82,7 +82,7 @@ class OrderOfflineController extends BaseController ->first(); if (empty($orderMain)) { - throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE); + throw new ErrorCodeException(ErrorCode::ORDER_NOT_AVAILABLE, '', ['message' => '当面付订单完成页', 'data' => ['global_order_id' => $globalOrderId,'user_id' => $userId]]); } return $this->success(['order_main' => $orderMain]); diff --git a/app/Controller/v3/WithdrawController.php b/app/Controller/v3/WithdrawController.php index 2ab1f62..e84b4aa 100644 --- a/app/Controller/v3/WithdrawController.php +++ b/app/Controller/v3/WithdrawController.php @@ -69,7 +69,7 @@ class WithdrawController extends BaseController $store = Store::query()->where(['user_id' => $userId, 'id' => $storeId])->first(); if (empty($store)) { $msg = ['user_id' => $userId, 'storeId' => $storeId]; - throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,json_encode($msg)); + throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,'',$msg); } $data['balance'] = UserBalance::query() @@ -130,7 +130,7 @@ class WithdrawController extends BaseController $store = Store::query()->where(['user_id' => $userId, 'id' => $storeId])->first(); if (empty($store)) { $msg = ['user_id' => $userId, 'storeId' => $storeId]; - throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE, '[稍后重试]' . json_encode($msg)); + throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE, '' , $msg); } // 校验余额 @@ -138,7 +138,7 @@ class WithdrawController extends BaseController ->where(['source_id' => $store->user_id, 'user_type' => UserType::STORE]) ->first(); if ($money > $balance->balance) { - throw new ErrorCodeException(ErrorCode::STORE_WITHDRAW_INSUFFICIENT_BALANCE); + throw new ErrorCodeException(ErrorCode::STORE_WITHDRAW_INSUFFICIENT_BALANCE, '', ['message' => '商户提现余额不足','data' => ['money' => $money, 'balance' => $balance->balance]]); } Db::beginTransaction(); diff --git a/app/Exception/ErrorCodeException.php b/app/Exception/ErrorCodeException.php index c6052f9..11aefa2 100644 --- a/app/Exception/ErrorCodeException.php +++ b/app/Exception/ErrorCodeException.php @@ -11,15 +11,29 @@ declare(strict_types=1); */ namespace App\Exception; +use App\Commons\Log; use App\Constants\v3\ErrorCode; +use App\Constants\v3\LogLabel; use Hyperf\Server\Exception\ServerException; use Throwable; +use Hyperf\Di\Annotation\Inject; class ErrorCodeException extends ServerException { - public function __construct(int $code = 0, string $message = null, Throwable $previous = null) + + /** + * @Inject + * @var Log + */ + protected $log; + + public function __construct(int $code = 0, string $message = null, $logData=[], Throwable $previous = null) { + if (!empty($logData)) { + $this->log->event(LogLabel::ERROR_CODE_EXCEPTION_LOG_DATA, ['logData' => json_encode($logData)]); + } + if (is_null($message)) { $message = ErrorCode::getMessage($code); } else { diff --git a/app/Service/v3/Implementations/AppointmentTimeService.php b/app/Service/v3/Implementations/AppointmentTimeService.php index 4eeec76..29bf609 100644 --- a/app/Service/v3/Implementations/AppointmentTimeService.php +++ b/app/Service/v3/Implementations/AppointmentTimeService.php @@ -39,7 +39,7 @@ class AppointmentTimeService implements AppointmentTimeServiceInterface $stores = Store::query()->whereIn('id',$sotreIds)->get()->toArray(); if(empty($stores)){ $msg = ['sotreIds'=>$sotreIds,"shopcartIds"=>$shopcartIds]; - throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,json_encode($msg)); + throw new ErrorCodeException(ErrorCode::STORE_NOT_AVAILABLE,'',$msg); } $time1Arr = []; $time2Arr = []; diff --git a/app/Service/v3/Implementations/CouponService.php b/app/Service/v3/Implementations/CouponService.php index 3a84cc1..aae0fdf 100644 --- a/app/Service/v3/Implementations/CouponService.php +++ b/app/Service/v3/Implementations/CouponService.php @@ -97,7 +97,7 @@ class CouponService implements CouponServiceInterface ->update(['number_remain' => $numberRemain, 'status' => $status]); if (!$upRes) { Db::rollBack(); - throw new ErrorCodeException(ErrorCode::COUPON_USE_FAILURE, '更新优惠券数量失败'); + throw new ErrorCodeException(ErrorCode::COUPON_USE_FAILURE, '更新优惠券数量失败', ['id' => $coupon['id'],'number_remain' => $numberRemain, 'status' => $status]); } // 缓存使用记录 $usedRes = $this->cacheTodayCouponUsed($coupon['user_id'], $coupon['coupon_id'], $coupon['id']); @@ -107,7 +107,17 @@ class CouponService implements CouponServiceInterface } } else { Db::rollBack(); - throw new ErrorCodeException(ErrorCode::COUPON_USE_FAILURE, '新增优惠券使用记录到数据库失败'); + throw new ErrorCodeException(ErrorCode::COUPON_USE_FAILURE, '新增优惠券使用记录到数据库失败', [ + 'user_id' => $coupon['user_id'], + 'user_receive_id' => $coupon['id'], + 'coupon_id' => $coupon['coupon_id'], + 'order_main_id' => $globalOrderId, + 'use_time' => time(), + 'return_time' => 0, + 'number' => 1, + 'status' => 1, + 'update_time' => 0, + ]); } } diff --git a/app/Service/v3/Implementations/DistributionPriceService.php b/app/Service/v3/Implementations/DistributionPriceService.php index 6856e3a..ebe8177 100644 --- a/app/Service/v3/Implementations/DistributionPriceService.php +++ b/app/Service/v3/Implementations/DistributionPriceService.php @@ -27,7 +27,7 @@ class DistributionPriceService implements DistributionPriceServiceInterface $distributionPrice = bcmul(1.50,($km-3),2); break; case ($km > $deliveryDistance) : - throw new ErrorCodeException(ErrorCode::LOCATION_LONG_DISTANCE); + throw new ErrorCodeException(ErrorCode::LOCATION_LONG_DISTANCE,'',['message' => '超出配送范围', 'data' => ['距离' => $km, '配送距离' => $deliveryDistance]]); break; default: $distributionPrice = 0; diff --git a/app/Service/v3/Implementations/VerifyCodeService.php b/app/Service/v3/Implementations/VerifyCodeService.php index 265c4e5..156df20 100644 --- a/app/Service/v3/Implementations/VerifyCodeService.php +++ b/app/Service/v3/Implementations/VerifyCodeService.php @@ -49,7 +49,7 @@ class VerifyCodeService implements VerifyCodeServiceInterface 600 ); if (!$expireRes) { - throw new ErrorCodeException(ErrorCode::VERIFY_CODE_ERROR); + throw new ErrorCodeException(ErrorCode::VERIFY_CODE_ERROR, '', ['message' => '验证码有效期设置失败', 'data' => ['ssdbkey' => SsdbKeys::VERIFY_CODE.$userId.'_'.$tel]]); } // 发送短信 diff --git a/app/Service/v3/Implementations/WxLoginService.php b/app/Service/v3/Implementations/WxLoginService.php index 4b9828d..e41338c 100644 --- a/app/Service/v3/Implementations/WxLoginService.php +++ b/app/Service/v3/Implementations/WxLoginService.php @@ -25,7 +25,7 @@ class WxLoginService implements \App\Service\v3\Interfaces\WxLoginServiceInterfa $result = $app->auth->session($code); if (isset($result['errcode'])&&$result['errcode'] != 0) { - throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE); + throw new ErrorCodeException(ErrorCode::WXLOGIN_INVALID_CODE, '', ['message' => '微信登录失败', 'data' => $result, 'code' => $code]); } // 更新或者插入用户数据