wesmiler 1 неделя назад
Родитель
Сommit
0be729dd91

+ 21 - 1
app/Http/Controllers/Api/v1/AccountController.php

@@ -61,7 +61,7 @@ class AccountController extends webApp
 
 
     /**
-     * 收入提现
+     * 提现
      * @return array
      */
     public function withdraw()
@@ -79,4 +79,24 @@ class AccountController extends webApp
             return showJson(1046, false, $error);
         }
     }
+
+    /**
+     * 转账
+     * @return array
+     */
+    public function transfer()
+    {
+        try {
+            $params = request()->all();
+
+            if (!$result = BalanceLogService::make()->transfer($this->userId, $params)) {
+                return showJson(BalanceLogService::make()->getError(), false);
+            } else {
+                return showJson(BalanceLogService::make()->getError(), true, $result);
+            }
+        } catch (\Exception $exception) {
+            $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
+            return showJson(1046, false, $error);
+        }
+    }
 }

+ 180 - 0
app/Services/Api/BalanceLogService.php

@@ -342,4 +342,184 @@ class BalanceLogService extends BaseService
         $this->error = '提现申请成功,请耐心等候审核~';
         return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
     }
+
+
+    /**
+     * 积分转账
+     * @param $userId
+     * @param $params
+     * @return array|false
+     */
+    public function transfer($userId, $params)
+    {
+        // 参数验证
+        $money = isset($params['money']) ? floatval($params['money']) : 0;
+        $accountType = isset($params['type']) && $params['type']? intval($params['type']) : 3;
+        $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
+        if ($money <= 0) {
+            $this->error = '请输入转账数量';
+            return false;
+        }
+
+        if (empty($mobile)) {
+            $this->error = '请输入对方手机账号';
+            return false;
+        }
+
+        $openTransfer = ConfigService::make()->getConfigByCode("transfer_{$accountType}_open", 1);
+        if (!$openTransfer) {
+            $this->error = '转账功能未开放';
+            return false;
+        }
+
+        // 锁
+        $cacheLockKey = "caches:members:transfer:{$userId}_{$accountType}";
+        if (RedisService::get($cacheLockKey)) {
+            $this->error = 1034;
+            return false;
+        }
+
+        // 判断用户账号状态
+        $fields = [2=>'property',3=>'bd_score',4=>'ls_score'];
+        $field = isset($fields[$accountType])? $fields[$accountType]:'bd_score';
+        RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
+        $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
+            ->select(['id', 'balance','mobile','nickname','property','bd_score','ls_score', 'status'])
+            ->first();
+        $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
+        $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
+        $userMobile = isset($userInfo['mobile']) &&$userInfo['mobile']? $userInfo['mobile'] : '';
+        $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
+        $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
+        if (empty($userInfo) || $status != 1) {
+            $this->error = 2016;
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        if ($money > $balance) {
+            $this->error = '该账户积分余额不足';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        $total = $money;
+        $actualMoney = $money;
+        $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
+        $accountTypeName = $accountTypeName?$accountTypeName:'账户';
+
+        // 对方账户验证
+       $transferAccount = MemberModel::where(['mobile' => $mobile, 'mark' => 1])
+            ->select(['id', 'balance','property','bd_score','ls_score', 'status'])
+            ->first();
+        $status = isset($transferAccount['status']) ? $transferAccount['status'] : 0;
+        $transferBalance = isset($transferAccount[$field]) ? $transferAccount[$field] : 0;
+        if (empty($transferAccount) || $status != 1) {
+            $this->error = '对方账户不存在或已冻结';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        // 转账处理
+        DB::beginTransaction();
+        $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
+
+        // 扣除会员账户
+        if (!MemberModel::where(['id' => $userId])->update($updateData)) {
+            DB::rollBack();
+            $this->error = '转账处理失败';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
+        if (!MemberModel::where(['mobile' => $mobile])->update($updateData)) {
+            DB::rollBack();
+            $this->error = '转账处理失败';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        $orderNo = get_order_num('TS');
+        $order = [
+            'user_id' => $userId,
+            'order_no' => $orderNo,
+            'money' => $money,
+            'total' => $total,
+            'after_money' => moneyFormat(max(0, $balance - $money), 2),
+            'actual_money' => $actualMoney,
+            'user_type' => 1,
+            'type' => 3,
+            'account_type' => $accountType,
+            'pay_type' => 30,
+            'realname' => '',
+            'account_name' => $accountTypeName,
+            'account' => $mobile,
+            'account_remark' => '',
+            'date' => date('Y-m-d'),
+            'create_time' => time(),
+            'status' => 4,
+            'mark' => 1
+        ];
+        if (!$orderId = $this->model::insertGetId($order)) {
+            DB::rollBack();
+            $this->error = '提现处理失败';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        $log = [
+            'user_id' => $userId,
+            'source_order_no' => $orderNo,
+            'user_type' => 1,
+            'account_type' => $accountType,
+            'type' => 9,
+            'money' => -$money,
+            'after_money' => moneyFormat(max(0, $balance - $money), 2),
+            'date' => date('Y-m-d'),
+            'create_time' => time(),
+            'remark' => "转账到{$mobile}",
+            'status' => 1,
+            'mark' => 1,
+        ];
+        if (!$accountId = AccountLogModel::insertGetId($log)) {
+            DB::rollBack();
+            $this->error = '转账处理失败';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+
+
+        $userMobileText = $userMobile?format_mobile($userMobile):$nickname;
+        $log = [
+            'user_id' => $userId,
+            'source_order_no' => $orderNo,
+            'user_type' => 1,
+            'account_type' => $accountType,
+            'type' => 9,
+            'money' => $money,
+            'after_money' => moneyFormat(max(0, $transferBalance + $money), 2),
+            'date' => date('Y-m-d'),
+            'create_time' => time(),
+            'remark' => "收到{$userMobileText}用户的{$accountTypeName}转账",
+            'status' => 1,
+            'mark' => 1,
+        ];
+        if (!$accountId = AccountLogModel::insertGetId($log)) {
+            DB::rollBack();
+            $this->error = '转账处理失败';
+            RedisService::clear($cacheLockKey);
+            return false;
+        }
+
+        DB::commit();
+
+        // 操作日志
+        ActionLogModel::setRecord($userId, ['type' => 2, 'title' => "{$accountTypeName}积分转账", 'content' => "账户{$userMobileText}转至{$mobile},数量{$money},单号:{$orderNo}", 'module' => 'balanceLog']);
+        ActionLogModel::record();
+        RedisService::clear($cacheLockKey);
+        $this->error = '转账成功~';
+        return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
+    }
 }

+ 4 - 1
app/Services/Common/BalanceLogService.php

@@ -247,6 +247,7 @@ class BalanceLogService extends BaseService
             }
         }else if($status == 2){
             // 线上直接打款逻辑
+
         }
 
 
@@ -273,7 +274,6 @@ class BalanceLogService extends BaseService
         $userInfo = isset($info['member'])? $info['member'] : [];
         $orderStatus = isset($info['status'])? $info['status'] : 0;
         $orderUserId = isset($info['user_id'])? $info['user_id'] : 0;
-        $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
         $money = isset($info['money'])? $info['money'] : 0;
         if(empty($info) || empty($userInfo) || $orderUserId<=0 || $money<=0){
             $this->error = '提现信息不存在或参数错误';
@@ -302,6 +302,9 @@ class BalanceLogService extends BaseService
             return false;
         }
 
+        // 线上打款逻辑
+
+
         DB::commit();
         $this->error = '提现打款状态更新成功';
         return ['id'=>$id,'money'=>$money,'status'=>$payStatus];

+ 1 - 0
routes/api.php

@@ -78,6 +78,7 @@ Route::prefix('v1')->middleware('web.login')->group(function() {
     Route::post('/account/index', [\App\Http\Controllers\Api\v1\AccountController::class, 'index']);
     Route::post('/account/info', [\App\Http\Controllers\Api\v1\AccountController::class, 'info']);
     Route::post('/account/withdraw', [\App\Http\Controllers\Api\v1\AccountController::class, 'withdraw']);
+    Route::post('/account/transfer', [\App\Http\Controllers\Api\v1\AccountController::class, 'transfer']);
     Route::post('/account/balance', [\App\Http\Controllers\Api\v1\AccountController::class, 'balance']);