Rule.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\validate\RuleCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Rule extends BaseController{
  15. public function index(){
  16. if (request()->isAjax()) {
  17. $rule = Db::name('adminRule')->order('sort asc,id asc')->select();
  18. return to_assign(0, '', $rule);
  19. } else {
  20. return view();
  21. }
  22. }
  23. //添加
  24. public function add()
  25. {
  26. $param = get_params();
  27. if (request()->isAjax()) {
  28. $param['src'] = preg_replace('# #','',$param['src']);
  29. if ($param['id'] > 0) { //编辑
  30. // $field_id = $param['field_id'];
  31. // Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $param['id']]);
  32. try {
  33. validate(RuleCheck::class)->scene('edit')->check($param);
  34. } catch (ValidateException $e) {
  35. // 验证失败 输出错误信息
  36. return to_assign(1, $e->getError());
  37. }
  38. Db::name('AdminRule')->strict(false)->field(true)->update($param);
  39. add_log('edit', $param['id'], $param);
  40. } else { //新增
  41. try {
  42. validate(RuleCheck::class)->scene('add')->check($param);
  43. } catch (ValidateException $e) {
  44. // 验证失败 输出错误信息
  45. return to_assign(1, $e->getError());
  46. }
  47. $param['create_time'] = time();
  48. $rid = Db::name('AdminRule')->strict(false)->field(true)->insertGetId($param);
  49. // $field_id = $param['field_id'];
  50. // Db::name('FieldRules')->where('id', $field_id)->update(['group_id' => $rid]);
  51. //自动为系统所有者管理组分配新增的节点
  52. $group = Db::name('AdminGroup')->find(1);
  53. if (!empty($group)) {
  54. $newGroup['id'] = 1;
  55. $newGroup['rules'] = $group['rules'] . ',' . $rid;
  56. Db::name('AdminGroup')->strict(false)->field(true)->update($newGroup);
  57. add_log('add', $rid, $param);
  58. }
  59. }
  60. // 删除后台节点缓存
  61. clear_cache('adminRules');
  62. return to_assign();
  63. } else {
  64. $id = isset($param['id']) ? $param['id'] : 0;
  65. $pid = isset($param['pid']) ? $param['pid'] : 0;
  66. if($id > 0){
  67. $detail = Db::name('AdminRule')->where('id',$id)->find();
  68. View::assign('detail', $detail);
  69. }
  70. View::assign('id', $id);
  71. View::assign('pid', $pid);
  72. return view();
  73. }
  74. }
  75. //删除
  76. public function delete(){
  77. $id = get_params("id");
  78. $count = Db::name('AdminRule')->where(["pid" => $id])->count();
  79. if ($count > 0) {
  80. return to_assign(1, "该节点下还有子节点,无法删除");
  81. }
  82. if (Db::name('AdminRule')->delete($id) !== false) {
  83. clear_cache('adminRules');
  84. add_log('delete', $id, []);
  85. return to_assign(0, "删除节点成功");
  86. } else {
  87. return to_assign(1, "删除失败");
  88. }
  89. }
  90. }