123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Jobs;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\Log;
- use App\Common\Library\Alltools;
- class RechargeJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $msgid;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct($msgid)
- {
- //
- $this->msgid = $msgid;
- Log::info(date('Y-m-d H:i:d').':'.$msgid);
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $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 goaaa(){
- //
- $data = DB::table('mt_pay_order')->where('id',$this->msgid)->first();
- if(empty($data)){return true;}
- $args = json_decode($data->args,true);
- $gold = isset($args['gold']) ? $args['gold'] : 0;
- if($gold <= 0){
- return true;
- }
- DB::beginTransaction();
- $Alltools = new Alltools;
- $result = $Alltools->recharge($data->user_id,$gold,'pay_order',$this->msgid);
- if($result !== true){
- DB::rollBack();
- return false;
- }
- DB::commit();
- return true;
- }
- private function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
- {
- $app_name = Config::get('app.name');// laravel name
- $key = "{$app_name}:{$key}";
- $redis = Redis::connection();
- //
- //指定键值新增+1 并获取
- $count = $redis->incr($key);
- if ($count > $apiLimit) {
- return false;
- }
- //设置过期时间
- if ($count == 1) {
- $redis->pExpire($key, $apiLimitTime);
- }
- return true;
- }
- }
|