Node.php 2.6 KB

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