From f8bd1c406ef31027967c0cfa8c2f5f097ae93ec9 Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期二, 07 十一月 2023 16:19:33 +0800 Subject: [PATCH] 群联系方式配置管理 --- src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 159 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java b/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java index 8a52d18..69e0c4a 100644 --- a/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java +++ b/src/main/java/com/hx/mp/util/MPWeixinBaseUtil.java @@ -3,6 +3,8 @@ import com.hx.util.OSSUtil; import com.hx.util.SimpleTool; import com.hx.util.StringUtils; +import com.hz.util.http.HttpHzUtil; +import com.hz.util.http.dto.HttpHzResponse; import net.sf.json.JSONException; import net.sf.json.JSONObject; import org.apache.commons.codec.binary.Base64; @@ -14,6 +16,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -34,12 +37,16 @@ private static final String JSCODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?"; /**鍙戦�佽闃呮秷鎭�氱煡閾炬帴*/ private static final String SEND_SUBSCRIBE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; - //鐢熸垚灏忕▼搴忎簩缁寸爜鍦板潃锛堟柟褰級 + /**鐢熸垚灏忕▼搴忎簩缁寸爜鍦板潃锛堟柟褰級*/ public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; - //鐢熸垚灏忕▼搴忎簩缁寸爜鍦板潃锛堝渾褰級 + /**鐢熸垚灏忕▼搴忎簩缁寸爜鍦板潃锛堝渾褰級*/ public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; - //鐢熸垚鏃犻檺浜岀淮鐮� + /**鐢熸垚鏃犻檺浜岀淮鐮�*/ public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; + /**鑾峰彇灏忕▼搴忚闃呬釜浜烘ā鏉垮垪琛�*/ + private static final String GET_APP_TEMPLATE = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token="; + /**鑾峰彇鍏紬鍙锋ā鏉垮垪琛�*/ + private static final String GET_GZH_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="; ////////////////////////////////////////////////// /** (灏忕▼搴�)閫氳繃code鎹㈠彇缃戦〉鎺堟潈access_token/濡傛灉鏄痵nsapi_base妯″紡鐨勬巿鏉冿紝杩欓噷灏卞彲浠ユ嬁鍒皁penId浜� ***/ @@ -178,6 +185,110 @@ obj.put("auto_color", autoColor); obj.put("line_color", lineColor); obj.put("is_hyaline", isHyaline); + + // 鍒涘缓url璧勬簮 + URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); + // 寤虹珛http杩炴帴 + conn = (HttpURLConnection) url.openConnection(); + // 璁剧疆鍏佽杈撳嚭 + conn.setDoOutput(true); + conn.setDoInput(true); + // 璁剧疆涓嶇敤缂撳瓨 + conn.setUseCaches(false); + // 璁剧疆浼犻�掓柟寮� + conn.setRequestMethod("POST"); + // 璁剧疆缁存寔闀胯繛鎺� + conn.setRequestProperty("Connection", "Keep-Alive"); + // 璁剧疆鏂囦欢瀛楃闆�: + conn.setRequestProperty("Charset", "UTF-8"); + // 璁剧疆鏂囦欢绫诲瀷: + conn.setRequestProperty("contentType", "application/json"); + // 寮�濮嬭繛鎺ヨ姹� + conn.connect(); + OutputStream out = conn.getOutputStream(); + // 鍐欏叆璇锋眰鐨勫瓧绗︿覆 + out.write((obj.toString()).getBytes()); + out.flush(); + out.close(); + + // 璇锋眰杩斿洖鐨勭姸鎬� + if (conn.getResponseCode() == 200) { + // 璇锋眰杩斿洖鐨勬暟鎹� + in = conn.getInputStream(); + //杈撳叆鍒颁复鏃舵枃浠� + /*OutputStream os = new FileOutputStream(file); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + }*/ + FileUtils.copyInputStreamToFile(in, file); + + conn.disconnect(); + conn = null; + } + + if (in != null) { + in.close(); + in = null; + } + + if (conn != null) { + conn.disconnect(); + conn = null; + } + }catch (Exception e) { + e.printStackTrace(); + if (in != null) { + try { + in.close(); + }catch (Exception ep) { + ep.printStackTrace(); + } + in = null; + } + + if (conn != null) { + conn.disconnect(); + conn = null; + } + }finally { + if(file != null){ + file.deleteOnExit(); + } + } + return file; + } + + + /**鐢熸垚鏈夐檺浜岀淮鐮�,杩斿洖涓存椂鏂� + * @param at 寰俊token + * @param page 璺宠浆閾炬帴 + * @param width 瀹藉害 + * @param autoColor 榛樿false + * @param lineColor 榛樿null + * @param isHyaline 榛樿false + * @param envVersion 瑕佹墦寮�鐨勫皬绋嬪簭鐗堟湰銆傛寮忕増涓� release锛屼綋楠岀増涓� trial锛屽紑鍙戠増涓� develop + * @return + */ + public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, + JSONObject lineColor, boolean isHyaline,String envVersion) { + String imgUrl = null; + InputStream in = null; + HttpURLConnection conn = null; + + //鍒涘缓涓存椂鏂囦欢 + File file = null; + try { + file = File.createTempFile("temp", ".jpg"); + //鐢熸垚鍙戦�佹暟鎹� + JSONObject obj = new JSONObject(); + obj.put("width", width); + obj.put("path", page); + obj.put("auto_color", autoColor); + obj.put("line_color", lineColor); + obj.put("is_hyaline", isHyaline); + obj.put("env_version", envVersion); // 鍒涘缓url璧勬簮 URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); @@ -606,14 +717,25 @@ } - /**鐢熸垚鏃犻檺浜岀淮鐮侊紝骞朵笂浼犲埌OSS*/ - public static String createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, - JSONObject lineColor, boolean isHyaline, String fileName, String keyId, - String keySecret, String endPoint, String bucket, String env_version) - { + + /**鐢熸垚鏃犻檺浜岀淮鐮�,杩斿洖涓存椂鏂� + * @param at 寰俊token + * @param scene 鍙傛暟锛屽彧鑳�32浣嶏紝鏈�濂戒笉瑕佷腑鏂� + * @param page 璺宠浆閾炬帴 + * @param width 瀹藉害 + * @param autoColor 榛樿false + * @param lineColor 榛樿null + * @param isHyaline 榛樿false + * @return + */ + public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, + JSONObject lineColor, boolean isHyaline, String env_version) throws IOException { String imgUrl = null; InputStream in = null; HttpURLConnection conn = null; + + //鍒涘缓涓存椂鏂囦欢 + File file = File.createTempFile("temp", ".jpg"); try { //鐢熸垚鍙戦�佹暟鎹� @@ -654,7 +776,15 @@ if (conn.getResponseCode() == 200) { // 璇锋眰杩斿洖鐨勬暟鎹� in = conn.getInputStream(); - imgUrl = OSSUtil.uploadImg(fileName, in, keyId, keySecret, endPoint, bucket); + //杈撳叆鍒颁复鏃舵枃浠� + /*OutputStream os = new FileOutputStream(file); + int bytesRead = 0; + byte[] buffer = new byte[8192]; + while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { + os.write(buffer, 0, bytesRead); + }*/ + FileUtils.copyInputStreamToFile(in, file); + conn.disconnect(); conn = null; } @@ -668,15 +798,12 @@ conn.disconnect(); conn = null; } - }catch (Exception e) - { + }catch (Exception e) { e.printStackTrace(); - if (in != null) { try { in.close(); - }catch (Exception ep) - { + }catch (Exception ep) { ep.printStackTrace(); } in = null; @@ -686,9 +813,26 @@ conn.disconnect(); conn = null; } + }finally { + file.deleteOnExit(); } + return file; + } - return imgUrl; + /** + * 鑾峰彇灏忕▼搴忚闃呮ā鏉� + * @param accessToken token + * **/ + public static HttpHzResponse appTemplateList(String accessToken) { + return HttpHzUtil.HttpURLUtilJson(GET_APP_TEMPLATE + accessToken, null,null,null,"GET",null); + } + + /** + * 鍏紬鍙疯闃呮ā鏉� + * @param accessToken token + * **/ + public static HttpHzResponse gzhTemplateList(String accessToken) { + return HttpHzUtil.HttpURLUtilJson(GET_GZH_TEMPLATE+accessToken, null,null,null,"GET",null); } } -- Gitblit v1.8.0