wesmiler vor 2 Jahren
Ursprung
Commit
80d8977bd2
2 geänderte Dateien mit 74 neuen und 109 gelöschten Zeilen
  1. 66 108
      app/Services/Api/MemberService.php
  2. 8 1
      resources/lang/zh-cn/api.php

+ 66 - 108
app/Services/Api/MemberService.php

@@ -849,35 +849,28 @@ class MemberService extends BaseService
      */
     public function transfer($userId, $params)
     {
-        $account = isset($params['account'])? trim($params['account']) : '';
+        $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0;
         $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
-        $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
-        $userType = isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1;
         $money = isset($params['money'])? floatval($params['money']) : 0;
-        if(empty($account) || $money<=0){
-            $this->error = 2501;
+        if(empty($toUserId) || $money<=0){
+            $this->error = 2420;
             return false;
         }
 
-        $cachekey = "caches:members:transfer:{$userId}";
-        if(RedisService::get($cachekey)){
+        $cacheKey = "caches:members:transfer:{$userId}";
+        if(RedisService::get($cacheKey)){
             $this->error = 1034;
             return false;
         }
 
         // 验证账户
-        $userInfo = $this->model->with(['merchant'])
-            ->where(['id'=> $userId,'mark'=>1])
-            ->select(['id','nickname','username','pay_password','balance','status'])
+        $userInfo = $this->model->where(['id'=> $userId,'mark'=>1])
+            ->select(['id','nickname','username','usdt','pay_password','balance','status'])
             ->first();
-        $merchant = isset($userInfo['merchant'])? $userInfo['merchant'] : [];
         $status = isset($userInfo['status'])? $userInfo['status'] : 0;
-        $balance = isset($userInfo['balance'])? $userInfo['balance'] : 0;
-        $username = isset($userInfo['username'])? $userInfo['username'] : '';
+        $usdt = isset($userInfo['usdt'])? $userInfo['usdt'] : 0;
         $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
-        $merchId = isset($merchant['id'])? $merchant['id'] : 0;
-        $merchName = isset($merchant['name'])? $merchant['name'] : '';
-        $merchBalance = isset($merchant['balance'])? $merchant['balance'] : 0;
+        $nickname = isset($userInfo['nickname'])? $userInfo['nickname'] : '';
         if(empty($userInfo) || $status != 1){
             $this->error = 2024;
             return false;
@@ -885,155 +878,114 @@ class MemberService extends BaseService
 
         // 余额支付支付密码验证
         if(empty($userPayPassword)){
-            $this->error ='1039';
+            $this->error = 1040;
             return false;
         }
+
         $payPassword = get_password($payPassword);
         if($payPassword != $userPayPassword){
-            $this->error ='1038';
+            $this->error = 2038;
             return false;
         }
 
         // 验证账户
-        $accountInfo = $this->model->where(['username'=> $account,'mark'=>1])
-            ->select(['id','nickname','username','balance','status'])
+        $accountInfo = $this->model->where(['id'=> $toUserId,'mark'=>1])
+            ->select(['id','nickname','usdt','username','balance','status'])
             ->first();
 
         $status = isset($accountInfo['status'])? $accountInfo['status'] : 0;
-        $accountBalance = isset($accountInfo['balance'])? $accountInfo['balance'] : 0;
+        $accountUsdt = isset($accountInfo['usdt'])? $accountInfo['usdt'] : 0;
+        $toNickname = isset($accountInfo['nickname'])? $accountInfo['nickname'] : '';
         if(empty($accountInfo) || $status != 1){
-            $this->error = 2025;
+            $this->error = 2421;
             return false;
         }
 
-        if($accountInfo['id'] == $userId && $type == 1){
-            $this->error = 2645;
+        if($money > $usdt){
+            $this->error = 2422;
             return false;
         }
 
         // 用户余额转账到用户
         DB::beginTransaction();
-        if($type == 1){
-            if($money>$balance){
-                $this->error = 2026;
-                return false;
-            }
-
-            // 扣除账户余额
-            RedisService::set($cachekey, $params, rand(2,5));
-            $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
-            if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
-                DB::rollBack();
-                $this->error = 2028;
-                RedisService::clear($cachekey);
-                return false;
-            }
-        }
-
-        // 商户余额转账到用户
-        else if($type == 2){
-            if($money>$merchBalance){
-                $this->error = 2027;
-                return false;
-            }
-
-            //
-            if($merchId<=0){
-                $this->error = 2018;
-                return false;
-            }
-
-            // 扣除商户余额
-            RedisService::set($cachekey, $params, rand(2,5));
-            $updateData = ['balance'=> DB::raw("balance - {$money}"),'update_time'=> time()];
-            if(!MerchantModel::where(['id'=> $merchId,'mark'=>1])->update($updateData)){
-                DB::rollBack();
-                $this->error = 2028;
-                RedisService::clear($cachekey);
-                return false;
-            }
-        }
-
-        // 到账
-        $updateData = ['balance'=> DB::raw("balance + {$money}"),'update_time'=> time()];
-        if(!$this->model->where(['id'=> $accountInfo['id'],'mark'=>1])->update($updateData)){
+        RedisService::set($cacheKey, $params, rand(2,5));
+        $updateData = ['usdt'=> DB::raw("usdt - {$money}"),'update_time'=> time()];
+        if(!$this->model->where(['id'=> $userId,'mark'=>1])->update($updateData)){
             DB::rollBack();
             $this->error = 2028;
-            RedisService::clear($cachekey);
+            RedisService::clear($cacheKey);
             return false;
         }
 
         // 明细
+        $orderNo = get_order_num('TR');
         $log = [
             'user_id' => $userId,
-            'merch_id' => $merchId,
-            'source_uid' => $accountInfo['id'],
-            'source_order_no' => '',
+            'source_id' => $toUserId,
+            'source_order_no' => $orderNo,
             'type' => 6,
-            'coin_type' => 4,
-            'user_type'=> $type==2? 2 : ($userType!=2? $userType : 1),
+            'coin_type' => 1,
+            'user_type'=> 1,
             'money' => -$money,
-            'balance' => $type==2? $merchBalance : $balance,
+            'actual_money' => -$money,
+            'balance' => $usdt,
             'create_time' => time(),
             'update_time' => time(),
-            'remark' => $type==2? "商户余额转账给【{$account}】": "账户余额转账给【{$account}】",
+            'remark' => "USDT转账",
             'status' => 1,
             'mark' => 1,
         ];
         if(!AccountLogModel::insertGetId($log)){
             DB::rollBack();
-            $this->error = 2029;
-            RedisService::clear($cachekey);
+            $this->error = 2423;
+            RedisService::clear($cacheKey);
             return false;
         }
 
-        // 到账明细
+        // 到账
+        $updateData = ['usdt'=> DB::raw("usdt + {$money}"),'update_time'=> time()];
+        if(!$this->model->where(['id'=> $toUserId,'mark'=>1])->update($updateData)){
+            DB::rollBack();
+            $this->error = 2424;
+            RedisService::clear($cacheKey);
+            return false;
+        }
+
+        // 明细
         $log = [
-            'user_id' => $accountInfo['id'],
-            'merch_id' => $merchId,
-            'source_uid' => $userId,
-            'source_order_no' => '',
+            'user_id' => $toUserId,
+            'source_id' => $userId,
+            'source_order_no' => $orderNo,
             'type' => 6,
-            'coin_type' => 4,
+            'coin_type' => 1,
             'user_type'=> 1,
             'money' => $money,
-            'balance' => $accountBalance,
+            'actual_money' => $money,
+            'balance' => $accountUsdt,
             'create_time' => time(),
             'update_time' => time(),
-            'remark' => $type==2? "来自商家【{$merchName}】的转账": "来自".($userType==3?'技师':($userType==4?'代理':'用户'))."【{$username}】的转账",
+            'remark' => "USDT转账",
             'status' => 1,
             'mark' => 1,
         ];
         if(!AccountLogModel::insertGetId($log)){
             DB::rollBack();
             $this->error = 2029;
-            RedisService::clear($cachekey);
+            RedisService::clear($cacheKey);
             return false;
         }
 
-        // 发送消息
-        $params = [
-            'title'=> "转账成功通知",
-            'body' => $type == 2? "收到一笔来自商家的转账":"收到一笔来自用户的转账",
-            'type' => 2, // 1-公告通知,2-订单通知,3-交易通知,4-其他
-            'content' => [
-                'account' => ['name' => $type==2?'来自商家':'来自用户', 'text' => $type==2? $merchName : $userInfo['username']],
-                'pay_time' => ['name' => '到账时间', 'text' => date('Y-m-d H:i:s')],
-                'money' => ['name' => '到账金额', 'text' => $money],
-                'status' => ['name' => '状态', 'text' => '已到账'],
-            ],
-            'click_type' => 'payload',
-            'url' => '/pages/account/index',
-        ];
-        if(!PushService::make()->pushMessageByUser($accountInfo['id'], $params, 0)){
-            DB::rollBack();
-            $this->error = PushService::make()->getError();
-            RedisService::clear($cachekey);
-            return false;
-        }
+        // 消息
+        $dateTime = date('Y-m-d H:i:s');
+        MessageService::make()->pushMessage($userId,'USDT转账成功',"您在{$dateTime}(UTC+8)成功支付{$money}USDT转账给用户【{$toUserId}】{$toNickname}",3);
+
+        $dateTime = date('Y-m-d H:i:s');
+        MessageService::make()->pushMessage($toUserId,'USDT转账成功',"您在{$dateTime}(UTC+8)收到用户【{$userId}】{$nickname}{$money}USDT的转账",3);
+
 
         DB::commit();
-        $this->error = 2030;
+        RedisService::clear($cacheKey);
+        $this->error = 2425;
         return true;
     }
 
@@ -1265,6 +1217,7 @@ class MemberService extends BaseService
         if(!$id = BalanceLogModel::insertGetId($data)){
             DB::rollBack();
             $this->error = 2405;
+            RedisService::clear($cacheKey);
             return false;
         }
 
@@ -1274,6 +1227,7 @@ class MemberService extends BaseService
             if(!MerchantModel::where(['user_id'=> $userId,'mark'=>1])->update($updateData)){
                 DB::rollBack();
                 $this->error = 2406;
+                RedisService::clear($cacheKey);
                 return false;
             }
 
@@ -1298,6 +1252,7 @@ class MemberService extends BaseService
             if (!AccountLogModel::insertGetId($log)) {
                 $this->error = 2407;
                 DB::rollBack();
+                RedisService::clear($cacheKey);
                 return false;
             }
         }
@@ -1307,6 +1262,7 @@ class MemberService extends BaseService
             if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
                 DB::rollBack();
                 $this->error = 2406;
+                RedisService::clear($cacheKey);
                 return false;
             }
 
@@ -1331,6 +1287,7 @@ class MemberService extends BaseService
             if (!AccountLogModel::insertGetId($log)) {
                 $this->error = 2407;
                 DB::rollBack();
+                RedisService::clear($cacheKey);
                 return false;
             }
         }
@@ -1363,6 +1320,7 @@ class MemberService extends BaseService
         }
 
         $this->error = $title;
+        RedisService::clear($cacheKey);
         return [
             'id'=> $id,
             'money'=> $money,

+ 8 - 1
resources/lang/zh-cn/api.php

@@ -39,6 +39,7 @@ return [
     '1037' => '入账钱包地址未配置',
     '1038' => '出账钱包地址未配置或余额不足,请先充值',
     '1039' => '数据不存在',
+    '1040' => '请先设置交易密码',
 
     // 登录注册
     '2001'=> '账号非法或未注册',
@@ -112,7 +113,13 @@ return [
     '2405'=>'创建提现订单失败',
     '2406'=>'提现账户扣款失败',
     '2407'=>'提现账户处理失败',
-    '2408'=> '',
+
+    '2420'=> '参数错误',
+    '2421'=> '转账账户不存或不交易',
+    '2422'=> '转账金额不能超过USDT余额',
+    '2423'=> '转账交易处理失败',
+    '2424'=> '转账交易处理失败',
+    '2425'=> '转账成功',
 
     
     '我正在直播,快来看看吧'=>'我正在直播,快来看看吧',