You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
11 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\API\H5;
use App\Http\Middleware\Log;
use App\Services\TencentSmsApiService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TransactionController
{
protected string $templateId='2567513';
public function GetList(Request $request)
{
$systemStatus = \Yz::systemStatusCheck();
if(!$systemStatus['status']){
return \Yz::echoError1($systemStatus['msg']);
}
$dayCutCheck = \Yz::DayCutCheck();
if(!$dayCutCheck['status']){
return \Yz::echoError1($dayCutCheck['msg']);
}
$type = request('type');
$userid = $request->get('userid');//中间件产生的参数
// 状态配置(带 tag_type
$transaction_status_map = [
0 => ['label' => '关闭', 'tag_type' => 'default'],
1 => ['label' => '待审核', 'tag_type' => 'warning'],
2 => ['label' => '拒绝', 'tag_type' => 'error'],
3 => ['label' => '完成', 'tag_type' => 'success'],
];
$loan_status_map = [
0 => ['label' => '关闭', 'tag_type' => 'default'],
1 => ['label' => '申请中', 'tag_type' => 'warning'],
2 => ['label' => '审核通过', 'tag_type' => 'info'],
3 => ['label' => '已放款', 'tag_type' => 'success'],
4 => ['label' => '已还款', 'tag_type' => 'success'],
5 => ['label' => '拒绝', 'tag_type' => 'error'],
];
$type_label = [
[
'key' => 0,
'label' => '全部',
'fuhao' => ' ',
'color' => '#333333'
],
[
'key' => 1,
'label' => '充值',
'fuhao' => '+',
'color' => '#009900'
],
[
'key' => 2,
'label' => '利息收益',
'fuhao' => '+',
'color' => '#009900'
],
[
'key' => 3,
'label' => '提现',
'fuhao' => '-',
'color' => '#FF0000'
],
[
'key' => 4,
'label' => '借款',
'fuhao' => '+',
'color' => '#FF0000'
]
];
$type_map = [];
foreach ($type_label as $tl) {
$type_map[$tl['key']] = $tl;
}
if ($type === 0) {
$ck_list = DB::table('transactions')
->select('id', 'type', 'amount', 'created_at', 'status','day_cut')
->where(['member_id' => $userid, 'is_del' => 0])
->get()
->map(function ($item) {
$item->source = 'transaction'; // 标记来源
return $item;
});
$jk_list = DB::table('loans')
->select('id', DB::raw('NULL as type'), 'amount', 'created_at', 'status',DB::raw('NULL as day_cut')) // 对齐字段
->where(['member_id' => $userid, 'is_del' => 0])
->get()
->map(function ($item) {
$item->source = 'loan'; // 标记来源
return $item;
});
$list = $ck_list->merge($jk_list)
->sortByDesc('created_at'); // 可选:按时间倒序排序
} else if ($type === 4) {
$list = DB::table('loans')
->select('id', DB::raw('NULL as type'), 'amount', 'created_at', 'status',DB::raw('NULL as day_cut')) // 对齐字段
->where(['member_id' => $userid, 'is_del' => 0])
->orderBy('id', 'desc')
->get()
->map(function ($item) {
$item->source = 'loan'; // 标记来源
return $item;
});
} else {
$list = DB::table('transactions')
->select('id', 'type', 'amount', 'created_at', 'status','day_cut')
->where(['member_id' => $userid, 'type' => $type, 'is_del' => 0])
->orderBy('id', 'desc')
->get()
->map(function ($item) {
$item->source = 'transaction'; // 标记来源
return $item;
});
}
// 处理标签和 tag_typePHP 7.3 安全)
$list = $list->map(function ($item) use ($type_map, $transaction_status_map, $loan_status_map) {
if ($item->source === 'loan') {
$actual_type = 4; // 借款固定对应 type=4
} else {
$actual_type = isset($item->type) ? $item->type : 0; // transaction 的 type若无则 fallback
}
if (isset($type_map[$actual_type])) {
$type_info = $type_map[$actual_type];
} else {
$type_info = $type_map[0];
}
$item->type_label = $type_info['label'];
$item->fuhao = $type_info['fuhao'];
$item->color = $type_info['color'];
// 设置 status_label 和 tag_type
if ($item->source === 'loan') {
if (isset($loan_status_map[$item->status])) {
$status_info = $loan_status_map[$item->status];
} else {
$status_info = ['label' => '未知状态', 'tag_type' => 'primary'];
}
} else {
if (isset($transaction_status_map[$item->status])) {
$status_info = $transaction_status_map[$item->status];
} else {
$status_info = ['label' => '未知状态', 'tag_type' => 'primary'];
}
}
$item->status_label = $status_info['label'];
$item->tag_type = $status_info['tag_type'];
return $item;
})->values()->toArray();
foreach ($list as $item){
if($item->type==2){
$item->created_at=$item->day_cut;
}
}
return \Yz::Return(true, '查询完成', ['list' => $list]);
}
//充值
public function Recharge(Request $request)
{
$systemStatus = \Yz::systemStatusCheck();
if(!$systemStatus['status']){
return \Yz::echoError1($systemStatus['msg']);
}
$dayCutCheck = \Yz::DayCutCheck();
if(!$dayCutCheck['status']){
return \Yz::echoError1($dayCutCheck['msg']);
}
$userid = $request->get('userid');//中间件产生的参数
$amount = request('amount');
$img = request('img');
// if (empty($img)) {
// return \Yz::echoError1('请上传充值凭证!');
// }
if (empty($amount) || $amount < 10000) {
return \Yz::echoError1('充值金额不能小于10000元');
}
$memberInfo = DB::table('members')->where(['id' => $userid, 'is_del' => 0])->first();
if (!$memberInfo) {
return \Yz::echoError1('用户不存在');
}
// var_dump($memberInfo->balance);
// var_dump($amount);
// $new_balance = bcadd($memberInfo->balance, $amount, 2);
// var_dump($new_balance);
$insertData = [
'member_id' => $userid,
'type' => 1,
'amount' => $amount,
'img' => $img,
'status' => 1,
'source' => 'member',
'operator_id' => $userid,
'created_at' => date('Y-m-d H:i:s'),
];
$id = DB::table('transactions')->insertGetId($insertData);
if (!!$id) {
$config=DB::table('configs')->where(['label'=>'管理员手机号'])->first();
$service = new TencentSmsApiService();
$ss= $service->send($config->value,$this->templateId, [$memberInfo->name,"转入"]);
return \Yz::Return(true, '充值成功!', ['id' => $id]);
} else {
return \Yz::echoError1('提交失败');
}
}
//提现
public function Withdraw(Request $request)
{
$systemStatus = \Yz::systemStatusCheck();
if (!$systemStatus['status']) {
return \Yz::echoError1($systemStatus['msg']);
}
$dayCutCheck = \Yz::DayCutCheck();
if (!$dayCutCheck['status']) {
return \Yz::echoError1($dayCutCheck['msg']);
}
$userid = $request->get('userid'); // 中间件产生的参数
$amount = request('amount');
if (empty($amount) || $amount <= 0) {
return \Yz::echoError1('提现金额不能为0元或负数');
}
$memberInfo = DB::table('members')->where(['id' => $userid, 'is_del' => 0])->first();
if (!$memberInfo) {
return \Yz::echoError1('用户不存在');
}
if ($memberInfo->balance < $amount) {
return \Yz::echoError1('余额不足');
}
DB::beginTransaction();
try {
// 1. 扣减余额
$newBalance = bcsub($memberInfo->balance, $amount, 2);
$updated = DB::table('members')
->where(['id' => $userid])
->update(['balance' => $newBalance]);
if (!$updated) {
throw new \Exception('更新余额失败');
}
// 2. 插入交易记录
$insertData = [
'member_id' => $userid,
'type' => 3,
'amount' => $amount,
'status' => 1,
'source' => 'member',
'operator_id' => $userid,
'created_at' => now(), // 使用 now() 更规范
];
$id = DB::table('transactions')->insertGetId($insertData);
if (!$id) {
throw new \Exception('创建交易记录失败');
}
// 3. 提交事务(关键!)
DB::commit();
// 4. 事务已提交,安全地发送短信(即使短信失败,也不影响资金操作)
try {
$config = DB::table('configs')->where(['label' => '管理员手机号'])->first();
if ($config) {
$service = new TencentSmsApiService();
$service->send($config->value, $this->templateId, [$memberInfo->name, "转出"]);
}
} catch (\Exception $smsException) {
// 短信失败可记录日志,但不中断流程
Log::warning('提现短信发送失败: ' . $smsException->getMessage());
}
return \Yz::Return(true, '提现成功!', ['id' => $id]);
} catch (\Exception $e) {
DB::rollBack();
return \Yz::echoError1($e->getMessage());
}
}
}