webman/app/controller/api/WithdrawController.php

210 lines
7.0 KiB
PHP
Raw Normal View History

2025-02-16 22:40:30 +08:00
<?php
namespace app\controller\api;
use app\model\Withdraw;
use app\model\User;
use app\model\ExchangeRate;
use app\model\Userbank;
use support\Request;
use App\Utils\API\Payment;
2025-02-18 17:22:37 +08:00
use App\Utils\API\PaymentNew;
2025-02-16 22:40:30 +08:00
use App\Utils\ApiResponseApp;
use App\dao\UserRewardDao;
use hg\apidoc\annotation as Apidoc;
use Carbon\Carbon;
/**
* @Apidoc\Title("提现订单")
*/
class WithdrawController
{
2025-02-18 12:36:42 +08:00
2025-02-16 22:40:30 +08:00
/**
* @Apidoc\Title("1.0 查询当前用户提现订单")
* @Apidoc\Url("api/withdraw/withdrawCashList")
* @Apidoc\Method("POST")
*/
public function withdrawCashList(Request $request)
{
2025-02-18 12:36:42 +08:00
$page = $request->post('page', 1); // 默认第一页
$size = $request->post('size', 15); // 默认每页15条
$status = $request->post('status', 0); // 默认状态为0全部
$time = $request->post('time', null); // 默认时间不做过滤
2025-02-16 22:40:30 +08:00
$userId = $request->data['id'];
2025-02-18 12:36:42 +08:00
// 初始化查询构建器
$query = Withdraw::query();
2025-02-16 22:40:30 +08:00
2025-02-18 12:36:42 +08:00
// 根据状态过滤
if ($status != 0) {
$query->where('status', $status);
2025-02-16 22:40:30 +08:00
}
2025-02-18 12:36:42 +08:00
// 根据时间过滤
if (!is_null($time)) {
$todayStart = Carbon::now()->startOfDay(); // 今天的开始时间 (00:00:00)
$todayEnd = Carbon::now()->endOfDay(); // 今天的结束时间 (23:59:59)
$yesterdayStart = Carbon::now()->subDay()->startOfDay(); // 昨天的开始时间
$sevenDaysAgoStart = Carbon::now()->subDays(7)->startOfDay(); // 七天前的开始时间
2025-02-16 22:40:30 +08:00
2025-02-18 12:36:42 +08:00
switch ($time) {
case 1:
$query->whereBetween('createtime2', [$todayStart, $todayEnd]);
break;
case 2:
$query->whereBetween('createtime2', [$yesterdayStart, $todayEnd]);
break;
case 3:
$query->whereBetween('createtime2', [$sevenDaysAgoStart, $todayEnd]);
break;
}
}
// 添加用户ID过滤条件
$query->where('user_id', $userId);
// 查询结果
$withdrawOrders = $query->orderBy('createtime2', 'desc')
->skip(($page - 1) * $size)
->take($size)
->get();
// 手动映射字段
$withdrawOrders = $withdrawOrders->map(function ($item) {
// 将 money 字段映射为 amount
$item->amount = $item->money;
unset($item->money); // 删除原始的 money 字段
return $item;
});
// 返回成功响应
return ApiResponseApp::success($withdrawOrders);
}
2025-02-16 22:40:30 +08:00
/**
* @Apidoc\Title("小于1000直接转账大于1000生成审批")
* @Apidoc\Url("api/withdraw/submit")
* @Apidoc\Method("POST")
*/
public function submit(Request $request)
{
$money_no = $request->post('money');
2025-02-18 13:12:23 +08:00
//用户存了一个 转账信息 这里是用户转账信息的id
2025-02-16 22:40:30 +08:00
$bank_id = $request->post('bank_id');
2025-02-18 12:36:42 +08:00
$user_id = $request->data['id'];
$user = User::find($user_id);
2025-02-16 22:40:30 +08:00
//提现金额
2025-02-18 12:36:42 +08:00
$rate = ExchangeRate::where('type', 'BDT')->get();
$money = $money_no / 100 * $rate[0]['points'];
2025-02-16 22:54:13 +08:00
var_dump($money);
2025-02-18 17:22:37 +08:00
2025-02-16 22:40:30 +08:00
//大于1000等待管理员审核
2025-02-18 12:36:42 +08:00
if ($money >= 1000) {
2025-02-16 22:40:30 +08:00
Withdraw::create([
'user_id' => $user_id,
'amount' => $money,
'status' => 1,
'createtime2' => date('Y-m-d H:i:s'),
'username' => $user->username,
'status_text' => '申请中',
]);
2025-02-18 12:36:42 +08:00
return ApiResponseApp::success(null, '等待管理员审核');
2025-02-16 22:40:30 +08:00
}
2025-02-18 17:22:37 +08:00
//用户积分减少$money
2025-02-18 17:50:13 +08:00
UserRewardDao::base($user_id, 1, - ($money_no + 300), '提现');
2025-02-18 19:44:57 +08:00
$userbank = Userbank::where('id', $bank_id)->first();
2025-02-18 17:50:13 +08:00
//存入数据库
2025-02-18 19:44:57 +08:00
$withdraw = Withdraw::create([
2025-02-18 17:50:13 +08:00
'user_id' => $user_id,
'amount' => $money,
'status' => 2,
'createtime2' => date('Y-m-d H:i:s'),
'username' => $user->username,
'status_text' => '已到账',
]);
//获取刚刚存入数据库的id(订单号)
$orderId = $withdraw->id;
PaymentNew::pushMoney($money, $userbank->bank_username, $userbank->account, $userbank->bank_name,$orderId);
2025-02-18 17:22:37 +08:00
// var_dump($res);
// //逻辑错误需要修改
// if ($res['Success'] == 200) {
// Withdraw::create([
// 'user_id' => $user_id,
// 'amount' => $money,
// 'status' => 2,
// 'createtime2' => date('Y-m-d H:i:s'),
// 'username' => $user->username,
// 'status_text' => '已到账',
// ]);
// return ApiResponseApp::success(null, '转账成功');
// } else {
// return ApiResponseApp::error(null, '转账失败');
// }
// return ApiResponseApp::success(null, '转账成功');
2025-02-16 22:40:30 +08:00
}
2025-02-18 12:36:42 +08:00
/**
2025-02-16 22:40:30 +08:00
* @Apidoc\Title("管理员同意转账")
* @Apidoc\Url("api/withdraw/pushMoney")
* @Apidoc\Method("POST")
*/
public function pushMoney(Request $request)
{
//金额
$amount = $request->post('amount');
//收款户名
$bank_username = $request->post('bank_username');
//收款帐号
$account = $request->post('account');
//收款银行名称
$bank_name = $request->post('bank_name');
//转账订单id
$id = $request->post('id');
//管理员审批结果
$type = $request->post('type');
2025-02-18 12:36:42 +08:00
if ($type == 1) {
2025-02-18 17:50:13 +08:00
$res = PaymentNew::pushMoney($amount, $bank_username, $account, $bank_name,$id);
// var_dump($res);
// if ($res['Success'] == 200) {
// $Withdraw = Withdraw::where('id', $id)->first();
// $Withdraw->status = 2;
// $Withdraw->status_text = '已到账';
// $Withdraw->save();
// return ApiResponseApp::success(null, '转账成功');
// } else {
// return ApiResponseApp::error(null, '转账失败');
// }
2025-02-18 12:36:42 +08:00
} else {
$Withdraw = Withdraw::where('id', $id)->first();
2025-02-17 23:27:45 +08:00
$Withdraw->status = 3;
$Withdraw->status_text = '已驳回';
$Withdraw->save();
2025-02-18 12:36:42 +08:00
$rate = ExchangeRate::where('type', 'BDT')->get();
2025-02-16 22:40:30 +08:00
//计算积分
2025-02-18 12:36:42 +08:00
$money = $amount * 100 / $rate[0]['points'] + 300;
2025-02-16 22:40:30 +08:00
//用户id
2025-02-18 12:36:42 +08:00
$user_id = $request->data['id'];
//用户积分增加$money
2025-02-16 22:40:30 +08:00
UserRewardDao::base($user_id, 1, $money, '提现失败返还');
2025-02-18 12:36:42 +08:00
return ApiResponseApp::error(null, '操作成功');
2025-02-16 22:40:30 +08:00
}
}
2025-02-18 17:22:37 +08:00
/**
* @Apidoc\Title("请求回调")
* @Apidoc\Url("api/withdraw/callback")
2025-02-18 17:45:10 +08:00
* @Apidoc\Method("GET")
2025-02-18 17:22:37 +08:00
*/
public function callback(Request $request)
{
var_dump($request->all());
}
2025-02-16 22:40:30 +08:00
}