feat(新增): 动态菜单
This commit is contained in:
parent
db21e70786
commit
4b2141bb5a
|
@ -47,7 +47,9 @@ public class LoginAuthInterceptor implements HandlerInterceptor {
|
|||
}
|
||||
|
||||
// 将用户信息放到ThreadLocal中
|
||||
BaseContext.setSysUser(JSON.parseObject(sysUserString, SysUser.class));
|
||||
SysUser sysUser = JSON.parseObject(sysUserString, SysUser.class);
|
||||
BaseContext.setSysUser(sysUser);
|
||||
BaseContext.setUserId(sysUser.getId());
|
||||
|
||||
// 更新Redis过期时间
|
||||
redisTemplate.expire(token, 7, TimeUnit.DAYS);
|
||||
|
|
|
@ -2,8 +2,11 @@ package com.atguigu.utils;
|
|||
|
||||
|
||||
import com.atguigu.spzx.model.entity.system.SysMenu;
|
||||
import com.atguigu.spzx.model.vo.system.SysMenuVo;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
@ -43,4 +46,26 @@ public class MenuHelper {
|
|||
|
||||
return sysMenu;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将List<SysMenu>对象转换成List<SysMenuVo>对象
|
||||
*
|
||||
* @param menus 全部菜单列表
|
||||
* @return 系统菜单响应结果实体类列表
|
||||
*/
|
||||
public static List<SysMenuVo> buildMenus(List<SysMenu> menus) {
|
||||
LinkedList<SysMenuVo> sysMenuVos = new LinkedList<>();
|
||||
menus.forEach(menu -> {
|
||||
SysMenuVo menuVo = new SysMenuVo();
|
||||
menuVo.setTitle(menu.getTitle());
|
||||
menuVo.setName(menu.getComponent());
|
||||
List<SysMenu> children = menu.getChildren();
|
||||
if (!CollectionUtils.isEmpty(children)) {
|
||||
menuVo.setChildren(buildMenus(children));
|
||||
}
|
||||
sysMenuVos.add(menuVo);
|
||||
});
|
||||
return sysMenuVos;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,13 +9,13 @@ import org.springframework.context.annotation.ComponentScan;
|
|||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Slf4j
|
||||
@MapperScan("com.atguigu.spzx.manger.mapper")
|
||||
@EnableCaching// 开启缓存注解
|
||||
@ComponentScan("com.atguigu")
|
||||
@EnableScheduling
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement // 开启注解方式的事务管理
|
||||
@EnableCaching// 开启缓存注解
|
||||
@EnableScheduling
|
||||
@ComponentScan("com.atguigu")
|
||||
@MapperScan("com.atguigu.spzx.manger.mapper")
|
||||
@Slf4j
|
||||
public class MangerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MangerApplication.class, args);
|
||||
|
|
|
@ -1,18 +1,22 @@
|
|||
package com.atguigu.spzx.manger.controller;
|
||||
|
||||
import com.atguigu.context.BaseContext;
|
||||
import com.atguigu.spzx.manger.service.SysMenuService;
|
||||
import com.atguigu.spzx.manger.service.SysUserService;
|
||||
import com.atguigu.spzx.manger.service.ValidateCodeService;
|
||||
import com.atguigu.spzx.model.dto.system.LoginDto;
|
||||
import com.atguigu.spzx.model.entity.system.SysUser;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import com.atguigu.spzx.model.vo.system.LoginVo;
|
||||
import com.atguigu.spzx.model.vo.system.SysMenuVo;
|
||||
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "用户相关接口")
|
||||
@RestController
|
||||
@RequestMapping("/admin/system/index")
|
||||
|
@ -20,6 +24,8 @@ public class IndexController {
|
|||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
@Autowired
|
||||
private SysMenuService sysMenuService;
|
||||
@Autowired
|
||||
private ValidateCodeService validateCodeService;
|
||||
|
||||
@Operation(summary = "生成验证码", description = "生成验证码信息")
|
||||
|
@ -49,4 +55,11 @@ public class IndexController {
|
|||
sysUserService.logout(token);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "动态菜单", description = "动态菜单")
|
||||
@GetMapping("menus")
|
||||
public Result<List<SysMenuVo>> menus() {
|
||||
List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
|
||||
return Result.success(sysMenuVoList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,4 +42,12 @@ public interface SysMenuMapper {
|
|||
* @param id 菜单id
|
||||
*/
|
||||
void deleteById(Long id);
|
||||
|
||||
/**
|
||||
* 根据用户ID查找
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<SysMenu> selectListByUserId(Long userId);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.atguigu.spzx.manger.service;
|
||||
|
||||
import com.atguigu.spzx.model.entity.system.SysMenu;
|
||||
import com.atguigu.spzx.model.vo.system.SysMenuVo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -32,4 +33,11 @@ public interface SysMenuService {
|
|||
* @param id 菜单id
|
||||
*/
|
||||
void removeById(Long id);
|
||||
|
||||
/**
|
||||
* 动态菜单
|
||||
*
|
||||
* @return 动态菜单列表
|
||||
*/
|
||||
List<SysMenuVo> findUserMenuList();
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package com.atguigu.spzx.manger.service.impl;
|
||||
|
||||
import com.atguigu.constant.MessageConstant;
|
||||
import com.atguigu.context.BaseContext;
|
||||
import com.atguigu.exception.BunnyException;
|
||||
import com.atguigu.spzx.manger.mapper.SysMenuMapper;
|
||||
import com.atguigu.spzx.manger.service.SysMenuService;
|
||||
import com.atguigu.spzx.model.entity.system.SysMenu;
|
||||
import com.atguigu.spzx.model.vo.system.SysMenuVo;
|
||||
import com.atguigu.utils.MenuHelper;
|
||||
import com.atguigu.utils.StringEmptyUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -64,4 +66,18 @@ public class SysMenuServiceImpl implements SysMenuService {
|
|||
}
|
||||
sysMenuMapper.deleteById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态菜单
|
||||
*
|
||||
* @return 动态菜单列表
|
||||
*/
|
||||
@Override
|
||||
public List<SysMenuVo> findUserMenuList() {
|
||||
Long userId = BaseContext.getUserId();
|
||||
// 根据用户ID查找
|
||||
List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId);
|
||||
sysMenuList = MenuHelper.buildTree(sysMenuList);
|
||||
return MenuHelper.buildMenus(sysMenuList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,4 +59,9 @@
|
|||
<include refid="columns"/>
|
||||
from sys_menu where is_deleted = 0 order by sort_value
|
||||
</select>
|
||||
|
||||
<!-- 根据用户ID查找 -->
|
||||
<select id="selectListByUserId" resultType="com.atguigu.spzx.model.entity.system.SysMenu">
|
||||
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
Loading…
Reference in New Issue