diff --git a/app/Endpoint/Api/Http/Controllers/Account/WalletController.php b/app/Endpoint/Api/Http/Controllers/Account/WalletController.php index 481956eb..abffbf59 100644 --- a/app/Endpoint/Api/Http/Controllers/Account/WalletController.php +++ b/app/Endpoint/Api/Http/Controllers/Account/WalletController.php @@ -365,6 +365,7 @@ class WalletController extends Controller } catch (Throwable $th) { DB::rollBack(); report($th); + throw new BizException('转账失败,请稍后再试'); } return response()->noContent(); diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php b/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php index f3645260..76f9c43a 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php @@ -11,6 +11,8 @@ use App\Exceptions\BizException; use App\Exceptions\PayPasswordIncorrectException; use App\Helpers\Paginator as PaginatorHelper; use App\Models\DealerWalletToBankLog; +use App\Models\User; +use App\Rules\PhoneNumber; use App\Services\Dealer\WalletService; use Illuminate\Http\Request; use Illuminate\Support\Arr; @@ -60,7 +62,7 @@ class WalletController extends Controller } //校验是否关闭提现 if (!$user->dealerWallet?->withdrawable) { - throw new BizException('可提账户已被限制提现'); + throw new BizException('账户已被限制提现'); } //检测是否可以提现 @@ -103,6 +105,53 @@ class WalletController extends Controller return response()->noContent(); } + /** + * 余额转账 + * + * @return void + */ + public function walletTransfer(Request $request, WalletService $walletService) + { + $input = $request->validate([ + 'phone' => ['bail', 'required', new PhoneNumber()], + 'amount' => ['bail', 'required', 'int', 'min:1'], + 'pay_password' => ['bail', 'required', 'filled', 'string', 'size:6'], + ]); + + $user = $request->user(); + + //校验是否关闭 + if (!$user->dealerWallet?->withdrawable) { + throw new BizException('账户已被限制转账'); + } + + //判断转账对象是否存在 + $toUser = User::where('phone', '=', $input['phone'])->first(); + if (is_null($toUser)) { + throw new BizException('转账对象不存在'); + } + //校验安全密码 + if (! $user->wallet?->verifyPassword($input['pay_password'])) { + throw new PayPasswordIncorrectException(); + } + + try { + DB::beginTransaction(); + //转出对象 + $log = $walletService->changeBalance($user, -Arr::get($input, 'amount', 0), DealerWalletAction::TransferOut, '转出-'.$toUser->phone.($toUser->userInfo?->nickname ? '【'.$toUser->userInfo->nickname.'】' : '')); + //转入对象 + $walletService->changeBalance($toUser, Arr::get($input, 'amount', 0), DealerWalletAction::TransferIn, $user->phone.($user->userInfo?->nickname ? '【'.$user->userInfo->nickname.'】' : '').'-转入', $log); + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('转账失败,请稍后再试'); + } + + return response()->noContent(); + } + /** * 提现记录 * diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index ad8c4153..3fc73b62 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -278,6 +278,7 @@ Route::group([ Route::post('earnings/{earning}/confirm', [Dealer\EarningController::class, 'confirmEarning']); //余额提现 Route::get('wallet', [Dealer\WalletController::class, 'index']); + Route::post('wallet/transfer', [Dealer\WalletController::class, 'walletTransfer']); Route::post('wallet/withdraw', [Dealer\WalletController::class, 'walletToBank']); Route::get('wallet/withdraw-logs', [Dealer\WalletController::class, 'walletToBankLogs']); Route::get('wallet/withdraw-logs/{withdraw_log}', [Dealer\WalletController::class, 'walletToBankLogShow']); diff --git a/app/Enums/DealerWalletAction.php b/app/Enums/DealerWalletAction.php index 8ab1f585..328a5809 100644 --- a/app/Enums/DealerWalletAction.php +++ b/app/Enums/DealerWalletAction.php @@ -11,4 +11,6 @@ enum DealerWalletAction: int { case WithdrawFiled = 6; case OrderPaid = 7; case OrderIncome = 8; + case TransferIn = 9; + case TransferOut = 10; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3bf420c6..fd94ce88 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -64,6 +64,7 @@ class AppServiceProvider extends ServiceProvider 'dealer_purchase_subsidy' => \App\Models\DealerPurchaseSubsidy::class, 'dealer_wallet_to_bank_log' => \App\Models\DealerWalletToBankLog::class, 'dealer_earnings'=> \App\Models\DealerEarning::class, + 'dealer_wallet_log'=> \App\Models\DealerWalletLog::class, ]); JsonResource::withoutWrapping(); diff --git a/app/Services/Dealer/WalletService.php b/app/Services/Dealer/WalletService.php index 9084d525..88aa7ef7 100644 --- a/app/Services/Dealer/WalletService.php +++ b/app/Services/Dealer/WalletService.php @@ -5,6 +5,7 @@ namespace App\Services\Dealer; use App\Enums\DealerWalletAction; use App\Exceptions\BizException; use App\Models\DealerWallet; +use App\Models\DealerWalletLog; use App\Models\User; use Illuminate\Database\Eloquent\Model; @@ -18,7 +19,7 @@ class WalletService * @param \App\Enums\DealerWalletAction $action * @param string|null $remarks * @param \Illuminate\Database\Eloquent\Model|null $loggable - * @return void + * @return DealerWalletLog $log */ public function changeBalance(User $user, $changeBalance, DealerWalletAction $action, ?string $remarks = null, ?Model $loggable = null) { @@ -53,7 +54,7 @@ class WalletService ]); } - $user->dealerWalletLogs()->create([ + return $user->dealerWalletLogs()->create([ 'loggable_id' => $loggable?->id, 'loggable_type' => $loggable?->getMorphClass(), 'before_balance' => $beforeBalance,