123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace app\admin\server\menu;
- use app\admin\model\SystemMenu;
- use app\admin\model\SystemRole;
- use support\Db;
- use support\Redis;
- class MenuServer
- {
- /**
- * Notes:获取菜单列表
- * @param string $keywords
- * @param int $page
- * @param int $limit
- * @return array
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function getMenuList(string $keywords)
- {
- if (!empty($keywords)){
- $list = SystemMenu::getMenuList($keywords);
- }else{
- $list = SystemMenu::getMenus(0);
- if (!empty($list)){
- for ($i=0;$i<count($list);$i++){
- // if ($list[$i]['menu_is_show'] == 1){
- // $list[$i]['menu_is_show'] = true;
- // }else{
- // $list[$i]['menu_is_show'] = false;
- // }
- $list[$i]['children'] = SystemMenu::getMenus($list[$i]['menu_id']);
- if (!empty($list[$i]['children'])){
- for ($s=0;$s<count($list[$i]['children']);$s++){
- // if ($list[$i]['children'][$s]['menu_is_show'] == 1){
- // $list[$i]['children'][$s]['menu_is_show'] = true;
- // }else{
- // $list[$i]['children'][$s]['menu_is_show'] = false;
- // }
- $list[$i]['children'][$s]['children'] = SystemMenu::getMenus($list[$i]['children'][$s]['menu_id']);
- // if (!empty($list[$i]['children'][$s]['children'])){
- // for ($j=0;$j<count($list[$i]['children'][$s]['children']);$j++){
- // if ($list[$i]['children'][$s]['children'][$j]['menu_is_show'] == 1){
- // $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = true;
- // }else{
- // $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = false;
- // }
- // }
- // }
- }
- }
- }
- }
- }
- return $list;
- }
- /**
- * Notes:修改角色
- * @param string $menu_name
- * @param int $menu_id
- * @param int $menu_pid
- * @return int
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function updateMenu($menu_id, $menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
- {
- SystemMenu::affairBegin();
- try {
- $where = [];
- $where['menu_id'] = $menu_id;
- $data = [];
- $data['menu_name'] = $menu_name;
- $data['level'] = 1;
- if (!empty($menu_pid)){
- $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
- }
- $data['menu_pid'] = $menu_pid;
- $data['menu_path'] = $menu_path;
- $data['menu_icon'] = $menu_icon;
- $data['menu_route'] = $menu_route;
- $data['menu_params'] = $menu_params;
- $data['menu_sort'] = $menu_sort;
- $data['menu_is_menu'] = $menu_is_menu;
- $data['menu_is_show'] = $menu_is_show;
- $result = SystemMenu::where($where)->update($data);
- if ($result !== false){
- SystemMenu::affairCommit();
- return true;
- }
- throw new \Exception('操作失败!');
- }catch (\Exception $exception){
- SystemMenu::affairRollback();
- throw new \Exception($exception->getMessage(), 500);
- }
- }
- /**
- * Notes:删除角色
- * @param int $menu_id
- * @return int
- * User: ZQ
- * Date: 2022/9/3
- */
- public static function delMenu($menu_id)
- {
- SystemMenu::affairBegin();
- try {
- $where = [];
- $where['menu_id'] = $menu_id;
- $data['menu_status'] = 2;
- $result = SystemMenu::where($where)->update($data);
- if (!empty($result)){
- SystemMenu::affairCommit();
- return true;
- }else{
- return false;
- }
- }catch (\Exception $exception){
- SystemMenu::affairRollback();
- throw new \Exception($exception->getMessage(), 500);
- }
- }
- /**
- * Notes: 添加菜单
- * @param string $menu_name
- * @param int $menu_pid
- * @return int
- * User: ZQ
- * Date: 2022/9/14
- */
- public static function insertMenu($menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
- {
- SystemMenu::affairBegin();
- try {
- $data = [];
- $data['menu_name'] = $menu_name;
- $data['level'] = 1;
- if (!empty($menu_pid)){
- $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
- }
- $data['menu_pid'] = $menu_pid;
- $data['menu_path'] = $menu_path;
- $data['menu_icon'] = $menu_icon;
- $data['menu_route'] = $menu_route;
- $data['menu_params'] = $menu_params;
- $data['menu_sort'] = $menu_sort;
- $data['menu_is_menu'] = $menu_is_menu;
- $data['menu_is_show'] = $menu_is_show;
- $result = SystemMenu::insertGetId($data);
- if (!empty($result)){
- SystemMenu::affairCommit();
- return $result;
- }
- throw new \Exception('操作失败!');
- }catch (\Exception $exception){
- SystemMenu::affairRollback();
- throw new \Exception($exception->getMessage(), 500);
- }
- }
- /**
- * Notes:查询角色
- * @param int $menu_id
- * @return int
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function menuInfo($menu_id)
- {
- $SystemMenu = new SystemMenu();
- $where = [];
- $where['menu_id'] = $menu_id;
- $result = SystemMenu::where($where)->first();
- $result['menu_is_menu'] = (int)$result['menu_is_menu'];
- $result['menu_pid_arr'] = $SystemMenu->getParentMenuId($result['menu_pid']);
- // $result['menu_is_show'] = ($result['menu_is_show'] == 1 ? true : false);
- return $result;
- }
- /**
- * Notes:获取菜单列表
- * @param string $keywords
- * @param int $page
- * @param int $limit
- * @return array
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function menuLevel()
- {
- $list = SystemMenu::getLevel(0);
- if (!empty($list)){
- for ($i=0;$i<count($list);$i++){
- $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
- if (!empty($list[$i]['children'])){
- for ($s=0;$s<count($list[$i]['children']);$s++){
- $list[$i]['children'][$s]['children'] = SystemMenu::getLevel($list[$i]['children'][$s]['value']);
- }
- }
- }
- }
- return $list;
- }
- /**
- * Notes:获取菜单列表
- * @param string $keywords
- * @param int $page
- * @param int $limit
- * @return array
- * User: ZQ
- * Date: 2022/9/13
- */
- public static function secondLevel()
- {
- $list = SystemMenu::getLevel(0);
- if (!empty($list)){
- for ($i=0;$i<count($list);$i++){
- $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
- }
- }
- return $list;
- }
- /**
- * Notes:修改角色
- * @param int $menu_is_show
- * @param int $menu_id
- * @return int
- * User: ZQ
- * Date: 2022/9/3
- */
- public static function updateStatus($menu_id, $menu_is_show)
- {
- SystemMenu::affairBegin();
- try {
- $where = [];
- $where['menu_id'] = $menu_id;
- $data = [];
- $data['menu_is_show'] = $menu_is_show;
- $result = SystemMenu::where($where)->update($data);
- if ($result !== false){
- SystemMenu::affairCommit();
- return true;
- }
- throw new \Exception('操作失败!');
- }catch (\Exception $exception){
- SystemMenu::affairRollback();
- throw new \Exception($exception->getMessage(), 500);
- }
- }
- /**
- * Notes:菜单列表
- * @param array $adminInfo
- * @return array
- * User: yym
- * Date: 2022/9/21
- */
- public static function getHomeMenuList(array $adminInfo)
- {
- // $redis_key = 'roles_' . $adminInfo['admin_roles'];
- // if(Redis::exists($redis_key))
- // {
- // $list = json_decode(Redis::get($redis_key), true);
- // }else{
- $menu_list_ids = array();
- if($adminInfo['admin_is_boos'] != 1)
- {
- $rules = SystemRole::getRuleList(explode(',', $adminInfo['admin_roles']));
- if(!empty($rules))
- {
- foreach ($rules as $row)
- {
- $menu_list_ids = empty($menu_list_ids) ? explode(',', $row['role_rules']) : array_merge($menu_list_ids, explode(',', $row['role_rules']));
- }
- }else{
- $menu_list_ids = array(0);
- }
- }
- if(!empty($menu_list_ids))
- {
- $SystemMenu = new SystemMenu();
- //获取每个自己的父级
- foreach ($menu_list_ids as $item)
- {
- if($item <= 0){
- continue;
- }
- $menu_list_ids = array_merge($menu_list_ids, $SystemMenu->getParentPid($item));
- }
- $menu_list_ids = array_unique($menu_list_ids);
- }
- $list = SystemMenu::getHomeMenuList($menu_list_ids);
- // Redis::setEx($redis_key, 86400, json_encode($list));
- // }
- return $list;
- }
- }
|