Przeglądaj źródła

Merge branch 'clx' of http://git.workervip.com/hechenghang/Small_goose into clx

# Conflicts:
#	src/main/java/com/zhentao/controller/WechatLoginController.java
caolinxuan 2 tygodni temu
rodzic
commit
bb660504e9

+ 18 - 18
src/main/java/com/zhentao/config/CorsConfig.java

@@ -1,18 +1,18 @@
-package com.zhentao.config;
-
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.CorsRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
-@Configuration
-public class CorsConfig implements WebMvcConfigurer {
-
-    @Override
-    public void addCorsMappings(CorsRegistry registry) {
-        registry.addMapping("/**")
-                .allowedOrigins("*")
-                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
-                .maxAge(3600);
-    }
-
-}
+//package com.zhentao.config;
+//
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.web.servlet.config.annotation.CorsRegistry;
+//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+//
+//@Configuration
+//public class CorsConfig implements WebMvcConfigurer {
+//
+//    @Override
+//    public void addCorsMappings(CorsRegistry registry) {
+//        registry.addMapping("/**")
+//                .allowedOrigins("*")
+//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
+//                .maxAge(3600);
+//    }
+//
+//}

+ 81 - 0
src/main/java/com/zhentao/controller/WechatLoginController.java

@@ -0,0 +1,81 @@
+package com.zhentao.controller;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.client.HttpClientErrorException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+public class WechatLoginController {
+
+    private static final String APPID = "wx8246d27305af5834";
+    private static final String SECRET = "8badbee7a035911171cdccb13c67de30";
+    private static final ObjectMapper objectMapper = new ObjectMapper(); // JSON 解析器
+
+    @PostMapping("/api/wx-login")
+    public Map<String, Object> wxLogin( @RequestBody Map<String, String> requestData) {
+        String code = requestData.get("code");
+        System.out.println(code);
+        if (code == null) {
+            return buildErrorResponse("Missing code");
+        }
+
+        String url = "https://api.weixin.qq.com/sns/jscode2session?" +
+                "appid=" + APPID +
+                "&secret=" + SECRET +
+                "&js_code=" + code +
+                "&grant_type=authorization_code";
+        RestTemplate restTemplate = new RestTemplate();
+        HttpHeaders headers = new HttpHeaders();
+        HttpEntity<String> entity = new HttpEntity<>(headers);
+
+        try {
+            // 改用 String 接收响应,兼容所有 Content-Type
+            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
+            String responseBody = response.getBody();
+
+            System.out.println(responseBody);
+
+            // 手动解析 JSON
+            Map<String, Object> result = objectMapper.readValue(responseBody, Map.class);
+            System.out.println(result);
+            if (result.containsKey("errcode")) { // 微信错误响应
+                return buildErrorResponse("微信接口返回错误: " + result.get("errmsg"));
+            }
+            // 成功响应
+            return buildSuccessResponse((String) result.get("openid"), (String) result.get("session_key"));
+
+        } catch (HttpClientErrorException e) { // 处理 HTTP 错误(如 400、500)
+            return buildErrorResponse("请求微信接口失败: " + e.getResponseBodyAsString());
+        } catch (Exception e) { // 处理 JSON 解析错误
+            return buildErrorResponse("响应解析失败: " + e.getMessage());
+        }
+    }
+
+    // 构建成功响应
+    private Map<String, Object> buildSuccessResponse(String openid, String sessionKey) {
+        Map<String, Object> response = new HashMap<>();
+        System.out.println("openid:"+openid);
+        System.out.println("sessionKey:"+sessionKey);
+        response.put("openid", openid);
+        response.put("session_key", sessionKey);
+        return response;
+    }
+
+    // 构建错误响应
+    private Map<String, Object> buildErrorResponse(String message) {
+        Map<String, Object> response = new HashMap<>();
+        response.put("error", message);
+        return response;
+    }
+}

+ 18 - 0
src/main/java/com/zhentao/shiro/config/CorsConfig.java

@@ -0,0 +1,18 @@
+package com.zhentao.shiro.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class CorsConfig implements WebMvcConfigurer {
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+            .allowedOrigins("*")
+            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
+            .maxAge(3600);
+    }
+
+}