Skip to content

Commit 0d4636c

Browse files
committed
CronAbstract 基类
调整
1 parent 643c93a commit 0d4636c

File tree

6 files changed

+119
-10
lines changed

6 files changed

+119
-10
lines changed

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"require": {
1414
"ext-json": "*",
1515
"ext-openssl": "*",
16-
"ext-bcmath": "*"
16+
"ext-bcmath": "*",
17+
"ext-curl": "*",
18+
"ext-simplexml": "*",
19+
"ext-libxml": "*"
1720
},
1821
"autoload": {
1922
"psr-4": {
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* 定时任务基础
4+
* User: Siam
5+
* Date: 2019/11/21
6+
* Time: 9:32
7+
*/
8+
9+
namespace Siam\AbstractInterface;
10+
11+
abstract class CronBase
12+
{
13+
protected static $singleCron = false;
14+
protected static $runtimePath;
15+
16+
/**
17+
* CronBase constructor.
18+
* @throws \Exception
19+
*/
20+
public function __construct()
21+
{
22+
static::$runtimePath = dirname(__FILE__);
23+
// 判断是否为单一任务 是的话停止运行
24+
$lockName = static::$runtimePath.DIRECTORY_SEPARATOR.md5($this->rule()).".txt";
25+
if ( static::$singleCron && file_exists($lockName) ){
26+
throw new \Exception($this->rule() . " is single cron job");
27+
}
28+
if (static::$singleCron){
29+
file_put_contents($lockName, time());
30+
}
31+
}
32+
33+
/**
34+
* 写明监控任务名
35+
* @return mixed
36+
*/
37+
abstract function rule();
38+
39+
/**
40+
* 执行逻辑
41+
* @return void
42+
* @throws \Throwable
43+
*/
44+
public function run(){
45+
$data = $this->before();
46+
47+
try{
48+
$res = $this->do($data);
49+
}catch (\Throwable $throwable){
50+
$this->clearClock();
51+
throw $throwable;
52+
}
53+
54+
$this->clearClock();
55+
$this->after($res);
56+
}
57+
58+
/**
59+
* @return mixed
60+
*/
61+
abstract function before();
62+
63+
/**
64+
* @param null $data
65+
* @return bool
66+
*/
67+
abstract function do($data = NULL);
68+
69+
/**
70+
* @param bool $res
71+
* @return mixed
72+
*/
73+
abstract function after($res = true);
74+
75+
private function clearClock()
76+
{
77+
$lockName = static::$runtimePath.DIRECTORY_SEPARATOR.md5($this->rule()).".txt";
78+
if (file_exists($lockName)){
79+
unlink($lockName);
80+
}
81+
}
82+
}

src/siam/Api.php

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public static function json($code, $data = [], $msg = '')
4040
exit;
4141
}
4242

43+
public static function send($code, $data = [], $msg = '')
44+
{
45+
$return = [
46+
'code' => "$code",
47+
'data' => (object) $data,
48+
'msg' => $msg,
49+
];
50+
51+
$json = json_encode($return, 256);
52+
53+
return $json;
54+
}
4355
/**
4456
* 输出调试
4557
* @param $name string 标签名

src/siam/Curl.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Curl
2323
/**
2424
* 设置Heard头
2525
* @Curl
26+
* @param $data
2627
*/
2728
public function setHead($data)
2829
{
@@ -32,6 +33,7 @@ public function setHead($data)
3233
/**
3334
* 设置超时
3435
* @Curl
36+
* @param $s
3537
*/
3638
public function setTimeout($s)
3739
{
@@ -97,7 +99,7 @@ public function send($url, $data = null, $cert = [], $post = true)
9799

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

103105
$this->responseHead = curl_getinfo($curl);

src/siam/JWT.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,25 @@ private function jwt()
133133
return $str;
134134
}
135135

136+
/**
137+
* @param $str
138+
* @return mixed
139+
* @throws \Exception
140+
*/
136141
function decode($str)
137142
{
138-
if ($str == '') return "STR NULL";
143+
if ($str == '') {
144+
throw new \Exception("token is null");
145+
}
139146

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

142-
if (empty($temArr) && !is_array($temArr)) return 'STR ERROR';
143-
if (count($temArr) != 3) return 'STR ERROR';
149+
if (empty($temArr) && !is_array($temArr)) {
150+
throw new \Exception("token sign error");
151+
}
152+
if (count($temArr) != 3) {
153+
throw new \Exception("token sign error");
154+
}
144155

145156
$this->headStr = $temArr[0];
146157
// 解head 拿算法
@@ -150,7 +161,7 @@ function decode($str)
150161
$this->alg = $head['alg'];
151162
}else{
152163
$this->clear();
153-
return 'ALG ERROR';
164+
throw new \Exception("alg error");
154165
}
155166

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

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

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

180191
// 返回解析数据

src/siam/Logger.php

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function __construct(LoggerAbstractInterface $logger = null)
3737
*/
3838
public function log($str, $level = 'debug')
3939
{
40-
// TODO: Implement log() method.
4140
return $this->logger->log($str, $level);
4241
}
4342
}

0 commit comments

Comments
 (0)