From 40c4894464266a7e6522bef0c8a7e49a59d888bd Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 16 Nov 2022 19:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E8=BD=AC=E8=B4=A6=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=89=8B=E5=8A=A8=E6=B7=BB=E5=8A=A0=20Dro?= =?UTF-8?q?ps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/UserAddDrops.php | 60 +++++++++++++ .../Controllers/Web/TransferController.php | 56 +++++++++++- app/Models/Transaction.php | 85 +++++++++++++++++-- app/View/Components/Payment.php | 3 +- resources/views/balances/index.blade.php | 3 - .../views/balances/transactions.blade.php | 2 +- resources/views/index.blade.php | 2 +- resources/views/layouts/app.blade.php | 6 +- resources/views/transfer/search.blade.php | 58 +++++++++++++ routes/web.php | 1 + 10 files changed, 262 insertions(+), 14 deletions(-) create mode 100644 app/Console/Commands/UserAddDrops.php diff --git a/app/Console/Commands/UserAddDrops.php b/app/Console/Commands/UserAddDrops.php new file mode 100644 index 0000000..49f8d9e --- /dev/null +++ b/app/Console/Commands/UserAddDrops.php @@ -0,0 +1,60 @@ +argument('user_id'); + $amount = $this->argument('amount'); + + $user = User::findOrFail($user_id); + + $transaction = new Transaction(); + + $current_drops = $transaction->getDrops($user->id); + + $this->info($user->name . ', 当前 ' . $current_drops. ' Drops'); + + $this->info($user->name . ', 当前余额: ' . $user->balance . ' 元'); + + $this->info('添加后 ' . $current_drops + $amount . ' Drops'); + + if (!$this->confirm('确认添加 ' . $amount . ' Drops?')) { + $this->info('已取消。'); + return; + } + + $transaction->increaseDrops($user->id, $amount, '管理员添加 Drops', 'console'); + + $this->info('添加成功。'); + + return CommandAlias::SUCCESS; + } +} diff --git a/app/Http/Controllers/Web/TransferController.php b/app/Http/Controllers/Web/TransferController.php index e2b42d6..7230781 100644 --- a/app/Http/Controllers/Web/TransferController.php +++ b/app/Http/Controllers/Web/TransferController.php @@ -3,13 +3,65 @@ namespace App\Http\Controllers\Web; use App\Http\Controllers\Controller; +use App\Models\Transaction; +use App\Models\User; use Illuminate\Http\Request; class TransferController extends Controller { // - public function index() { - return view('transfer.search'); + public function index(Request $request) + { + $user = $request->user(); + $balance = $user->balance; + $drops = (new Transaction())->getDrops($user->id); + + return view('transfer.search', compact('balance', 'drops')); + } + + public function transfer(Request $request) + { + $request->validate([ + 'amount' => 'numeric|min:1|max:100', + 'description' => 'nullable|string|max:100', + 'type' => 'string|in:balance,drops' + ]); + + + $to = User::where('email', $request->to)->first(); + if (!$to) { + return back()->withErrors(['to' => '找不到用户。']); + } + + $user = $request->user(); + if ($request->to == $user->email) { + return back()->withErrors(['to' => '不能转给自己。']); + } + + $transaction = new Transaction(); + + if ($request->type === 'balance') { + if ($user->balance < $request->amount) { + return back()->withErrors(['amount' => '您的余额不足。']); + } else { + $transaction->transfer($user, $to, $request->amount, $request->description); + } + } else if ($request->type === 'drops') { + $transaction = new Transaction(); + $drops = $transaction->getDrops($user->id); + if ($drops < $request->amount) { + return back()->withErrors(['amount' => '您的 Drops 不足。']); + } else { + if (!$transaction->transferDrops($user, $to, $request->amount, $request->description)) { + return back()->withErrors(['amount' => '转账失败。']); + } + } + } + + + return back()->with('success', '转账成功,已达对方账户。'); + + } } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index eb114fd..17cf21e 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -74,7 +74,7 @@ public function increaseCurrentUserDrops($amount = 0) return $this->increaseDrops(auth()->id(), $amount); } - public function increaseDrops($user_id, $amount = 0) + public function increaseDrops($user_id, $amount, $description = null, $payment = null) { $cache_key = 'user_drops_' . $user_id; @@ -86,6 +86,8 @@ public function increaseDrops($user_id, $amount = 0) Cache::forever($cache_key, $current_drops); + $this->addIncomeDrops($user_id, $amount, $description, $payment); + return $current_drops['drops']; } @@ -113,6 +115,24 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = // $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); } + public function reduceDropsWithoutHost($user_id, $amount = 0, $description = null) + { + + $cache_key = 'user_drops_' . $user_id; + + $current_drops = Cache::get($cache_key, [ + 'drops' => 0, + ]); + + $current_drops['drops'] = $current_drops['drops'] - $amount; + + $current_drops['drops'] = round($current_drops['drops'], 5); + + Cache::forever($cache_key, $current_drops); + + $this->addPayoutDrops($user_id, $amount, $description, null, null); + } + public function addPayoutDrops($user_id, $amount, $description, $host_id, $module_id) { $data = [ @@ -178,11 +198,11 @@ public function getDrops($user_id = null): float return $drops['drops']; } - public function addIncomeDrops($user_id, $amount, $description) + public function addIncomeDrops($user_id, $amount, $description, $payment = 'balances') { $data = [ 'type' => 'income', - 'payment' => 'balances', + 'payment' => $payment, 'description' => $description, 'income' => 0, 'income_drops' => (float)$amount, @@ -220,7 +240,7 @@ public function addPayoutBalance($user_id, $amount, $description, $module_id = n { $data = [ 'type' => 'payout', - 'payment' => 'balances', + 'payment' => 'balance', 'description' => $description, 'income' => 0, 'income_drops' => 0, @@ -345,7 +365,7 @@ public function addIncomeBalance($user_id, $payment, $amount, $description) 'type' => 'income', 'payment' => $payment, 'description' => $description, - 'income' => (float) $amount, + 'income' => (float)$amount, 'income_drops' => 0, 'outcome' => 0, 'outcome_drops' => 0, @@ -353,4 +373,59 @@ public function addIncomeBalance($user_id, $payment, $amount, $description) return $this->addLog($user_id, $data); } + + public function transfer(User $user, User $to, float $amount, string $description): float + { + $lock = Cache::lock("user_balance_lock_" . $user->id, 10); + $lock_to = Cache::lock("user_balance_lock_" . $to->id, 10); + try { + + $lock->block(5); + $lock_to->block(5); + + $user->balance -= $amount; + $user->save(); + + $to->balance += $amount; + $to->save(); + + $description_new = "转账给 {$to->name}({$to->email}) {$amount} 元,{$description}"; + + $this->addPayoutBalance($user->id, $amount, $description_new); + + $description_new = "收到来自 {$user->name}($user->email) 转来的 {$amount} 元, $description"; + + $this->addIncomeBalance($to->id, 'transfer', $amount, $description_new); + + return $user->balance; + } finally { + optional($lock)->release(); + optional($lock_to)->release(); + } + + } + + public function transferDrops(User $user, User $to, float $amount, string|null $description = null): bool + { + + $user_drops = $this->getDrops($user->id); + + // if drops not enough + if ($user_drops < $amount) { + return false; + } + + $description_new = "转账给 {$to->name}($to->email) {$amount} Drops, $description"; + + $this->reduceDropsWithoutHost($user->id, $amount, $description_new); + + $description_new = "收到来自 {$to->name}($to->email) 转来的 {$amount} Drops, $description"; + + + $this->increaseDrops($to->id, $amount, $description_new, 'transfer'); + + return true; + + + } } diff --git a/app/View/Components/Payment.php b/app/View/Components/Payment.php index b9bec21..ff511cb 100644 --- a/app/View/Components/Payment.php +++ b/app/View/Components/Payment.php @@ -31,10 +31,11 @@ public function render() 'alipay' => '支付宝', 'wechat', 'wepay' => '微信支付', 'drops' => 'Drops', - 'balance' => '余额', + 'balance', 'balances' => '余额', 'unfreeze' => '解冻', 'freeze' => '冻结', 'console' => '控制台', + 'transfer' => '转账', default => $this->payment, }; diff --git a/resources/views/balances/index.blade.php b/resources/views/balances/index.blade.php index bc02664..fed1534 100644 --- a/resources/views/balances/index.blade.php +++ b/resources/views/balances/index.blade.php @@ -102,7 +102,4 @@ function calc(el) { - {{-- {{ }}--}} - - @endsection diff --git a/resources/views/balances/transactions.blade.php b/resources/views/balances/transactions.blade.php index e1a644e..a68c878 100644 --- a/resources/views/balances/transactions.blade.php +++ b/resources/views/balances/transactions.blade.php @@ -55,7 +55,7 @@ - {{ $t->balance }} 元 + {{ $t->balances }} 元
{{ $t->drops }} Drops diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 7a9a219..06ef753 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -1,6 +1,6 @@ @extends('layouts.app') -@section('title', '用户') +@section('title', '密钥管理') @section('content') diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index d665e6d..a88a9c9 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -40,9 +40,13 @@ + + @@ -80,7 +84,7 @@ class="d-none"> -
+
diff --git a/resources/views/transfer/search.blade.php b/resources/views/transfer/search.blade.php index e69de29..657ab3f 100644 --- a/resources/views/transfer/search.blade.php +++ b/resources/views/transfer/search.blade.php @@ -0,0 +1,58 @@ +@extends('layouts.app') + +@section('title', '转账') + +@section('content') +

转账

+

将您的余额转入到其他莱云账号,并且无需对方确认。

+

您有: {{ $balance }} 元,以及 {{ $drops }} Drops

+ +
+ @csrf +
+ + +
+ +
+ + + +
+ + +
+ +
+ + +
+ + + + + + + + +@endsection diff --git a/routes/web.php b/routes/web.php index 59efcd3..1158987 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,6 +18,7 @@ Route::resource('balances', BalanceController::class); Route::get('transfer', [TransferController::class, 'index'])->name('transfer'); + Route::post('transfer', [TransferController::class, 'transfer']); });