Skip to content

Commit

Permalink
CronAbstract 基类
Browse files Browse the repository at this point in the history
调整
  • Loading branch information
xuanyanwow committed Dec 18, 2019
1 parent 643c93a commit 0d4636c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"require": {
"ext-json": "*",
"ext-openssl": "*",
"ext-bcmath": "*"
"ext-bcmath": "*",
"ext-curl": "*",
"ext-simplexml": "*",
"ext-libxml": "*"
},
"autoload": {
"psr-4": {
Expand Down
82 changes: 82 additions & 0 deletions src/siam/AbstractInterface/CronBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* 定时任务基础
* User: Siam
* Date: 2019/11/21
* Time: 9:32
*/

namespace Siam\AbstractInterface;

abstract class CronBase
{
protected static $singleCron = false;
protected static $runtimePath;

/**
* CronBase constructor.
* @throws \Exception
*/
public function __construct()
{
static::$runtimePath = dirname(__FILE__);
// 判断是否为单一任务 是的话停止运行
$lockName = static::$runtimePath.DIRECTORY_SEPARATOR.md5($this->rule()).".txt";
if ( static::$singleCron && file_exists($lockName) ){
throw new \Exception($this->rule() . " is single cron job");
}
if (static::$singleCron){
file_put_contents($lockName, time());
}
}

/**
* 写明监控任务名
* @return mixed
*/
abstract function rule();

/**
* 执行逻辑
* @return void
* @throws \Throwable
*/
public function run(){
$data = $this->before();

try{
$res = $this->do($data);
}catch (\Throwable $throwable){
$this->clearClock();
throw $throwable;
}

$this->clearClock();
$this->after($res);
}

/**
* @return mixed
*/
abstract function before();

/**
* @param null $data
* @return bool
*/
abstract function do($data = NULL);

/**
* @param bool $res
* @return mixed
*/
abstract function after($res = true);

private function clearClock()
{
$lockName = static::$runtimePath.DIRECTORY_SEPARATOR.md5($this->rule()).".txt";
if (file_exists($lockName)){
unlink($lockName);
}
}
}
12 changes: 12 additions & 0 deletions src/siam/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public static function json($code, $data = [], $msg = '')
exit;
}

public static function send($code, $data = [], $msg = '')
{
$return = [
'code' => "$code",
'data' => (object) $data,
'msg' => $msg,
];

$json = json_encode($return, 256);

return $json;
}
/**
* 输出调试
* @param $name string 标签名
Expand Down
4 changes: 3 additions & 1 deletion src/siam/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Curl
/**
* 设置Heard头
* @Curl
* @param $data
*/
public function setHead($data)
{
Expand All @@ -32,6 +33,7 @@ public function setHead($data)
/**
* 设置超时
* @Curl
* @param $s
*/
public function setTimeout($s)
{
Expand Down Expand Up @@ -97,7 +99,7 @@ public function send($url, $data = null, $cert = [], $post = true)

if ($response === false) {
curl_close($curl); // 释放cURL句柄,关闭一个cURL会话
throw new \Exception("curlError||" . curl_errno($curl) . "||" . curl_error($curl));
throw new \Exception("[". curl_errno($curl) . "]" . curl_error($curl));
}

$this->responseHead = curl_getinfo($curl);
Expand Down
25 changes: 18 additions & 7 deletions src/siam/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,25 @@ private function jwt()
return $str;
}

/**
* @param $str
* @return mixed
* @throws \Exception
*/
function decode($str)
{
if ($str == '') return "STR NULL";
if ($str == '') {
throw new \Exception("token is null");
}

$temArr = explode('.', $str);

if (empty($temArr) && !is_array($temArr)) return 'STR ERROR';
if (count($temArr) != 3) return 'STR ERROR';
if (empty($temArr) && !is_array($temArr)) {
throw new \Exception("token sign error");
}
if (count($temArr) != 3) {
throw new \Exception("token sign error");
}

$this->headStr = $temArr[0];
// 解head 拿算法
Expand All @@ -150,7 +161,7 @@ function decode($str)
$this->alg = $head['alg'];
}else{
$this->clear();
return 'ALG ERROR';
throw new \Exception("alg error");
}

$this->dataStr = $temArr[1];
Expand All @@ -159,7 +170,7 @@ function decode($str)
$this->makeSign();
if ($temArr[2] !== $this->signStr) {
$this->clear();
return 'SIGN ERROR';
throw new \Exception("token sign error");
}

$data = json_decode(Base64Url::decode($this->dataStr), TRUE);
Expand All @@ -168,13 +179,13 @@ function decode($str)
// 在此之前不可用
if (!empty($data['nbf']) && ($data['nbf'] > $time)){
$this->clear();
return 'NOTBEFORE';
throw new \Exception("token not before");
}

// 是否已经过期
if (!empty($data['exp']) && ($data['exp'] < $time)){
$this->clear();
return 'EXP';
throw new \Exception("token exp");
}

// 返回解析数据
Expand Down
1 change: 0 additions & 1 deletion src/siam/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function __construct(LoggerAbstractInterface $logger = null)
*/
public function log($str, $level = 'debug')
{
// TODO: Implement log() method.
return $this->logger->log($str, $level);
}
}

0 comments on commit 0d4636c

Please sign in to comment.