想给自己推送下账单提醒什么的,顺便处理下相关内容,然后发现这个逻辑真不是人玩的!都快赶上公司的业务逻辑代码看了!
还有一种既视感就是,好好的PHP代码,硬是写出来JS的感觉,当然这是为了某种特殊的原因,最大化降低代码耦合度,每个任务彼此独立。
还有,定时任务真的需要这样处理么?不知道有没有更好的方式,看来得找个开源程序喵喵了,已经超出我理解范围了!这里最大的问题就是重复性检查的问题,通知几次!
(function() use ($_LOCK_FILE) {
$offset_day = [
'bill' => 3,//延迟三天查询账单
'repayment' => 2//提前两天还款
];
$card_cfg = [
'gf' => [
'name' => '广发信用卡',
'bill' => 1,
'repayment' => 1
],
'cmb' => [
'name' => '招行信用卡',
'bill' => 1,
'repayment' => 1
]
];
\TaskCron\Log::debug("开始检测信用卡时间");
if(!in_array(date("H"), [11, 20]) && ((int)date("i")) < 5) {
//仅仅在11点和20点进行通知,0-5分钟进行检查,防止延迟未执行
return;
}
$cfg = \TaskCron\Lock::read($_LOCK_FILE);
if(!empty($cfg)) {
$cfg = json_decode($cfg);
}
if(!is_array($cfg)) {
$cfg = [];
}
\TaskCron\Log::debug("读取配置文件");
\TaskCron\Log::debug($cfg);
$pushover = function($title, $content) {
\TaskCron\Log::log("开始通知:{$title}");
$client = new \Beanstalk\Client();
$client->connect();
$id = $client->put(1, 0, 0, json_encode(array(
'type' => 'pushover',
'content' => $content,
'title' => $title,
)));
$client->disconnect();
\TaskCron\Log::log("通知结束:{$id}");
};
$bill_month_func = function($bill_date) {
return date("m", strtotime("-1month", strtotime($bill_date)));
};
foreach($card_cfg as $name => $item) {
$bill_time = strtotime("-{$offset_day['bill']}days");
$bill_date = date("Y-m-j", $bill_time);
\TaskCron\Log::debug("Bill date: {$bill_date}");
if($bill_date == date("Y-m-", $bill_time).$item['bill']) {
//检测查询账单
\TaskCron\Log::debug("Bill check date: {$bill_date}");
$key = "{$name}-bill-{$bill_date}-".date("H");
if(!in_array($key, $cfg)) {
//不在此列,执行
$pushover(
"{$item['name']}{$bill_month_func($bill_date)}月账单查看提醒",
"银行:{$item['name']}\n账单日:{$item['bill']}\n记得核算信息!");
$cfg[] = $key;
}
}
//还款通知
$repayment_time = strtotime("+{$offset_day['repayment']}days");
$repayment_date = date("Y-m-j", $repayment_time);
\TaskCron\Log::debug("Repayment date: {$repayment_date}");
if($repayment_date == date("Y-m-", $repayment_time).$item['repayment']) {
//检测到还款到期
$key = "{$name}-repayment-{$repayment_date}-".date("H");
\TaskCron\Log::debug("Repayment check date: {$repayment_date}");
if(!in_array($key, $cfg)) {
$bill_month = date("m", strtotime("-".($item['bill'] > $item['repayment'] ? 2 : 1)."month", $repayment_time));
$pushover(
"{$item['name']}{$bill_month}月账单还款提醒",
"银行:{$item['name']}\n还款日:{$item['repayment']}\n如有可能使用工资卡还款!");
$cfg[] = $key;
}
}
}
\TaskCron\Lock::write($_LOCK_FILE, json_encode($cfg));
})();
$offset_day = [
'bill' => 3,//延迟三天查询账单
'repayment' => 2//提前两天还款
];
$card_cfg = [
'gf' => [
'name' => '广发信用卡',
'bill' => 1,
'repayment' => 1
],
'cmb' => [
'name' => '招行信用卡',
'bill' => 1,
'repayment' => 1
]
];
\TaskCron\Log::debug("开始检测信用卡时间");
if(!in_array(date("H"), [11, 20]) && ((int)date("i")) < 5) {
//仅仅在11点和20点进行通知,0-5分钟进行检查,防止延迟未执行
return;
}
$cfg = \TaskCron\Lock::read($_LOCK_FILE);
if(!empty($cfg)) {
$cfg = json_decode($cfg);
}
if(!is_array($cfg)) {
$cfg = [];
}
\TaskCron\Log::debug("读取配置文件");
\TaskCron\Log::debug($cfg);
$pushover = function($title, $content) {
\TaskCron\Log::log("开始通知:{$title}");
$client = new \Beanstalk\Client();
$client->connect();
$id = $client->put(1, 0, 0, json_encode(array(
'type' => 'pushover',
'content' => $content,
'title' => $title,
)));
$client->disconnect();
\TaskCron\Log::log("通知结束:{$id}");
};
$bill_month_func = function($bill_date) {
return date("m", strtotime("-1month", strtotime($bill_date)));
};
foreach($card_cfg as $name => $item) {
$bill_time = strtotime("-{$offset_day['bill']}days");
$bill_date = date("Y-m-j", $bill_time);
\TaskCron\Log::debug("Bill date: {$bill_date}");
if($bill_date == date("Y-m-", $bill_time).$item['bill']) {
//检测查询账单
\TaskCron\Log::debug("Bill check date: {$bill_date}");
$key = "{$name}-bill-{$bill_date}-".date("H");
if(!in_array($key, $cfg)) {
//不在此列,执行
$pushover(
"{$item['name']}{$bill_month_func($bill_date)}月账单查看提醒",
"银行:{$item['name']}\n账单日:{$item['bill']}\n记得核算信息!");
$cfg[] = $key;
}
}
//还款通知
$repayment_time = strtotime("+{$offset_day['repayment']}days");
$repayment_date = date("Y-m-j", $repayment_time);
\TaskCron\Log::debug("Repayment date: {$repayment_date}");
if($repayment_date == date("Y-m-", $repayment_time).$item['repayment']) {
//检测到还款到期
$key = "{$name}-repayment-{$repayment_date}-".date("H");
\TaskCron\Log::debug("Repayment check date: {$repayment_date}");
if(!in_array($key, $cfg)) {
$bill_month = date("m", strtotime("-".($item['bill'] > $item['repayment'] ? 2 : 1)."month", $repayment_time));
$pushover(
"{$item['name']}{$bill_month}月账单还款提醒",
"银行:{$item['name']}\n还款日:{$item['repayment']}\n如有可能使用工资卡还款!");
$cfg[] = $key;
}
}
}
\TaskCron\Lock::write($_LOCK_FILE, json_encode($cfg));
})();
当前还没有任何评论