微信小程序-获取openId/accessToken/订阅消息等接口
工具类
package test;
import com.fasterxml.jackson.databind.ObjectMapper;
import test.util.RequestSender;
importtest.response.Code2SessionResponse;
import test.response.GetAccessTokenResponse;
import test.response.SubscribeMessageSendResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
public class WxAppletHelper {
private static final String APPID = "wx6985e45642d2sdfs";
private static final String SECRET = "456648212asdfsdf5a4f6sdf4s2df1";
private static final String Template_Id = "Ru8464564asd1f2asdf456sd4fs2d1fs2fs3-Tg";
private static final String Page = "pages/index/index";
private static final String URL_Code2Session = "https://api.weixin.qq.com/sns/jscode2session?appid={APPID}&secret={SECRET}&js_code={JSCODE}&grant_type=authorization_code";
private static final String URL_GetAccessToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={SECRET}";
private static final String URL_SubscribeMessageSend = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={ACCESS_TOKEN}";
public static Code2SessionResponse authCode2Session(String code) throws IOException, URISyntaxException {
RequestSender requestSender = RequestSender.builder().url(URL_Code2Session.replace("{APPID}", APPID).replace("{SECRET}", SECRET).replace("{JSCODE}", code)).build();
requestSender.doRequest();
String result = requestSender.getResult();
Code2SessionResponse code2SessionResponse = new ObjectMapper().readValue(result, Code2SessionResponse.class);
return code2SessionResponse;
}
public static GetAccessTokenResponse getAccessToken() throws IOException, URISyntaxException {
RequestSender requestSender = RequestSender.builder().url(URL_GetAccessToken.replace("{APPID}", APPID).replace("{SECRET}", SECRET)).build();
requestSender.doRequest();
String result = requestSender.getResult();
GetAccessTokenResponse accessTokenResponse = new ObjectMapper().readValue(result, GetAccessTokenResponse.class);
return accessTokenResponse;
}
public static SubscribeMessageSendResponse subscribeMessageSend(String openId, Map<String, Object> params) throws IOException, URISyntaxException {
GetAccessTokenResponse accessToken = getAccessToken();
RequestSender requestSender = RequestSender.builder()
.url(URL_SubscribeMessageSend.replace("{ACCESS_TOKEN}", accessToken.getAccess_token()))
.method("POST")
.jsonFormated(true)
.param("touser", openId)
.param("template_id", Template_Id)
.param("page", Page)
.param("data", params)
.build();
requestSender.doRequest();
String result = requestSender.getResult();
SubscribeMessageSendResponse subscribeMessageSendResponse = new ObjectMapper().readValue(result, SubscribeMessageSendResponse.class);
return subscribeMessageSendResponse;
}
public static void main(String[] args) throws IOException, URISyntaxException {
Code2SessionResponse code2SessionResponse = WxAppletHelper.authCode2Session("12345");
System.out.println(code2SessionResponse.toString());
Map<String, Object> params = new HashMap<String, Object>();
String time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
params.put("date1", new HashMap<String, Object>() {
{
put("value", time);
}
});
params.put("thing1", new HashMap<String, Object>() {
{
put("value", "消息已回复,请查看详情");
}
});
WxAppletHelper.subscribeMessageSend("oX5-i5Te8rGikPNstFKH47G1ufp8", params);
}
}
各接口响应数据类
package test.response;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class Code2SessionResponse {
private String openid;
private String session_key;
private String unionid;
private Integer errcode;
private String errmsg;
}
package test.response;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class GetAccessTokenResponse {
private String access_token;
private Long expires_in;
private Integer errcode;
private String errmsg;
}
package test.response;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class SubscribeMessageSendResponse {
private Integer errcode;
private String errmsg;
}
其中的RequestSender工具类的使用,参照该文章HttpClient的使用
其他微信公众平台使用小程序时需要配置的内容
@GetMapping("/get")
public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("==================get====================");
Map<String, String[]> parameterMap = request.getParameterMap();
if(parameterMap != null && !parameterMap.isEmpty()){
Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
for(Map.Entry<String,String[]> entry : entries){
System.out.println(entry.getKey() + ":" + Arrays.toString(entry.getValue()));
}
}
String echostr = request.getParameter("echostr");
response.getWriter().print(echostr == null ? "" : echostr);
}
@PostMapping("/post")
public void post(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("==================post====================");
Map<String, String[]> parameterMap = request.getParameterMap();
if(parameterMap != null && !parameterMap.isEmpty()){
Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
for(Map.Entry<String,String[]> entry : entries){
System.out.println(entry.getKey() + ":" + Arrays.toString(entry.getValue()));
}
}
response.getWriter().print("success");
}
|