Node.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\admin\controller\system;
  12. use app\common\model\SystemNode;
  13. use app\admin\service\TriggerService;
  14. use app\common\controller\AdminController;
  15. use EasyAdmin\annotation\ControllerAnnotation;
  16. use EasyAdmin\annotation\NodeAnotation;
  17. use app\admin\service\NodeService;
  18. use think\App;
  19. /**
  20. * @ControllerAnnotation(title="系统节点管理")
  21. * Class Node
  22. * @package app\admin\controller\system
  23. */
  24. class Node extends AdminController
  25. {
  26. use \app\admin\traits\Curd;
  27. public function __construct(App $app)
  28. {
  29. parent::__construct($app);
  30. $this->model = new SystemNode();
  31. }
  32. /**
  33. * @NodeAnotation(title="列表")
  34. */
  35. public function index()
  36. {
  37. if ($this->request->isAjax()) {
  38. if (input('selectFields')) {
  39. return $this->selectList();
  40. }
  41. $count = $this->model
  42. ->count();
  43. $list = $this->model
  44. ->getNodeTreeList();
  45. $data = [
  46. 'code' => 0,
  47. 'msg' => '',
  48. 'count' => $count,
  49. 'data' => $list,
  50. ];
  51. return json($data);
  52. }
  53. return $this->fetch();
  54. }
  55. /**
  56. * @NodeAnotation(title="系统节点更新")
  57. */
  58. public function refreshNode($force = 0)
  59. {
  60. $this->checkPostRequest();
  61. $nodeList = (new NodeService())->getNodelist();
  62. empty($nodeList) && $this->error('暂无需要更新的系统节点');
  63. $model = new SystemNode();
  64. try {
  65. if ($force == 1) {
  66. $updateNodeList = $model->whereIn('node', array_column($nodeList, 'node'))->select();
  67. $formatNodeList = array_format_key($nodeList, 'node');
  68. foreach ($updateNodeList as $vo) {
  69. isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->update([
  70. 'title' => $formatNodeList[$vo['node']]['title'],
  71. 'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
  72. ]);
  73. }
  74. }
  75. $existNodeList = $model->field('node,title,type,is_auth')->select();
  76. foreach ($nodeList as $key => $vo) {
  77. foreach ($existNodeList as $v) {
  78. if ($vo['node'] == $v->node) {
  79. unset($nodeList[$key]);
  80. break;
  81. }
  82. }
  83. }
  84. $model->saveAll($nodeList);
  85. TriggerService::updateNode();
  86. } catch (\Exception $e) {
  87. $this->error('节点更新失败');
  88. }
  89. $this->success('节点更新成功');
  90. }
  91. /**
  92. * @NodeAnotation(title="清除失效节点")
  93. */
  94. public function clearNode()
  95. {
  96. $this->checkPostRequest();
  97. $nodeList = (new NodeService())->getNodelist();
  98. $model = new SystemNode();
  99. try {
  100. $existNodeList = $model->field('id,node,title,type,is_auth')->select()->toArray();
  101. $formatNodeList = array_format_key($nodeList, 'node');
  102. foreach ($existNodeList as $vo) {
  103. !isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->delete();
  104. }
  105. TriggerService::updateNode();
  106. } catch (\Exception $e) {
  107. $this->error('节点更新失败');
  108. }
  109. $this->success('节点更新成功');
  110. }
  111. }