list = departmentService.list(null);
+ return list;
+ } catch (Exception e) {
+ this.logger.info("\n获取组织机构出错" + e.getMessage());
+ }
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorController.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorController.java
new file mode 100644
index 000000000..e8ca444ca
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorController.java
@@ -0,0 +1,29 @@
+package com.ruoyi.wx.cp.error;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ *
+ * 出错页面控制器
+ * Created by Binary Wang on 2018/8/25.
+ *
+ *
+ * @author Binary Wang
+ */
+@Controller
+@RequestMapping("/error")
+public class ErrorController {
+
+ @GetMapping(value = "/404")
+ public String error404() {
+ return "error";
+ }
+
+ @GetMapping(value = "/500")
+ public String error500() {
+ return "error";
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorPageConfiguration.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorPageConfiguration.java
new file mode 100644
index 000000000..cbf011760
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/error/ErrorPageConfiguration.java
@@ -0,0 +1,27 @@
+package com.ruoyi.wx.cp.error;
+
+import org.springframework.boot.web.server.ErrorPage;
+import org.springframework.boot.web.server.ErrorPageRegistrar;
+import org.springframework.boot.web.server.ErrorPageRegistry;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Component;
+
+/**
+ *
+ * 配置错误状态与对应访问路径
+ * Created by Binary Wang on 2018/8/25.
+ *
+ *
+ * @author Binary Wang
+ */
+@Component
+public class ErrorPageConfiguration implements ErrorPageRegistrar {
+ @Override
+ public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
+ errorPageRegistry.addErrorPages(
+ new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"),
+ new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500")
+ );
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/AbstractHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/AbstractHandler.java
new file mode 100644
index 000000000..b0cdc35e7
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/AbstractHandler.java
@@ -0,0 +1,13 @@
+package com.ruoyi.wx.cp.handler;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.chanjar.weixin.cp.message.WxCpMessageHandler;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+public abstract class AbstractHandler implements WxCpMessageHandler {
+ protected Logger logger = LoggerFactory.getLogger(getClass());
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ContactChangeHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ContactChangeHandler.java
new file mode 100644
index 000000000..267040db2
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ContactChangeHandler.java
@@ -0,0 +1,31 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import com.ruoyi.wx.cp.builder.TextBuilder;
+import org.springframework.stereotype.Component;
+
+import com.ruoyi.wx.cp.utils.JsonUtils;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * 通讯录变更事件处理器.
+ *
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class ContactChangeHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+ String content = "收到通讯录变更事件,内容:" + JsonUtils.toJson(wxMessage);
+ this.logger.info(content);
+
+ return new TextBuilder().build(content, wxMessage, cpService);
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/EnterAgentHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/EnterAgentHandler.java
new file mode 100644
index 000000000..ae0008da8
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/EnterAgentHandler.java
@@ -0,0 +1,29 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import lombok.extern.slf4j.Slf4j;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ *
+ *
+ * Created by Binary Wang on 2018/8/27.
+ *
+ *
+ * @author Binary Wang
+ */
+@Slf4j
+public class EnterAgentHandler extends AbstractHandler {
+ private static final int TEST_AGENT = 1000002;
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
+ // do something
+ return null;
+ }
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LocationHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LocationHandler.java
new file mode 100644
index 000000000..4c7c20ea8
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LocationHandler.java
@@ -0,0 +1,43 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import com.ruoyi.wx.cp.builder.TextBuilder;
+import org.springframework.stereotype.Component;
+
+import me.chanjar.weixin.common.api.WxConsts;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class LocationHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+ if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
+ //TODO 接收处理用户发送的地理位置消息
+ try {
+ String content = "感谢反馈,您的的地理位置已收到!";
+ return new TextBuilder().build(content, wxMessage, null);
+ } catch (Exception e) {
+ this.logger.error("位置消息接收处理失败", e);
+ return null;
+ }
+ }
+
+ //上报地理位置事件
+ this.logger.info("\n上报地理位置,纬度 : {}\n经度 : {}\n精度 : {}",
+ wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));
+
+ //TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用
+
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LogHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LogHandler.java
new file mode 100644
index 000000000..e4ec42439
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/LogHandler.java
@@ -0,0 +1,25 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import org.springframework.stereotype.Component;
+
+import com.ruoyi.wx.cp.utils.JsonUtils;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class LogHandler extends AbstractHandler {
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+ this.logger.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MenuHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MenuHandler.java
new file mode 100644
index 000000000..a2b030749
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MenuHandler.java
@@ -0,0 +1,35 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import org.springframework.stereotype.Component;
+
+import me.chanjar.weixin.common.api.WxConsts.MenuButtonType;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class MenuHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+
+ String msg = String.format("type:%s, event:%s, key:%s",
+ wxMessage.getMsgType(), wxMessage.getEvent(),
+ wxMessage.getEventKey());
+ if (MenuButtonType.VIEW.equals(wxMessage.getEvent())) {
+ return null;
+ }
+
+ return WxCpXmlOutMessage.TEXT().content(msg)
+ .fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName())
+ .build();
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MsgHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MsgHandler.java
new file mode 100644
index 000000000..2db6ab23d
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/MsgHandler.java
@@ -0,0 +1,36 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import com.ruoyi.wx.cp.builder.TextBuilder;
+import org.springframework.stereotype.Component;
+
+import com.ruoyi.wx.cp.utils.JsonUtils;
+import me.chanjar.weixin.common.api.WxConsts;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class MsgHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+
+ if (!wxMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT)) {
+ //TODO 可以选择将消息保存到本地
+ }
+
+ //TODO 组装回复消息
+ String content = "收到信息内容:" + JsonUtils.toJson(wxMessage);
+
+ return new TextBuilder().build(content, wxMessage, cpService);
+
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/NullHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/NullHandler.java
new file mode 100644
index 000000000..80411a77b
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/NullHandler.java
@@ -0,0 +1,24 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import org.springframework.stereotype.Component;
+
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class NullHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ScanHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ScanHandler.java
new file mode 100644
index 000000000..48631c1e6
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/ScanHandler.java
@@ -0,0 +1,8 @@
+package com.ruoyi.wx.cp.handler;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+public abstract class ScanHandler extends AbstractHandler {
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/SubscribeHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/SubscribeHandler.java
new file mode 100644
index 000000000..52bce1228
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/SubscribeHandler.java
@@ -0,0 +1,62 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import com.ruoyi.wx.cp.builder.TextBuilder;
+import org.springframework.stereotype.Component;
+
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpUser;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class SubscribeHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) throws WxErrorException {
+
+ this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUserName());
+
+ // 获取微信用户基本信息
+ WxCpUser userWxInfo = cpService.getUserService().getById(wxMessage.getFromUserName());
+
+ if (userWxInfo != null) {
+ // TODO 可以添加关注用户到本地
+ }
+
+ WxCpXmlOutMessage responseResult = null;
+ try {
+ responseResult = handleSpecial(wxMessage);
+ } catch (Exception e) {
+ this.logger.error(e.getMessage(), e);
+ }
+
+ if (responseResult != null) {
+ return responseResult;
+ }
+
+ try {
+ return new TextBuilder().build("感谢关注", wxMessage, cpService);
+ } catch (Exception e) {
+ this.logger.error(e.getMessage(), e);
+ }
+
+ return null;
+ }
+
+ /**
+ * 处理特殊请求,比如如果是扫码进来的,可以做相应处理
+ */
+ private WxCpXmlOutMessage handleSpecial(WxCpXmlMessage wxMessage) {
+ //TODO
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/UnsubscribeHandler.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/UnsubscribeHandler.java
new file mode 100644
index 000000000..1172ef4ae
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/handler/UnsubscribeHandler.java
@@ -0,0 +1,27 @@
+package com.ruoyi.wx.cp.handler;
+
+import java.util.Map;
+
+import org.springframework.stereotype.Component;
+
+import me.chanjar.weixin.common.session.WxSessionManager;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
+import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+@Component
+public class UnsubscribeHandler extends AbstractHandler {
+
+ @Override
+ public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map context, WxCpService cpService,
+ WxSessionManager sessionManager) {
+ String openId = wxMessage.getFromUserName();
+ this.logger.info("取消关注用户 OPENID: " + openId);
+ // TODO 可以更新本地数据库为取消关注状态
+ return null;
+ }
+
+}
diff --git a/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/utils/JsonUtils.java b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/utils/JsonUtils.java
new file mode 100644
index 000000000..b9a5ed822
--- /dev/null
+++ b/ruoyi-weixin/src/main/java/com/ruoyi/wx/cp/utils/JsonUtils.java
@@ -0,0 +1,28 @@
+package com.ruoyi.wx.cp.utils;
+
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+/**
+ * @author Binary Wang(https://github.com/binarywang)
+ */
+public class JsonUtils {
+ private static final ObjectMapper JSON = new ObjectMapper();
+
+ static {
+ JSON.setSerializationInclusion(Include.NON_NULL);
+ JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
+ }
+
+ public static String toJson(Object obj) {
+ try {
+ return JSON.writeValueAsString(obj);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+}
diff --git a/ruoyi-weixin/src/main/resources/application-weixin.yml b/ruoyi-weixin/src/main/resources/application-weixin.yml
new file mode 100644
index 000000000..4a57ad0c1
--- /dev/null
+++ b/ruoyi-weixin/src/main/resources/application-weixin.yml
@@ -0,0 +1,12 @@
+wechat:
+ cp:
+ corpId: wwffb70143e94111eb
+ appConfigs:
+ - agentId: 999999
+ secret: yeL6mZp5IFIkfQ3wWMGFI8G-L4a6BEF194fnRzhvCy0
+ token: 111
+ aesKey: 111
+ - agentId: 1000002
+ secret: 1111
+ token: 111
+ aesKey: 111
\ No newline at end of file