package com.stylefeng.guns.modular.system.controller.system; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.stylefeng.guns.core.common.constant.JwtConstants; import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.shiro.ShiroUser; import com.stylefeng.guns.core.util.JwtTokenUtil; import com.stylefeng.guns.modular.system.model.MenuEntryConfig; import com.stylefeng.guns.modular.system.model.User; import com.stylefeng.guns.modular.system.service.IMenuEntryConfigService; import com.stylefeng.guns.modular.system.service.IUserService; import com.stylefeng.guns.modular.system.util.ResultUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.List; /** * 后台嵌入外部页面。 */ @Controller @RequestMapping("") public class EmbeddedPageController { private static final String EMBEDDED_IFRAME_VIEW = "/system/embedded/iframe.html"; @Value("${embedded.route-switch-url}") private String routeSwitchUrl; @Value("${embedded.statistical-table-url}") private String statisticalTableUrl; @Autowired private IMenuEntryConfigService menuEntryConfigService; @Autowired private IUserService userService; @RequestMapping("/embedded/route-switch") public String routeSwitch(Model model) throws UnsupportedEncodingException { model.addAttribute("title", "路由切换页"); model.addAttribute("iframeSrc", buildTokenUrl(routeSwitchUrl)); return EMBEDDED_IFRAME_VIEW; } @RequestMapping("/embedded/statistical-table") public String statisticalTable(Model model) throws UnsupportedEncodingException { model.addAttribute("title", "统计板"); model.addAttribute("iframeSrc", buildTokenUrl(statisticalTableUrl)); return EMBEDDED_IFRAME_VIEW; } @RequestMapping("/gunsApi/tCompany/entry_config_list") @ResponseBody public Object entryConfigList(HttpServletRequest request) { Integer companyId = getCurrentCompanyId(request); if (companyId == null) { return ResultUtil.tokenErr("登录信息无效"); } return ResultUtil.success(getMenuEntryConfigList(companyId)); } private List getMenuEntryConfigList(Integer companyId) { return menuEntryConfigService.selectList( new EntityWrapper().eq("openCityId", companyId).orderBy("id", true)); } private Integer getCurrentCompanyId(HttpServletRequest request) { String token = getBearerToken(request); if (token == null) { return null; } String userId = JwtTokenUtil.getUsernameFromToken(token); User user = userService.selectById(userId); return user == null ? null : user.getObjectId(); } private String getBearerToken(HttpServletRequest request) { String requestHeader = request.getHeader(JwtConstants.AUTH_HEADER); if (requestHeader != null && requestHeader.startsWith("Bearer ")) { return requestHeader.substring(7); } return null; } private String buildTokenUrl(String baseUrl) throws UnsupportedEncodingException { return baseUrl + generateCurrentUserToken(); } private String generateCurrentUserToken() throws UnsupportedEncodingException { ShiroUser user = ShiroKit.getUser(); if (user == null || user.getId() == null) { return ""; } String token = JwtTokenUtil.generateToken(user.getId().toString()); return URLEncoder.encode(token, "UTF-8"); } }