Skip to content

Commit

Permalink
- 优化函数
Browse files Browse the repository at this point in the history
  • Loading branch information
dtapps committed Apr 9, 2020
1 parent 5edd044 commit a46987d
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 99 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v1.0.9 / 2020-04-07
- 优化函数

## v1.0.8 / 2020-04-07
- 优化

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": ">=7.0",
"ext-json": "*",
"ext-curl": "*"
"ext-curl": "*",
"liguangchun/curl": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
49 changes: 16 additions & 33 deletions src/BaseBt.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace DtApp\Bt;

use DtApp\Curl\BtCn;
use DtApp\Curl\CurlException;

/**
* Class BaseBt
* @package DtApp\Bt
Expand All @@ -25,7 +28,7 @@ class BaseBt
/**
* 定义当前版本
*/
const VERSION = '1.0.8';
const VERSION = '1.0.9';

/**
* 配置
Expand All @@ -47,14 +50,18 @@ public function __construct(array $options)

/**
* 发起POST请求
* @param String $url 目标网填,带http://
* @param array $data 欲提交的数据
* @param int $timeout
* @return string
* @param String $url 网址
* @param array $data 数据
* @param bool $is_json 是否返回Json格式
* @return bool|mixed|string
* @throws CurlException
*/
protected function HttpPostCookie($url, $data = [], $timeout = 60)
protected function HttpPostCookie($url, $data = [], $is_json = true)
{
$p_data = $this->GetKeyData();
$config = [
'bt_panel' => $this->config->get('panel'),
'bt_key' => $this->config->get('key')
];
//定义cookie保存位置
$file = __DIR__ . '/../cookie/';
$cookie_file = $file . md5($this->config->get('panel')) . '.cookie';
Expand All @@ -63,32 +70,8 @@ protected function HttpPostCookie($url, $data = [], $timeout = 60)
$fp = fopen($cookie_file, 'w+');
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->config->get('panel') . $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array_merge($p_data, $data));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

/**
* 构造带有签名的关联数组
*/
private function GetKeyData()
{
$now_time = time();
return array(
'request_token' => md5($now_time . '' . md5($this->config->get('key'))),
'request_time' => $now_time
);
$BtCn = new BtCn($config);
return $BtCn->httpPost($url, $data, $cookie_file, 60, $is_json);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 系统监控
* Class Control
Expand All @@ -29,15 +31,14 @@ class Control extends BaseBt
* @param int $start_time 开始时间
* @param int $end_time 结束时间
* @return mixed
* @throws CurlException
*/
public function getInfo($type = 'GetCpuIo', $start_time = 0, $end_time = 0)
{
if (empty($start_time)) $start_time = strtotime(date('Y-m-d'));
if (empty($end_time)) $end_time = time();
$url = "/ajax?action={$type}&start={$start_time}&end={$end_time}";
//请求面板接口
$result = $this->HttpPostCookie($url, []);
//解析JSON数据
return json_decode($result, true);
return $this->HttpPostCookie($url, []);
}
}
9 changes: 5 additions & 4 deletions src/CronTab.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 计划任务
* Class CronTab
Expand All @@ -26,18 +28,17 @@ class CronTab extends BaseBt
/**
* 获取网站列表
* @return mixed
* @throws CurlException
*/
public function getDataList()
{
$url = '/crontab?action=GetDataList';
$p_data['type'] = 'sites';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
$data = json_decode($result, true);
return [
'data' => $data['data'],
'orderOpt' => $data['orderOpt']
'data' => $result['data'],
'orderOpt' => $result['orderOpt']
];
}
}
15 changes: 8 additions & 7 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 数据库管理
* Class Database
Expand All @@ -29,6 +31,7 @@ class Database extends BaseBt
* @param int $limit
* @param string $search
* @return mixed
* @throws CurlException
*/
public function getList($page = 1, $limit = 15, $search = '')
{
Expand All @@ -41,14 +44,12 @@ public function getList($page = 1, $limit = 15, $search = '')
$p_data['order'] = 'id desc';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
$data = json_decode($result, true);
if (empty($data['data'])) $data['data'] = [];
if (empty($data['page'])) $data['page'] = 0;
if (!is_array($data['data'])) $data['data'] = [];
if (empty($result['data'])) $result['data'] = [];
if (empty($result['page'])) $result['page'] = 0;
if (!is_array($result['data'])) $result['data'] = [];
return [
'data' => $data['data'],
'count' => $this->getCountData($data['page'])
'data' => $result['data'],
'count' => $this->getCountData($result['page'])
];
}
}
28 changes: 14 additions & 14 deletions src/Firewall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 系统安全
* Class firewall
Expand All @@ -28,6 +30,7 @@ class Firewall extends BaseBt
* @param int $page
* @param int $limit
* @return mixed
* @throws CurlException
*/
public function getList($page = 1, $limit = 10)
{
Expand All @@ -40,14 +43,12 @@ public function getList($page = 1, $limit = 10)
$p_data['order'] = 'id desc';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
$data = json_decode($result, true);
if (empty($data['data'])) $data['data'] = [];
if (empty($data['page'])) $data['page'] = 0;
if (!is_array($data['data'])) $data['data'] = [];
if (empty($result['data'])) $result['data'] = [];
if (empty($result['page'])) $result['page'] = 0;
if (!is_array($result['data'])) $result['data'] = [];
return [
'data' => $data['data'],
'count' => $this->getCountData($data['page'])
'data' => $result['data'],
'count' => $this->getCountData($result['page'])
];
}

