|
@@ -1,124 +1,31 @@
|
|
|
package com.tzld.piaoquan.api.controller;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.tzld.piaoquan.common.common.constant.WeComServerConstant;
|
|
|
-import com.tzld.piaoquan.api.service.UserService;
|
|
|
-import com.tzld.piaoquan.common.utils.wecom.WXBizMsgCrypt;
|
|
|
-import com.tzld.piaoquan.common.utils.wecom.WxUtil;
|
|
|
+import com.tzld.piaoquan.api.model.vo.WeComPushMessageParam;
|
|
|
+import com.tzld.piaoquan.api.model.vo.WeComPushMessageVo;
|
|
|
+import com.tzld.piaoquan.api.service.WeComService;
|
|
|
+import com.tzld.piaoquan.common.common.base.CommonResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import javax.servlet.ServletInputStream;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.PrintWriter;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import static com.tzld.piaoquan.common.common.enums.CorpEnum.HNWQ;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
-@RequestMapping("/wecom/server")
|
|
|
+@RequestMapping("/wecom")
|
|
|
public class WeComController {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
- private UserService userService;
|
|
|
-
|
|
|
- @GetMapping("/verify")
|
|
|
- public void verifyGet(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- try {
|
|
|
- // 微信加密签名
|
|
|
- String msgSignature = request.getParameter("msg_signature");
|
|
|
- // 时间戳
|
|
|
- String timestamp = request.getParameter("timestamp");
|
|
|
- // 随机数
|
|
|
- String nonce = request.getParameter("nonce");
|
|
|
- // 随机字符串
|
|
|
- // 如果是刷新,需返回原echostr
|
|
|
- String echoStr = request.getParameter("echostr");
|
|
|
- // 微信加密签名
|
|
|
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN,
|
|
|
- WeComServerConstant.ENCODING_AES_KEY,
|
|
|
- WeComServerConstant.CORP_ID);
|
|
|
-
|
|
|
- String sEchoStr = ""; //需要返回的明文
|
|
|
- PrintWriter out;
|
|
|
-
|
|
|
- sEchoStr = wxcpt.VerifyURL(msgSignature, timestamp,
|
|
|
- nonce, echoStr);
|
|
|
- log.info("verifyurl echostr: " + sEchoStr);
|
|
|
-
|
|
|
- // 验证URL成功,将sEchoStr返回
|
|
|
- out = response.getWriter();
|
|
|
- out.print(sEchoStr);
|
|
|
- } catch (Exception e) {
|
|
|
- //验证URL失败,错误原因请查看异常
|
|
|
- log.error("verifyGet error", e);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 刷新 ticket
|
|
|
- */
|
|
|
- @PostMapping(value = "/verify")
|
|
|
- public String verifyPost(HttpServletRequest request) {
|
|
|
-
|
|
|
- try {
|
|
|
- // 微信加密签名
|
|
|
- String msg_signature = request.getParameter("msg_signature");
|
|
|
- // 时间戳
|
|
|
- String timestamp = request.getParameter("timestamp");
|
|
|
- // 随机数
|
|
|
- String nonce = request.getParameter("nonce");
|
|
|
-
|
|
|
- String id = WeComServerConstant.CORP_ID;
|
|
|
-
|
|
|
- WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.TOKEN, WeComServerConstant.ENCODING_AES_KEY, id);
|
|
|
-
|
|
|
- StringBuilder postData = new StringBuilder(); // 密文,对应POST请求的数据
|
|
|
- //1.获取加密的请求消息:使用输入流获得加密请求消息postData
|
|
|
- ServletInputStream in = request.getInputStream();
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
-
|
|
|
- String tempStr = ""; //作为输出字符串的临时串,用于判断是否读取完毕
|
|
|
- while (null != (tempStr = reader.readLine())) {
|
|
|
- postData.append(tempStr);
|
|
|
- }
|
|
|
-
|
|
|
- String suiteXml = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, postData.toString());
|
|
|
- log.info("suiteXml: " + suiteXml);
|
|
|
+ private WeComService weComService;
|
|
|
|
|
|
- Map suiteMap = WxUtil.transferXmlToMap(suiteXml);
|
|
|
- log.info("suiteMap = {}", JSONObject.toJSONString(suiteMap));
|
|
|
- if (suiteMap != null) {
|
|
|
- String changeType = (String) suiteMap.get("ChangeType");
|
|
|
- if (StringUtils.isNotEmpty(changeType) && changeType.equals("add_external_contact")) {
|
|
|
- String userId = (String) suiteMap.get("UserID");
|
|
|
- String externalUserId = (String) suiteMap.get("ExternalUserID");
|
|
|
- log.info("addStaffWithUser userId={} externalUserId={}", userId, externalUserId);
|
|
|
- userService.addStaffWithUser(externalUserId, userId, HNWQ.getId());
|
|
|
- }
|
|
|
|
|
|
- if (StringUtils.isNotEmpty(changeType) && changeType.equals("del_follow_user")) {
|
|
|
- String userId = (String) suiteMap.get("UserID");
|
|
|
- String externalUserId = (String) suiteMap.get("ExternalUserID");
|
|
|
- log.info("delStaffWithUser userId={} externalUserId={}", userId, externalUserId);
|
|
|
- userService.delStaffWithUser(externalUserId, userId, System.currentTimeMillis());
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("verifyPost error", e);
|
|
|
- }
|
|
|
- String success = "success";
|
|
|
- return success;
|
|
|
+ @PostMapping("/pushMessage/get")
|
|
|
+ public CommonResponse<List<WeComPushMessageVo>> getPushMessage(@RequestBody WeComPushMessageParam param) {
|
|
|
+ log.info("param={}", param);
|
|
|
+ return weComService.getPushMessage(param);
|
|
|
}
|
|
|
}
|