123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Request;
- use app\admin\model\SysMenu as SysMenuModel;
- use app\admin\model\SysRole as SysRoleModel;
- class Base extends Controller
- {
- protected $uid;
- protected $username;
- protected $role_id;
- private $sysMenuModel;
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
-
- if (!session('uid') && !session('username') ) {
- $this->error('您还没有登录, 请登录', '/admin/login');
- }
- $this->sysMenuModel = new SysMenuModel();
-
- $this->uid = session('uid');
- $this->username = session('username');
- $this->role_id = session('role_id');
-
-
- if (!$this->checkAuthor($this->role_id)) {
- $this->error('你无权限操作');
- }
-
-
- $this->addLog();
-
- $data_menu = obj_tree($this->getMenu($this->role_id));
-
- $controller = $request->controller();
- $menu_url = strtolower('admin/'.preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $controller)).'/index';
-
- $active_pid = $this->sysMenuModel->where('url', $menu_url)->value('pid');
-
-
-
- $controller = strtolower($controller);
- $action = strtolower($request->action());
-
- $this->assign('data_menu', $data_menu);
- $this->assign('active_pid', $active_pid);
- $this->assign('controller', $controller);
- $this->assign('action', $action);
- }
-
- * 目录获取
- */
- private function getMenu($rid)
- {
- if ($rid === 1) {
- $data = $this->sysMenuModel
- ->where('type!=2')
- ->field('id, pid, name, url, type, icon')
- ->select();
- } else {
- $sysRoleModel = new SysRoleModel();
- $permission_ids = $sysRoleModel->where('id', $rid)->value('permissions');
- $data = $this->sysMenuModel
- ->where('type!=2')
- ->where('id', 'IN', $permission_ids)
- ->field('id, pid, name, url, icon')
- ->select();
- }
- return $data;
- }
-
- * 权限检查
- */
- private function checkAuthor($rid)
- {
- if (!$rid) {
- return false;
- }
- if ($rid==1) {
- return true;
- }
- $c = strtolower(request()->controller());
- $a = strtolower(request()->action());
- if (preg_match('/^public_/', $a)) {
- return true;
- }
- if ($c == 'index' && $a == 'index') {
- return true;
- }
- $permission_ids = $this->getMenu($rid);
- $permissions = $this->sysMenuModel->where('id', 'IN', $permission_ids)->field('id, pid, name, url')->select();
-
- foreach ($permissions as $v) {
- if($v->url=='admin/'.$c.'/'.$a) {
- return true;
- }
- }
- return false;
- }
-
- * 记录日志
- */
- private function addLog()
- {
- $data = array();
- $data['querystring'] = request()->query()?'?'.request()->query():'';
- $data['m'] = request()->module();
- $data['c'] = request()->controller();
- $data['a'] = request()->action();
- $data['method'] = request()->method();
- $data['userid'] = $this->uid;
- $data['username'] = $this->username;
- $data['ip'] = request()->ip();
- $data['time'] = time();
- $arr = [];
-
- $logLevel = 1;
-
- switch ($logLevel) {
- case 2:
- $arr = array_merge($arr, ['SysUser/index','SysRole/index', 'SysMenu/index']);
- case 1:
- $arr = array_merge($arr, ['Category/index','Article/index', 'FileManager/index']);
- default:
- $arr = array_merge($arr, ['Index/index','SysLog/index','SysSet/index']);
- break;
- }
-
-
-
- if (!in_array($data['c'].'/'.$data['a'], $arr)) {
- db('sys_log')->insert($data);
- }
- }
- }
|