From f68f81917ae301f57ddca376688a6a968880681c Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期一, 07 九月 2026 15:25:05 +0800
Subject: [PATCH] 【丽水】SSO重定向跳转详情页面

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java |  210 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 142 insertions(+), 68 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java
index bdad035..da15e57 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java
@@ -5,24 +5,31 @@
 import com.ruoyi.common.core.domain.model.LoginUser;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.HttpUtil;
+import com.ruoyi.common.utils.OkHttpExample;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.http.HttpUtils;
+import com.ruoyi.framework.web.service.SysLoginService;
 import com.ruoyi.framework.web.service.TokenService;
 import com.ruoyi.system.service.ISysConfigService;
 import com.ruoyi.system.service.ISysUserService;
 import com.smartor.domain.SSOTokenResponse;
 import com.smartor.domain.SSOUserInfo;
+import com.smartor.domain.ServiceSubtask;
+import com.smartor.domain.VO.ServiceSubtaskVO;
+import com.smartor.domain.entity.ServiceSubtaskEntity;
+import com.smartor.service.IServiceSubtaskService;
+import com.smartor.service.impl.ServiceSubtaskServiceImpl;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.*;
 import org.springframework.http.client.SimpleClientHttpRequestFactory;
+
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.servlet.view.RedirectView;
 import org.springframework.web.util.UriComponentsBuilder;
@@ -31,8 +38,7 @@
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 /**
  * SSO鍗曠偣鐧诲綍鎺у埗鍣�
@@ -85,9 +91,15 @@
     private TokenService tokenService;
 
     @Autowired
+    private SysLoginService loginService;
+
+    @Autowired
     private ISysConfigService sysConfigService;
 
     private final RestTemplate restTemplate;
+
+    @Autowired
+    private IServiceSubtaskService serviceSubtaskService;
 
     public SSOController() {
         // 閰嶇疆RestTemplate瓒呮椂
@@ -127,66 +139,134 @@
      * 璁块棶璺緞锛歨ttp://鍩熷悕:8095/sso/ssoLoginLyra
      */
     @GetMapping("ssoLoginLyra")
