|
@@ -5,6 +5,7 @@ import org.springframework.http.HttpEntity;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.HttpMethod;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
@@ -15,14 +16,14 @@ import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
-public class WechatLoginApplication {
|
|
|
|
|
|
+public class WechatLoginController {
|
|
|
|
|
|
private static final String APPID = "wxc8a6a80ca6808238";
|
|
private static final String APPID = "wxc8a6a80ca6808238";
|
|
private static final String SECRET = "96f6ef6782c1080b1c33016c0d0555b5";
|
|
private static final String SECRET = "96f6ef6782c1080b1c33016c0d0555b5";
|
|
private static final ObjectMapper objectMapper = new ObjectMapper(); // JSON 解析器
|
|
private static final ObjectMapper objectMapper = new ObjectMapper(); // JSON 解析器
|
|
|
|
|
|
@PostMapping("/api/wx-login")
|
|
@PostMapping("/api/wx-login")
|
|
- public Map<String, Object> wxLogin(@RequestBody Map<String, String> requestData) {
|
|
|
|
|
|
+ public Map<String, Object> wxLogin( @RequestBody Map<String, String> requestData) {
|
|
String code = requestData.get("code");
|
|
String code = requestData.get("code");
|
|
System.out.println(code);
|
|
System.out.println(code);
|
|
if (code == null) {
|
|
if (code == null) {
|
|
@@ -34,7 +35,6 @@ public class WechatLoginApplication {
|
|
"&secret=" + SECRET +
|
|
"&secret=" + SECRET +
|
|
"&js_code=" + code +
|
|
"&js_code=" + code +
|
|
"&grant_type=authorization_code";
|
|
"&grant_type=authorization_code";
|
|
-
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpEntity<String> entity = new HttpEntity<>(headers);
|
|
HttpEntity<String> entity = new HttpEntity<>(headers);
|
|
@@ -44,6 +44,8 @@ public class WechatLoginApplication {
|
|
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
|
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
|
String responseBody = response.getBody();
|
|
String responseBody = response.getBody();
|
|
|
|
|
|
|
|
+ System.out.println(responseBody);
|
|
|
|
+
|
|
// 手动解析 JSON
|
|
// 手动解析 JSON
|
|
Map<String, Object> result = objectMapper.readValue(responseBody, Map.class);
|
|
Map<String, Object> result = objectMapper.readValue(responseBody, Map.class);
|
|
System.out.println(result);
|
|
System.out.println(result);
|
|
@@ -63,6 +65,8 @@ public class WechatLoginApplication {
|
|
// 构建成功响应
|
|
// 构建成功响应
|
|
private Map<String, Object> buildSuccessResponse(String openid, String sessionKey) {
|
|
private Map<String, Object> buildSuccessResponse(String openid, String sessionKey) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
Map<String, Object> response = new HashMap<>();
|
|
|
|
+ System.out.println("openid:"+openid);
|
|
|
|
+ System.out.println("sessionKey:"+sessionKey);
|
|
response.put("openid", openid);
|
|
response.put("openid", openid);
|
|
response.put("session_key", sessionKey);
|
|
response.put("session_key", sessionKey);
|
|
return response;
|
|
return response;
|