Expand All @@ -56,6 +57,7 @@ public function getList($page = 1, $limit = 10)
* @param int $page
* @param int $limit
* @return mixed
* @throws CurlException
*/
public function getLog($page = 1, $limit = 10)
{
Expand All @@ -68,14 +70,12 @@ public function getLog($page = 1, $limit = 10)
$p_data['order'] = 'id desc';
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
$data = json_decode($result, true);
if (empty($data['data'])) $data['data'] = [];
if (empty($data['page'])) $data['page'] = 0;
if (!is_array($data['data'])) $data['data'] = [];
if (empty($result['data'])) $result['data'] = [];
if (empty($result['page'])) $result['page'] = 0;
if (!is_array($result['data'])) $result['data'] = [];
return [
'data' => $data['data'],
'count' => $this->getCountData($data['page'])
'data' => $result['data'],
'count' => $this->getCountData($result['page'])
];
}
}
7 changes: 4 additions & 3 deletions src/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 设置
* Class Setup
Expand All @@ -26,13 +28,12 @@ class Setup extends BaseBt
/**
* 获取消息通道
* @return mixed
* @throws CurlException
*/
public function getNews()
{
$url = '/config?action=get_settings';
//请求面板接口
$result = $this->HttpPostCookie($url, []);
//解析JSON数据
return json_decode($result, true);
return $this->HttpPostCookie($url, []);
}
}
20 changes: 10 additions & 10 deletions src/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 网站管理
* Class Site
Expand All @@ -30,6 +32,7 @@ class Site extends BaseBt
* @param string $search
* @param int $type
* @return mixed
* @throws CurlException
*/
public function getList($page = 1, $limit = 15, $search = '', $type = -1)
{
Expand All @@ -43,27 +46,24 @@ public function getList($page = 1, $limit = 15, $search = '', $type = -1)
$p_data['type'] = $type;
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
$data = json_decode($result, true);
if (empty($data['data'])) $data['data'] = [];
if (empty($data['page'])) $data['page'] = 0;
if (!is_array($data['data'])) $data['data'] = [];
if (empty($result['data'])) $result['data'] = [];
if (empty($result['page'])) $result['page'] = 0;
if (!is_array($result['data'])) $result['data'] = [];
return [
'data' => $data['data'],
'count' => $this->getCountData($data['page'])
'data' => $result['data'],
'count' => $this->getCountData($result['page'])
];
}

/**
* 获取网站分类
* @return mixed
* @throws CurlException
*/
public function getTypes()
{
$url = '/site?action=get_site_types';
//请求面板接口
$result = $this->HttpPostCookie($url, []);
//解析JSON数据
return json_decode($result, true);
return $this->HttpPostCookie($url, []);
}
}
7 changes: 4 additions & 3 deletions src/Soft.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace DtApp\Bt;

use DtApp\Curl\CurlException;

/**
* 软件管理
* Class Soft
Expand All @@ -30,6 +32,7 @@ class Soft extends BaseBt
* @param int $force
* @param string $query
* @return mixed
* @throws CurlException
*/
public function getList($page = 1, $type = 0, $force = 0, $query = '')
{
Expand All @@ -40,8 +43,6 @@ public function getList($page = 1, $type = 0, $force = 0, $query = '')
$p_data['force'] = $force;// 是否更新列表 1=是 0=否
$p_data['query'] = $query; // 搜索
//请求面板接口
$result = $this->HttpPostCookie($url, $p_data);
//解析JSON数据
return json_decode($result, true);
return $this->HttpPostCookie($url, $p_data);
}
}

0 comments on commit a46987d

Please sign in to comment.