|
@@ -1,8 +1,10 @@
|
|
|
package com.hfln.portal.domain.customer.util;
|
|
|
|
|
|
+import cn.hfln.framework.common.redis.service.RedisService;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.google.gson.Gson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
@@ -11,8 +13,11 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.validation.constraints.NotBlank;
|
|
|
import java.io.IOException;
|
|
@@ -20,14 +25,24 @@ import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 微信公众号客户端
|
|
|
*/
|
|
|
@Slf4j
|
|
|
+@Service
|
|
|
@Component
|
|
|
public class WxOfficeAccountClient {
|
|
|
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ private static final String ACCESS_TOKEN_KEY = "wx:access_token";
|
|
|
+
|
|
|
@NotBlank
|
|
|
@Value("${lnxx.wechat.office.account.appId}")
|
|
|
private String appId;
|
|
@@ -42,6 +57,10 @@ public class WxOfficeAccountClient {
|
|
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
|
+ public WxOfficeAccountClient(RedisService redisService) {
|
|
|
+ this.redisService = redisService;
|
|
|
+ }
|
|
|
+
|
|
|
public void sendMsg(String devId, String devName, String phoneNo, String fwhOpenId, String msg) {
|
|
|
// 1. 获取 access_token
|
|
|
String accessToken = getAccessToken();
|
|
@@ -83,6 +102,13 @@ public class WxOfficeAccountClient {
|
|
|
}
|
|
|
|
|
|
private String getAccessToken() {
|
|
|
+ //1.尝试从Redis中获取access_token
|
|
|
+ String cachedToken = stringRedisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
|
|
|
+ if (StringUtils.isNotEmpty(cachedToken)) {
|
|
|
+ log.info("从Redis中获取access_token成功: {}", cachedToken);
|
|
|
+ return cachedToken;
|
|
|
+ }
|
|
|
+ //2.如果Redis中没有,则调用微信接口获取
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token" +
|
|
|
"?grant_type=client_credential" +
|
|
|
"&appid=" + appId +
|
|
@@ -94,13 +120,27 @@ public class WxOfficeAccountClient {
|
|
|
HttpEntity entity = response.getEntity();
|
|
|
String result = EntityUtils.toString(entity);
|
|
|
Map<String, Object> map = gson.fromJson(result, Map.class);
|
|
|
- log.info("access_token:{}",map.get("access_token"));
|
|
|
- return (String) map.get("access_token");
|
|
|
+
|
|
|
+ //3.判断是否有错误码返回
|
|
|
+ if (map.containsKey("errcode")) {
|
|
|
+ log.error("微信接口返回错误码:{}", result);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (map.containsKey("access_token")) {
|
|
|
+ String accessToken = (String) map.get("access_token");
|
|
|
+
|
|
|
+ //4.写入redis,有效期7080秒
|
|
|
+ stringRedisTemplate.opsForValue().set(ACCESS_TOKEN_KEY, accessToken, 7080, TimeUnit.SECONDS);
|
|
|
+ log.info("从微信中获取 access_token 并写入Redis:{}", accessToken);
|
|
|
+ return accessToken;
|
|
|
+ } else {
|
|
|
+ log.error("微信接口未返回 access_token, 原始响应:{}", result);
|
|
|
+ }
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- log.error("获取accessToke失败", e);
|
|
|
- return null;
|
|
|
+ log.error("调用微信接口异常,获取 access_token 失败", e);
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
private boolean sendTemplateMessage(String accessToken, String openid,
|
|
@@ -130,4 +170,10 @@ public class WxOfficeAccountClient {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //调用getaccess_token方法,给一键获取手机号登录使用
|
|
|
+ public String fetchAccessToken() {
|
|
|
+ return getAccessToken();
|
|
|
+ }
|
|
|
+
|
|
|
}
|