Node.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\command;
  3. use app\admin\model\SystemNode;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Option;
  7. use think\console\Output;
  8. use app\admin\service\NodeService;
  9. class Node extends Command
  10. {
  11. protected function configure()
  12. {
  13. $this->setName('node')
  14. ->addOption('force', null, Option::VALUE_REQUIRED, '是否强制刷新', 0)
  15. ->setDescription('系统节点刷新服务');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. $force = $input->getOption('force');
  20. $output->writeln("========正在刷新节点服务:=====" . date('Y-m-d H:i:s'));
  21. $check = $this->refresh($force);
  22. $check !== true && $output->writeln("节点刷新失败:" . $check);
  23. $output->writeln("刷新完成:" . date('Y-m-d H:i:s'));
  24. }
  25. protected function refresh($force)
  26. {
  27. $nodeList = (new NodeService())->getNodeList();
  28. if (empty($nodeList)) {
  29. return true;
  30. }
  31. $model = new SystemNode();
  32. try {
  33. if ($force == 1) {
  34. $updateNodeList = $model->whereIn('node', array_column($nodeList, 'node'))->select();
  35. $formatNodeList = array_format_key($nodeList, 'node');
  36. foreach ($updateNodeList as $vo) {
  37. isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->update([
  38. 'title' => $formatNodeList[$vo['node']]['title'],
  39. 'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
  40. ]);
  41. }
  42. }
  43. $existNodeList = $model->field('node,title,type,is_auth')->select();
  44. foreach ($nodeList as $key => $vo) {
  45. foreach ($existNodeList as $v) {
  46. if ($vo['node'] == $v->node) {
  47. unset($nodeList[$key]);
  48. break;
  49. }
  50. }
  51. }
  52. $model->insertAll($nodeList);
  53. } catch (\Exception $e) {
  54. return $e->getMessage();
  55. }
  56. return true;
  57. }
  58. }