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 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("批量获取用户基本信息"); } } }