123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package com.care.common.util;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.care.client.dto.WxTemplateDTO;
- import com.care.client.dto.WxUniformTemplateDTO;
- import com.care.client.vo.WxAccessTokenVO;
- import com.care.common.cache.RedisKeyConstant;
- import com.google.gson.Gson;
- import com.google.gson.JsonSyntaxException;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.web.bind.annotation.RequestMethod;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSocketFactory;
- import javax.net.ssl.TrustManager;
- import java.io.*;
- import java.net.ConnectException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.security.SecureRandom;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- /**
- * 微信接口访问工具封装
- */
- public class WxQrcodeUtil {
- // 临时二维码
- private final static String QR_SCENE = "QR_SCENE";
- /**
- * 创建二维码
- */
- public final static String GEN_QRCODE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=ACCESS_TOKEN";
- /**
- * 创建二维码
- */
- public final static String SHOW_QRCODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
- /**
- * 创建临时带参数二维码
- * @param accessToken
- * @expireSeconds 该二维码有效时间,以秒为单位。 最大不超过2592000(即30天),此字段如果不填,则默认有效期为30秒。
- * @param sceneId 场景Id
- * @return
- */
- public static String createTempTicket(String accessToken, String expireSeconds, Integer sceneId) {
- TreeMap<String,String> params = new TreeMap<>();
- params.put("access_token", accessToken);
- Map<String,Integer> intMap = new HashMap<>();
- intMap.put("scene_id",sceneId);
- Map<String,Map<String,Integer>> mapMap = new HashMap<>();
- mapMap.put("scene", intMap);
- //
- Map<String,Object> paramsMap = new HashMap<>();
- paramsMap.put("expire_seconds", expireSeconds);
- paramsMap.put("action_name", QR_SCENE);
- paramsMap.put("action_info", mapMap);
- String url = GEN_QRCODE_URL.replace("ACCESS_TOKEN", accessToken);
- JSONObject resultJsonObj = WxTemplateUtil.httpRequest(url, "POST", JSONUtil.toJsonStr(paramsMap));
- return resultJsonObj == null ? null : showQrcode(resultJsonObj.getStr("ticket"));
- }
- /**
- * 获取二维码ticket后,通过ticket换取二维码图片展示
- * @param ticket
- * @return
- */
- public static String showQrcode(String ticket) {
- Map<String,String> params = new TreeMap<>();
- try {
- params.put("ticket", URLEncoder.encode(ticket, "utf-8"));
- return setParmas(params, SHOW_QRCODE_URL,"utf-8");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
- // public static void main(String[] args) {
- //
- // WxQrcodeUtil.createTempTicket("51_D55rGmfv71A8PjThis1K2V4ffC6xz1V-uE-VJiOvyA31P0zISnCsTrM319NDNLP5h-EEXIAZeVqCrpkA4VmglSRdUUNAKX2pdav7ayhwLHQh3BsdkDGF2MvcFRKIXdqCT1wla1R-3yYkKYqYXZJaAGABDN", "172800", 799);
- // }
- /**
- * 设置参数
- *
- * @param map
- * 参数map
- * @param path
- * 需要赋值的path
- * @param charset
- * 编码格式 默认编码为utf-8(取消默认)
- * @return 已经赋值好的url 只需要访问即可
- */
- public static String setParmas(Map<String, String> map, String path, String charset) throws Exception {
- String result = "";
- boolean hasParams = false;
- if (path != null && !"".equals(path)) {
- if (map != null && map.size() > 0) {
- StringBuilder builder = new StringBuilder();
- Set<Map.Entry<String, String>> params = map.entrySet();
- for (Map.Entry<String, String> entry : params) {
- String key = entry.getKey().trim();
- String value = entry.getValue().trim();
- if (hasParams) {
- builder.append("&");
- } else {
- hasParams = true;
- }
- if(charset != null && !"".equals(charset)){
- //builder.append(key).append("=").append(URLDecoder.(value, charset));
- builder.append(key).append("=").append(URLEncoder.encode(value, "utf-8"));
- }else{
- builder.append(key).append("=").append(value);
- }
- }
- result = builder.toString();
- }
- }
- return doUrlPath(path, result).toString();
- }
- /**
- * 设置连接参数
- *
- * @param path
- * 路径
- * @return
- */
- private static URL doUrlPath(String path, String query) throws Exception {
- URL url = new URL(path);
- if (StringUtils.isEmpty(path)) {
- return url;
- }
- if (StringUtils.isEmpty(url.getQuery())) {
- if (path.endsWith("?")) {
- path += query;
- } else {
- path = path + "?" + query;
- }
- } else {
- if (path.endsWith("&")) {
- path += query;
- } else {
- path = path + "&" + query;
- }
- }
- return new URL(path);
- }
- }
|