ApiService.php 10.4 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19 20 21
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

namespace DtApp\ThinkLibrary\service\bt;

李光春's avatar
李光春 已提交
22
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
23
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
24
use DtApp\ThinkLibrary\service\curl\BtService;
李光春's avatar
李光春 已提交
25 26 27

/**
 * 宝塔Api
李光春's avatar
李光春 已提交
28
 * https://www.bt.cn/
李光春's avatar
李光春 已提交
29 30 31 32 33 34 35 36 37 38
 * Class ApiService
 * @package DtApp\ThinkLibrary\service\bt
 */
class ApiService extends Service
{
    private $url = '';
    private $page = 1;
    private $limit = 15;
    private $order = 'id desc';
    private $where = [];
李光春's avatar
李光春 已提交
39
    private $contents, $backtrack, $key, $panel;
李光春's avatar
李光春 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52

    public function key(string $key)
    {
        $this->key = $key;
        return $this;
    }

    public function panel(string $panel)
    {
        $this->panel = $panel;
        return $this;
    }

53 54 55 56 57 58
    /**
     * 获取配置信息
     * @return $this
     */
    private function getConfig()
    {
59 60
        $this->key = $this->app->config->get('dtapp.bt.key');
        $this->panel = $this->app->config->get('dtapp.bt.panel');
61 62 63
        return $this;
    }

李光春's avatar
李光春 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    /**
     * 获取监控信息
     * @param string $type 类型 GetCpuIo = CPU信息/内存 GetDiskIo = 磁盘IO GetNetWorkIo = 网络IO
     * @param int $start_time 开始时间
     * @param int $end_time 结束时间
     * @return mixed
     */
    public function getCpuIoInfo($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();
        $this->url = "/ajax?action={$type}&start={$start_time}&end={$end_time}";
        return $this;
    }

    /**
     * 获取网站列表
     * @return mixed
     */
    public function getSites()
    {
        $this->url = $this->mimes('GetDataList');
        $this->where['type'] = 'sites';
        return $this;
    }

