From 240ec6cefeda101e6cf5eff64c291b1b9eb94642 Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期三, 10 四月 2024 10:35:51 +0800 Subject: [PATCH] 1.新增微信公众号工具类 --- src/main/java/com/hx/wx/gzh/WxGzhUtil.java | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 174 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/wx/gzh/WxGzhUtil.java b/src/main/java/com/hx/wx/gzh/WxGzhUtil.java new file mode 100644 index 0000000..f83aa4d --- /dev/null +++ b/src/main/java/com/hx/wx/gzh/WxGzhUtil.java @@ -0,0 +1,174 @@ +package com.hx.wx.gzh; + +import com.hx.common.service.CommonService; +import com.hx.exception.TipsException; +import com.hx.util.HttpUtil; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * 寰俊鍏紬鍙峰伐鍏风被 + * @author wzh + */ +public class WxGzhUtil { + + private static Logger logger = LoggerFactory.getLogger(WxGzhUtil.class); + + public static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create"; + public static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete"; + public static final String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info"; + public static final String GET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"; + + + /** + * 鍒涘缓鍏紬鍙疯嚜瀹氫箟鑿滃崟 + * @param token + * @param dataString + * @return + */ + public static String createMenu (String token, String dataString) { + try { + return HttpUtil.post(CREATE_MENU_URL, token, dataString); + } catch (Exception e) { + logger.error("鏇存柊寰俊鍏紬鍙疯嚜瀹氫箟鑿滃崟澶辫触锛�" + e); + throw new TipsException("鏇存柊澶辫触"); + } + } + + /** + * 鍒犻櫎鍏紬鍙疯嚜瀹氫箟鑿滃崟 + * @param token + * @param dataString + * @return + */ + public static String deleteMenu (String token, String dataString) { + try { + return HttpUtil.post(DELETE_MENU_URL, token, dataString); + } catch (Exception e) { + logger.error("鍒犻櫎寰俊鍏紬鍙疯嚜瀹氫箟鑿滃崟澶辫触锛�" + e); + throw new TipsException("鍒犻櫎澶辫触"); + } + } + + /** + * 鏌ヨ涓庢媺鍙栧井淇″叕浼楀彿鑷畾涔夎彍鍗� + * @param token + * @return + */ + public static String getMenuInfo (String token, CommonService commonService) { + try { + String result = HttpUtil.post(GET_MENU_URL, token, ""); + JSONObject obj = JSONObject.fromObject(result); + JSONObject menuInfo = obj.getJSONObject("selfmenu_info"); + if (menuInfo != null) { + JSONArray button = menuInfo.getJSONArray("button"); +// if (button != null && button.size() > 0) { +// // 鍒犻櫎鏃ф暟鎹� +// SqlSentence sqlSentence = new SqlSentence(); +// Map<String,Object> sqlMap = new HashMap<>(); +// sqlMap.put("isDel", BaseEntity.NO); +// sqlMap.put("del", BaseEntity.YES); +// sqlSentence.sqlSentence(" isDel = #{m.del} where isDel = #{m.isDel}", sqlMap); +// commonService.updateWhere(MenuMapper.class, sqlSentence); +// button.forEach(b -> { +// JSONObject j = JSONObject.fromObject(b); +// if (j != null) { +// getButton(j, commonService, ""); +// } +// }); +// } + } + return result; + } catch (Exception e) { + logger.error("鏌ヨ寰俊鍏紬鍙疯嚜瀹氫箟鑿滃崟澶辫触锛�" + e); + throw new TipsException("鏌ヨ澶辫触"); + } + } + + /** + * 鑾峰彇绱犳潗鍒楄〃 + * @param token + * @return + */ + public static JSONObject materialList (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token="+token,request); + } catch (Exception e) { + logger.error("鑾峰彇绱犳潗鍒楄〃锛�" + e); + throw new TipsException("鑾峰彇绱犳潗鍒楄〃"); + } + } + + /** + * 鑾峰彇绱犳潗鍒楄〃 + * @param token + * @return + */ + public static JSONObject qrcodeCreate (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token,request); + } catch (Exception e) { + logger.error("鑾峰彇浜岀淮鐮佷俊鎭細" + e); + throw new TipsException("鑾峰彇浜岀淮鐮佷俊鎭け璐�"); + } + } + + /** + * 鑾峰彇妯℃澘鍒楄〃 + * @param token + * @return + */ + public static JSONObject templateList (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="+token,request); + } catch (Exception e) { + logger.error("鑾峰彇妯℃澘鍒楄〃锛�" + e); + throw new TipsException("鑾峰彇妯℃澘鍒楄〃澶辫触"); + } + } + + /** + * 鍙戦�佹ā鏉挎秷鎭� + * @param token + * @return + */ + public static JSONObject templateSend (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token,request); + } catch (Exception e) { + logger.error("鍙戦�佹ā鏉挎秷鎭細" + e); + throw new TipsException("鍙戦�佹ā鏉挎秷鎭け璐�"); + } + } + + /** + * 鑾峰彇姘镐箙绱犳潗 + * @param token + * @return + */ + public static JSONObject mediaGet (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/material/get_material?access_token="+token,request); + } catch (Exception e) { + logger.error("鑾峰彇姘镐箙绱犳潗锛�" + e); + throw new TipsException("鑾峰彇姘镐箙绱犳潗澶辫触"); + } + } + + /** + * 鎵归噺鑾峰彇鐢ㄦ埛鍩烘湰淇℃伅 + * @param token + * @return + */ + public static JSONObject batchGetUserInfo (String token, String request) { + try { + return HttpUtil.HttpURLUtil("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token="+token,request); + } catch (Exception e) { + logger.error("鎵归噺鑾峰彇鐢ㄦ埛鍩烘湰淇℃伅锛�" + e); + throw new TipsException("鎵归噺鑾峰彇鐢ㄦ埛鍩烘湰淇℃伅"); + } + } +} -- Gitblit v1.8.0