AdminBaseController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\admin;
  3. use app\BaseController;
  4. use app\common\model\AdminModel;
  5. use app\common\model\MenuModel;
  6. use think\facade\Session;
  7. use think\facade\View;
  8. /**
  9. * 控制器基础类
  10. */
  11. class AdminBaseController extends BaseController
  12. {
  13. // 初始化
  14. protected function initialize()
  15. {
  16. // 验证登陆信息
  17. $access_admin = session('access_admin');
  18. if (!$access_admin) {
  19. $this->adminReturn('请重新登录');
  20. }
  21. // 验证权限
  22. if ($access_admin['role'] != AdminModel::ROLE_SUPER && $this->request->controller() != 'Home') {
  23. $url = mb_strtolower($this->request->controller() . '/' . $this->request->action());
  24. $check = MenuModel::where('urlstr', $url)->find();
  25. if (!empty($check)) {
  26. $url_ids = explode(',',$access_admin['powerids']);
  27. if (!in_array($check['id'],$url_ids)) {
  28. $this->adminReturn('您没有访问权限');
  29. }
  30. if ($check['status'] == MenuModel::STATUS_DISABLE) {
  31. $this->adminReturn('您没有访问权限');
  32. }
  33. }
  34. }
  35. $lay = mb_strtolower($this->request->controller()) . '-' . $this->request->action();
  36. $lay_table = "lay-{$lay}-table";
  37. $lay_btn = "lay-{$lay}-btn";
  38. View::assign([
  39. 'lay_table' => $lay_table,
  40. 'lay_btn' => $lay_btn,
  41. ]);
  42. }
  43. /**
  44. * 返回
  45. */
  46. protected function adminReturn($msg)
  47. {
  48. if ($this->request->isAjax()) {
  49. ajax_return(1001, $msg);
  50. } else {
  51. $content = '<script language="javascript" type="text/javascript"> top.location.href="/admin/login/index"; </script>';
  52. $response = \think\Response::create($content);
  53. throw new \think\exception\HttpResponseException($response);
  54. }
  55. }
  56. /**
  57. * 等于条件
  58. */
  59. protected function dealEqualInput($result, $equal_array)
  60. {
  61. foreach ($equal_array as $k => $v) {
  62. if (!is_numeric($k)) {
  63. $value = input($k);
  64. } else {
  65. $value = input($v);
  66. }
  67. if (!empty($value)) {
  68. $result[] = [$v, '=', $value];
  69. }
  70. }
  71. return $result;
  72. }
  73. /**
  74. * like条件
  75. */
  76. protected function dealLikeInput($result, $like_array)
  77. {
  78. foreach ($like_array as $k => $v) {
  79. if (!is_numeric($k)) {
  80. $value = input($k);
  81. } else {
  82. $value = input($v);
  83. }
  84. if (!empty($value)) {
  85. $result[] = [$v, 'like', "%{$value}%"];
  86. }
  87. }
  88. return $result;
  89. }
  90. }