Files
tougao/application/common/QueueJob.php
2025-07-07 17:54:17 +08:00

82 lines
2.0 KiB
PHP

<?php
namespace app\common;
use think\Db;
use think\Cache;
class QueueJob
{
//必填参数
protected $aField = ['job_id','job_class','status','create_time','update_time','error','params'];
// 记录任务开始
public function addLog($aParam = []) {
//数据处理
$aField = $this->aField;
$aInsert = [];
foreach ($aField as $key => $value) {
if(isset($aParam[$value])){
$aInsert[$value] = $aParam[$value];
}
}
$result = 0;
if(!empty($aInsert)){
$result = DB::name('wechat_queue_logs')->insertGetId($aParam);
}
return $result;
}
// 记录任务成功
public function updateLog($aParam = []) {
$iLogId = empty($aParam['log_id']) ? 0 : $aParam['log_id'];
if(empty($iLogId)){
return false;
}
//数据处理
$aField = $this->aField;
$aUpdate = [];
foreach ($aField as $key => $value) {
if(isset($aParam[$value])){
$aUpdate[$value] = $aParam[$value];
}
}
unset($aParam['log_id']);
$result = DB::name('wechat_queue_logs')->where('log_id',$iLogId)->limit(1)->update($aUpdate);
return $result;
}
/**
* 写入Reids 防止1小时内重复操作
*/
public function setRedisLabel($aParam = []){
//判断数据是否为空
if(empty($aParam['redis_key'])){
return 3;
}
//获取值
$sValue = $this->getRedisLabel($aParam['redis_key']);
if($sValue == $aParam['redis_key']){
return 4;
}
$result = Cache::set($aParam['redis_key'], $aParam['redis_key'], 3600);
if($result == true){
return 1;
}
//写入
return 2;
}
/**
* 获取Reids值
*/
public function getRedisLabel($sRedisKey = ''){
if(empty($sRedisKey)){
return '';
}
return Cache::get($sRedisKey);
}
}
?>