userName 2 nedēļas atpakaļ
vecāks
revīzija
55e3b4529f

+ 8 - 0
pom.xml

@@ -77,9 +77,17 @@
             <artifactId>spring-boot-starter-json</artifactId>
         </dependency>
         <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+        </dependency>
+        <dependency>
             <groupId>javax.validation</groupId>
             <artifactId>validation-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+        </dependency>
         <!-- ini格式处理 -->
         <dependency>
             <groupId>org.ini4j</groupId>

+ 9 - 4
src/main/java/com/zhentao/controller/LoginController.java

@@ -6,9 +6,10 @@ import com.zhentao.util.ResultVo;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.util.Map;
 
 @RestController
-@RequestMapping("/user")
+@RequestMapping("/api")
 public class LoginController {
     @Resource
     private GooseUserService gooseUserService;
@@ -16,8 +17,12 @@ public class LoginController {
     public ResultVo login(@RequestBody GooseUser gooseUser){
         return gooseUserService.login(gooseUser);
     }
-    @RequestMapping("/register")
-    public ResultVo register(@RequestBody GooseUser gooseUser){
-        return gooseUserService.register(gooseUser);
+    @RequestMapping("/wx-login")
+    public Map<String, Object> Wxlogin(@RequestBody Map<String, String> requestData){
+        return gooseUserService.Wxlogin(requestData);
+    }
+    @RequestMapping("/getCode")
+    public ResultVo getCode(@RequestBody GooseUser user){
+        return gooseUserService.getCode(user);
     }
 }

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

@@ -1,81 +0,0 @@
-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 = "wxc8a6a80ca6808238";
-    private static final String SECRET = "96f6ef6782c1080b1c33016c0d0555b5";
-    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;
-    }
-}

+ 11 - 0
src/main/java/com/zhentao/domain/WechatAuthResponse.java

@@ -0,0 +1,11 @@
+package com.zhentao.domain;
+
+import lombok.Data;
+
+@Data
+public class WechatAuthResponse {
+    private String openid;
+    private String session_key;
+    private String errcode;
+    private String errmsg;
+}

+ 2 - 2
src/main/java/com/zhentao/dto/AppIdSecretValidator.java

