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 }}

-
+

添加到余额

+ @csrf - +
+ Drops + +
+
+ 请注意: 由于计费方式的特殊性,我们不支持退款,请合理充值。 +
+ + 必看! 充值后金额没有立即到账的原因。 + +
+
+ 请注意: 由于计费方式的特殊性,我们不支持退款,请合理充值。 +
+ + 必看! 充值后金额没有立即到账的原因。 + +
+
+ 请注意: 由于计费方式的特殊性,我们不支持退款,请合理充值。 +
+ + 必看! 充值后金额没有立即到账的原因。 + +
+
+ + +

充值记录

+
+ + + + + + + + + + + @foreach($balances as $b) + + + + + + + @endforeach + + +
订单号支付方式金额完成时间
{{ $b->order_id }} + + + {{ $b->amount }} + + {{ $b->paid_at }} +
+
+ + {{ $balances->links() }} + + + + + {{-- {{ }}--}} @endsection diff --git a/resources/views/balances/pay_process.blade.php b/resources/views/balances/pay_process.blade.php new file mode 100644 index 0000000..fa83771 --- /dev/null +++ b/resources/views/balances/pay_process.blade.php @@ -0,0 +1,11 @@ +@extends('layouts.app') + +@section('title', '支付处理') + +@section('content') +

正在处理

+ +

我们正在处理,您的余额很快就到账。

+ + +@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') +

交易记录

+ +
+ + + + + + + + + + + + + + @foreach ($transactions as $t) + + + + + + + + + + + + + @endforeach + + +
类型与模块支付方式说明入账支出余额交易时间
+ @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 }} +
+
+ + {{ $transactions->links() }} + + + + + +@endsection diff --git a/resources/views/banned.blade.php b/resources/views/banned.blade.php index 16d9f30..4faf6e3 100644 --- a/resources/views/banned.blade.php +++ b/resources/views/banned.blade.php @@ -1,19 +1,21 @@ - - +@extends('layouts.app') - - - - - - 您已被封禁 - +@section('title', '您已被封禁') - -

很抱歉,您可能违反了我们的规定。

-

{{ auth()->user()->banned_reason }}

+@section('content') -更换账号 - + @if (auth()->user()->banned_at) +

很抱歉,您可能违反了我们的规定。

+

{{ auth()->user()->banned_reason }}

- +
+ @csrf + +
+ @else +

您的账号正常。

+ 返回首页 + + @endif + +@endsection diff --git a/resources/views/components/payment.blade.php b/resources/views/components/payment.blade.php new file mode 100644 index 0000000..c56a7a9 --- /dev/null +++ b/resources/views/components/payment.blade.php @@ -0,0 +1 @@ +{{ $payment }} diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index e7449d6..7a9a219 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -21,7 +21,7 @@

嗨, {{ auth('web')->user()->name }} -

在这里,你可以获取新的 Token 来对接其他应用程序。

+

在这里,你可以获取新的 Token 来对接其他应用程序或者访问 控制面板。

@csrf diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index bac383e..1918b56 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -35,11 +35,14 @@ diff --git a/resources/views/notifications/user/balance.blade.php b/resources/views/notifications/user/balance.blade.php index 101a390..aa48843 100644 --- a/resources/views/notifications/user/balance.blade.php +++ b/resources/views/notifications/user/balance.blade.php @@ -1,3 +1,3 @@ -@if ($balance->paid_at !== null) - {{ $user->name }} 在 {{ $balance->paid_at->toDateTimeString() }} 充值了 {{ $balance->amount }} 元。 +@if ($balances->paid_at !== null) + {{ $user->name }} 在 {{ $balance->paid_at->toDateTimeString() }} 充值了 {{ $balances->amount }} 元。 @endif diff --git a/resources/views/pay_process.blade.php b/resources/views/pay_process.blade.php deleted file mode 100644 index 97d6a0f..0000000 --- a/resources/views/pay_process.blade.php +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - 支付进度 - - - -

非常感谢。我们正在处理您的支付,稍后您就可以在交易记录中看到了。

- - - diff --git a/resources/views/transfer/search.blade.php b/resources/views/transfer/search.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/routes/api.php b/routes/api.php index 3b67174..88f2881 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,5 @@ only(['index', 'store']); -Route::get('balances/transactions', [BalanceController::class, 'transactions']); -Route::get('balances/drops', [BalanceController::class, 'drops']); +// Route::get('balances/transactions', [BalanceController::class, 'transactions']); +// Route::get('balances/drops', [BalanceController::class, 'drops']); Route::apiResource('work-orders', WorkOrderController::class)->only(['index', 'store', 'show', 'update']); diff --git a/routes/web.php b/routes/web.php index dff63a9..59efcd3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,19 +2,22 @@ use App\Http\Controllers\Web\AuthController; use App\Http\Controllers\Web\BalanceController; +use App\Http\Controllers\Web\TransferController; use Illuminate\Support\Facades\Route; -Route::middleware(['auth'])->group(function () { - Route::view('banned', 'banned')->name('banned'); +Route::middleware(['auth', 'banned'])->group(function () { + Route::view('banned', 'banned')->name('banned')->withoutMiddleware('banned'); + Route::post('logout', [AuthController::class, 'logout'])->name('logout')->withoutMiddleware('banned'); - Route::post('/newToken', [AuthController::class, 'newToken'])->name('newToken'); - Route::delete('/deleteAll', [AuthController::class, 'deleteAll'])->name('deleteAll'); - - // logout - Route::post('/logout', [AuthController::class, 'logout'])->name('logout'); + Route::post('newToken', [AuthController::class, 'newToken'])->name('newToken'); + Route::delete('deleteAll', [AuthController::class, 'deleteAll'])->name('deleteAll'); - Route::resource('/balances', BalanceController::class); + + Route::get('transactions', [BalanceController::class, 'transactions'])->name('transactions'); + Route::resource('balances', BalanceController::class); + + Route::get('transfer', [TransferController::class, 'index'])->name('transfer'); }); @@ -24,7 +27,7 @@ Route::get('callback', [AuthController::class, 'callback'])->name('callback'); }); -Route::get('/', [AuthController::class, 'index'])->name('index'); +Route::get('/', [AuthController::class, 'index'])->name('index')->middleware('banned'); Route::view('not_verified', 'not_verified')->name('not_verified');