Node.php 2.63 KB
Newer Older
zazaname's avatar
zazaname committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目:https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\common\command;


use app\admin\model\SystemNode;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use app\admin\service\NodeService;

class Node extends Command
{
    protected function configure()
    {
        $this->setName('node')
            ->addOption('force', null, Option::VALUE_REQUIRED, '是否强制刷新', 0)
            ->setDescription('系统节点刷新服务');
    }

    protected function execute(Input $input, Output $output)
    {
        $force = $input->getOption('force');
        $output->writeln("========正在刷新节点服务:=====" . date('Y-m-d H:i:s'));
        $check = $this->refresh($force);
        $check !== true && $output->writeln("节点刷新失败:" . $check);
        $output->writeln("刷新完成:" . date('Y-m-d H:i:s'));
    }

    protected function refresh($force)
    {
        $nodeList = (new NodeService())->getNodelist();
        if (empty($nodeList)) {
            return true;
        }
        $model = new SystemNode();
        try {
            if ($force == 1) {
                $updateNodeList = $model->whereIn('node', array_column($nodeList, 'node'))->select();
                $formatNodeList = array_format_key($nodeList, 'node');
                foreach ($updateNodeList as $vo) {
                    isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->update([
                        'title'   => $formatNodeList[$vo['node']]['title'],
                        'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
                    ]);
                }
            }
            $existNodeList = $model->field('node,title,type,is_auth')->select();
            foreach ($nodeList as $key => $vo) {
                foreach ($existNodeList as $v) {
                    if ($vo['node'] == $v->node) {
                        unset($nodeList[$key]);
                        break;
                    }
                }
            }
            $model->insertAll($nodeList);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }

}