WxAppUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package platform.common.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.xiaoleilu.hutool.http.HttpUtil;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClientBuilder;
  10. import org.apache.http.protocol.HTTP;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * 微信小程序的工具类
  17. */
  18. public class WxAppUtil {
  19. // 获取openid的链接
  20. public static final String CODE2_SESSION_URL_STRING = "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code";
  21. // 获取access_token
  22. public static final String ACCESS_TOKEN_URL_STRING = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}";
  23. // 获取图片地址
  24. public static final String CREATE_QRCODE_URL_STRING = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={}";
  25. public static final String GET_QRCODE_URL_STRING = "https://api.weixin.qq.com/wxa/getwxacode?access_token={}";
  26. public static final String GET_UNLIMITED_QRCODE_URL_STRING = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={}";
  27. public static final String SESSION_KEY = "session_key";
  28. // 获取完整获取openid的接口url
  29. public static String getCode2SessionURLString(String appid, String appsecret, String code) {
  30. return StringUtils.format(CODE2_SESSION_URL_STRING, appid, appsecret, code);
  31. }
  32. // 获取access_token接口返回
  33. public static String getAccessToken(String appid, String appSecret) {
  34. String realURLString = StringUtils.format(ACCESS_TOKEN_URL_STRING, appid, appSecret);
  35. return HttpUtil.get(realURLString);
  36. }
  37. public static String getSessionKey(String appid, String appsecret, String code) {
  38. JSONObject jsonObject = JSONObject.parseObject(HttpUtil.get(getCode2SessionURLString(appid, appsecret, code)));
  39. System.out.println("appid = " + appid);
  40. System.out.println("appsecret = " + appsecret);
  41. System.out.println("code = " + code);
  42. System.out.println(jsonObject.toString());
  43. if (jsonObject.containsKey(SESSION_KEY)) {
  44. return (String) jsonObject.get(SESSION_KEY);
  45. }
  46. return null;
  47. }
  48. /**
  49. * 创建二维码
  50. *
  51. * @param accessToken
  52. */
  53. public static InputStream createQRCode(String accessToken, String path, String width) throws IOException {
  54. Map<String, Object> params = new HashMap<>();
  55. params.put("path", path);
  56. params.put("width", width);
  57. return getInputStreamFromUrl(CREATE_QRCODE_URL_STRING, params, accessToken);
  58. }
  59. public static InputStream getQRCode(String accessToken, String path, String width) throws IOException {
  60. Map<String, Object> params = new HashMap<>();
  61. params.put("path", path);
  62. params.put("width", width);
  63. return getInputStreamFromUrl(GET_QRCODE_URL_STRING, params, accessToken);
  64. }
  65. public static InputStream getUnlimitedQRCode(String accessToken, String path, String width, String scene) throws IOException {
  66. Map<String, Object> params = new HashMap<>();
  67. params.put("page", path);
  68. params.put("width", width);
  69. params.put("scene", scene);
  70. return getInputStreamFromUrl(GET_UNLIMITED_QRCODE_URL_STRING, params, accessToken);
  71. }
  72. private static InputStream getInputStreamFromUrl(String not_parsed_url, Map params, String accessToken) throws IOException {
  73. String url = StringUtils.format(not_parsed_url, accessToken);
  74. CloseableHttpClient httpClient = HttpClientBuilder.create().build();
  75. HttpPost httpPost = new HttpPost(url); // 接口
  76. httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
  77. String body = JSON.toJSONString(params); //必须是json模式的 post
  78. StringEntity entity;
  79. entity = new StringEntity(body);
  80. entity.setContentType("image/png");
  81. httpPost.setEntity(entity);
  82. HttpResponse response;
  83. response = httpClient.execute(httpPost);
  84. InputStream inputStream = response.getEntity().getContent();
  85. return inputStream;
  86. }
  87. }