feat: 贷款模块后端 — 申请+审批+放款

This commit is contained in:
testadmin
2026-04-28 20:53:00 +08:00
parent a323b7da8e
commit c426a5e7f6
4 changed files with 316 additions and 0 deletions
@@ -0,0 +1,112 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Loan;
use App\UsersWallet;
use App\AccountLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class LoanController extends Controller
{
public function index()
{
return view('manages.loan.index');
}
public function list(Request $request)
{
$limit = $request->input('limit', 20);
$status = $request->input('status', '');
$query = Loan::query()
->leftJoin('users', 'loan.user_id', '=', 'users.id')
->select('loan.*', 'users.phone as user_phone', 'users.email as user_email', 'users.account_number');
if ($status !== '') {
$query->where('loan.status', $status);
}
$keyword = $request->input('keyword');
if ($keyword) {
$query->where(function ($q) use ($keyword) {
$q->where('loan.user_id', $keyword)
->orWhere('users.phone', 'like', "%{$keyword}%")
->orWhere('users.email', 'like', "%{$keyword}%");
});
}
$data = $query->orderBy('loan.id', 'desc')->paginate($limit);
return $this->layuiData($data);
}
public function approve(Request $request)
{
$id = $request->input('id');
$loan = Loan::find($id);
if (!$loan) return $this->error('Loan not found');
if ($loan->status != 0) return $this->error('Loan already processed');
DB::beginTransaction();
try {
$wallet = UsersWallet::where('user_id', $loan->user_id)
->where('currency', 3)->lockForUpdate()->first();
if (!$wallet) {
DB::rollBack();
return $this->error('User wallet not found');
}
$before = $wallet->legal_balance;
$wallet->legal_balance = bc_add($wallet->legal_balance, $loan->amount, 6);
$wallet->save();
AccountLog::insertLog(
[
'user_id' => $loan->user_id,
'value' => $loan->amount,
'created_time' => time(),
'info' => 'Loan Approved - ' . $loan->institution,
'type' => AccountLog::USER_LOAN_ORDER_BUY,
'currency' => 3,
],
[
'balance_type' => 1,
'wallet_id' => $wallet->id,
'lock_type' => 0,
'before' => $before,
'change' => $loan->amount,
'after' => $wallet->legal_balance,
'create_time' => time(),
]
);
$loan->status = 1;
$loan->update_time = now();
$loan->save();
DB::commit();
return $this->success('Loan approved');
} catch (\Exception $e) {
DB::rollBack();
return $this->error('Error: ' . $e->getMessage());
}
}
public function reject(Request $request)
{
$id = $request->input('id');
$remark = $request->input('remark', '');
$loan = Loan::find($id);
if (!$loan) return $this->error('Loan not found');
if ($loan->status != 0) return $this->error('Loan already processed');
$loan->status = 2;
$loan->remark = $remark;
$loan->update_time = now();
$loan->save();
return $this->success('Loan rejected');
}
}
@@ -0,0 +1,74 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\Controller;
use App\Loan;
use App\UsersWallet;
use App\AccountLog;
use Illuminate\Http\Request;
use App\Token;
use App\Users;
use Illuminate\Support\Facades\DB;
class LoanController extends Controller
{
public function institution()
{
$name = DB::table('settings')->where('key', 'loan_institution')->value('value') ?? 'Global Finance';
return $this->success(['institution' => $name]);
}
public function submit(Request $request)
{
$user_id = Token::getUserIdByToken(Token::getToken());
$user = \App\Users::find($user_id);
if (!$user) return $this->error('Please login first');
$amount = $request->input('amount');
$card_image = $request->input('card_image', '');
$cardbm_image = $request->input('cardbm_image', '');
$hand_image = $request->input('hand_image', '');
if (!$amount || !is_numeric($amount) || $amount <= 0) {
return $this->error('Please enter a valid amount');
}
if (!$card_image || !$cardbm_image || !$hand_image) {
return $this->error('Please upload all required documents');
}
$pending = Loan::where('user_id', $user->id)->where('status', 0)->first();
if ($pending) {
return $this->error('You already have a pending loan application');
}
$institution = DB::table('settings')->where('key', 'loan_institution')->value('value') ?? 'Global Finance';
Loan::create([
'user_id' => $user->id,
'amount' => $amount,
'card_image' => $card_image,
'cardbm_image' => $cardbm_image,
'hand_image' => $hand_image,
'institution' => $institution,
'status' => 0,
'create_time' => now(),
]);
return $this->success('Loan application submitted successfully');
}
public function myLoans(Request $request)
{
$user_id = Token::getUserIdByToken(Token::getToken());
$user = \App\Users::find($user_id);
if (!$user) return $this->error('Please login first');
$list = Loan::where('user_id', $user->id)->orderBy('id', 'desc')->limit(20)->get();
$statusMap = ['0' => 'Pending', '1' => 'Approved', '2' => 'Rejected'];
foreach ($list as $item) {
$item->status_text = $statusMap[$item->status] ?? '';
}
return $this->success($list);
}
}