TypingJob.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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)->first();
  52. if(empty($data)){return true;}
  53. DB::beginTransaction();
  54. $Alltools = new Alltools;
  55. $result = $Alltools->shouyi($data->to_user_id,$data->money,'user_match_typing_log',$this->msgid);
  56. if($result !== true){
  57. DB::rollBack();
  58. return false;
  59. }
  60. DB::commit();
  61. return true;
  62. }
  63. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  64. {
  65. $app_name = Config::get('app.name');// laravel name
  66. $key = "{$app_name}:{$key}";
  67. $redis = Redis::connection();
  68. //
  69. //指定键值新增+1 并获取
  70. $count = $redis->incr($key);
  71. if ($count > $apiLimit) {
  72. return false;
  73. }
  74. //设置过期时间
  75. if ($count == 1) {
  76. $redis->pExpire($key, $apiLimitTime);
  77. }
  78. return true;
  79. }
  80. }