|
|
@@ -1,9 +1,13 @@
|
|
|
package com.java110.front.controller;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.java110.core.base.controller.BaseController;
|
|
|
+import com.java110.core.context.IPageData;
|
|
|
+import com.java110.core.context.PageData;
|
|
|
import com.java110.core.factory.AuthenticationFactory;
|
|
|
-import com.java110.core.factory.WechatFactory;
|
|
|
+import com.java110.front.smo.wechatGateway.IWechatGatewaySMO;
|
|
|
+import com.java110.utils.cache.MappingCache;
|
|
|
import com.java110.utils.constant.WechatConstant;
|
|
|
import com.java110.utils.util.StringUtil;
|
|
|
import org.dom4j.Document;
|
|
|
@@ -11,6 +15,7 @@ import org.dom4j.DocumentHelper;
|
|
|
import org.dom4j.Element;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
@@ -18,11 +23,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import javax.servlet.ServletInputStream;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStreamReader;
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
@@ -39,6 +40,9 @@ public class WechatGatewayController extends BaseController {
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(WechatGatewayController.class);
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IWechatGatewaySMO wechatGatewaySMOImpl;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 微信登录接口
|
|
|
@@ -48,7 +52,7 @@ public class WechatGatewayController extends BaseController {
|
|
|
@RequestMapping(path = "/gateway", method = RequestMethod.GET)
|
|
|
public ResponseEntity<String> gateway(HttpServletRequest request) {
|
|
|
|
|
|
- String token = WechatConstant.TOKEN;
|
|
|
+ String token = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.TOKEN);
|
|
|
String signature = request.getParameter("signature");
|
|
|
String timestamp = request.getParameter("timestamp");
|
|
|
String nonce = request.getParameter("nonce");
|
|
|
@@ -66,47 +70,7 @@ public class WechatGatewayController extends BaseController {
|
|
|
logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
|
|
|
try {
|
|
|
if (signature1.equals(signature)) {
|
|
|
- if (echostr == null) {
|
|
|
- String postStr = this.readStreamParameter(request.getInputStream());
|
|
|
- if (StringUtil.isEmpty(postStr)) {
|
|
|
- responseStr = "未输入任何内容";
|
|
|
- } else {
|
|
|
- /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
|
|
|
- logger.debug(" This type is iso-8859-1");
|
|
|
- postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");
|
|
|
-
|
|
|
- }
|
|
|
- if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
|
|
|
- logger.debug(" This type is GB2312" + postStr);
|
|
|
- postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
|
|
|
- logger.debug(" change postStr to utf-8 " + postStr);
|
|
|
- }
|
|
|
- if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
|
|
|
- logger.debug(" This type is GBK");
|
|
|
- postStr = new String(postStr.getBytes("GBK"), "UTF-8");
|
|
|
-
|
|
|
- }
|
|
|
- postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
|
|
|
- Document document = DocumentHelper.parseText(postStr);
|
|
|
- Element root = document.getRootElement();
|
|
|
- String fromUserName = root.elementText("FromUserName");
|
|
|
- String toUserName = root.elementText("ToUserName");
|
|
|
- String keyword = root.elementTextTrim("Content");
|
|
|
- String msgType = root.elementTextTrim("MsgType");
|
|
|
- String event = root.elementText("Event");
|
|
|
- String eventKey = root.elementText("EventKey");
|
|
|
- if (WechatConstant.MSG_TYPE_TEXT.equals(msgType)) {
|
|
|
- responseStr = textResponseHandler(fromUserName, toUserName, keyword);
|
|
|
- } else if (WechatConstant.MSG_TYPE_EVENT.equals(msgType)) {
|
|
|
- responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
|
|
|
- } else {
|
|
|
- responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- } else {
|
|
|
- responseStr = echostr;
|
|
|
- }
|
|
|
+ responseStr = echostr;
|
|
|
logger.debug(">>>>>>>>>>>>>..responseStr>>>>>>>>>>>>>>>" + responseStr);
|
|
|
} else {
|
|
|
responseStr = "亲,非法访问,签名失败";
|
|
|
@@ -128,12 +92,14 @@ public class WechatGatewayController extends BaseController {
|
|
|
@RequestMapping(path = "/gateway", method = RequestMethod.POST)
|
|
|
public ResponseEntity<String> gateway(@RequestBody String param, HttpServletRequest request) {
|
|
|
|
|
|
- String token = WechatConstant.TOKEN;
|
|
|
+ String token = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, WechatConstant.TOKEN);
|
|
|
String signature = request.getParameter("signature");
|
|
|
String timestamp = request.getParameter("timestamp");
|
|
|
String nonce = request.getParameter("nonce");
|
|
|
String openId = request.getParameter("openid");
|
|
|
+ String java110AppId = request.getParameter("java110AppId");
|
|
|
String responseStr = "";
|
|
|
+ ResponseEntity<String> responseEntity = null;
|
|
|
logger.debug("token = " + token + "||||" + "signature = " + signature + "|||" + "timestamp = "
|
|
|
+ timestamp + "|||" + "nonce = " + nonce + "|||| param = " + param + "|||| openId= " + openId);
|
|
|
String sourceString = "";
|
|
|
@@ -145,137 +111,42 @@ public class WechatGatewayController extends BaseController {
|
|
|
String signature1 = AuthenticationFactory.SHA1Encode(sourceString).toLowerCase();
|
|
|
logger.debug("sourceString = " + sourceString + "||||" + "signature1 = " + signature1);
|
|
|
try {
|
|
|
- if (signature1.equals(signature)) {
|
|
|
- String postStr = param;//this.readStreamParameter(request.getInputStream());
|
|
|
- if (StringUtil.isEmpty(postStr)) {
|
|
|
- responseStr = "未输入任何内容";
|
|
|
- } else {
|
|
|
- /*if (postStr.equals(new String(postStr.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
|
|
|
- logger.debug(" This type is iso-8859-1");
|
|
|
- postStr = new String(postStr.getBytes("ISO-8859-1"), "UTF-8");
|
|
|
-
|
|
|
- }
|
|
|
- if (postStr.equals(new String(postStr.getBytes("GB2312"), "GB2312"))) {
|
|
|
- logger.debug(" This type is GB2312" + postStr);
|
|
|
- postStr = new String(postStr.getBytes("GB2312"), "UTF-8");
|
|
|
- logger.debug(" change postStr to utf-8 " + postStr);
|
|
|
- }
|
|
|
- if (postStr.equals(new String(postStr.getBytes("GBK"), "GBK"))) {
|
|
|
- logger.debug(" This type is GBK");
|
|
|
- postStr = new String(postStr.getBytes("GBK"), "UTF-8");
|
|
|
-
|
|
|
- }
|
|
|
- postStr = new String(postStr.getBytes("GB2312"), "UTF-8");*/
|
|
|
- Document document = DocumentHelper.parseText(postStr);
|
|
|
- Element root = document.getRootElement();
|
|
|
- String fromUserName = root.elementText("FromUserName");
|
|
|
- String toUserName = root.elementText("ToUserName");
|
|
|
- String keyword = root.elementTextTrim("Content");
|
|
|
- String msgType = root.elementTextTrim("MsgType");
|
|
|
- String event = root.elementText("Event");
|
|
|
- String eventKey = root.elementText("EventKey");
|
|
|
- if (WechatConstant.MSG_TYPE_TEXT.equals(msgType)) {
|
|
|
- responseStr = textResponseHandler(fromUserName, toUserName, keyword);
|
|
|
- } else if (WechatConstant.MSG_TYPE_EVENT.equals(msgType)) {
|
|
|
- responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
|
|
|
- } else {
|
|
|
- responseStr = eventResponseHandler(fromUserName, toUserName, keyword, event, eventKey);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- logger.debug(">>>>>>>>>>>>>..responseStr>>>>>>>>>>>>>>>" + responseStr);
|
|
|
- } else {
|
|
|
+ if (!signature1.equals(signature)) {
|
|
|
responseStr = "亲,非法访问,签名失败";
|
|
|
+ return new ResponseEntity<String>(responseStr, HttpStatus.OK);
|
|
|
+ }
|
|
|
+ String postStr = param;
|
|
|
+ if (StringUtil.isEmpty(postStr)) {
|
|
|
+ responseStr = "未输入任何内容";
|
|
|
+ return new ResponseEntity<String>(responseStr, HttpStatus.OK);
|
|
|
}
|
|
|
+ Document document = DocumentHelper.parseText(postStr);
|
|
|
+ Element root = document.getRootElement();
|
|
|
+ String fromUserName = root.elementText("FromUserName");
|
|
|
+ String toUserName = root.elementText("ToUserName");
|
|
|
+ String keyword = root.elementTextTrim("Content");
|
|
|
+ String msgType = root.elementTextTrim("MsgType");
|
|
|
+ String event = root.elementText("Event");
|
|
|
+ String eventKey = root.elementText("EventKey");
|
|
|
+ JSONObject paramIn = new JSONObject();
|
|
|
+ paramIn.put("fromUserName", fromUserName);
|
|
|
+ paramIn.put("toUserName", toUserName);
|
|
|
+ paramIn.put("keyword", keyword);
|
|
|
+ paramIn.put("msgType", msgType);
|
|
|
+ paramIn.put("event", event);
|
|
|
+ paramIn.put("eventKey", eventKey);
|
|
|
+ IPageData pd = PageData.newInstance().builder("", "", "", paramIn.toJSONString(),
|
|
|
+ "", "", "", "",
|
|
|
+ java110AppId);
|
|
|
+ responseEntity = wechatGatewaySMOImpl.gateway(pd);
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
logger.error("处理失败", e);
|
|
|
responseStr = "亲,网络超时,请稍后重试";
|
|
|
+ responseEntity = new ResponseEntity<String>(responseStr, HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
- return new ResponseEntity<String>(responseStr, HttpStatus.OK);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 鏂囨湰淇℃伅鍥炲
|
|
|
- *
|
|
|
- * @param fromUserName
|
|
|
- * @param toUserName
|
|
|
- * @param keyword
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String textResponseHandler(String fromUserName, String toUserName,
|
|
|
- String keyword) {
|
|
|
- if (keyword == null || keyword.length() == 0) {
|
|
|
- return WechatFactory
|
|
|
- .formatText(toUserName, fromUserName, "未包含任何信息");
|
|
|
- } else {
|
|
|
- String responseStr = keyWordHandler(fromUserName, toUserName,
|
|
|
- keyword);
|
|
|
- return WechatFactory
|
|
|
- .formatText(toUserName, fromUserName, responseStr);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String keyWordHandler(String fromUserName, String toUserName,
|
|
|
- String keyword) {
|
|
|
- // TODO Auto-generated method stub
|
|
|
- String domain = WechatConstant.TOKEN;
|
|
|
- //String url = domain + "/IMSS/indexPage.do";
|
|
|
- return "HC小区物业管理系统是由java110团队于2017年4月份发起的前后端分离、分布式架构开源项目,目前我们的代码开源在github 和gitee上,开源项目由HC小区管理系统后端,HC小区管理系统前端,HC小区管理系统业主手机版和HC小区管理系统物业手机版,业务技术交流群:827669685";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 事件处理
|
|
|
- *
|
|
|
- * @param fromUserName
|
|
|
- * @param toUserName
|
|
|
- * @param event
|
|
|
- * @param eventKey
|
|
|
- * @return
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- @SuppressWarnings({"unchecked"})
|
|
|
- public String eventResponseHandler(String fromUserName, String toUserName, String keyWords, String event,
|
|
|
- String eventKey) throws Exception {
|
|
|
- String resultStr = "";
|
|
|
- //
|
|
|
- if (event.equals("subscribe")) {
|
|
|
- resultStr = "HC小区物业管理系统是由java110团队于2017年4月份发起的前后端分离、分布式架构开源项目,目前我们的代码开源在github 和gitee上,开源项目由HC小区管理系统后端,HC小区管理系统前端,HC小区管理系统业主手机版和HC小区管理系统物业手机版,业务技术交流群:827669685";
|
|
|
- } else if (event.equals("unsubscribe")) {
|
|
|
-
|
|
|
- } else if (event.equalsIgnoreCase("CLICK")) {
|
|
|
- resultStr = textResponseHandler(fromUserName, toUserName,
|
|
|
- eventKey);
|
|
|
- } else {
|
|
|
-
|
|
|
- }
|
|
|
- return WechatFactory.formatText(toUserName, fromUserName, resultStr);
|
|
|
- }
|
|
|
-
|
|
|
- public String readStreamParameter(ServletInputStream in) {
|
|
|
- StringBuilder buffer = new StringBuilder();
|
|
|
- BufferedReader reader = null;
|
|
|
- try {
|
|
|
- reader = new BufferedReader(new InputStreamReader(in));
|
|
|
- String line = null;
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
- buffer.append(line);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (null != reader) {
|
|
|
- try {
|
|
|
- reader.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- logger.debug("公众号内容:" + buffer.toString());
|
|
|
- return buffer.toString();
|
|
|
+ return responseEntity;
|
|
|
}
|
|
|
}
|