187 lines
6.9 KiB
PHP
187 lines
6.9 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: sunny
|
||
* Date: 2019/2/26
|
||
* Time: 11:01 AM
|
||
*/
|
||
|
||
namespace App\Api\Controllers;
|
||
|
||
|
||
use App\Api\Resources\WithdrawLogsResource;
|
||
use App\Models\Bank;
|
||
use App\Models\Withdraw;
|
||
use App\User;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Validator;
|
||
|
||
class WithdrawController extends Controller
|
||
{
|
||
public function __construct()
|
||
{
|
||
// $this->middleware('auth.api');
|
||
// $this->user = \Auth::guard('api')->user();
|
||
// $this->uid = \Auth::guard('api')->id();
|
||
$this->user = User::find(824);
|
||
$this->uid = 824;
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
if ($this->user->identity->identity_id == 0) {
|
||
return $this->failed('请开通VIP后继续操作');
|
||
}
|
||
$withdraw_explain = \Params::get('withdraw_explain');
|
||
|
||
$withdraw_explain = str_replace("\n", "<br />", $withdraw_explain);
|
||
$withdraw_explain = str_replace("\r", "<br />", $withdraw_explain);
|
||
$logs = Withdraw::where('user_id', $this->uid)->orderBy('id', 'desc')->get();
|
||
$withdraw_total = sprintf("%.2f", abs($this->user->account->logs()->whereIn('rule_id', [10, 11])->sum('variable')));
|
||
|
||
return [
|
||
'data' => [
|
||
'withdraw_total' => $withdraw_total,
|
||
'withdraw_explain' => $withdraw_explain,
|
||
'logsLists' => WithdrawLogsResource::collection($logs),
|
||
],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
}
|
||
|
||
public function create()
|
||
{
|
||
if ($this->user->identity->identity_id == 0) {
|
||
return $this->failed('请开通VIP后继续操作');
|
||
}
|
||
$week_num = 1;
|
||
$week_start = Carbon::now()->startOfWeek()->toDateTimeString();
|
||
$week_end = Carbon::now()->endOfWeek()->toDateTimeString();
|
||
|
||
$num = Withdraw::where('user_id', $this->uid)->whereBetween('created_at', [$week_start, $week_end])->where('state', '<>', 2)->count();
|
||
if ($num > 0) {
|
||
$week_num = 0;
|
||
}
|
||
|
||
$banks = Bank::orderBy('sort', 'asc')->get();
|
||
|
||
$withdraw_explain = \Params::get('withdraw_explain');
|
||
|
||
$withdraw_explain = str_replace("\n", "<br />", $withdraw_explain);
|
||
$withdraw_explain = str_replace("\r", "<br />", $withdraw_explain);
|
||
|
||
return [
|
||
'data' => [
|
||
'week_num' => $week_num,
|
||
'banks' => $banks,
|
||
'withdraw_explain' => $withdraw_explain,
|
||
],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
}
|
||
|
||
public function store(Request $request)
|
||
{
|
||
if ($this->user->identity->identity_id == 0) {
|
||
return $this->failed('请开通VIP后继续操作');
|
||
}
|
||
$week_start = Carbon::now()->startOfWeek()->toDateTimeString();
|
||
$week_end = Carbon::now()->endOfWeek()->toDateTimeString();
|
||
|
||
$num = Withdraw::where('user_id', $this->uid)->whereBetween('created_at', [$week_start, $week_end])->where('state', '<>', 2)->count();
|
||
if ($num > 0) {
|
||
return $this->failed('您本周已提现过,下周再来吧~');
|
||
}
|
||
if ($this->user->paypass) {
|
||
if ($this->user->paypass !== md5($request->paypass)) {
|
||
return $this->failed('支付密码验证失败');
|
||
}
|
||
} else {
|
||
return $this->failed('请您先设置支付密码', route('settings.pwd'));
|
||
}
|
||
if (empty($request->way)) {
|
||
return $this->failed('请选择提现方式');
|
||
} elseif ($request->way == 'WenxinNo') {
|
||
if (empty($request->wechat)) {
|
||
return $this->failed('请输入微信号,工作人员会在1-2个工作日内为您转款');
|
||
}
|
||
} elseif ($request->way == 'Alipay') {
|
||
if (empty($request->alipay)) {
|
||
return $this->failed('请输入支付宝账号,工作人员会在1-2个工作日内为您转款');
|
||
}
|
||
} elseif ($request->way == 'Wechat') {
|
||
if ($request->money > 200) {
|
||
return $this->failed('单次提现金额最大200元');
|
||
}
|
||
} elseif ($request->way == 'Bankcard') {
|
||
|
||
$validator = Validator::make($request->all(), [
|
||
'mobile' => 'required|mobile|unique:users',
|
||
'code' => 'required|sms_check:mobile,BIND',
|
||
], [
|
||
'mobile.required' => '手机号码必须填写',
|
||
'mobile.mobile' => '手机号码格式不正确',
|
||
'mobile.unique' => '手机号码已经绑定',
|
||
'code.required' => '验证码必须填写',
|
||
'code.sms_check' => '验证码不正确',
|
||
]);
|
||
if ($validator->fails()) {
|
||
return $this->failed($validator->errors()->first());
|
||
}
|
||
if (!preg_match("/^([\x{4e00}-\x{9fa5}]+)$/u", $request->payee)) {
|
||
return $this->failed('请输入确认的收款人姓名');
|
||
|
||
}
|
||
}
|
||
|
||
if ($request->money < 100) {
|
||
return $this->failed('提现金额必须大于等于100元');
|
||
}
|
||
|
||
if ($request->money > $this->user->account->cash) {
|
||
return $this->failed('账户余额不足');
|
||
}
|
||
if (empty($request->openid)) {
|
||
return $this->failed('未获取到微信信息');
|
||
}
|
||
|
||
try {
|
||
DB::transaction(function () use ($request) {
|
||
$tax = round($request->money * 0.05, 3);
|
||
$take = $request->money - $tax;
|
||
Withdraw::create([
|
||
'user_id' => $this->uid,
|
||
'amount' => $request->money,
|
||
'tax' => $tax, //手续费
|
||
'take' => $take, //实到金额
|
||
'balance' => $this->user->account->cash - $request->money,
|
||
'openid' => $request->openid,
|
||
'way' => $request->way,
|
||
'wechat' => $request->has('wechat') ? $request->wechat : '',
|
||
'alipay' => $request->has('alipay') ? $request->alipay : '',
|
||
'bank_no' => $request->has('bank_no') ? $request->bank_no : '',
|
||
'bank_name' => $request->has('bank_name') ? $request->bank_name : '',
|
||
'payee' => $request->has('payee') ? $request->payee : '',
|
||
'state' => 0,
|
||
]);
|
||
|
||
if ($request->way == 'Bankcard') {
|
||
$userinfo = $this->user->info;
|
||
$userinfo->bank_no = $request->bank_no;
|
||
$userinfo->bank_name = $request->bank_name;
|
||
$userinfo->payee = $request->payee;
|
||
$userinfo->save();
|
||
}
|
||
$this->user->rule('withdraw', -$request->money);
|
||
});
|
||
} catch (\Exception $e) {
|
||
return $this->failed($e->getmessage());
|
||
}
|
||
return $this->success('申请提现成功,等待管理员审核');
|
||
|
||
}
|
||
} |