first commit
This commit is contained in:
105
app/Http/Controllers/WithdrawController.php
Normal file
105
app/Http/Controllers/WithdrawController.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\AccountResource;
|
||||
use App\Http\Resources\BankCollection;
|
||||
use App\Http\Resources\WithdrawResource;
|
||||
use App\Models\Bank;
|
||||
use App\Models\Config;
|
||||
use App\Models\User;
|
||||
use App\Rules\Checkmobile;
|
||||
use Illuminate\Http\Request;
|
||||
use Validator;
|
||||
|
||||
class WithdrawController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 提现
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$banks = Bank::where('status', 1)->get();
|
||||
|
||||
$data['account'] = new AccountResource($this->user->account);
|
||||
$data['bank'] = new BankCollection($banks);
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
function do(Request $request) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'money' => 'required|integer',
|
||||
'mobile' => ['required', new Checkmobile],
|
||||
'bank_name' => 'required',
|
||||
'bank_card' => 'required',
|
||||
'bank_user' => 'required|min:2|max:8',
|
||||
], [
|
||||
'money.required' => '提现金额必须填写',
|
||||
'money.integer' => '提现金额必须是整数',
|
||||
'bank_name.required' => '收款银行必须填写',
|
||||
'mobile.required' => '手机号必须填写',
|
||||
'bank_card.required' => '银行卡号必须填写',
|
||||
'bank_user.required' => '收款人必须填写',
|
||||
'bank_user.min' => '收款人不能少于:min个汉字',
|
||||
'bank_user.max' => '收款人不能多于:max个汉字',
|
||||
]);
|
||||
|
||||
if (!preg_match("/^([\x{4e00}-\x{9fa5}]+)$/u", $request->bank_user)) {
|
||||
return $this->failed('收款人必须是中文');
|
||||
}
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->failed($validator->errors()->first());
|
||||
}
|
||||
|
||||
$widthdraw_min_money = Config::where('name', 'widthdraw_min_money')->value('value');
|
||||
if ($request->money < $widthdraw_min_money) {
|
||||
return $this->failed('最少提现金额为' . $widthdraw_min_money);
|
||||
}
|
||||
|
||||
$money = $request->money;
|
||||
$type = $request->type ?? 'bank';
|
||||
$bank_name = $request->bank_name;
|
||||
$mobile = $request->mobile;
|
||||
$bank_card = $request->bank_card;
|
||||
$bank_user = $request->bank_user;
|
||||
|
||||
$widthdraw_rate = Config::where('name', 'widthdraw_rate')->value('value');
|
||||
|
||||
$service = ($money * $widthdraw_rate) / 100;
|
||||
|
||||
$this->user->withdraws()->create([
|
||||
'money' => $money - $service,
|
||||
'service' => $service,
|
||||
'type' => $type,
|
||||
'bank_name' => $bank_name,
|
||||
'mobile' => $mobile,
|
||||
'bank_card' => $bank_card,
|
||||
'bank_user' => $bank_user,
|
||||
]);
|
||||
|
||||
return $this->success('提现成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现记录
|
||||
*/
|
||||
public function logs(Request $request)
|
||||
{
|
||||
$status = $request->status;
|
||||
|
||||
$list = $this->user
|
||||
->withdraws()
|
||||
->when($status, function ($query, $status) {
|
||||
$query->where('status', $status);
|
||||
})
|
||||
->latest()
|
||||
->paginate(10);
|
||||
|
||||
return WithdrawResource::collection($list);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user