|
@@ -1,10 +1,12 @@
|
|
|
package com.fjhx.utils;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fjhx.constants.WxAppletConstant;
|
|
|
import lombok.Data;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
+import org.springblade.core.redis.cache.BladeRedis;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
import org.springframework.web.client.ResourceAccessException;
|
|
@@ -17,7 +19,7 @@ import java.util.HashMap;
|
|
|
public class WxAppletUtil {
|
|
|
|
|
|
private static final RestTemplate restTemplate = new RestTemplate();
|
|
|
- // private static final BladeRedis bladeRedis = SpringUtil.getBean(BladeRedis.class);
|
|
|
+ private static final BladeRedis bladeRedis = SpringUtil.getBean(BladeRedis.class);
|
|
|
private static final String APPLET_ACCESS_TOKEN_KEY = "wxAppletAccessTokenKey:";
|
|
|
|
|
|
static {
|
|
@@ -50,31 +52,33 @@ public class WxAppletUtil {
|
|
|
String redisKey = APPLET_ACCESS_TOKEN_KEY + url;
|
|
|
|
|
|
// 缓存获取accessToken
|
|
|
-// String accessToken = bladeRedis.get(redisKey);
|
|
|
-// if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
-// return accessToken;
|
|
|
-// }
|
|
|
+ String accessToken = bladeRedis.get(redisKey);
|
|
|
+ if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
|
|
|
- AccessTokenEntity body;
|
|
|
+ AccessTokenResult body;
|
|
|
try {
|
|
|
- body = restTemplate.getForEntity(url, AccessTokenEntity.class).getBody();
|
|
|
+ body = restTemplate.getForEntity(url, AccessTokenResult.class).getBody();
|
|
|
} catch (ResourceAccessException ignored) {
|
|
|
throw new ServiceException("网络连接失败");
|
|
|
}
|
|
|
|
|
|
+ String errorStr = "微信小程序获取accessToken失败";
|
|
|
if (body == null) {
|
|
|
- throw new ServiceException("微信小程序获取accessToken失败");
|
|
|
+ throw new ServiceException(errorStr);
|
|
|
}
|
|
|
|
|
|
- if (ObjectUtil.isNotEmpty(body.getErrcode())) {
|
|
|
- throw new ServiceException("微信小程序获取accessToken失败, errorcode= " + body.getErrcode() + " ,errormsg= " + body.getErrmsg());
|
|
|
+ Integer errcode = body.getErrcode();
|
|
|
+ if (errcode != null) {
|
|
|
+ throw new ServiceException(errorStr + ", errorcode= " + errcode + " ,errormsg= " + body.getErrmsg());
|
|
|
}
|
|
|
|
|
|
String access_token = body.getAccess_token();
|
|
|
Integer expires_in = body.getExpires_in();
|
|
|
|
|
|
// 赋值缓存
|
|
|
-// bladeRedis.setEx(redisKey, access_token, new Double(expires_in * 0.8).longValue());
|
|
|
+ bladeRedis.setEx(redisKey, access_token, new Double(expires_in * 0.8).longValue());
|
|
|
|
|
|
return access_token;
|
|
|
}
|
|
@@ -82,39 +86,31 @@ public class WxAppletUtil {
|
|
|
|
|
|
/**
|
|
|
* 小程序发送消息
|
|
|
- * TODO 测试中
|
|
|
*/
|
|
|
- public static void sendMessage(String openId) {
|
|
|
-
|
|
|
- UniformSendEntity uniformSendEntity = new UniformSendEntity();
|
|
|
- uniformSendEntity.setTouser(openId);
|
|
|
- uniformSendEntity.setTemplate_id("Tt0C6SOrtd2QXIdLqrm0CNdarUE4LG_lNJblUnkqTPM");
|
|
|
- uniformSendEntity.setPage("cloudApi/supplier/list");
|
|
|
-
|
|
|
- HashMap<Object, Object> data = new HashMap<>();
|
|
|
- data.put("thing1", "test1");
|
|
|
- data.put("thing2", "test2");
|
|
|
- data.put("time3", "test3");
|
|
|
-
|
|
|
- String dataStr = JSONObject.toJSONString(data);
|
|
|
- uniformSendEntity.setData(dataStr);
|
|
|
-
|
|
|
+ public static void sendMessage(UniformSendEntity uniformSendEntity) {
|
|
|
String sendMessageUrl = WxAppletConstant.SEND_MESSAGE_URL + getAccessToken(WxAppletConstant.GET_ACCESS_TOKEN_URL);
|
|
|
-
|
|
|
- String s = restTemplate.postForObject(sendMessageUrl, uniformSendEntity, String.class);
|
|
|
- System.out.println(s);
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- sendMessage("odMjv5Q66Euc7WdtzJVdm2G8QqOc");
|
|
|
+ restTemplate.postForObject(sendMessageUrl, uniformSendEntity, String.class);
|
|
|
}
|
|
|
|
|
|
+// public static void main(String[] args) {
|
|
|
+//
|
|
|
+// UniformSendEntity uniformSendEntity = new UniformSendEntity();
|
|
|
+// // openId
|
|
|
+// uniformSendEntity.setTouser("odMjv5Yndizr4ybYaShMt9s9GMVI");
|
|
|
+// // 赋值模板id
|
|
|
+// uniformSendEntity.setTemplate_id("Tt0C6SOrtd2QXIdLqrm0CNdarUE4LG_lNJblUnkqTPM");
|
|
|
+// // 跳转路径
|
|
|
+// uniformSendEntity.setPage("pages/user/login");
|
|
|
+// // 模板信息
|
|
|
+// uniformSendEntity.addData("thing1", "小小的一个测试");
|
|
|
+// uniformSendEntity.addData("thing2", "测试啥我也不懂");
|
|
|
+// uniformSendEntity.addData("time3", DateUtil.format(new Date(), "yyyy年MM月dd日 HH:mm"));
|
|
|
+//
|
|
|
+// sendMessage(uniformSendEntity);
|
|
|
+// }
|
|
|
|
|
|
@Data
|
|
|
- public static class AccessTokenEntity implements Serializable {
|
|
|
+ public static class AccessTokenResult implements Serializable {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
// 获取到的凭证
|
|
@@ -131,7 +127,7 @@ public class WxAppletUtil {
|
|
|
public static class UniformSendEntity implements Serializable {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
- // 用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid
|
|
|
+ // 用户openid,可以是小程序的openid
|
|
|
private String touser;
|
|
|
// 小程序模板ID
|
|
|
private String template_id;
|
|
@@ -139,8 +135,15 @@ public class WxAppletUtil {
|
|
|
private String page;
|
|
|
// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
|
|
private String miniprogram_state;
|
|
|
- // 小程序模板数据
|
|
|
- private String data;
|
|
|
+ // 模板参数
|
|
|
+ private HashMap<String, Object> data = new HashMap<>();
|
|
|
+
|
|
|
+ public void addData(String key, String value) {
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("value", value);
|
|
|
+ data.put(key, map);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|