-1, 'name' => '不选择', ); private $search_default = array ( 'value' => -1, 'name' => '菜单', ); /** * __construct * * @return mixed */ public function __construct() { } public function getManageMenu() { # 联动总数,默认到县区 $level_total = Dever::input('level_total', 3); # 当前联动级别 $level_num = Dever::input('level_num'); # 一般为id $level_id = Dever::input('level_id'); # 是否是搜索列表页 $level_search = Dever::input('level_search'); if ($level_search) { $default = $this->search_default; if ($level_num == 1) { $default['name'] = '分组选择'; } elseif ($level_num == 2) { $default['name'] = '主菜单选择'; } elseif ($level_num == 3) { $default['name'] = '子菜单选择'; } } else { $default = $this->default; } if ($level_num == 1) { $data = Dever::db('manage/menu_group')->getAll(); } elseif ($level_num == 2) { $where = array('group_id' => $level_id, 'menu_id' => -1); $data = Dever::db('manage/menu')->getAll($where); if ($data) { foreach ($data as $k => $v) { $child = Dever::db('manage/menu')->total(array('menu_id' => $v['id'])); if (!$child) { unset($data[$k]); } } } } elseif ($level_num == 3) { if ($level_id == -1) { Dever::alert('error'); } $where = array('menu_id' => $level_id); $data = Dever::db('manage/menu')->getAll($where); } if (!$data) { Dever::alert('error'); } if ($level_search || $level_num > 1) { array_unshift($data, $default); } $result['level_total'] = $level_total; $result['list'] = $data; return $result; } public function updateKey($id, $name, $data) { $key = Dever::param('key', $data); $menu = Dever::param('menu_id', $data); if (!$key || $key == 'null' || strstr($key, 'menu_')) { Dever::config('base')->hook = true; $prefix = ''; if ($menu > 0) { $info = Dever::db('manage/menu')->one($menu); if ($info) { if (!$info['key']) { $info['key'] = $info_update['key'] = 'menu_' . $info['id']; $info_update['where_id'] = $info['id']; Dever::db('manage/menu')->update($info_update); } $prefix .= $info['key'] . '.'; } } $update['key'] = $prefix . 'menu_' . $id; $update['where_id'] = $id; Dever::db('manage/menu')->update($update); } } public function test_api() { $data = Dever::db('manage/menu-in', array('where_id' => '2,1')); print_r(Dever::sql());die; } public function checkKey($id = 0) { $id = Dever::input('where_id'); $key = Dever::input('update_key'); if ($key && !$id) { $info = Dever::db('manage/menu')->key(array('where_key' => $key)); if ($info) { Dever::alert('该菜单唯一key已被使用,请换一个吧'); } } elseif ($id > 0) { $info = Dever::db('manage/menu')->keyId(array('where_key' => $key, 'where_id' => $id)); if ($info) { Dever::alert('该菜单唯一key已被使用,请换一个吧'); } } } # 菜单无限分类 public function getCate() { # 联动总数 $level_total = 1; # 当前联动级别 $level_num = Dever::input('level_num', 1); # 一般为id $level_id = Dever::input('level_id'); # 传入的参数 $level_param = Dever::input('level_param'); if ($level_param) { parse_str($level_param, $param); $where['id'] = $param['id']; } # 是否是搜索列表页 $level_search = Dever::input('level_search'); if ($level_search) { $default = $this->search_default; $default['name'] = $level_num . '级' . $default['name']; } else { $default = $this->default; } $data = array(); # 三级联动 if ($level_num == 1) { $where['menu_id'] = -1; } elseif($level_id > 0) { $where['menu_id'] = $level_id; } if (isset($where['menu_id'])) { $data = Dever::db('manage/menu')->getAll($where); } if ($data || $level_num == 1) { array_unshift($data, $default); } if (!$data) { Dever::alert('error'); } $result['level_total'] = $level_total; $result['list'] = $data; return $result; } /** * menu 从数据库中得到所有菜单,如果为空,则从项目中读取一次 * * @return array */ private function get($auth = '', $state = 2) { $ms = Dever::input('ms'); $param = array(); if ($ms) { $param['option_key'] = $ms; } $menu = Dever::db('manage/menu')->main($param); if ($menu) { $this->fast = array(); $child = Dever::db('manage/menu')->child(); if ($auth && is_array($auth)) { //$auth = implode(',', $auth); } $console = false; if (layadmin()) { $console = true; } foreach ($menu as $k => $v) { if (!$console && $k == 'console') { unset($menu[$k]); continue; } if ($auth && !in_array($v['id'], $auth)) { //unset($menu[$k]); //continue; } /* if ($auth && strpos($auth . '.', $k . '.') === false) { unset($menu[$k]); continue; } */ $path = Dever::load('manage/project.path', $v['key']); if ($path && strpos($path, 'http://') === 0) { unset($menu[$k]); continue; } $menu[$k] = $this->getChild($menu[$k], $child, $v, $auth); if (!isset($menu[$k]['child'])) { unset($menu[$k]); } else { $this->group[$menu[$k]['group_id']] = $menu[$k]['group_id']; } } } elseif (!$ms && $state == 2) { # 数据为空,读取一次,后续就得手动读取了 $this->load(); $menu = $this->get($auth, 1); } return $menu; } public function getChild($menu, $child, $v, $auth, $state = false) { if (isset($child[$v['id']])) { foreach ($child[$v['id']] as $i => $j) { //if ($state == false && $auth && $i && strpos($auth . '.', $i) === false) { if ($state == false && $auth && !in_array($j['id'], $auth)) { //unset($child[$parent][$i]); //continue; } else { if ($j['link'] && $i != 'console.index') { $j['link'] = Dever::url($j['link'] . '&menu_id=' . $j['id']); } $menu['child'][$i] = $j; $menu['child'][$i] = $this->getChild($menu['child'][$i], $child, $j, $auth, true); $menu['child'][$i]['func'] = Dever::db('manage/menu_func')->select(array('menu_id' => $menu['child'][$i]['id'])); if ($j['fast'] == 1) { $this->fast[$i] = $j; } } } } return $menu; } /** * 清空所有菜单 * * @return array */ public function delete() { $data = $menu = Dever::db('manage/menu')->all; foreach ($data as $k => $v) { } Dever::out('yes'); } /** * 更新菜单 * * @return array */ public function update_api() { $this->load(); Dever::out('yes'); } private function getKey($k, $i, $j) { if (is_string($j['menu'])) { $m = $j['menu']; } else { $m = $k; } if ($m == $k) { $key = $k . '.' . $i; } else { $key = $m . '.' . $k . '.' . $i; } return array($m, $key); } /** * 载入从项目中定义好的菜单 * * @return array */ public function load() { $project = array(); $project['console'] = array ( 'name' => '主页', 'lang' => '主页', 'path' => 'console', 'icon' => 'layui-icon-home', 'order' => 10000, 'child' => array ( 'index' => Array ( 'key' => 'console.index', 'name' => '控制台', 'link' => Dever::url('console'), 'order' => 10, 'fast' => 2, ) ) ); $project += Dever::load('manage/project.get'); $database = Dever::load('manage/database'); $top = Dever::load('manage/top'); $auth = Dever::load('manage/auth'); $child = array(); foreach ($project as $k => $v) { $project[$k]['group_id'] = 1; if (!isset($v['url'])) { continue; } if (!isset($v['lang'])) { continue; } if (isset($v['group_name'])) { $group = Dever::db('manage/menu_group')->find(array('name' => $v['group_name'])); if (!$group) { $project[$k]['group_id'] = Dever::db('manage/menu_group')->insert(array('name' => $v['group_name'], 'reorder' => $v['group_order'])); } else { $project[$k]['group_id'] = $group['id']; } } if (isset($v['base']) && isset($v['rel'])) { $v['path'] = DEVER_INCLUDE_PATH . $v['rel']; } $project[$k]['name'] = $v['lang']; $config = $database->config($k, $v['path']); if ($config) { foreach ($config as $i => $j) { //$this->sync('top', 'update', $j, $k, $i, $v, $top); $this->sync('auth', 'update_action', $j, $k, $i, $v, $auth); if (!isset($j['menu'])) { $j['menu'] = true; } if (isset($j['manage']) && $j['menu'] != false) { list($m, $key) = $this->getKey($k, $i, $j); $project[$m]['child'][$i]['key'] = $key; $project[$m]['child'][$i]['name'] = $j['lang']; $project[$m]['child'][$i]['link'] = 'project/database/list?project=' . $k . '&table=' . $i . '&menu=' . $m . '&search_option_state=1'; $project[$m]['child'][$i]['order'] = isset($j['order']) ? $j['order'] : 1; $project[$m]['child'][$i]['fast'] = isset($j['fast']) ? $j['fast'] : 2; if (isset($j['parent'])) { $parent = $j['parent']; $parent['menu'] = $parent['key']; list($parent_menu, $parent_key) = $this->getKey($k, $parent['name'], $parent); $project[$m]['child'][$i]['parent'] = array(); $project[$m]['child'][$i]['parent']['key'] = $parent_key; $project[$m]['child'][$i]['parent']['name'] = $parent['name']; $project[$m]['child'][$i]['parent']['link'] = ''; $project[$m]['child'][$i]['parent']['order'] = isset($parent['order']) ? $parent['order'] : 1; $project[$m]['child'][$i]['parent']['fast'] = isset($parent['fast']) ? $parent['fast'] : 2; } } } } else { unset($project[$k]); } } //print_r($project);die; # 将所有数据都记录到菜单中 if ($project) { $menu = Dever::config('base', 'manage')->menu; if ($menu && layadmin()) { $old = $project; $project = array(); if (!isset($menu['console']) && isset($old['console'])) { $project['console'] = $old['console']; } foreach ($menu as $k => $v) { $project[$k] = $v; $project[$k]['key'] = $v['project']; $project[$k]['lang'] = $v['name']; $project[$k]['path'] = $k; $project[$k]['icon'] = $old[$v['icon']]['icon']; $child = explode(',', $v['project']); foreach ($child as $k1 => $v1) { $project[$k]['child'][$v1] = $old[$v1]; $project[$k]['child'][$v1]['key'] = $v1; $project[$k]['child'][$v1]['link'] = $old[$v1]['url']; $project[$k]['child'][$v1]['fast'] = 2; } } if (!isset($project['manage']) && isset($old['manage'])) { $project['manage'] = $old['manage']; } } foreach ($project as $k => $v) { if (!isset($v['child'])) { unset($project[$k]); continue; } if ($v && isset($v['lang']) && $v['child']) { $id = $this->updateMenu($k, $v['lang'], $v['path'] . ':' . $v['name'], -1, $v['order'], 2, (isset($v['icon']) ? $v['icon'] : ''), $v['group_id']); if ($id > 0) { foreach ($v['child'] as $i => $j) { if (isset($j['child']) && $j['child']) { $parent = $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast'], '', $v['group_id']); foreach ($j['child'] as $i1 => $j1) { $this->updateMenu($j1['key'], $j1['name'], $j1['link'], $parent, $j1['order'], $j1['fast'], '', $v['group_id']); } } elseif (isset($j['parent']) && $j['parent']) { $p = $j['parent']; $parent = $this->updateMenu($p['key'], $p['name'], $p['link'], $id, $p['order'], $p['fast'], '', $v['group_id']); if ($parent) { $this->updateMenu($j['key'], $j['name'], $j['link'], $parent, $j['order'], $j['fast'], '', $v['group_id']); } } else { $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast'], '', $v['group_id']); } } } } } } return $project; } private function sync($key, $update, $data, $k, $i, $project, $model) { if (isset($data[$key]) && is_array($data[$key])) { # 加入头部左侧控制权限的菜单 if ($key == 'top') { $data[$key]['name'] = $project['lang'] . '-' . $data[$key]['name']; } if (isset($data[$key]['type']) && $data[$key]['type'] == 2) { $id = false; } else { $id = $model->{$update}($data[$key]); } if (strpos($data[$key]['data'], '/')) { $parent = Dever::load($data[$key]['data']); } else { $parent = Dever::load($k . '/' . $i . '-' . $data[$key]['data']); } //print_r($data[$key]);die; if ($parent) { if (!$id) { foreach ($parent as $k => $v) { if (isset($v['child']) && $v['child']) { $param = array(); $param['project'] = $project['name']; $param['project_name'] = $project['lang']; $param['name'] = $v[$data[$key]['col']]; if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v[$data[$key]['type']])) { $type = $v[$data[$key]['type']]; $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v['id']; } else { $param['key'] = $data[$key]['key'] . '_' . $v['id']; } $param[$key] = -1; $param['value'] = $v['id']; $pid = $model->{$update}($param); foreach ($v['child'] as $k1 => $v1) { $param = array(); $param['project'] = $project['name']; $param['project_name'] = $project['lang']; if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v1[$data[$key]['type']])) { $type = $v1[$data[$key]['type']]; $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v1['id']; } else { $param['key'] = $data[$key]['key'] . '_' . $v1['id']; } $param['name'] = $v1[$data[$key]['col']]; $param[$key] = $pid; $param['value'] = $v1['id']; $model->{$update}($param); } } } } elseif ($id) { foreach ($parent as $v) { $param = array(); if ($key == 'auth') { $param['project'] = $project['name']; $param['project_name'] = $project['lang']; } if (isset($data[$key]['type']) && $data[$key]['type'] && isset($v[$data[$key]['type']])) { $type = $v[$data[$key]['type']]; $param['key'] = $data[$key]['key'] . '-' . $type . '_' . $v['id']; } else { if (!isset($v['id'])) { print_r($parent);die; } $param['key'] = $data[$key]['key'] . '_' . $v['id']; } $param['name'] = $v[$data[$key]['col']]; $param[$key] = $id; $param['value'] = $v['id']; $model->{$update}($param); } } } } } /** * 更新菜单到数据库 * * @return array */ public function updateMenu($key, $name, $link, $menu, $order, $fast, $icon = '', $group_id = 1) { if (!$key) { return; } //$where['name'] = $name; //$where['menu_id'] = $menu; $where['key'] = $key; $info = Dever::db('manage/menu')->one($where); if (!$info) { $update['add_key'] = $key; $update['add_name'] = $name; $update['add_link'] = $link; $update['add_menu_id'] = $menu; $update['add_menu'] = $menu; $update['add_reorder'] = $order; $update['add_fast'] = $fast; $update['add_state'] = 1; $update['add_type'] = 2; if ($icon) { $update['add_icon'] = $icon; } if ($group_id) { $update['group_id'] = $group_id; } $info['id'] = Dever::db('manage/menu')->insert($update); } /* elseif($link != $info['link']) { $update['set_link'] = $link; $id = $update['where_id'] = $info['id']; Dever::load('manage/menu-update', $update); } */ return $info['id']; } private function getGroup() { $company_id = Dever::load('manage/company')->get(); $where = array(); if ($company_id) { $company = Dever::db('manage/company')->find($company_id); if ($company && $company['is_project'] == 2 && $company['project']) { $where['ids'] = $company['project']; } } if ($this->group) { $group = Dever::db('manage/menu_group')->getIds($where); foreach ($group as $k => $v) { if (!isset($this->group[$k])) { unset($group[$k]); } } return $group; } else { $this->group = Dever::db('manage/menu_group')->getIds($where); } return $this->group; } /** * menu 生成顶部菜单 * * @return array */ public function getTop() { Dever::load('manage/menu.getHtml'); $data = $this->getGroup(); $html = '