-    public RedirectView ssoLoginLyra() {
-        log.info("鏀跺埌SSO鐧诲綍璇锋眰锛屽紑濮嬮噸瀹氬悜鍒版巿鏉冩湇鍔″櫒");
-        String redirectUri = internalRedirectUri;
-        redirectUri = URLEncoder.encode(internalRedirectUri);
+    public RedirectView ssoLoginLyra(@RequestParam(value = "code", required = false) String code) {
+        log.info("鏀跺埌SSOLyra鐧诲綍璇锋眰锛屽紑濮嬮噸瀹氬悜鍒版巿鏉冩湇鍔″櫒,code = {}",code);
         String path = sysConfigService.selectConfigByKey("sys.qddz");
-        String lyraPath = "https://9.208.39.29:13021";
-        String clientId = "1553548571532333056";
+        String redirectUri = "https://9.208.2.190:8092/prod-api/sso/ssoLoginLyra";
+        String lyraPath = "https://9.0.124.104:13021";
+        String clientId = "1553588321874087936";
         String clientSecret = "suifangxt";
         String scope = "openid";
 
-        String authorizeUrl = lyraPath + "/mediinfo-lyra-authserver/connect/authorize";
+//        String authorizeUrl = lyraPath + "/mediinfo-lyra-authserver/connect/authorize";
         String accessTokenUrl = lyraPath + "/mediinfo-lyra-authserver/connect/token";
         String userInfoUrl =  lyraPath + "/mediinfo-lyra-authserver/connect/userinfo";
         // 閲嶅畾鍚戝湴鍧�瀵硅薄(閲嶅畾鍚戝湴鍧�
         RedirectView redirectView = new RedirectView();
-
-        // Authorize閴存潈鎺ュ彛
-        String param = "client_id=" + clientId + "&redirect_uri=" + redirectUri + "&response_type=code" +
-                "&state=" + state + "&scope=" + scope;
-        log.info("銆怉uthorize閴存潈鎺ュ彛銆戝叆鍙備负锛歿}", param);
-//        String url = internalAuthorizeUrl;
-        String s = HttpUtils.sendGet(authorizeUrl, param);
-        Map<String, String> result = getResult(s);
-        String code = result.get("code");
-        if (StringUtils.isEmpty(code)) {
-            log.error("鏈幏鍙栧埌鎺堟潈鐮�");
-            throw new BaseException("SSO鎺堟潈澶辫触");
-        }
         try {
-            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, clientSecret,true);
+            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, clientSecret, redirectUri,true);
+            log.info("鑾峰彇鍒扮殑token锛�" + accessToken);
             SSOUserInfo userInfo = getUserInfoLyra(accessToken.getAccess_token(), userInfoUrl,true);
-
-            createLocalSession(userInfo);
+            log.info("鑾峰彇鍒扮殑鐢ㄦ埛淇℃伅锛�" + userInfo);
+//            createLocalSession(userInfo);
             if (StringUtils.isEmpty(path)) {
                 throw new BaseException("璇烽厤缃墠绔湴鍧�");
             }
-//            String reviewUrl = path + "/loginSSO?token=" + accessToken.getAccess_token() + "&orgid=" +
-//                    userInfo.getZuZhiJGID() + "&orgname=" + userInfo.getZuZhiJGMC() +
-//                    "&ZuHuID="+ userInfo.getYongHuID() +"&deptCode=null";
-            String reviewUrl = "";
-            reviewUrl = UriComponentsBuilder.fromHttpUrl(path)
-                    .path("/loginSSO")
-                    .queryParam("token", accessToken.getAccess_token())
-                    .queryParam("orgid", userInfo.getZuZhiJGID())
-                    .queryParam("orgname", userInfo.getZuZhiJGMC())
-                    .queryParam("ZuHuID", userInfo.getYongHuID())
-                    .queryParam("deptCode", "null")
-                    .build()
-                    .toUriString();
 
-            log.info("鍗曠偣鐧婚檰閲嶅畾鍚戝湴鍧�涓猴細{}", reviewUrl);
-            redirectView.setUrl(reviewUrl);
-            redirectView.setStatusCode(HttpStatus.FOUND);
+            String userName =  userInfo.getZhiGongID();
+            String orgid = userInfo.getZuZhiJGID();
+            String campusid = "1";
+            String deptId = null;
+            String token = null;
+            log.info("銆怱SO鐧诲綍銆戣幏鍙栫敤鎴稵oken鍙傛暟锛�" + userInfo);
+            String loginToken = loginService.ssoLogin(userName, orgid, deptId, campusid, token);
+            if (StringUtils.isEmpty(loginToken)) {
+                log.error("銆怱SO鐧诲綍銆戠櫥褰曞け璐ワ紝userName={}, orgid={}", userName, orgid);
+                return buildRedirect(path + "/login");
+            }
+
+            String redirectUrl = path + "/loginSSO?token=" + loginToken + "&orgid=" + orgid + "&orgname=null&ZuHuID=null&deptCode=null";
+            log.info("銆怱SO鐧诲綍銆戦噸瀹氬悜鍦板潃锛歿}", redirectUrl);
+            return buildRedirect(redirectUrl);
+
         } catch (Exception e) {
             log.error("SSO鐧诲綍澶辫触", e);
             redirectView.setUrl(path + "/login?error=sso_failed");
             return redirectView;
 
         }
+    }
 