@@ -10,8 +10,8 @@ import java.util.HashMap;
 import java.util.Map;
 
 public class AppIdSecretValidator {
-    private static final String APPID = "wxc8a6a80ca6808238";
-    private static final String SECRET = "96f6ef6782c1080b1c33016c0d0555b5";
+    private static final String APPID = "wx8246d27305af5834";
+    private static final String SECRET = "8badbee7a035911171cdccb13c67de30";
 
     public static void main(String[] args) {
         String url = "https://api.weixin.qq.com/cgi-bin/token?" +

+ 5 - 1
src/main/java/com/zhentao/service/GooseUserService.java

@@ -4,6 +4,8 @@ import com.zhentao.domain.user.GooseUser;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.zhentao.util.ResultVo;
 
+import java.util.Map;
+
 /**
 * @author 31810
 * @description 针对表【goose_user】的数据库操作Service
@@ -13,5 +15,7 @@ public interface GooseUserService extends IService<GooseUser> {
 
     ResultVo login(GooseUser gooseUser);
 
-    ResultVo register(GooseUser gooseUser);
+    Map<String, Object> Wxlogin(Map<String, String> requestData);
+
+    ResultVo getCode(GooseUser user);
 }

+ 143 - 5
src/main/java/com/zhentao/service/impl/GooseUserServiceImpl.java

@@ -1,12 +1,29 @@
 package com.zhentao.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.zhentao.domain.WechatAuthResponse;
 import com.zhentao.domain.user.GooseUser;
 import com.zhentao.service.GooseUserService;
 import com.zhentao.mapper.GooseUserMapper;
-import com.zhentao.util.ResultVo;
+import com.zhentao.util.*;
+import lombok.SneakyThrows;
+import org.apache.http.HttpResponse;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
+import redis.clients.jedis.Jedis;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
 
 /**
 * @author 31810
@@ -17,20 +34,141 @@ import org.springframework.stereotype.Service;
 public class GooseUserServiceImpl extends ServiceImpl<GooseUserMapper, GooseUser>
     implements GooseUserService{
     @Autowired
-    private GooseUserMapper gooseUserMapper;
-
+    private GooseUserMapper userMapper;
+    private static final String APPID = "wx8246d27305af5834";
+    private static final String SECRET = "8badbee7a035911171cdccb13c67de30";
+    private static final ObjectMapper objectMapper = new ObjectMapper(); // JSON 解析器
     @Override
     public ResultVo login(GooseUser gooseUser) {
+        if(StringUtils.checkValNotNull(gooseUser.getPhone())){
+            QueryWrapper<GooseUser>queryWrapper=new QueryWrapper<>();
+            queryWrapper.eq("phone",gooseUser.getPhone());
+            GooseUser user = userMapper.selectOne(queryWrapper);
+            if(user==null){
+                GooseUser gooseUser1=new GooseUser();
+                gooseUser1.setPhone(gooseUser.getPhone());
+                if(StringUtils.checkValNull(gooseUser.getPassword())){
+                    gooseUser1.setPassword(gooseUser.getPassword());
+                }
+                Long Id= SnowflakeIdGenerator.getSnowId();
+                gooseUser1.setId(Id);
+                userMapper.insert(gooseUser1);
+                String token= TokenUtils.createJwtToken(Id.toString());
+                return ResultVo.OK(token);
+            }
+            String token= TokenUtils.createJwtToken(user.getId().toString());
+            return ResultVo.OK(token);
+        }
+        return ResultVo.ERROR();
+    }
 
+    @SneakyThrows
+    @Override
+    public Map<String, Object> Wxlogin(Map<String, String> requestData) {
+        String code = requestData.get("code");
+        System.out.println(code);
+        if (code == null) {
+            return buildErrorResponse("Missing code");
+        }
+        WechatAuthResponse authInfo = WechatLoginUtil.getAuthInfo(code);
+        System.out.println(authInfo);
+        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);
 
-        return null;
+        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());
+        }
     }
 
     @Override
-    public ResultVo register(GooseUser gooseUser) {
+    public ResultVo getCode(GooseUser user) {
+        String host = "https://gyytz.market.alicloudapi.com";
+        String path = "/sms/smsSend";
+        String method = "POST";
+        String appcode = "b685e5e231ce404c855db67359acb1e1";
+        Map<String, String> headers = new HashMap<String, String>();
+        // 最后在 header 中的格式(中间是英文空格)为 Authorization:APPCODE 83359fd73fe94948385f570e3c139105
+        headers.put("Authorization", "APPCODE " + appcode);
+        Map<String, String> querys = new HashMap<String, String>();
+        System.out.println(user.getPhone());
+        querys.put("mobile",user.getPhone());
+        long uuidLong = UUID.randomUUID().getMostSignificantBits() ^ UUID.randomUUID().getLeastSignificantBits();
+
+        // 2. 转换为正整数并取模1000000(确保6位范围)
+        long code = Math.abs(uuidLong) % 1000000;
 
+        String code1=String.format("%06d", code);
+        System.out.println(code1);
+        if(code1!=null){
+            Jedis jedis=new Jedis("localhost",6379);
+            jedis.set("code",code1);
+        }
+
+        querys.put("param", "**code**:"+code1+",**minute**:5");
+        // smsSignId(短信前缀)和 templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html
+        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
+        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
+        Map<String, String> bodys = new HashMap<String, String>();
+        try {
+            /**
+             * 重要提示如下:
+             * HttpUtils 请从
+             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
+             * 下载
+             *
+             * 相应的依赖请参照
+             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
+             */
+            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
+            System.out.println(response.toString());
+            // 获取 response 的 body
+            // System.out.println(EntityUtils.toString(response.getEntity()));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
         return null;
     }
+
+    // 构建成功响应
+    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;
+    }
 }
 
 

+ 312 - 0
src/main/java/com/zhentao/util/HttpUtils.java

