|
|
@ -1,7 +1,6 @@ |
|
|
|
package com.ccsens.util.wx; |
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.core.util.URLUtil; |
|
|
|
import cn.hutool.http.HttpRequest; |
|
|
|
import com.alibaba.fastjson.JSONException; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
@ -9,11 +8,12 @@ import com.ccsens.util.DateUtil; |
|
|
|
import com.ccsens.util.JacksonUtil; |
|
|
|
import com.ccsens.util.RedisUtil; |
|
|
|
import com.ccsens.util.WebConstant; |
|
|
|
import com.ccsens.util.bean.wx.po.*; |
|
|
|
import com.ccsens.util.bean.wx.po.WxAccessToken; |
|
|
|
import com.ccsens.util.bean.wx.po.WxOauth2AccessToken; |
|
|
|
import com.ccsens.util.bean.wx.po.WxOauth2UserInfo; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import com.ccsens.util.exception.BusinessException; |
|
|
|
import com.ccsens.util.exception.WxException; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -24,7 +24,6 @@ import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.security.MessageDigest; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author __zHangSan |
|
|
@ -210,11 +209,11 @@ public class WxGzhUtil { |
|
|
|
/** |
|
|
|
* 获取Access_token |
|
|
|
*/ |
|
|
|
public static String getAccessToken() throws BaseException { |
|
|
|
public static String getAccessToken(String appId,String secret) throws BaseException { |
|
|
|
Object obj = util.redisUtil.get(WebConstant.Wx.ACCESS_TOKEN); |
|
|
|
if(obj == null || StrUtil.isBlank((String)obj) ){ |
|
|
|
WxAccessToken wxAccessToken = null; |
|
|
|
String url = String.format(URL_GET_ACCESS_TOKEN,"client_credential",appid_health,secret_health); |
|
|
|
String url = String.format(URL_GET_ACCESS_TOKEN,"client_credential",appId,secret); |
|
|
|
String response = HttpRequest.get(url).execute().body(); |
|
|
|
log.info("getAccessToken: {}",response); |
|
|
|
try { |
|
|
@ -238,50 +237,50 @@ public class WxGzhUtil { |
|
|
|
logger.info("读取reids的token:{}", obj); |
|
|
|
return (String)obj; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 查询微信公众号底部菜单 |
|
|
|
*/ |
|
|
|
public static String queryMenu() throws BaseException { |
|
|
|
String url = String.format(URL_QUERY_MENU,getAccessToken()); |
|
|
|
String menuString = HttpRequest.get(url).execute().body(); |
|
|
|
log.info("url:{}, menuString: {}",url,menuString); |
|
|
|
return menuString; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建微信公众号底部菜单 |
|
|
|
*/ |
|
|
|
public static void createMenu(WxGzhMenu gzhMenu) throws BaseException { |
|
|
|
String url = String.format(URL_CREATE_MENU,getAccessToken()); |
|
|
|
String menuString = null; |
|
|
|
try { |
|
|
|
menuString = JacksonUtil.beanToJson(gzhMenu); |
|
|
|
log.info(menuString); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
throw new BusinessException(-1,e.getMessage()); |
|
|
|
} |
|
|
|
String response = HttpRequest.post(url).body(menuString).execute().body(); |
|
|
|
if(StrUtil.isEmpty(response)){ |
|
|
|
throw new BusinessException(-1,"the response of HttpRequest is empty."); |
|
|
|
} |
|
|
|
Map<String,Object> map = null; |
|
|
|
try { |
|
|
|
map = JacksonUtil.jsonToMap(response); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new BusinessException(-1,e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
Object errcode = map.get("errcode"); |
|
|
|
if(errcode == null || ((Integer)errcode) != 0) { |
|
|
|
throw new WxException((Integer)errcode,(String)map.get("errmsg")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static String getAuthedUrl(String url, WxGzhAuthType wxGzhAuthType){ |
|
|
|
return String.format(GZH_AUTH_URL,APPID, URLUtil.encode(url),wxGzhAuthType.getText()); |
|
|
|
} |
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * 查询微信公众号底部菜单
|
|
|
|
// */
|
|
|
|
// public static String queryMenu() throws BaseException {
|
|
|
|
// String url = String.format(URL_QUERY_MENU,getAccessToken());
|
|
|
|
// String menuString = HttpRequest.get(url).execute().body();
|
|
|
|
// log.info("url:{}, menuString: {}",url,menuString);
|
|
|
|
// return menuString;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * 创建微信公众号底部菜单
|
|
|
|
// */
|
|
|
|
// public static void createMenu(WxGzhMenu gzhMenu) throws BaseException {
|
|
|
|
// String url = String.format(URL_CREATE_MENU,getAccessToken());
|
|
|
|
// String menuString = null;
|
|
|
|
// try {
|
|
|
|
// menuString = JacksonUtil.beanToJson(gzhMenu);
|
|
|
|
// log.info(menuString);
|
|
|
|
// } catch (JsonProcessingException e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// throw new BusinessException(-1,e.getMessage());
|
|
|
|
// }
|
|
|
|
// String response = HttpRequest.post(url).body(menuString).execute().body();
|
|
|
|
// if(StrUtil.isEmpty(response)){
|
|
|
|
// throw new BusinessException(-1,"the response of HttpRequest is empty.");
|
|
|
|
// }
|
|
|
|
// Map<String,Object> map = null;
|
|
|
|
// try {
|
|
|
|
// map = JacksonUtil.jsonToMap(response);
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// throw new BusinessException(-1,e.getMessage());
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Object errcode = map.get("errcode");
|
|
|
|
// if(errcode == null || ((Integer)errcode) != 0) {
|
|
|
|
// throw new WxException((Integer)errcode,(String)map.get("errmsg"));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public static String getAuthedUrl(String url, WxGzhAuthType wxGzhAuthType){
|
|
|
|
// return String.format(GZH_AUTH_URL,APPID, URLUtil.encode(url),wxGzhAuthType.getText());
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取网页授权凭证 |
|
|
@ -351,11 +350,15 @@ public class WxGzhUtil { |
|
|
|
return getOauth2UserInfo(wxOauth2AccessToken.getAccessToken(),wxOauth2AccessToken.getOpenId()); |
|
|
|
} |
|
|
|
|
|
|
|
public static void sendTemplate(WxTemplateMessage message) { |
|
|
|
String url = String.format(MESSAGE_TEMPLATE_SEND,getAccessToken()); |
|
|
|
String response = HttpRequest.post(url).body(JSONObject.toJSONString(message)).execute().body(); |
|
|
|
pageResponseSuccess(response); |
|
|
|
} |
|
|
|
// /**
|
|
|
|
// * 发送消息模板
|
|
|
|
// * @param message
|
|
|
|
// */
|
|
|
|
// public static void sendTemplate(WxTemplateMessage message) {
|
|
|
|
// String url = String.format(MESSAGE_TEMPLATE_SEND,getAccessToken());
|
|
|
|
// String response = HttpRequest.post(url).body(JSONObject.toJSONString(message)).execute().body();
|
|
|
|
// pageResponseSuccess(response);
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* 判断结果是否正确, 错误抛异常(errcode!=0为错误) |
|
|
|