|
@@ -7,16 +7,18 @@ import lombok.Data;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
+import org.springframework.web.client.ResourceAccessException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
import java.util.Arrays;
|
|
|
+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 String APPLET_ACCESS_TOKEN_KEY = "appletAccessTokenKey";
|
|
|
+ private static final String APPLET_ACCESS_TOKEN_KEY = "wxAppletAccessTokenKey:";
|
|
|
|
|
|
static {
|
|
|
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
|
|
@@ -25,23 +27,40 @@ public class WxAppletUtil {
|
|
|
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
|
|
|
}
|
|
|
|
|
|
- public static String getOpenId(String appid, String secret, String code) {
|
|
|
-
|
|
|
- String wxResult = restTemplate.getForObject("https://api.weixin.qq.com/sns/jscode2session?appid="
|
|
|
- + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code", String.class);
|
|
|
+ /**
|
|
|
+ * 获取openId
|
|
|
+ */
|
|
|
+ public static String getOpenId(String url, String code) {
|
|
|
+ String wxResult;
|
|
|
+ try {
|
|
|
+ wxResult = restTemplate.getForObject(String.format(url, code), String.class);
|
|
|
+ } catch (ResourceAccessException ignored) {
|
|
|
+ throw new ServiceException("网络连接失败");
|
|
|
+ }
|
|
|
|
|
|
JSONObject wxResultMap = JSONObject.parseObject(wxResult);
|
|
|
-
|
|
|
return wxResultMap.get("openid").toString();
|
|
|
}
|
|
|
|
|
|
- private static String getAccessToken() {
|
|
|
-// String accessToken = bladeRedis.get(APPLET_ACCESS_TOKEN_KEY);
|
|
|
+ /**
|
|
|
+ * 获取accessToken
|
|
|
+ */
|
|
|
+ public synchronized static String getAccessToken(String url) {
|
|
|
+
|
|
|
+ String redisKey = APPLET_ACCESS_TOKEN_KEY + url;
|
|
|
+
|
|
|
+ // 缓存获取accessToken
|
|
|
+// String accessToken = bladeRedis.get(redisKey);
|
|
|
// if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
// return accessToken;
|
|
|
// }
|
|
|
- AccessTokenEntity body = restTemplate.getForEntity(WxAppletConstant.GET_ACCESS_TOKEN_URL, AccessTokenEntity.class).getBody();
|
|
|
|
|
|
+ AccessTokenEntity body;
|
|
|
+ try {
|
|
|
+ body = restTemplate.getForEntity(url, AccessTokenEntity.class).getBody();
|
|
|
+ } catch (ResourceAccessException ignored) {
|
|
|
+ throw new ServiceException("网络连接失败");
|
|
|
+ }
|
|
|
|
|
|
if (body == null) {
|
|
|
throw new ServiceException("微信小程序获取accessToken失败");
|
|
@@ -52,20 +71,52 @@ public class WxAppletUtil {
|
|
|
}
|
|
|
|
|
|
String access_token = body.getAccess_token();
|
|
|
+ Integer expires_in = body.getExpires_in();
|
|
|
|
|
|
+ // 赋值缓存
|
|
|
+// bladeRedis.setEx(redisKey, access_token, new Double(expires_in * 0.8).longValue());
|
|
|
|
|
|
return access_token;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- AccessTokenEntity body = restTemplate.getForObject(WxAppletConstant.GET_ACCESS_TOKEN_URL, AccessTokenEntity.class);
|
|
|
|
|
|
+ /**
|
|
|
+ * 小程序发送消息
|
|
|
+ * 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);
|
|
|
+
|
|
|
+ String sendMessageUrl = WxAppletConstant.SEND_MESSAGE_URL + getAccessToken(WxAppletConstant.GET_ACCESS_TOKEN_URL);
|
|
|
|
|
|
- System.out.println(body);
|
|
|
+ String s = restTemplate.postForObject(sendMessageUrl, uniformSendEntity, String.class);
|
|
|
+ System.out.println(s);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ sendMessage("odMjv5Q66Euc7WdtzJVdm2G8QqOc");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Data
|
|
|
public static class AccessTokenEntity implements Serializable {
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
// 获取到的凭证
|
|
|
private String access_token;
|
|
|
// 凭证有效时间,单位:秒。目前是7200秒之内的值。
|
|
@@ -76,4 +127,20 @@ public class WxAppletUtil {
|
|
|
private String errmsg;
|
|
|
}
|
|
|
|
|
|
+ @Data
|
|
|
+ public static class UniformSendEntity implements Serializable {
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
+ // 用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid
|
|
|
+ private String touser;
|
|
|
+ // 小程序模板ID
|
|
|
+ private String template_id;
|
|
|
+ // 小程序页面路径
|
|
|
+ private String page;
|
|
|
+ // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
|
|
+ private String miniprogram_state;
|
|
|
+ // 小程序模板数据
|
|
|
+ private String data;
|
|
|
+ }
|
|
|
+
|
|
|
}
|