81 lines
2.7 KiB
PHP
81 lines
2.7 KiB
PHP
<?php
|
|
namespace addon\supply\model;
|
|
use app\model\BaseModel;
|
|
class SupplyCron extends BaseModel
|
|
{
|
|
public function AddCron($a_id, $event_name, $data = [])
|
|
{
|
|
$appInfo = model('supply_app')->getInfo(['id' => $a_id]);
|
|
if ($appInfo) {
|
|
$data = [
|
|
'site_id' => $appInfo['site_id'],
|
|
'app_id' => $appInfo['app_id'],
|
|
'app_name' => $appInfo['app_name'],
|
|
'app_secret' => $appInfo['app_secret'],
|
|
'event_name' => $event_name,
|
|
'push_url' => $appInfo['push_url'],
|
|
'push_data' => json_encode($data),
|
|
'execute_time' => time(),
|
|
'create_time' => time(),
|
|
'last_time' => time(),
|
|
];
|
|
$res = model('supply_cron')->add($data);
|
|
return $this->success($res);
|
|
}
|
|
}
|
|
|
|
public function deleteCron($condition)
|
|
{
|
|
$res = model('supply_cron')->delete($condition);
|
|
return $this->success($res);
|
|
}
|
|
|
|
|
|
/****
|
|
* 执行任务
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function execute()
|
|
{
|
|
ini_set('memory_limit', '1024M');
|
|
$list = model('supply_cron')->getList([['push_num', '<', 15], ['execute_time', '<=', time()]]);
|
|
$now_time = time();
|
|
if (!empty($list)) {
|
|
foreach ($list as $k => $v) {
|
|
$data = [
|
|
'msgId' => Date('YmdHis', $now_time) . rand(100000, 999999),
|
|
'type' => $v['event_name'],
|
|
'timestamp' => Date('Y-m-d H:i:s', $now_time),
|
|
'data' => $v['push_data'],
|
|
'userInfo' => $v['app_id'],
|
|
];
|
|
$data['signature'] = $this->getSign($data, $v['app_secret']);
|
|
$res = http($v['push_url'], 5, [], $data);
|
|
if ($res == 'success') {
|
|
$this->deleteCron(['id' => $v['id']]);
|
|
} else {
|
|
$push_num = $v['push_num'] + 1;
|
|
$up = [
|
|
'push_num' => $push_num,
|
|
'last_time' => $v['execute_time'],
|
|
'execute_time' => $push_num * 300,
|
|
];
|
|
model('supply_cron')->update($up, ['id' => $v['id']]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getSign($data, $appSecret = '')
|
|
{
|
|
unset($data['signature']);
|
|
ksort($data);
|
|
$content = http_build_query($data);
|
|
$sign = hash_hmac("sha1", $content, $appSecret, true);
|
|
$signHexWithLowcase = bin2hex($sign);
|
|
$signHexUppercase = strtoupper($signHexWithLowcase);
|
|
return $signHexUppercase;
|
|
}
|
|
}
|