|
@@ -0,0 +1,79 @@
|
|
|
+package com.fjhx.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fjhx.constants.WxAppletConstant;
|
|
|
+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.RestTemplate;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+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";
|
|
|
+
|
|
|
+ static {
|
|
|
+ MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
|
|
|
+ mappingJackson2HttpMessageConverter.setSupportedMediaTypes(
|
|
|
+ Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM, MediaType.TEXT_HTML));
|
|
|
+ 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);
|
|
|
+
|
|
|
+ JSONObject wxResultMap = JSONObject.parseObject(wxResult);
|
|
|
+
|
|
|
+ return wxResultMap.get("openid").toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getAccessToken() {
|
|
|
+// String accessToken = bladeRedis.get(APPLET_ACCESS_TOKEN_KEY);
|
|
|
+// if (ObjectUtil.isNotEmpty(accessToken)) {
|
|
|
+// return accessToken;
|
|
|
+// }
|
|
|
+ AccessTokenEntity body = restTemplate.getForEntity(WxAppletConstant.GET_ACCESS_TOKEN_URL, AccessTokenEntity.class).getBody();
|
|
|
+
|
|
|
+
|
|
|
+ if (body == null) {
|
|
|
+ throw new ServiceException("微信小程序获取accessToken失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(body.getErrcode())) {
|
|
|
+ throw new ServiceException("微信小程序获取accessToken失败, errorcode= " + body.getErrcode() + " ,errormsg= " + body.getErrmsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ String access_token = body.getAccess_token();
|
|
|
+
|
|
|
+
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ AccessTokenEntity body = restTemplate.getForObject(WxAppletConstant.GET_ACCESS_TOKEN_URL, AccessTokenEntity.class);
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println(body);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ public static class AccessTokenEntity implements Serializable {
|
|
|
+ // 获取到的凭证
|
|
|
+ private String access_token;
|
|
|
+ // 凭证有效时间,单位:秒。目前是7200秒之内的值。
|
|
|
+ private Integer expires_in;
|
|
|
+ // 错误码
|
|
|
+ private Integer errcode;
|
|
|
+ // 错误信息
|
|
|
+ private String errmsg;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|