TypingJob.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Redis;
  10. use Illuminate\Support\Facades\Config;
  11. use Illuminate\Support\Facades\Log;
  12. use App\Common\Library\Alltools;
  13. class TypingJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $msgid;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($msgid)
  23. {
  24. //
  25. $this->msgid = $msgid;
  26. Log::info(date('Y-m-d H:i:d').':'.$msgid);
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. $result = $this->goaaa();
  36. /*if($result === false){
  37. //防止失败
  38. $key = 'kgelavarel_giftjob_'.$this->msgid;
  39. $limit_rs = $this->apiLimit(2,3600000,$key);//一小时最多另执行2次
  40. if($limit_rs === true){
  41. //echo 123;
  42. dispatch((new GiftJob($this->msgid))->delay(60));//加入队列
  43. }else{
  44. //echo 345;
  45. }
  46. }*/
  47. }
  48. //主要调用
  49. public function goaaa(){
  50. //
  51. $data = DB::table('mt_user_match_typing_log')->where('id',$this->msgid)->where('plantask_status',0)->first();
  52. if(empty($data)){return true;}
  53. DB::beginTransaction();
  54. if($data->is_free == 0 && $data->money > 0){
  55. $Alltools = new Alltools;
  56. $result = $Alltools->shouyi($data->to_user_id,$data->money,'user_match_typing_log',$this->msgid);
  57. if($result !== true){
  58. DB::rollBack();
  59. return false;
  60. }
  61. }
  62. //
  63. $update = [
  64. 'plantask_status' => 1,
  65. ];
  66. $rs_update = DB::table('mt_user_match_typing_log')->where('id',$this->msgid)->update($update);
  67. if(!$rs_update){
  68. Db::rollBack();
  69. return false;
  70. }
  71. DB::commit();
  72. return true;
  73. }
  74. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  75. {
  76. $app_name = Config::get('app.name');// laravel name
  77. $key = "{$app_name}:{$key}";
  78. $redis = Redis::connection();
  79. //
  80. //指定键值新增+1 并获取
  81. $count = $redis->incr($key);
  82. if ($count > $apiLimit) {
  83. return false;
  84. }
  85. //设置过期时间
  86. if ($count == 1) {
  87. $redis->pExpire($key, $apiLimitTime);
  88. }
  89. return true;
  90. }
  91. }