RechargeJob.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 RechargeJob 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_pay_order')->where('id',$this->msgid)->first();
  52. if(empty($data)){return true;}
  53. $args = json_decode($data->args,true);
  54. $gold = isset($args['gold']) ? $args['gold'] : 0;
  55. if($gold <= 0){
  56. return true;
  57. }
  58. DB::beginTransaction();
  59. $Alltools = new Alltools;
  60. $result = $Alltools->recharge($data->user_id,$gold,'pay_order',$this->msgid);
  61. if($result !== true){
  62. DB::rollBack();
  63. return false;
  64. }
  65. DB::commit();
  66. return true;
  67. }
  68. private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
  69. {
  70. $app_name = Config::get('app.name');// laravel name
  71. $key = "{$app_name}:{$key}";
  72. $redis = Redis::connection();
  73. //
  74. //指定键值新增+1 并获取
  75. $count = $redis->incr($key);
  76. if ($count > $apiLimit) {
  77. return false;
  78. }
  79. //设置过期时间
  80. if ($count == 1) {
  81. $redis->pExpire($key, $apiLimitTime);
  82. }
  83. return true;
  84. }
  85. }