赛亿官网

SysMenu.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\admin\controller;
  3. use think\controller;
  4. use think\Request;
  5. use app\admin\model\SysMenu as SysMenuModel;
  6. class SysMenu extends Base
  7. {
  8. protected $model;
  9. public function __construct(Request $request = null)
  10. {
  11. parent::__construct($request);
  12. $this->model = new SysMenuModel();
  13. }
  14. public function index()
  15. {
  16. $data = $this->model->select();
  17. $data = obj_tree($data);
  18. $this->assign("data", $data);
  19. $this->assign("type", ['目录', '菜单', '按钮']);
  20. return $this->fetch();
  21. }
  22. public function save(Request $request = null, $id = 0)
  23. {
  24. if ($request->isPost()) {
  25. $param = $request->param();
  26. if ($param['name'] == '') {
  27. $this->error("目录名不能为空");
  28. }
  29. try {
  30. if ($param['id'] != 0) {
  31. $this->model->save([
  32. 'pid' => $param['pid'],
  33. 'name' => $param['name'],
  34. 'url' => $param['url'],
  35. 'type' => $param['type'],
  36. 'icon' => $param['icon']
  37. ], ['id' => $param['id']]);
  38. } else {
  39. $this->model->save([
  40. 'pid' => $param['pid'],
  41. 'name' => $param['name'],
  42. 'url' => $param['url'],
  43. 'type' => $param['type'],
  44. 'icon' => $param['icon']
  45. ]);
  46. }
  47. } catch (\Exception $e) {
  48. $msg = $e->getMessage();
  49. $this->error("错误提示:".$msg);
  50. }
  51. $this->success('操作成功', 'admin/sys_menu/index');
  52. } else {
  53. if ($id != 0) {
  54. $data = $this->model->find($id);
  55. } else {
  56. $data = [
  57. 'id' => 0,
  58. 'pid' => 0,
  59. 'name' => '',
  60. 'url' => '',
  61. 'perms' => '',
  62. 'type' => 0,
  63. 'icon' =>''
  64. ];
  65. }
  66. $data_permission = $this->model->field('id, pid, name')->where('type', '<>', 2)->select();
  67. $data_permission = obj_tree($data_permission);
  68. $this->assign('data', $data);
  69. $this->assign('data_permission', $data_permission);
  70. return $this->fetch();
  71. }
  72. }
  73. public function delete($id = null)
  74. {
  75. if (Request::instance()->isAjax()) {
  76. if ($this->model->where('pid', 'in', $id)->value('id')) {
  77. return ['code'=>0, 'msg'=>'此权限子权限不为空, 若要删除请先清空子权限'];
  78. }
  79. if ($this->model->destroy($id)) {
  80. return ['code' => 1,'msg'=>"删除成功"];
  81. } else {
  82. return ['code' => 0,'msg'=>"删除失败"];
  83. }
  84. }
  85. }
  86. }