Node.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\SystemNode;
  4. use app\admin\service\TriggerService;
  5. use app\common\controller\AdminController;
  6. use app\admin\service\annotation\ControllerAnnotation;
  7. use app\admin\service\annotation\NodeAnnotation;
  8. use app\admin\service\NodeService;
  9. use app\Request;
  10. use think\App;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\response\Json;
  15. #[ControllerAnnotation(title: '系统节点管理')]
  16. class Node extends AdminController
  17. {
  18. public function __construct(App $app)
  19. {
  20. parent::__construct($app);
  21. self::$model = SystemNode::class;
  22. }
  23. #[NodeAnnotation(title: '列表', auth: true)]
  24. public function index(Request $request): Json|string
  25. {
  26. if ($request->isAjax()) {
  27. if (input('selectFields')) {
  28. return $this->selectList();
  29. }
  30. $count = self::$model::count();
  31. $list = self::$model::getNodeTreeList();
  32. $data = [
  33. 'code' => 0,
  34. 'msg' => '',
  35. 'count' => $count,
  36. 'data' => $list,
  37. ];
  38. return json($data);
  39. }
  40. return $this->fetch();
  41. }
  42. #[NodeAnnotation(title: '系统节点更新', auth: true)]
  43. public function refreshNode($force = 0): void
  44. {
  45. $this->checkPostRequest();
  46. $nodeList = (new NodeService())->getNodeList();
  47. empty($nodeList) && $this->error('暂无需要更新的系统节点');
  48. try {
  49. if ($force == 1) {
  50. $updateNodeList = self::$model::whereIn('node', array_column($nodeList, 'node'))->select();
  51. $formatNodeList = array_format_key($nodeList, 'node');
  52. foreach ($updateNodeList as $vo) {
  53. isset($formatNodeList[$vo['node']])
  54. && self::$model::where('id', $vo['id'])->update(
  55. [
  56. 'title' => $formatNodeList[$vo['node']]['title'],
  57. 'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
  58. ]
  59. );
  60. }
  61. }
  62. $existNodeList = self::$model::field('node,title,type,is_auth')->select();
  63. foreach ($nodeList as $key => $vo) {
  64. foreach ($existNodeList as $v) {
  65. if ($vo['node'] == $v->node) {
  66. unset($nodeList[$key]);
  67. break;
  68. }
  69. }
  70. }
  71. if (!empty($nodeList)) {
  72. (new self::$model)->saveAll($nodeList);
  73. TriggerService::updateNode();
  74. }
  75. }catch (\Exception $e) {
  76. $this->error('节点更新失败');
  77. }
  78. $this->success('节点更新成功');
  79. }
  80. #[NodeAnnotation(title: '清除失效节点', auth: true)]
  81. public function clearNode(): void
  82. {
  83. $this->checkPostRequest();
  84. $nodeList = (new NodeService())->getNodeList();
  85. try {
  86. $existNodeList = self::$model::field('id,node,title,type,is_auth')->select()->toArray();
  87. $formatNodeList = array_format_key($nodeList, 'node');
  88. foreach ($existNodeList as $vo) {
  89. !isset($formatNodeList[$vo['node']]) && self::$model::where('id', $vo['id'])->delete();
  90. }
  91. TriggerService::updateNode();
  92. }catch (\Exception $e) {
  93. $this->error('节点更新失败');
  94. }
  95. $this->success('节点更新成功');
  96. }
  97. }