Module.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-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\ModuleCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Module extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $module = Db::name('AdminModule')->select();
  20. return to_assign(0, '', $module);
  21. } else {
  22. return view();
  23. }
  24. }
  25. //添加新增/编辑
  26. public function add()
  27. {
  28. $param = get_params();
  29. if (request()->isAjax()) {
  30. $param['name'] = preg_replace('# #', '', $param['name']);
  31. if ($param['id'] > 0) {
  32. $module = Db::name('AdminModule')->where('id', $param['id'])->find();
  33. if ($module['type'] == 1) {
  34. return to_assign(1, '系统默认模块不能编辑');
  35. }
  36. try {
  37. validate(ModuleCheck::class)->scene('edit')->check($param);
  38. } catch (ValidateException $e) {
  39. // 验证失败 输出错误信息
  40. return to_assign(1, $e->getError());
  41. }
  42. $param['update_time'] = time();
  43. Db::name('AdminModule')->strict(false)->field(true)->update($param);
  44. add_log('edit', $param['id'], $param);
  45. } else {
  46. try {
  47. validate(ModuleCheck::class)->scene('add')->check($param);
  48. } catch (ValidateException $e) {
  49. // 验证失败 输出错误信息
  50. return to_assign(1, $e->getError());
  51. }
  52. $param['create_time'] = time();
  53. $mid = Db::name('AdminModule')->strict(false)->field(true)->insertGetId($param);
  54. add_log('add', $mid, $param);
  55. }
  56. return to_assign();
  57. } else {
  58. $id = isset($param['id']) ? $param['id'] : 0;
  59. if ($id > 0) {
  60. $detail = Db::name('AdminModule')->where('id', $id)->find();
  61. View::assign('detail', $detail);
  62. }
  63. View::assign('id', $id);
  64. return view();
  65. }
  66. }
  67. //禁用/启用
  68. public function disable()
  69. {
  70. $param = get_params();
  71. $module = Db::name('AdminModule')->where('id', $param['id'])->find();
  72. if ($module['type'] == 1) {
  73. return to_assign(1, '系统模块不能禁用');
  74. }
  75. $param['update_time'] = time();
  76. $res = Db::name('AdminModule')->strict(false)->field('status,update_time')->update($param);
  77. if ($res !== false) {
  78. Db::name('AdminRule')->strict(false)->where('module', $module['name'])->field('status')->update(['status' => $param['status']]);
  79. // 删除后台节点缓存
  80. clear_cache('adminRules');
  81. if ($param['status'] == 0) {
  82. add_log('disable', $param['id'], $param);
  83. } else if ($param['status'] == 1) {
  84. add_log('recovery', $param['id'], $param);
  85. }
  86. return to_assign();
  87. } else {
  88. return to_assign(1, '操作失败');
  89. }
  90. }
  91. }