|
@@ -0,0 +1,76 @@
|
|
|
+package com.rongwei.zhsw.utils;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.rongwe.zhsw.vo.WeChatLoginReturnVo;
|
|
|
+import com.rongwei.commonservice.service.RedisService;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.zhsw.config.WeChatLoginApiPara;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+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.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * WeChatUtils class
|
|
|
+ *
|
|
|
+ * @author XH
|
|
|
+ * @date 2025/03/08
|
|
|
+ */
|
|
|
+
|
|
|
+@Component
|
|
|
+public class WeChatUtils {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeChatLoginApiPara autoWeChatLoginApiPara;
|
|
|
+
|
|
|
+ private static WeChatLoginApiPara weChatLoginApiPara;
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(WeChatUtils.class);
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ weChatLoginApiPara = autoWeChatLoginApiPara;
|
|
|
+ }
|
|
|
+ public static final String WECHAT_LOGIN_URL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static WeChatLoginReturnVo login(String jsCode){
|
|
|
+ String url = String.format(WECHAT_LOGIN_URL, weChatLoginApiPara.getAppid(), weChatLoginApiPara.getSecret(), jsCode);
|
|
|
+
|
|
|
+ RequestConfig config = RequestConfig.custom().setConnectTimeout(35000).setSocketTimeout(35000).build();
|
|
|
+ CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ WeChatLoginReturnVo returnVo=null;
|
|
|
+ try {
|
|
|
+ response = httpclient.execute(httpGet);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if(statusCode !=200){
|
|
|
+ log.error("接口状态异常:{}", statusCode);
|
|
|
+ throw new CustomException("微信登录失败");
|
|
|
+ }
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity != null) {
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ returnVo = objectMapper.readValue(EntityUtils.toString(entity, "utf-8"), WeChatLoginReturnVo.class);
|
|
|
+ }else{
|
|
|
+ return returnVo;
|
|
|
+ }
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取session_key出现异常:{}",response);
|
|
|
+ throw new CustomException("微信登录异常");
|
|
|
+ }
|
|
|
+ return returnVo;
|
|
|
+ }
|
|
|
+}
|