init
This commit is contained in:
255
app/admin/controller/BaseController.php
Executable file
255
app/admin/controller/BaseController.php
Executable file
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use think\Lang;
|
||||
use think\Loader;
|
||||
use think\Config;
|
||||
use think\Session;
|
||||
use app\common\controller\BaseController as Base;
|
||||
|
||||
class BaseController extends Base {
|
||||
|
||||
//当前用户
|
||||
protected $user_id;
|
||||
//当前是否管理员 0:否 1:是
|
||||
protected $administrator = 0;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
// 初始化
|
||||
protected function _initialize() {
|
||||
parent::_initialize();
|
||||
$this->user_id = is_session_login('user');
|
||||
|
||||
if ($this->user_id <= 0) {
|
||||
abort(redirect(url('/signin'))->remember());
|
||||
}
|
||||
$this->administrator = is_administrator();
|
||||
$role_id = Session::get('user_auth.role_id');
|
||||
$base_role = Loader::model('UserRole')->getRow(['id' => $role_id]);
|
||||
$menu_list = $this->getMenuList($base_role['yesorno'], $base_role['rbac_acl'], $this->module, $this->controller, $this->action);
|
||||
|
||||
//echo "<pre>=="; print_r($menu_list); die;
|
||||
$this->view->assign('menu_list', $menu_list);
|
||||
$this->view->assign('user_id', $this->user_id);
|
||||
$this->view->assign('administrator', $this->administrator);
|
||||
$this->adminlang = $this->request->langset();
|
||||
$this->view->assign('adminlang', $this->adminlang);
|
||||
$this->country_code = session('cit') != '' ? strtoupper(session('cit')) : 'ZH';
|
||||
|
||||
|
||||
$product_country_code = "products_".strtolower($this->country_code)."_color";
|
||||
$productColor = config($product_country_code);
|
||||
$this->view->assign('productColor', $productColor);
|
||||
|
||||
|
||||
$this->view->assign('country_code', $this->country_code);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function getMenuList($yesorno, $act_list, $module, $controller, $action) {
|
||||
//无需验证的操作
|
||||
$uncheck = array('login', 'logout', 'register');
|
||||
if (strpos($controller, 'ajax') !== false || in_array($action, $uncheck)) {
|
||||
//所有ajax请求不需要验证权限
|
||||
return '';
|
||||
}
|
||||
//$menu_list = $this->cacheGet('menu_list');
|
||||
// $menu_list = [];
|
||||
if (empty($menu_list)) {
|
||||
$menu_list = Loader::model('Dept')->getMenu(['pid' => 0, 'stat' => 0,], ['sort' => 'asc', 'id' => 'desc']);
|
||||
$this->cacheTag('DeptTag')->set('menu_list', $menu_list);
|
||||
}
|
||||
|
||||
foreach($menu_list as $kn => $items) {
|
||||
$menu_list[$kn]['ctrls'] = array();
|
||||
if(sizeof($items)){
|
||||
$ctrls = array();
|
||||
foreach($items['child'] as $kd => $item) {
|
||||
$ctrls[] = strtolower($item['ctrl']);
|
||||
}
|
||||
$menu_list[$kn]['ctrls'] = array_unique($ctrls);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//echo "<pre>++++++++++"; print_r($menu_list); die;
|
||||
if ($act_list == 'all') {
|
||||
if ($this->request->isAjax())
|
||||
return '';
|
||||
$menu = '<ul class="sidebar-menu" data-widget="tree">' . "\n";
|
||||
$menu .=$this->getAllHtmlMenuList($menu_list, $menu_list, $module, $controller);
|
||||
$menu .='</ul>' . "\n";
|
||||
} else {
|
||||
$access = $this->cacheGet($act_list);
|
||||
// $access = [];
|
||||
if (empty($access)) {
|
||||
$access = Loader::model('AuthAccess')->getList(['id' => [$yesorno ? 'in' : 'not in', $act_list], 'module' => $module], null, ['access']);
|
||||
$this->cacheTag('AuthAccessTag')->set($act_list, $access);
|
||||
}
|
||||
$role_right = '';
|
||||
// $permission = [];
|
||||
foreach ($access as $val) {
|
||||
$role_right .= $val['access'] . ',';
|
||||
}
|
||||
|
||||
$permission = explode(',', trim($role_right, ','));
|
||||
$permission[] = 'Index@index';
|
||||
|
||||
//检查是否拥有此操作权限
|
||||
|
||||
|
||||
if (!in_array($controller . '@' . $action, $permission)) {
|
||||
$this->error(_lang_('no permission to operate') . '[' . ($controller . '@' . $action) . ']', url('/admin/Index/index'));
|
||||
}
|
||||
if ($this->request->isAjax())
|
||||
return '';
|
||||
|
||||
//$this->getForMenuList($menu_list, $permission, $module);
|
||||
$menu = '<ul class="sidebar-menu" data-widget="tree">' . "\n";
|
||||
$menu .=$this->getHtmlMenuList($menu_list, $permission, $module, $controller);
|
||||
$menu .='</ul>' . "\n";
|
||||
}
|
||||
return $menu;
|
||||
}
|
||||
|
||||
protected function getHtmlMenuList($list, $permission, $module, $controller) {
|
||||
// tiaoshi($permission);
|
||||
// tiaoshi($module);
|
||||
// tiaoshi($list[2]);die;
|
||||
//
|
||||
|
||||
$menu = '';
|
||||
if(is_array($list) && sizeof($list)) {
|
||||
foreach ($list as $k1 => $list1) {
|
||||
// tiaoshi($list[$k1]['child']);die;
|
||||
switch ($list1['functype']) {
|
||||
case 0:
|
||||
if (!in_array($list1['url'], $permission)) {
|
||||
continue;
|
||||
}
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($list1['haschild'] && !empty($list1['child'])) {
|
||||
$active = in_array(strtolower($controller), $list1['ctrls']) ? "active" : "";
|
||||
$menu .= '<li class="treeview '.$active.'">
|
||||
<a href="#">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>' . "\n";
|
||||
$menu .= '<ul class="treeview-menu">';
|
||||
$menu .= $this->getHtmlMenuList($list[$k1]['child'], $permission, $module, $controller);
|
||||
|
||||
$menu .= '</ul>' . "\n" . '</li>' . "\n";
|
||||
} else {
|
||||
$menu .= '<li>
|
||||
<a href="#">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><small class="label pull-right bg-green">temp</small></span>
|
||||
</a>
|
||||
</li>' . "\n";
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (!in_array($list1['ctrl'] . '@' . $list1['action'], $permission)) {
|
||||
continue;
|
||||
}
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
$menu .= '<li data-ctrl="' . $list1['ctrl'] . '" data-action="' . $list1['action'] . '">
|
||||
<a href="' . url($list1['url']) . '">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>
|
||||
</li>' . "\n";
|
||||
break;
|
||||
case 2:
|
||||
if (!in_array($list1['url'], $permission)) {
|
||||
continue;
|
||||
}
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
$menu .= '<li>
|
||||
<a href="' . $list1['url'] . '">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>
|
||||
</li>';
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $menu;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function getAllHtmlMenuList(&$list, &$permission, $module, $controller) {
|
||||
$menu = '';
|
||||
foreach ($list as $k1 => $list1) {
|
||||
switch ($list1['functype']) {
|
||||
case 0:
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
if ($list1['haschild'] && !empty($list1['child'])) {
|
||||
$active = in_array(strtolower($controller), $list1['ctrls']) ? "active" : "";
|
||||
$menu .= '<li class="treeview '.$active.'">
|
||||
<a href="#">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>' . "\n";
|
||||
$menu .= '<ul class="treeview-menu">';
|
||||
$menu .=$this->getAllHtmlMenuList($list[$k1]['child'], $permission, $module, $controller);
|
||||
$menu .= '</ul>' . "\n" . '</li>' . "\n";
|
||||
} else {
|
||||
$menu .= '<li>
|
||||
<a href="#">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><small class="label pull-right bg-green">temp</small></span>
|
||||
</a>
|
||||
</li>' . "\n";
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
$menu .= '<li data-ctrl="' . $list1['ctrl'] . '" data-action="' . $list1['action'] . '">
|
||||
<a href="' . url($list1['url']) . '">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>
|
||||
</li>' . "\n";
|
||||
break;
|
||||
case 2:
|
||||
if ($list1['hidden']) {
|
||||
continue;
|
||||
}
|
||||
$menu .= '<li>
|
||||
<a href="' . $list1['url'] . '">
|
||||
<i class="' . $list1['icon'] . '"></i> <span>' . $list1['name'] . '</span>
|
||||
<span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span>
|
||||
</a>
|
||||
</li>';
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $menu;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user