@@ -0,0 +1,312 @@
+package com.zhentao.util;
+
+
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class HttpUtils {
+
+	/**
+	 * get
+	 *
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doGet(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpGet request = new HttpGet(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * post form
+	 *
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @param bodys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys,
+			Map<String, String> bodys)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        if (bodys != null) {
+            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
+
+            for (String key : bodys.keySet()) {
+                nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
+            }
+            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
+            formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
+            request.setEntity(formEntity);
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * Post String
+	 *
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys,
+			String body)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        if (StringUtils.isNotBlank(body)) {
+        	request.setEntity(new StringEntity(body, "utf-8"));
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * Post stream
+	 *
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys,
+			byte[] body)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpPost request = new HttpPost(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        if (body != null) {
+        	request.setEntity(new ByteArrayEntity(body));
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * Put String
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPut(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys,
+			String body)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpPut request = new HttpPut(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        if (StringUtils.isNotBlank(body)) {
+        	request.setEntity(new StringEntity(body, "utf-8"));
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * Put stream
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPut(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys,
+			byte[] body)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpPut request = new HttpPut(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        if (body != null) {
+        	request.setEntity(new ByteArrayEntity(body));
+        }
+
+        return httpClient.execute(request);
+    }
+
+	/**
+	 * Delete
+	 *
+	 * @param host
+	 * @param path
+	 * @param method
+	 * @param headers
+	 * @param querys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doDelete(String host, String path, String method,
+			Map<String, String> headers,
+			Map<String, String> querys)
+            throws Exception {
+    	HttpClient httpClient = wrapClient(host);
+
+    	HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
+        for (Map.Entry<String, String> e : headers.entrySet()) {
+        	request.addHeader(e.getKey(), e.getValue());
+        }
+
+        return httpClient.execute(request);
+    }
+
+	private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
+    	StringBuilder sbUrl = new StringBuilder();
+    	sbUrl.append(host);
+    	if (!StringUtils.isBlank(path)) {
+    		sbUrl.append(path);
+        }
+    	if (null != querys) {
+    		StringBuilder sbQuery = new StringBuilder();
+        	for (Map.Entry<String, String> query : querys.entrySet()) {
+        		if (0 < sbQuery.length()) {
+        			sbQuery.append("&");
+        		}
+        		if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
+        			sbQuery.append(query.getValue());
+                }
+        		if (!StringUtils.isBlank(query.getKey())) {
+        			sbQuery.append(query.getKey());
+        			if (!StringUtils.isBlank(query.getValue())) {
+        				sbQuery.append("=");
+        				sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
+        			}
+                }
+        	}
+        	if (0 < sbQuery.length()) {
+        		sbUrl.append("?").append(sbQuery);
+        	}
+        }
+
+    	return sbUrl.toString();
+    }
+
+	private static HttpClient wrapClient(String host) {
+		HttpClient httpClient = new DefaultHttpClient();
+		if (host.startsWith("https://")) {
+			sslClient(httpClient);
+		}
+
+		return httpClient;
+	}
+
+	private static void sslClient(HttpClient httpClient) {
+        try {
+            SSLContext ctx = SSLContext.getInstance("TLS");
+            X509TrustManager tm = new X509TrustManager() {
+                public X509Certificate[] getAcceptedIssuers() {
+                    return null;
+                }
+                public void checkClientTrusted(X509Certificate[] xcs, String str) {
+
+                }
+                public void checkServerTrusted(X509Certificate[] xcs, String str) {
+
+                }
+            };
+            ctx.init(null, new TrustManager[] { tm }, null);
+            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
+            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+            ClientConnectionManager ccm = httpClient.getConnectionManager();
+            SchemeRegistry registry = ccm.getSchemeRegistry();
+            registry.register(new Scheme("https", 443, ssf));
+        } catch (KeyManagementException ex) {
+            throw new RuntimeException(ex);
+        } catch (NoSuchAlgorithmException ex) {
+        	throw new RuntimeException(ex);
+        }
+    }
+}

+ 42 - 0
src/main/java/com/zhentao/util/WechatLoginUtil.java

@@ -0,0 +1,42 @@
+package com.zhentao.util;
+
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.zhentao.domain.WechatAuthResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+
+public class WechatLoginUtil {
+
+    private static final String APP_ID = "wx8246d27305af5834"; // 替换为你的AppID
+    private static final String APP_SECRET = "8badbee7a035911171cdccb13c67de30"; // 替换为你的AppSecret
+    private static final String WECHAT_LOGIN_URL = "https://api.weixin.qq.com/sns/jscode2session";
+
+    public static WechatAuthResponse getAuthInfo(String code) throws IOException {
+        // 构造微信登录接口的URL,包含必要的参数
+        String url = WECHAT_LOGIN_URL + "?appid=" + APP_ID + "&secret=" + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
+
+        // 创建一个默认的HTTP客户端,用于发送HTTP请求
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+
+        // 创建一个HTTP GET请求对象,指定请求的URL
+        HttpGet httpGet = new HttpGet(url);
+
+        // 执行HTTP GET请求,获取微信服务器的响应
+        CloseableHttpResponse response = httpClient.execute(httpGet);
+
+        // 从响应中获取响应体内容,并将其转换为字符串
+        String jsonResponse = EntityUtils.toString(response.getEntity());
+        // 创建一个Jackson的ObjectMapper对象,用于处理JSON数据
+        ObjectMapper objectMapper = new ObjectMapper();
+
+        // 将微信返回的JSON字符串解析为WechatAuthResponse对象
+        // WechatAuthResponse是一个自定义的Java类,用于封装微信返回的用户认证信息
+        return objectMapper.readValue(jsonResponse, WechatAuthResponse.class);
+    }
+}