    /**
     * 获取数据库列表
     * @return mixed
     */
    public function getDatabases()
    {
        $this->url = $this->mimes('getData');
        $this->where['tojs'] = 'database.get_list';
        $this->where['table'] = 'databases';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取防火墙
     * @return mixed
     */
    public function getFirewalls()
    {
        $this->url = $this->mimes('getData');
        $this->where['tojs'] = 'firewall.get_list';
        $this->where['table'] = 'firewall';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取面板日志
     * @return mixed
     */
    public function getLogs()
    {
        $this->url = $this->mimes('getData');
        $this->where['tojs'] = 'firewall.get_log_list';
        $this->where['table'] = 'logs';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取消息通道
     * @return mixed
     */
    public function getNews()
    {
        $this->url = $this->mimes('get_settings');
        return $this;
    }

    /**
     * 获取网站列表
     * @return mixed
     */
    public function getCronTabs()
    {
        $this->url = $this->mimes('getData');
        $this->where['tojs'] = 'site.get_list';
        $this->where['table'] = 'sites';
        $this->where['limit'] = $this->limit;
        $this->where['p'] = $this->page;
        $this->where['order'] = $this->order;
        return $this;
    }

    /**
     * 获取网站分类
     * @return mixed
     */
    public function getTypes()
    {
        $this->url = $this->mimes('get_site_types');
        return $this;
    }

    /**
     * 获取软件列表
     * @return mixed
     */
    public function getSoFts()
    {
        $this->url = $this->mimes('get_soft_list');
        $this->where['p'] = $this->page;
        $this->where['tojs'] = 'soft.get_list';
        return $this;
    }

    /**
     * 获取硬盘信息
     * @return mixed
     */
    public function getDiskInfo()
    {
        $this->url = $this->mimes('GetDiskInfo');
        return $this;
    }

    /**
     * 获取信息系统
     * @return mixed
     */
    public function getSystemTotal()
    {
        $this->url = $this->mimes('GetSystemTotal');
        return $this;
    }

    /**
     * 获取用户信息
     * @return mixed
     */
    public function getUserInfo()
    {
        $this->url = $this->mimes('GetUserInfo');
        return $this;
    }

    /**
     * 获取网络信息
     * @return mixed
     */
    public function getNetWork()
    {
        $this->url = $this->mimes('GetNetWork');
        return $this;
    }

    /**
     * 获取插件信息
     * @return mixed
     */
    public function getPlugin()
    {
        $this->url = $this->mimes('get_index_list');
        return $this;
    }

    /**
     * 获取软件信息
     * @return mixed
     */
    public function getSoft()
    {
        $this->url = $this->mimes('get_soft_list');
        return $this;
    }

    /**
     * 获取更新信息
     * @return mixed
     */
    public function getUpdatePanel()
    {
        $this->url = $this->mimes('UpdatePanel');
        return $this;
    }

    /**
     * 当前页码
     * @param int $is
     * @return ApiService
     */
    public function page(int $is = 1)
    {
        $this->page = $is;
        return $this;
    }

    /**
     * 返回数量
     * @param int $is
     * @return ApiService
     */
    public function limit(int $is = 15)
    {
        $this->limit = $is;
        return $this;
    }

    /**
     * 排序
     * @param string $ss
     * @return $this
     */
    public function order(string $ss = 'id desc')
    {
        $this->order = $ss;
        return $this;
    }

    /**
     * 查询条件
     * @param array $array
     * @return ApiService
     */
    public function where($array = [])
    {
        $this->where = $array;
        return $this;
    }

    /**
     * 获取数据和总数
     */
    private function getDataWithOrderOpt()
    {
        $this->backtrack['data'] = $this->contents['data'];
        $this->backtrack['orderOpt'] = $this->contents['orderOpt'];
        return $this;
    }

    /**
     * 获取数据和总数
     */
    private function getDataWithCount()
    {
        if (empty($this->contents['data'])) $this->contents['data'] = [];
        if (!is_array($this->contents['data'])) $this->contents['data'] = [];
        $this->backtrack['data'] = $this->contents;
        if (empty($this->contents['page'])) $this->contents['page'] = 0;
        $this->backtrack['count'] = $this->getCountData($this->contents['page']);
        return $this;
    }

    /**
     * 获取数据
     * @return $this
     */
    private function getData()
    {
        $this->backtrack['data'] = $this->contents;
        return $this;
    }

    /**
     * 发起网络请求
     * @return $this
李光春's avatar
李光春 已提交
332
     * @throws DtaException
李光春's avatar
李光春 已提交
333 334 335 336 337 338 339 340 341 342 343
     */
    private function getHttp()
    {
        //请求面板接口
        $this->contents = $this->HttpPostCookie($this->url, $this->where);
        return $this;
    }

    /**
     * 返回Array
     * @return array|mixed
李光春's avatar
李光春 已提交
344
     * @throws DtaException
李光春's avatar
李光春 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
     */
    public function toArray()
    {
        $this->getHttp();
        switch ($this->where['type']) {
            case 'sites':
                $this->getDataWithOrderOpt();
                break;
            default:
                $this->getDataWithCount();
                break;
        }
        if (empty($this->backtrack)) return [];
        if (is_array($this->backtrack)) return $this->backtrack;
        return json_decode($this->backtrack, true);
    }

    /**
     * 获取文件的信息
     * @param $name
     * @return string
     */
    private function mimes($name): string
    {
        $mimes = include __DIR__ . '/bin/mimes.php';
        if (isset($mimes[$name])) return '/' . $mimes[$name];
        return '';
    }

    /**
     * 发起POST请求
     * @param string $url 网址
     * @param array $data 数据
     * @param bool $is_json 是否返回Json格式
     * @return bool|mixed|string
李光春's avatar
李光春 已提交
380
     * @throws DtaException
李光春's avatar
李光春 已提交
381 382 383
     */
    protected function HttpPostCookie(string $url, array $data = [], bool $is_json = true)
    {
384
        if (empty($this->panel)) $this->getConfig();
李光春's avatar
李光春 已提交
385
        if (empty($this->panel)) throw new DtaException('请检查panel参数');
李光春's avatar
李光春 已提交
386 387 388 389 390 391 392 393
        //定义cookie保存位置
        $file = app()->getRootPath() . 'runtime/dtapp/bt/cookie/';
        $cookie_file = $file . md5($this->panel) . '.cookie';
        is_dir($file) or mkdir($file, 0777, true);
        if (!file_exists($cookie_file)) {
            $fp = fopen($cookie_file, 'w+');
            fclose($fp);
        }
394
        if (empty($this->key)) $this->getConfig();
李光春's avatar
李光春 已提交
395
        if (empty($this->key)) throw new DtaException('请检查key参数');
李光春's avatar
李光春 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
        return BtService::instance()
            ->panel($this->panel)
            ->key($this->key)
            ->url($url)
            ->cookie($cookie_file)
            ->data($data)
            ->toArray($is_json);
    }

    /**
     * 获取总数
     * @param string $str
     * @return false|int|string
     */
    protected function getCountData(string $str)
    {
        $start = strpos($str, "共");
        $end = strpos($str, "条数据");
        $count = substr($str, $start + 3, $end - $start - 3);
        if (empty($count)) return 0;
        return $count;
    }
}