where('is_top', 1)->column('menu_id'); $where[] = ['id', 'in', $roleMenu]; } else { $where[] = ['pid', '=', 0]; } $menu = SystemMenu::where($where) ->order(['sort' => 'desc', 'id' => 'asc']) ->select(); return linear_to_tree($menu, 'children'); } public static function getModelList($id){ $where = []; $where[] = ['is_disable', '=', 0]; $where[] = ['id', '=', $id]; $model_name = SystemMenu::where($where)->value('paths'); if($model_name){ unset($where[1]); $where[] = ['type', 'in', ['M', 'C']]; $where[] = ['model_name', '=', $model_name]; $menu = SystemMenu::where($where) ->order(['sort' => 'desc', 'id' => 'asc']) ->select(); return linear_to_tree($menu, 'children','id','pid',$id); } return []; } /** * @notes 添加菜单 * @param array $params * @return SystemMenu|\think\Model * @author 段誉 * @date 2022/6/30 10:06 */ public static function add(array $params) { return SystemMenu::create([ 'pid' => $params['pid'], 'type' => $params['type'], 'name' => $params['name'], 'icon' => $params['icon'] ?? '', 'sort' => $params['sort'], 'perms' => $params['perms'] ?? '', 'paths' => $params['paths'] ?? '', 'component' => $params['component'] ?? '', 'selected' => $params['selected'] ?? '', 'params' => $params['params'] ?? '', 'is_cache' => $params['is_cache'], 'is_show' => $params['is_show'], 'is_disable' => $params['is_disable'], ]); } /** * @notes 编辑菜单 * @param array $params * @return SystemMenu * @author 段誉 * @date 2022/6/30 10:07 */ public static function edit(array $params) { $is_pid = true; $pid=$params['pid']; $model_name=''; if ($pid > 0) { while ($is_pid==true) { $find= SystemMenu::where('id', $pid)->field('id,pid,paths')->find(); if($find['pid']==0){ $is_pid=false; $model_name=$find['paths']; }else{ $pid=$find['pid']; } } } return SystemMenu::update([ 'id' => $params['id'], 'pid' => $params['pid'], 'model_name' => $model_name, 'type' => $params['type'], 'name' => $params['name'], 'icon' => $params['icon'] ?? '', 'sort' => $params['sort'], 'perms' => $params['perms'] ?? '', 'paths' => $params['paths'] ?? '', 'component' => $params['component'] ?? '', 'selected' => $params['selected'] ?? '', 'params' => $params['params'] ?? '', 'is_cache' => $params['is_cache'], 'is_show' => $params['is_show'], 'is_disable' => $params['is_disable'], ]); } /** * @notes 详情 * @param $params * @return array * @author 段誉 * @date 2022/6/30 9:54 */ public static function detail($params) { return SystemMenu::findOrEmpty($params['id'])->toArray(); } /** * @notes 删除菜单 * @param $params * @author 段誉 * @date 2022/6/30 9:47 */ public static function delete($params) { // 删除菜单 SystemMenu::destroy($params['id']); // 删除角色-菜单表中 与该菜单关联的记录 SystemRoleMenu::where(['menu_id' => $params['id']])->delete(); } /** * @notes 更新状态 * @param array $params * @return SystemMenu * @author 段誉 * @date 2022/7/6 17:02 */ public static function updateStatus(array $params) { return SystemMenu::update([ 'id' => $params['id'], 'is_disable' => $params['is_disable'] ]); } /** * @notes 全部数据 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author 段誉 * @date 2022/10/13 11:03 */ public static function getAllData() { $data = SystemMenu::where(['is_disable' => YesNoEnum::NO]) ->field('id,pid,name') ->order(['sort' => 'desc', 'id' => 'desc']) ->select() ->toArray(); return linear_to_tree($data, 'children'); } }