|
@@ -3,8 +3,11 @@ package com.flyer.foster.util;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.flyer.exception.BusinessException;
|
|
|
+import com.flyer.foster.consts.RedisKeyConst;
|
|
|
+import com.flyer.foster.consts.WechatConst;
|
|
|
import com.flyer.foster.converter.WxMappingJackson2HttpMessageConverter;
|
|
|
import com.flyer.foster.pojo.Code2SessionResp;
|
|
|
+import com.flyer.util.RedisUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpEntity;
|
|
@@ -16,6 +19,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
@@ -29,6 +33,9 @@ public class WeChatApiUtil {
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void initRestTemplate() {
|
|
|
// 解决调用微信服务返回信息无法反序列化
|
|
@@ -63,8 +70,7 @@ public class WeChatApiUtil {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- public JSONObject getAccessToken(String appId, String secret) {
|
|
|
- String accessToken = null;
|
|
|
+ public JSONObject getAccessToken1(String appId, String secret) {
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type={grantType}&appid={appId}&secret={secret}";
|
|
|
HashMap<String, String> map = new HashMap<>();
|
|
|
map.put("grantType", "client_credential");
|
|
@@ -80,6 +86,33 @@ public class WeChatApiUtil {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+ public String getAccessToken(String appId, String secret) {
|
|
|
+ // 从缓存中获取accessToken
|
|
|
+ Object accessTokenCache = redisUtil.get(RedisKeyConst.WECHAT_ACCESS_TOKEN_KEY + appId);
|
|
|
+ if (accessTokenCache == null) {
|
|
|
+ String accessToken;
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type={grantType}&appid={appId}&secret={secret}";
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("grantType", "client_credential");
|
|
|
+ map.put("appId", appId);
|
|
|
+ map.put("secret", secret);
|
|
|
+ JSONObject res;
|
|
|
+ try {
|
|
|
+ res = restTemplate.getForObject(url, JSONObject.class, map);
|
|
|
+ // token失效时间
|
|
|
+ Integer expires = res.getInteger("expires_in");
|
|
|
+ long cacheExpires = LocalDateTime.now().plusSeconds(expires - 100).toEpochSecond(ZoneOffset.of("+8"));
|
|
|
+ accessToken = res.getString("access_token");
|
|
|
+ redisUtil.set(RedisKeyConst.WECHAT_ACCESS_TOKEN_KEY + appId, accessToken, cacheExpires);
|
|
|
+ } catch (RestClientException e) {
|
|
|
+ throw new BusinessException("调用微信服务错误");
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ } else {
|
|
|
+ return accessTokenCache.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// public String getPhoneNumber(String appId, String appSecret, String code) {
|
|
|
// String accessToken = this.getAccessToken(appId, appSecret);
|
|
|
// String tel = "";
|
|
@@ -101,7 +134,15 @@ public class WeChatApiUtil {
|
|
|
// return tel;
|
|
|
// }
|
|
|
|
|
|
- public Map<String, Object> subscribeMessage(String accessToken, String openId) {
|
|
|
+ /**
|
|
|
+ * 给订阅用户发送消息
|
|
|
+ *
|
|
|
+ * @param accessToken
|
|
|
+ * @param openId
|
|
|
+ * @param templateId 消息模板id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> subscribeMessage(String accessToken, String openId, String templateId) {
|
|
|
// 首先获取微信调用凭证
|
|
|
if (null == accessToken) {
|
|
|
throw new BusinessException("获取微信调用凭证失败");
|
|
@@ -109,15 +150,19 @@ public class WeChatApiUtil {
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
|
|
|
|
|
HashMap<String, Object> param = new HashMap<>();
|
|
|
- param.put("template_id", "4HrnTVgSa1LU1m5a-2ySDz_1N6Ndi3WnkV6rRsV3ROM");
|
|
|
+ param.put("template_id", templateId);
|
|
|
param.put("touser", openId);
|
|
|
param.put("page", "pages/subPages/login/index");
|
|
|
- JSONObject data = new JSONObject();
|
|
|
- data.put("thing1", setVal("value", "test12399"));
|
|
|
- data.put("date2", setVal("value", "2024年10月1日"));
|
|
|
- param.put("data", data);
|
|
|
param.put("miniprogram_state", "trial");
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ // 留言内容
|
|
|
+ data.put("thing2", setVal("value", "test12399"));
|
|
|
+ // 留言人
|
|
|
+ data.put("name1", setVal("value", "守护一棵树"));
|
|
|
+ // 留言时间
|
|
|
+ data.put("time3", setVal("value", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
|
|
|
|
|
|
+ param.put("data", data);
|
|
|
try {
|
|
|
// 微信调用返回结果
|
|
|
JSONObject res = restTemplate.postForObject(url, param, JSONObject.class);
|