瀏覽代碼

kge计划任务

lizhen_gitee 1 年之前
父節點
當前提交
a99aae58bf
共有 2 個文件被更改,包括 72 次插入15 次删除
  1. 1 1
      app/Http/Controllers/Controller.php
  2. 71 14
      app/Jobs/GiftJob.php

+ 1 - 1
app/Http/Controllers/Controller.php

@@ -19,7 +19,7 @@ class Controller extends BaseController
         /*$easemob = new Easemob();
         $rs = $easemob->push_text(1009,'标题','内容');
         dd($rs);*/
-        (new GiftJob(1))->handle();
+        (new GiftJob(8343))->handle();
     }
 
 

+ 71 - 14
app/Jobs/GiftJob.php

@@ -9,7 +9,7 @@ use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 
 use Illuminate\Support\Facades\DB;
-
+use Redis;
 use Illuminate\Support\Facades\Log;
 
 class GiftJob implements ShouldQueue
@@ -38,25 +38,79 @@ class GiftJob implements ShouldQueue
      */
     public function handle()
     {
-        //$this->goaaa();
+        $result = $this->goaaa();
+
+
+        if($result === false){
+            //防止失败
+            $key = 'kgelavarel_giftjob_'.$this->msgid;
+
+            $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
+
+            if($limit_rs === true){
+                //echo 123;
+                dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
+            }else{
+                //echo 345;
+            }
+        }
+    }
+
+    public function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
+    {
+        $redisconfig = [
+            // 主机
+            'host'     => 'r-uf6s12w1bbuff46572.redis.rds.aliyuncs.com',
+            'host'     => 'r-uf6s12w1bbuff46572pd.redis.rds.aliyuncs.com',//外网
+            // 端口
+            'port'      => 6379,
+            'redis_pwd' => 'tA2sXnCJcKdwzxi6',
+            'redis_selectdb' => 0,
+        ];
+
+        $redis = new Redis();
+        $redis->connect($redisconfig["host"], $redisconfig["port"]);
+        if ($redisconfig['redis_pwd']) {
+            $redis->auth($redisconfig['redis_pwd']);
+        }
+        if($redisconfig['redis_selectdb'] > 0){
+            $redis->select($redisconfig['redis_selectdb']);
+        }
+
+        //
+        //指定键值新增+1 并获取
+        $count = $redis->incr($key);
+        if ($count > $apiLimit) {
+            return false;
+        }
+
+        //设置过期时间
+        if ($count == 1) {
+            $redis->pExpire($key, $apiLimitTime);
+        }
+
+        return true;
     }
 
     public function goaaa(){
+        
         //
         $data = DB::table('hx_gift_user_party')->where('id',$this->msgid)->first();
 
+        DB::beginTransaction();
+
         //用户信息
-        $authuser_info = DB::table('hx_user')->where('id',$data['user_id'])->value('nickname');
-        $partyInfo     = DB::table('hx_party')->where('id',$data['party_id'])->first();
+        $authuser_info = DB::table('hx_user')->where('id',$data->user_id)->value('nickname');
+        $partyInfo     = DB::table('hx_party')->where('id',$data->party_id)->first();
 
 
         $money_to_jewel = 10000;
-        $user_id = $data['user_to_id'];
+        $user_id = $data->user_to_id;
         // 添加获赠用户余额
-        if($data['getvalue'] > 0){
-            $getMoney = bcdiv($data['getvalue'],$money_to_jewel,2);
+        if($data->getvalue > 0){
+            $getMoney = bcdiv($data->getvalue,$money_to_jewel,2);
             if($getMoney > 0){
-                $rs_wallet = $this->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$authuser_info}送你{$data['gift_name']}x{$data['number']}",101,'money');
+                $rs_wallet = $this->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$authuser_info}送你{$data->gift_name}x{$data->number}",101,'money');
                 if($rs_wallet['status'] == false){
                     Db::rollback();
                     return false;
@@ -66,16 +120,19 @@ class GiftJob implements ShouldQueue
 
         // 增加房主抽成
         if ($partyInfo) {
-            $guilderMoney = $data['guildermoney'];
+            $guilderMoney = $data->guildermoney;
             if($guilderMoney > 0){
                 $touserInfo    = DB::table('hx_user')->where('id',$user_id)->value('nickname');
-                $rs_wallet = $this->lockChangeAccountRemain($partyInfo['user_id'],$guilderMoney,'+',0,"{$authuser_info}送礼物{$data['gift_name']}x{$data['number']}给{$touserInfo},房间礼物抽成",102,'money');
+                $rs_wallet = $this->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$authuser_info}送礼物{$data->gift_name}x{$data->number}给{$touserInfo},房间礼物抽成",102,'money');
                 if($rs_wallet['status'] == false){
                     Db::rollback();
                     return false;
                 }
             }
         }
+
+        Db::commit();
+        return true;
     }
 
 
@@ -136,13 +193,13 @@ class GiftJob implements ShouldQueue
 
         //检测
         $wallet = DB::table('hx_user_wallet')->where('user_id',$user_id)->lockForUpdate()->first();
-        if(!$wallet)
+        if(empty($wallet))
         {
             $result['msg'] = '不存在的用户';
             return $result;
         }
 
-        if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
+        if(bccomp(bcadd($wallet->$accountType, $number), 0) === -1)
         {
             $result['code'] = 10;
             $result['msg'] = $wallet_name.'余额不足!';
@@ -157,8 +214,8 @@ class GiftJob implements ShouldQueue
             $data['user_id'] = $user_id;
             $data['value'] = $money;
             $data['mode'] = $mode;
-            $data['before'] = $wallet[$accountType];
-            $data['balance'] = bcadd($wallet[$accountType], $number);
+            $data['before'] = $wallet->$accountType;
+            $data['balance'] = bcadd($wallet->$accountType, $number);
             $data['detail'] = $remark;
             $data['createtime'] = time();
             $data['type'] = $logtype;