diff --git a/app/Http/Controllers/Api/BalanceController.php b/app/Http/Controllers/Api/BalanceController.php deleted file mode 100644 index 5e1623f..0000000 --- a/app/Http/Controllers/Api/BalanceController.php +++ /dev/null @@ -1,101 +0,0 @@ -simplePaginate(30); - return $this->success($balances); - } - - public function checkAndCharge(Balance $balance, $check = false): JsonResponse|bool - { - - if ($check) { - $alipay = Pay::alipay()->find(['out_trade_no' => $balance->order_id,]); - - if ($alipay->trade_status !== 'TRADE_SUCCESS') { - return false; - } - } - - if ($balance->paid_at !== null) { - return true; - } - - try { - (new Transaction)->addAmount($balance->user_id, 'alipay', $balance->amount); - - $balance->update([ - 'paid_at' => now() - ]); - } catch (InvalidResponseException $e) { - Log::error($e->getMessage()); - return $this->error('无法验证支付结果。'); - } catch (ChargeException $e) { - Log::error($e->getMessage()); - return $this->error('暂时无法处理充值。'); - - } - - return true; - } - - /** - * 获取交易记录 - * - * @param mixed $request - * - * @return JsonResponse - */ - public function transactions(Request $request): JsonResponse - { - $transactions = Transaction::where('user_id', auth()->id()); - - if ($request->has('type')) { - $transactions = $transactions->where('type', $request->type); - } - - if ($request->has('payment')) { - $transactions = $transactions->where('payment', $request->payment); - } - - $transactions = $transactions->latest()->simplePaginate(30); - - return $this->success($transactions); - } - - /** - * 获取 Drops - * - * @return JsonResponse - */ - public function drops(): JsonResponse - { - $user_id = auth()->id(); - - $resp = [ - 'drops' => (new Transaction())->getDrops($user_id), - 'rate' => config('drops.rate'), - ]; - - return $this->success($resp); - } -} diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index 70202a9..6754edc 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -19,6 +19,7 @@ public function index(Request $request) $user['drops'] = $transaction->getDrops($user['id']); $user['drops_rate'] = config('drops.rate'); + return $this->success($user); } } diff --git a/app/Http/Controllers/Web/AuthController.php b/app/Http/Controllers/Web/AuthController.php index 665c373..707f03a 100644 --- a/app/Http/Controllers/Web/AuthController.php +++ b/app/Http/Controllers/Web/AuthController.php @@ -39,14 +39,6 @@ public function index(Request $request) } } - if (Auth::check()) { - $user = Auth::user(); - if ($user->banned_at !== null) { - // $user->tokens()->delete(); - return redirect()->route('banned'); - } - } - return view('index'); } diff --git a/app/Http/Controllers/Web/BalanceController.php b/app/Http/Controllers/Web/BalanceController.php index 658b366..5a595d6 100644 --- a/app/Http/Controllers/Web/BalanceController.php +++ b/app/Http/Controllers/Web/BalanceController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Models\Balance; +use App\Models\Module; use App\Models\Transaction; use Illuminate\Contracts\View\View; use Illuminate\Http\JsonResponse; @@ -24,7 +25,11 @@ public function index(Request $request): View $balance = $request->user()->balance; - return view('balances.index', compact('drops', 'balance')); + $balances = Balance::thisUser()->latest()->paginate(50); + + $drops_rate = config('drops.rate'); + + return view('balances.index', compact('drops', 'balance', 'balances', 'drops_rate')); } public function store(Request $request) @@ -66,7 +71,6 @@ public function store(Request $request) } /** - * @throws \Laravel\Octane\Exceptions\DdException */ public function show(Balance $balance) { @@ -105,7 +109,7 @@ public function notify(Request $request): View|JsonResponse // 检测订单是否已支付 if ($balance->paid_at !== null) { // return $this->success('订单已支付'); - return view('pay_process'); + return view('balances.pay_process'); } // try { @@ -128,10 +132,40 @@ public function notify(Request $request): View|JsonResponse // throw new ChargeException('商户不匹配'); // } - if ((new \App\Http\Controllers\Api\BalanceController())->checkAndCharge($balance, true)) { - return view('pay_process'); - } else { - abort(500, '支付失败'); + return view('pay_process'); + + // + // if ((new \App\Jobs\CheckAndChargeBalance())->checkAndCharge($balance, true)) { + // return view('pay_process'); + // } else { + // abort(500, '支付失败'); + // } + } + + /** + * 获取交易记录 + * + * @param mixed $request + * + * @return View + */ + public function transactions(Request $request): View + { + + $modules = Module::all(); + + $transactions = Transaction::where('user_id', auth()->id()); + + if ($request->has('type')) { + $transactions = $transactions->where('type', $request->type); } + + if ($request->has('payment')) { + $transactions = $transactions->where('payment', $request->payment); + } + + $transactions = $transactions->latest()->paginate(30); + + return view('balances.transactions', compact('transactions', 'modules')); } } diff --git a/app/Http/Controllers/Web/TransferController.php b/app/Http/Controllers/Web/TransferController.php new file mode 100644 index 0000000..e2b42d6 --- /dev/null +++ b/app/Http/Controllers/Web/TransferController.php @@ -0,0 +1,15 @@ + \App\Http\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + 'banned' => \App\Http\Middleware\ValidateUserIfBanned::class, ]; } diff --git a/app/Http/Middleware/ValidateUserIfBanned.php b/app/Http/Middleware/ValidateUserIfBanned.php new file mode 100644 index 0000000..016c477 --- /dev/null +++ b/app/Http/Middleware/ValidateUserIfBanned.php @@ -0,0 +1,27 @@ +user(); + if ($user) if ($user->banned_at !== null) { + return redirect()->route('banned'); + } + + return $next($request); + } +} diff --git a/app/Jobs/CheckAndChargeBalance.php b/app/Jobs/CheckAndChargeBalance.php index 26532cd..311f18e 100644 --- a/app/Jobs/CheckAndChargeBalance.php +++ b/app/Jobs/CheckAndChargeBalance.php @@ -2,8 +2,12 @@ namespace App\Jobs; -use App\Http\Controllers\Api\BalanceController; +use App\Exceptions\ChargeException; use App\Models\Balance; +use App\Models\Transaction; +use Illuminate\Support\Facades\Log; +use Yansongda\LaravelPay\Facades\Pay; +use Yansongda\Pay\Exception\InvalidResponseException; class CheckAndChargeBalance extends Job { @@ -24,13 +28,9 @@ public function __construct() */ public function handle() { - // - - $bc = new BalanceController(); - - Balance::where('paid_at', null)->chunk(100, function ($balances) use ($bc) { + Balance::where('paid_at', null)->chunk(100, function ($balances) { foreach ($balances as $balance) { - if (!$bc->checkAndCharge($balance, true)) { + if (!$this->checkAndCharge($balance, true)) { if (now()->diffInDays($balance->created_at) > 1) { $balance->delete(); } @@ -40,4 +40,37 @@ public function handle() Balance::where('paid_at', null)->where('created_at', '<', now()->subDays(2))->delete(); } + + public function checkAndCharge(Balance $balance, $check = false): bool + { + + if ($check) { + $alipay = Pay::alipay()->find(['out_trade_no' => $balance->order_id]); + + if ($alipay->trade_status !== 'TRADE_SUCCESS') { + return false; + } + } + + if ($balance->paid_at !== null) { + return true; + } + + try { + (new Transaction)->addAmount($balance->user_id, 'alipay', $balance->amount); + + $balance->update([ + 'paid_at' => now() + ]); + } catch (InvalidResponseException $e) { + Log::error($e->getMessage()); + return false; + } catch (ChargeException $e) { + Log::error($e->getMessage()); + return false; + + } + + return true; + } } diff --git a/app/View/Components/Payment.php b/app/View/Components/Payment.php new file mode 100644 index 0000000..b9bec21 --- /dev/null +++ b/app/View/Components/Payment.php @@ -0,0 +1,43 @@ +payment = $payment; + } + + /** + * Get the view / contents that represent the component. + * + * @return \Illuminate\Contracts\View\View|\Closure|string + */ + public function render() + { + + $this->payment = match ($this->payment) { + 'alipay' => '支付宝', + 'wechat', 'wepay' => '微信支付', + 'drops' => 'Drops', + 'balance' => '余额', + 'unfreeze' => '解冻', + 'freeze' => '冻结', + 'console' => '控制台', + default => $this->payment, + }; + + return view('components.payment', ['payment' => $this->payment]); + } +} diff --git a/composer.json b/composer.json index 424daee..52b6b78 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ ], "license": "MIT", "require": { - "php": "^8.0.2", + "php": "^8.1", "doctrine/dbal": "^3.4", "fruitcake/laravel-cors": "^3.0", "genealabs/laravel-model-caching": "^0.12.5", diff --git a/resources/views/balances/index.blade.php b/resources/views/balances/index.blade.php index 25e7830..bc02664 100644 --- a/resources/views/balances/index.blade.php +++ b/resources/views/balances/index.blade.php @@ -9,11 +9,100 @@
您的余额: {{ $balance }} 元
Drops: {{ $drops }}
- + ≈ Drops + +订单号 | +支付方式 | +金额 | +完成时间 | +
---|---|---|---|
{{ $b->order_id }} | +
+ |
+ + {{ $b->amount }} + | ++ {{ $b->paid_at }} + | +
我们正在处理,您的余额很快就到账。
+ + +@endsection diff --git a/resources/views/balances/transactions.blade.php b/resources/views/balances/transactions.blade.php new file mode 100644 index 0000000..e1a644e --- /dev/null +++ b/resources/views/balances/transactions.blade.php @@ -0,0 +1,101 @@ +@extends('layouts.app') + +@section('title', '交易记录') + +@section('content') +类型与模块 | +支付方式 | +说明 | +入账 | +支出 | +余额 | +交易时间 | +
---|---|---|---|---|---|---|
+ @if ($t->type = 'payout') + + 支出 + + @else($t->type = 'payin') + + 收入 + + @endif + + {{ $t->module_id }} + + | +
+ |
+ + {{ $t->description }} + | +
+ {{ $t->income }} 元
+ + {{ $t->income_drops }} Drops + |
+
+
+ {{ $t->outcome }} 元
+ + {{ $t->outcome_drops }} Drops + |
+
+
+ {{ $t->balance }} 元
+ + {{ $t->drops }} Drops + |
+ + {{ $t->created_at }} + | +
{{ auth()->user()->banned_reason }}
+@section('content') -更换账号 - + @if (auth()->user()->banned_at) +{{ auth()->user()->banned_reason }}
- + + @else +嗨, {{ auth('web')->user()->name }} -
在这里,你可以获取新的 Token 来对接其他应用程序。
+在这里,你可以获取新的 Token 来对接其他应用程序或者访问 控制面板。