yanderong
2026-07-15 f2389841191b36908ea5a079c317bc210caa87b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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<MenuEntryConfig> getMenuEntryConfigList(Integer companyId) {
        return menuEntryConfigService.selectList(
                new EntityWrapper<MenuEntryConfig>().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");
    }
}