diff --git a/composer.json b/composer.json index fc7fbd3..dbd8da7 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,10 @@ "require": { "ext-json": "*", "ext-openssl": "*", - "ext-bcmath": "*" + "ext-bcmath": "*", + "ext-curl": "*", + "ext-simplexml": "*", + "ext-libxml": "*" }, "autoload": { "psr-4": { diff --git a/src/siam/AbstractInterface/CronBase.php b/src/siam/AbstractInterface/CronBase.php new file mode 100644 index 0000000..925871a --- /dev/null +++ b/src/siam/AbstractInterface/CronBase.php @@ -0,0 +1,82 @@ +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); + } + } +} \ No newline at end of file diff --git a/src/siam/Api.php b/src/siam/Api.php index 4424902..c9f281d 100644 --- a/src/siam/Api.php +++ b/src/siam/Api.php @@ -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 标签名 diff --git a/src/siam/Curl.php b/src/siam/Curl.php index 2966405..54439ce 100644 --- a/src/siam/Curl.php +++ b/src/siam/Curl.php @@ -23,6 +23,7 @@ class Curl /** * 设置Heard头 * @Curl + * @param $data */ public function setHead($data) { @@ -32,6 +33,7 @@ public function setHead($data) /** * 设置超时 * @Curl + * @param $s */ public function setTimeout($s) { @@ -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); diff --git a/src/siam/JWT.php b/src/siam/JWT.php index 5d5c82b..352bbb2 100644 --- a/src/siam/JWT.php +++ b/src/siam/JWT.php @@ -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 拿算法 @@ -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]; @@ -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); @@ -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"); } // 返回解析数据 diff --git a/src/siam/Logger.php b/src/siam/Logger.php index 190de42..22a11ef 100644 --- a/src/siam/Logger.php +++ b/src/siam/Logger.php @@ -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); } } \ No newline at end of file