+    /**
+     * ssoGoServiceSubtaskList
+     * code 鏄�/mediinfo-lyra-authserver/connect/authorize 鐢県is鎻愪緵
+     * 鐧诲綍瀹㈡埛绔疘D鏄浐瀹氱殑 clientId = 1553588321874087936  clientSecret = suifangxt
+     */
+    @GetMapping("ssoGoServiceSubtaskList")
+    public RedirectView ssoGoServiceSubtaskList(@RequestParam(value = "code", required = false) String code,
+                                                @RequestParam(value = "sfzh", required = false) String sfzh) {
+        // 閲嶅畾鍚戝湴鍧�瀵硅薄(閲嶅畾鍚戝湴鍧�
+        RedirectView redirectView = new RedirectView();
+
+        log.info("鏀跺埌ssoGoServiceSubtaskList璇锋眰锛屽紑濮嬮噸瀹氬悜鍒版巿鏉冩湇鍔″櫒,code = {}, sfzh = {}",code, sfzh);
+        String path = sysConfigService.selectConfigByKey("sys.qddz");
+        String redirectUri = "https://9.208.2.190:8092/prod-api/sso/ssoLoginLyra";
+        String lyraPath = "https://9.0.124.104:13021";
+        String clientId = "1553588321874087936";
+        String clientSecret = "suifangxt";
+        String scope = "openid";
+
+        ServiceSubtaskEntity subtask = new ServiceSubtaskEntity();
+        subtask.setSfzh(sfzh);
+        subtask.setSort(0);
+        List<ServiceSubtask> serviceSubtaskList = serviceSubtaskService.selectServiceSubtaskList(subtask);
+        ServiceSubtask serviceSubtask = null;
+        if(CollectionUtils.isNotEmpty(serviceSubtaskList)){
+            serviceSubtask = serviceSubtaskList.get(0);
+        }else {
+            redirectView.setUrl(path + "/login?error=sso_failed");
+            return redirectView;
+        }
+
+//        String authorizeUrl = lyraPath + "/mediinfo-lyra-authserver/connect/authorize";
+        String accessTokenUrl = lyraPath + "/mediinfo-lyra-authserver/connect/token";
+        String userInfoUrl =  lyraPath + "/mediinfo-lyra-authserver/connect/userinfo";
+        try {
+            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, clientSecret, redirectUri,true);
+            log.info("鑾峰彇鍒扮殑token锛�" + accessToken);
+            SSOUserInfo userInfo = getUserInfoLyra(accessToken.getAccess_token(), userInfoUrl,true);
+            log.info("鑾峰彇鍒扮殑鐢ㄦ埛淇℃伅锛�" + userInfo);
+//            createLocalSession(userInfo);
+            if (StringUtils.isEmpty(path)) {
+                throw new BaseException("璇烽厤缃墠绔湴鍧�");
+            }
+
+            String userName =  userInfo.getZhiGongID();
+            String orgid = userInfo.getZuZhiJGID();
+            String campusid = "1";
+            String deptId = null;
+            String token = null;
+            log.info("銆怱SO鐧诲綍銆戣幏鍙栫敤鎴稵oken鍙傛暟锛�" + userInfo);
+            String loginToken = loginService.ssoLogin(userName, orgid, deptId, campusid, token);
+            if (StringUtils.isEmpty(loginToken)) {
+                log.error("銆怱SO鐧诲綍銆戠櫥褰曞け璐ワ紝userName={}, orgid={}", userName, orgid);
+                return buildRedirect(path + "/login");
+            }
+
+            String voiceType = (StringUtils.isNotEmpty(serviceSubtask.getType())
+                    && serviceSubtask.getType().equals("1")) ? "1" : "0";
+            String redirectUrl = path + "/loginSSO?token=" + loginToken + "&orgid=" + orgid +
+                    "&taskid=" + serviceSubtask.getTaskid() + "&patid=" + serviceSubtask.getPatid() +
+                    "&id=" + serviceSubtask.getId() + "&voiceType=" + voiceType +
+                    "&visitCount=" + serviceSubtask.getVisitCount() + "&sfzh=" + sfzh +
+                    "&orgname=null&ZuHuID=null&deptCode=null";
+            log.info("銆怱SO鐧诲綍銆戦噸瀹氬悜鍦板潃锛歿}", redirectUrl);
+            return buildRedirect(redirectUrl);
+
+        } catch (Exception e) {
+            log.error("SSO鐧诲綍澶辫触", e);
+            redirectView.setUrl(path + "/login?error=sso_failed");
+            return redirectView;
+
+        }
+    }
+    /**
+     * 鏋勫缓閲嶅畾鍚戣鍥�
+     */
+    private RedirectView buildRedirect(String url) {
+        RedirectView redirectView = new RedirectView();
+        redirectView.setUrl(url);
+        redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
         return redirectView;
     }
 
