love_php/app/Jobs/CheckCoinSafe.php

69 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Coin;
use App\Models\CoinLog;
use App\Http\Response\ResponseJson;
use Illuminate\Support\Facades\Redis;
class CheckCoinSafe implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ResponseJson;
protected $user_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user_id)
{
$this->user_id = $user_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
//coin账号
$coin = Coin::where('user_id', $this->user_id)->first();
if (empty($coin)) throw new \Exception("用户id".$this->user_id."coin账号不存在");
//监听时间间隔 单位分钟
$key = 'coin_safe_time';
$coin_safe_time = Redis::get($key);
if (empty($coin_safe_time)) {
$coin_safe_time = 5;
Redis::set($key, $coin_safe_time);
}
//监听分享用户数 单位个
$num_key = 'coin_share_num';
$coin_share_num = Redis::get($num_key);
if (empty($coin_share_num)) {
$coin_share_num = 10;
Redis::set($num_key, $coin_share_num);
}
$start_time = date('Y-m-d H:i:s', time() - $coin_safe_time * 60);
$end_time = date('Y-m-d H:i:s');
$count = CoinLog::where('user_id', $this->user_id)->where('type', 'INVIT')->where('remark', "商家入驻")->whereBetween('created_at', [$start_time, $end_time])->count();
if ($count >= $coin_share_num) {
$coin->is_safe = 0;
$coin->save();
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
}