这里是文章模块栏目内容页
thinkphp对接workman的任务系统



<?php

namespace app\admin\controller\crontab;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use GuzzleHttp\Client;

/**
 * @ControllerAnnotation(title="crontab_task")
 */
class Task extends AdminController
{

    use \app\admin\traits\Curd;

    private $client;
    private $host;
    private $headers;
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\CrontabTask();
        
        $this->assign('getTypeList', $this->model->getTypeList());

        $this->assign('getStatusList', $this->model->getStatusList());

        $this->client = new Client(['timeout' => 20, 'verify' => false]);
        $this->host = '127.0.0.1:2345';
        $this->headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->each(function($item){
                    $item->last_running_time = date('Y-m-d H:i:s', $item->last_running_time);
                });
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            $result = false;
            try {
                $url = $this->host . '/crontab/add';
                $response = $this->client->post($url, [
                    'headers' => $this->headers,
                    'form_params' => $post
                ]);
                //获取body返回值信息
                $body = json_decode($response->getBody(), true);
                if (!empty($body['data']) && $body['data'] == true) {
                    $result = true;
                }
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $result ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $result = false;
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                // 调用插件的方法,里面只能修改状态和排序
                $url = $this->host . '/crontab/edit?id='.$id;
                $response = $this->client->post($url, [
                    'headers' => $this->headers,
                    'form_params' => $post
                ]);
                //获取body返回值信息
                $body = json_decode($response->getBody(), true);
                if (!empty($body['code']) && $body['code'] == 200) {
                    $result = true;
                }
            } catch (\Exception $e) {
                $this->error('保存失败'.$e->getMessage());
            }
            $result ? $this->success('保存成功') : $this->error('保存失败!');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $url = $this->host . '/crontab/delete';
        $params = [
            'id' => $id,
        ];
        $result = false;
        try {
            $response = $this->client->post($url, [
                'headers' => $this->headers,
                'form_params' => $params
            ]);
            //获取body返回值信息
            $body = json_decode($response->getBody(), true);
            if (!empty($body['data']) && $body['data'] == true) {
                $result = true;
            }
        } catch (\Exception $e) {
            $this->error('删除失败:'.$e->getMessage());
        }
        $result ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="重启")
     */
    public function reload($id)
    {
        $result = false;
        try {
            $url = $this->host . '/crontab/reload';
            $params = [
                'id' => $id,
            ];
            $response = $this->client->post($url, [
                'headers' => $this->headers,
                'form_params' => $params
            ]);
            //获取body返回值信息
            $body = json_decode($response->getBody(), true);
            if (!empty($body['data']) && $body['data'] == true) {
                $result = true;
            }
        } catch (\Exception $e) {
            $this->error('操作失败:'.$e->getMessage());
        }
        $result ? $this->success('操作成功') : $this->error('操作失败');
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $result = false;
        $this->checkPostRequest();
        $post = $this->request->post();
        $rule = [
            'id|ID'    => 'require',
            'field|字段' => 'require',
            'value|值'  => 'require',
        ];
        $this->validate($post, $rule);

        try {
            $url = $this->host . '/crontab/modify';
            $response = $this->client->post($url, [
                'headers' => $this->headers,
                'form_params' => $post
            ]);
            //获取body返回值信息
            $body = json_decode($response->getBody(), true);
            if (!empty($body['data']) && $body['data'] == true) {
                $result = true;
            }
        } catch (\Exception $e) {
            $this->error('操作失败:'.$e->getMessage());
        }
        $result ? $this->success('操作成功') : $this->error('操作失败');
    }

    /**
     * ping
     * @return void
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function ping()
    {
        $response = $this->client->request('GET', $this->host.'/crontab/ping', []);
        //获取body返回值信息
        $body = json_decode($response->getBody(),true);
        if(!empty($body['code']) && $body['code'] == 200){
            $this->success($body['msg'], $body);
        }else{
            $this->error($body['msg'], $body);
        }
    }

    /**
     * 定时器池
     * @return mixed|\think\response\Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function pool()
    {
        if ($this->request->isAjax()) {
            $url = $this->host.'/crontab/pool';
            $response = $this->client->get($url);
            //获取body返回值信息
            $body = json_decode($response->getBody(),true);
            if(!empty($body['code']) && $body['code'] == 200){
                $data = [
                    'code'  => 0,
                    'msg'   => $body['msg'],
                    'count' => count($body['data']),
                    'data'  => $body['data'],
                ];
            }else{
                $data = [
                    'code'  => $body['code'],
                    'msg'   => $body['msg'],
                    'count' => 0,
                    'data'  => [],
                ];
            }
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 日志列表
     * filter    否    {"sid":"1"}    检索字段值,查询参数 sid 定时任务id
     * op    否    {"sid":"="}    检索字段操作
     * @return mixed|\think\response\Json
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function flow()
    {
        $id = $this->request->get('id');
        if ($this->request->isAjax()) {
            $page = $this->request->get('page', 1);
            $limit = $this->request->get('limit', 15);
            $url = $this->host.'/crontab/flow?sid='.$id.'&page='.$page.'&limit='.$limit;
            $response = $this->client->get($url);
            //获取body返回值信息
            $body = json_decode($response->getBody(),true);
            if(!empty($body['code']) && $body['code'] == 200){
                $data = [
                    'code'  => 0,
                    'msg'   => $body['msg'],
                    'count' => $body['data']['count'],
                    'data'  => $body['data']['list'],
                ];
            }else{
                $data = [
                    'code'  => $body['code'],
                    'msg'   => $body['msg'],
                    'count' => 0,
                    'data'  => [],
                ];
            }
            return json($data);
        }
        $this->assign('id', $id);
        return $this->fetch();
    }

}


更多栏目
相关内容