@@ -272,29 +352,26 @@
     /**
      * 鑾峰彇璁块棶浠ょ墝
      */
-    private SSOTokenResponse getAccessTokenLyra(String code, String accessTokenUrl, String clientId, String clientSecret, boolean isInternal) throws Exception {
+    private SSOTokenResponse getAccessTokenLyra(String code, String accessTokenUrl, String clientId, String clientSecret, String redirectUri, boolean isInternal) throws Exception {
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
 
-        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
-        String redirectUri = getRedirectUri(isInternal);
-        params.add("client_id", clientId);
-        params.add("client_secret", clientSecret);
-        params.add("code", code);
-        params.add("grant_type", "authorization_code");
-        params.add("redirect_uri", redirectUri);
+        Map<String, String> params = new HashMap<>();
+        params.put("client_id", clientId);
+        params.put("client_secret", clientSecret);
+        params.put("code", code);
+        params.put("grant_type", "authorization_code");
+        params.put("redirect_uri", redirectUri);
 
-        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
+        log.info("getAccessTokenLyra: url = {}, params = {}" , accessTokenUrl, params);
+        String result = OkHttpExample.postFormUnsafe(accessTokenUrl, params);
+        log.info("Token鍝嶅簲: {}", result);
 
-        ResponseEntity<String> response = restTemplate.exchange(accessTokenUrl, HttpMethod.POST, request, String.class);
-
-        log.info("Token鍝嶅簲: {}", response.getBody());
-
-        if (response.getBody() == null || response.getBody().trim().isEmpty()) {
+        if (result == null || result.trim().isEmpty()) {
             throw new RuntimeException("Token鍝嶅簲涓虹┖");
         }
 
-        SSOTokenResponse tokenResponse = JSON.parseObject(response.getBody(), SSOTokenResponse.class);
+        SSOTokenResponse tokenResponse = JSON.parseObject(result, SSOTokenResponse.class);
 
         if (tokenResponse == null || StringUtils.isEmpty(tokenResponse.getAccess_token())) {
             throw new RuntimeException("鑾峰彇access_token澶辫触");
@@ -309,18 +386,15 @@
     private SSOUserInfo getUserInfoLyra(String accessToken, String userInfoUrl, boolean isInternal) throws Exception {
         HttpHeaders headers = new HttpHeaders();
         headers.set("Authorization", "Bearer " + accessToken);
+        log.info("getUserInfoLyra: url = {}, accessToken = {}" , userInfoUrl, accessToken);
+        String result = OkHttpExample.getUnsafe(userInfoUrl,accessToken);
+        log.info("鐢ㄦ埛淇℃伅鍝嶅簲: {}", result);
 
-        HttpEntity<String> entity = new HttpEntity<>(headers);
-
-        ResponseEntity<String> response = restTemplate.exchange(userInfoUrl, HttpMethod.GET, entity, String.class);
-
-        log.info("鐢ㄦ埛淇℃伅鍝嶅簲: {}", response.getBody());
-
-        if (response.getBody() == null || response.getBody().trim().isEmpty()) {
+        if (result == null || result.trim().isEmpty()) {
             throw new RuntimeException("鐢ㄦ埛淇℃伅鍝嶅簲涓虹┖");
         }
 
-        SSOUserInfo userInfo = JSON.parseObject(response.getBody(), SSOUserInfo.class);
+        SSOUserInfo userInfo = JSON.parseObject(result, SSOUserInfo.class);
 
         if (userInfo == null || StringUtils.isEmpty(userInfo.getName())) {
             throw new RuntimeException("鑾峰彇鐢ㄦ埛淇℃伅澶辫触鎴栫敤鎴峰悕涓虹┖");

--
Gitblit v1.9.3