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