246 lines
9.0 KiB
PHP
246 lines
9.0 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Events\WithdrawCompleted;
|
||
use App\Models\Withdraw;
|
||
use App\User;
|
||
use Auth;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\View;
|
||
use Session;
|
||
use Validator;
|
||
|
||
class WithdrawController extends Controller
|
||
{
|
||
|
||
public function __construct(Request $request)
|
||
{
|
||
parent::__construct($request);
|
||
$this->middleware('auth');
|
||
view()->share('nav', 2);
|
||
|
||
}
|
||
|
||
public function index(Request $request)
|
||
{
|
||
$user_id = $request->user_id;
|
||
if ($user_id) {
|
||
$user = User::find($user_id);
|
||
} else {
|
||
$user = Auth::user();
|
||
}
|
||
|
||
$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', $user->id)->orderBy('id', 'desc')->get();
|
||
$withdraw_total = sprintf("%.2f", abs($user->account->logs()->whereIn('rule_id', [1, 2])->sum('variable')));
|
||
return view('withdraw.index', compact('logs', 'withdraw_total', 'withdraw_explain'));
|
||
}
|
||
|
||
public function store(Request $request)
|
||
{
|
||
die();
|
||
$week_start = Carbon::now()->startOfWeek()->toDateTimeString();
|
||
$week_end = Carbon::now()->endOfWeek()->toDateTimeString();
|
||
|
||
$num = Withdraw::where('user_id', Auth::id())->whereBetween('created_at', [$week_start, $week_end])->where('state', '<>', 2)->count();
|
||
if ($num > 0) {
|
||
return $this->error('您本周已提现过,下周再来吧~');
|
||
}
|
||
|
||
if (empty($request->way)) {
|
||
return $this->error('请选择提现方式');
|
||
} elseif ($request->way == 'WenxinNo') {
|
||
if (empty($request->wechat)) {
|
||
return $this->error('请输入微信号,工作人员会在1-2个工作日内为您转款');
|
||
}
|
||
} elseif ($request->way == 'Alipay') {
|
||
if (empty($request->alipay)) {
|
||
return $this->error('请输入支付宝账号,工作人员会在1-2个工作日内为您转款');
|
||
}
|
||
} elseif ($request->way == 'Wechat') {
|
||
if ($request->money > 200) {
|
||
return $this->error('单次提现金额最大200元');
|
||
}
|
||
} elseif ($request->way == 'Bankcard') {
|
||
$request->validate([
|
||
'bank_name' => 'required',
|
||
'bank_address' => 'required',
|
||
'bank_no' => 'required',
|
||
'payee' => 'required|min:2|max:8',
|
||
], [
|
||
'bank_name.required' => '收款银行必须填写',
|
||
'bank_address.required' => '收款支行必须填写',
|
||
'bank_no.required' => '银行卡号必须填写',
|
||
'payee.required' => '收款人必须填写',
|
||
'payee.min' => '收款人不能少于:min个汉字',
|
||
'payee.max' => '收款人不能多于:max个汉字',
|
||
]);
|
||
if (!preg_match("/^([\x{4e00}-\x{9fa5}]+)$/u", $request->payee)) {
|
||
return $this->error('请输入确认的收款人姓名');
|
||
|
||
}
|
||
}
|
||
|
||
if ($request->money < 100) {
|
||
return $this->error('提现金额必须大于等于100元');
|
||
}
|
||
|
||
if ($request->money > Auth::user()->account->cash) {
|
||
return $this->error('账户余额不足');
|
||
}
|
||
|
||
if (empty($request->openid)) {
|
||
return $this->error('未获取到微信信息');
|
||
}
|
||
|
||
try {
|
||
DB::transaction(function () use ($request) {
|
||
|
||
$tax = round($request->money * 0.05, 3);
|
||
$take = $request->money - $tax;
|
||
Withdraw::create([
|
||
'user_id' => Auth::id(),
|
||
'amount' => $request->money,
|
||
'tax' => $tax, //手续费
|
||
'take' => $take, //实到金额
|
||
'balance' => Auth::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 : '',
|
||
'bank_name' => $request->has('bank_address') ? $request->bank_address : '',
|
||
'payee' => $request->has('payee') ? $request->payee : '',
|
||
'state' => 0,
|
||
]);
|
||
|
||
if ($request->way == 'Bankcard') {
|
||
$userinfo = Auth::user()->info;
|
||
$userinfo->bank_no = $request->bank_no;
|
||
$userinfo->bank_name = $request->bank_name;
|
||
$userinfo->bank_address = $request->bank_address;
|
||
$userinfo->payee = $request->payee;
|
||
$userinfo->save();
|
||
}
|
||
|
||
Auth::user()->rule('withdraw', -$request->money);
|
||
});
|
||
} catch (\Exception $e) {
|
||
return $this->error($e->getmessage());
|
||
}
|
||
return $this->success('申请提现成功,等待管理员审核', route('withdraw.index'));
|
||
|
||
}
|
||
|
||
public function wechat(Request $request)
|
||
{
|
||
|
||
$withdraw_min = \Params::get('withdraw_min');
|
||
$withdraw_max = \Params::get('withdraw_max');
|
||
return view('withdraw.wechat', compact('withdraw_min', 'withdraw_max'));
|
||
}
|
||
|
||
public function wechatdo(Request $request)
|
||
{
|
||
// $user = Auth::user();
|
||
// if (!in_array($user->id, [1, 7])) {
|
||
// return $this->error('暂未开通');
|
||
// }
|
||
$data = $request->all();
|
||
|
||
$validator = Validator::make($data, [
|
||
'amount' => 'required|integer',
|
||
], [
|
||
'amount.required' => '提现金额必须填写',
|
||
'amount.integer' => '提现金额必须是整数',
|
||
]);
|
||
if ($validator->fails()) {
|
||
return $this->error($validator->errors()->first());
|
||
}
|
||
|
||
$user = Auth::user();
|
||
|
||
$withdraw_min = \Params::get('withdraw_min');
|
||
$withdraw_max = \Params::get('withdraw_max');
|
||
|
||
if ($request->amount < $withdraw_min) {
|
||
return $this->error('单次最少提现金额为' . $withdraw_min);
|
||
}
|
||
|
||
if ($request->amount > $user->account->cash) {
|
||
return $this->error('账户余额不足,当前提现金额为' . $user->account->cash);
|
||
}
|
||
|
||
if ($request->amount > $withdraw_max) {
|
||
return $this->error('单次最多提现金额为' . $withdraw_max);
|
||
}
|
||
|
||
$validator = Validator::make($data, [
|
||
'code' => 'required|sms_check:mobile,DEFAULT',
|
||
], [
|
||
'code.required' => '验证码必须填写',
|
||
'code.sms_check' => '验证码不正确',
|
||
]);
|
||
|
||
if ($validator->fails()) {
|
||
return $this->error($validator->errors()->first());
|
||
}
|
||
|
||
$in_data = [
|
||
'amount' => $request->amount,
|
||
'tax' => 0,
|
||
'type' => 'Wechat',
|
||
'take' => $request->amount,
|
||
'user_id' => $user->id,
|
||
];
|
||
|
||
$info = Withdraw::create($in_data);
|
||
|
||
if ($info) {
|
||
$app = app('wechat.payment');
|
||
$no = 'W' . date('YmdHis') . str_pad(rand(0, 9999), 4, "0", STR_PAD_LEFT);
|
||
$str = $app->transfer->toBalance([
|
||
'partner_trade_no' => $no,
|
||
'openid' => $user->openid,
|
||
'check_name' => 'NO_CHECK',
|
||
'amount' => $info->take * 100,
|
||
'desc' => '提现到账',
|
||
]);
|
||
if ($str->return_code == 'SUCCESS' && $str->result_code == 'SUCCESS') {
|
||
$user->rule('withdraw', -$info->take, true);
|
||
$info->remark = '微信自动到账';
|
||
$info->partner_trade_no = $no;
|
||
$info->state = 1;
|
||
$info->save();
|
||
event(new WithdrawCompleted($info));
|
||
|
||
return $this->success('提现成功,查看提现记录请点击《跳转》,继续提现点击《取消》。', route('withdraw.index'));
|
||
} else {
|
||
$info->remark = $str->return_msg . ',' . $str->err_code_des;
|
||
$info->state = -1;
|
||
$info->save();
|
||
\Log::error($str);
|
||
return $this->error('系统繁忙。');
|
||
}
|
||
} else {
|
||
return $this->error('提现失败');
|
||
}
|
||
}
|
||
|
||
public function code()
|
||
{
|
||
$app = app('wechat.official_account');
|
||
$user = $app->oauth->user();
|
||
Session::put('withdraw_openid', $user->id);
|
||
return redirect()->route('withdraw.create');
|
||
}
|
||
|
||
}
|