微信action接口
public String weChatInterface() throws IOException {String signature = getParameterValue("signature"); // 时间戳 String timestamp = getParameterValue("timestamp"); // 随机数 String nonce = getParameterValue("nonce"); // 随机字符串 String echostr = getParameterValue("echostr"); // 获取服务器域名 SystemConfigBean sconfig = getElearningManagerFacade().getSystemConfigManager().getSystemConfigBean(); String contextPath = getRequest().getContextPath(); String headerReferer = sconfig.getUrl(); String basePath = headerReferer.substring(0, headerReferer.indexOf(contextPath) + contextPath.length() + 1); if (WeChatUtil.checkSignature(signature, timestamp, nonce, sconfig.getToken())) { // 公众号提交服务器创建菜单 if (isSet(echostr) && echostr.length() > 1) { // 调用接口获取access_token AccessToken at = WeChatUtil.getAccessToken(sconfig.getAppId(), sconfig.getAppSecret()); if (null != at) { // weChatContext.xml中获取微信菜单 JSONObject jsonMenu = new JSONObject(); Menu menu = SuiteContextManager.getMenuByKey("weChat"); JSONArray array = new JSONArray(); for (Menu menuItem : menu.getMenuItems()) { String weChatUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=URL&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; JSONObject jsonItem = new JSONObject(); jsonItem.accumulate("key", menuItem.getKey()); jsonItem.accumulate("name", menuItem.getTitle()); jsonItem.accumulate("type", menuItem.getType()); String url = basePath + "weChatPublic/weChatView.do?action=" + menuItem.getAction(); weChatUrl = weChatUrl.replace("APPID", sconfig.getAppId()).replace("URL", url); jsonItem.accumulate("url", weChatUrl); array.add(jsonItem); } jsonMenu.accumulate("button", array); // 调用接口创建菜单 int result = WeChatUtil.createMenu(jsonMenu.toString(), at.getToken()); // 判断菜单创建结果 if (0 == result) { log.info("菜单创建成功!"); return exportAJAXData(echostr); } else { log.info("菜单创建失败,错误码:" + result); return exportAJAXData("faile"); } } } } // 读取微信服务器发过来的xml问件 StringBuffer sb = new StringBuffer(); InputStream is = getRequest().getInputStream(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader br = new BufferedReader(isr); String s = ""; while ((s = br.readLine()) != null) { sb.append(s); } String xml = sb.toString(); // 次即为接收到微信端发送过来的xml数据 ReceiveXmlEntity xmlEntity = ParseReceiveXml.getMsgEntity(xml); String msgType = xmlEntity.getMsgType(); JSONObject json = new JSONObject(); if (msgType.equals(ReceiveXmlEntity.REQ_MESSAGE_TYPE_EVENT)) { String event = xmlEntity.getEvent(); if (event.equals(ReceiveXmlEntity.EVENT_TYPE_SUBSCRIBE)) { String weChatUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=URL&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; String url = basePath + "weChatPublic/weChatView.do?action=weChatHippo"; weChatUrl = weChatUrl.replace("APPID", sconfig.getAppId()).replace("URL", url); String content = "!\n<a href='"+weChatUrl+"'>立刻点击,身份验证</a>\n。"; WeChatUtil.formatXmlAnswer(xmlEntity, content, getResponse()); } if (event.equals(ReceiveXmlEntity.EVENT_TYPE_VIEW)) { String content = "eventKey=" + xmlEntity.getEventKey(); WeChatUtil.formatXmlAnswer(xmlEntity, content, getResponse()); } if (event.equals(ReceiveXmlEntity.EVENT_TYPE_CLICK)) { String content = "eventKey=" + xmlEntity.getEventKey(); WeChatUtil.formatXmlAnswer(xmlEntity, content, getResponse()); } if (event.equals(ReceiveXmlEntity.EVENT_TYPE_UNSUBSCRIBE)) { String openId = xmlEntity.getFromUserName(); String userKey = getElearningManagerFacade().getUserManager().getUserKeyByOpenId(openId); if(isSet(userKey)){ getElearningManagerFacade().getUserManager().unRelationOpenId(openId); } } } String content = "你说的太高深了 听不懂\n <a href='http://www.baidu.com'>点击</a>"; WeChatUtil.formatXmlAnswer(xmlEntity, content, getResponse()); return exportAJAXData(json.toString());
}
2微信公用类
public class WeChatUtil {
/**
* 验证签名
* @param signature
* @param timestamp
* @param nonce
* @param
* @return
*/
public static boolean checkSignature(String signature, String timestamp, String nonce,String token){
String[] arr = new String[]{token, timestamp, nonce};
// 将 token, timestamp, nonce 三个参数进行字典排序
Arrays.sort(arr);
StringBuilder content = new StringBuilder();
for(int i = 0; i < arr.length; i++){
content.append(arr[i]);
}
MessageDigest md = null;
String tmpStr = null;
try {
md = MessageDigest.getInstance("SHA-1");
// 将三个参数字符串拼接成一个字符串进行 shal 加密
byte[] digest = md.digest(content.toString().getBytes());
tmpStr = byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content = null;
// 将sha1加密后的字符串可与signature对比,标识该请求来源于微信
return tmpStr != null ? tmpStr.equals(signature.toUpperCase()): false;
}
/**
* 将字节数组转换为十六进制字符串
* @param digest
* @return
*/
private static String byteToStr(byte[] digest) {
// TODO Auto-generated method stub
String strDigest = "";
for(int i = 0; i < digest.length; i++){
strDigest += byteToHexStr(digest[i]);
}
return strDigest;
}
/**
* 将字节转换为十六进制字符串
* @param b
* @return
*/
private static String byteToHexStr(byte b) {
// TODO Auto-generated method stub
char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] tempArr = new char[2];
tempArr[0] = Digit[(b >>> 4) & 0X0F];
tempArr[1] = Digit[b & 0X0F];
String s = new String(tempArr);
return s;
}
/**
* 公众平台通用接口工具类
*/
private static Logger log = LoggerFactory.getLogger(WeChatUtil.class);
/**
* 发起https请求并获取结果
*
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log.error("Weixin server connection timed out.");
} catch (Exception e) {
log.error("https request error:{}", e);
}
return jsonObject;
}
// 获取access_token的接口地址(GET) 限200(次/天)
public final static String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**
* 获取access_token
*
* @param appid 凭证
* @param appsecret 密钥
* @return
*/
public static AccessToken getAccessToken(String appid, String appsecret) {
AccessToken accessToken = null;
String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
// 如果请求成功
if (null != jsonObject) {
try {
accessToken = new AccessToken();
accessToken.setToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
accessToken = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return accessToken;
}
// 通过oauth2.0网页授权的接口地址获取openId
public final static String openId_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
/**
* OAuth2.0网页授权获取openId
*
* @param appid 凭证
* @param appsecret 密钥
* @return
*/
public static String getOpenId(String appId, String appSecret,String code) {
String openId = null;
String requestUrl = null;
if(code != null){
requestUrl = openId_url.replace("APPID", appId).replace("APPSECRET", appSecret).replace("CODE", code);
}
JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
// 如果请求成功
if (null != jsonObject) {
try {
openId = jsonObject.getString("openid");
} catch (JSONException e) {
openId = null;
// 获取token失败
log.error("获取openid失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return openId;
}
// 菜单创建(POST) 限100(次/天)
public static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
/**
* 创建菜单
*
* @param menu 菜单实例
* @param accessToken 有效的access_token
* @return 0表示成功,其他值表示失败
*/
public static int createMenu(String jsonMenu, String accessToken) {
int result = 0;
// 拼装创建菜单的url
String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
// 将菜单对象转换成json字符串
// 调用接口创建菜单
JSONObject jsonObject = httpRequest(url, "POST", jsonMenu);
if (null != jsonObject) {
if (0 != jsonObject.getInt("errcode")) {
result = jsonObject.getInt("errcode");
log.error("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return result;
}
/**
* 封装文字类的返回消息
* @param to
* @param from
* @param content
* @return
*/
public static void formatXmlAnswer(ReceiveXmlEntity xmlEntity, String content,HttpServletResponse response) {
StringBuffer sb1 = new StringBuffer();
Date date = new Date();
sb1.append("<xml><ToUserName><![CDATA[");
sb1.append(xmlEntity.getFromUserName());
sb1.append("]]></ToUserName><FromUserName><![CDATA[");
sb1.append(xmlEntity.getToUserName());
sb1.append("]]></FromUserName><CreateTime>");
sb1.append(date.getTime());
sb1.append("</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[");
sb1.append(content);
sb1.append("]]></Content><FuncFlag>0</FuncFlag></xml>");
try {
OutputStream os = response.getOutputStream();
os.write(sb1.toString().getBytes("UTF-8"));
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.接收到的微信xml实体类ReceiveXmlEntity:
public static String REQ_MESSAGE_TYPE_EVENT = “event”;//事件推送
public static String EVENT_TYPE_SUBSCRIBE =”subscribe”;//关注事件
public static String EVENT_TYPE_CLICK =”CLICK”;//菜单点击事件
public static String EVENT_TYPE_VIEW =”VIEW”;//菜单点击事件
public static String EVENT_TYPE_UNSUBSCRIBE =”unsubscribe”;//取消关注事件
private String ToUserName=”“;
private String FromUserName=”“;
private String CreateTime=”“;
private String MsgType=”“;
private String MsgId=”“;
private String Event=”“;
private String EventKey=”“;
private String Ticket=”“;
private String Latitude=”“;
private String Longitude=”“;
private String Precision=”“;
private String PicUrl=”“;
private String MediaId=”“;
private String Title=”“;
private String Description=”“;
private String Url=”“;
private String Location_X=”“;
private String Location_Y=”“;
private String Scale=”“;
private String Label=”“;
private String Content=”“;
private String Format=”“;
private String Recognition=”“;
private String MenuId = “”;