package com.tzld.piaoquan.api.controller; import com.alibaba.fastjson.JSONObject; import com.tzld.piaoquan.growth.common.model.vo.SendRequestParam; import com.tzld.piaoquan.api.service.WeComAutoReply; import com.tzld.piaoquan.growth.common.common.constant.WeComServerConstant; import com.tzld.piaoquan.growth.common.component.HttpPoolClient; import com.tzld.piaoquan.growth.common.service.WeComUserService; import com.tzld.piaoquan.growth.common.utils.wecom.WXBizMsgCrypt; import com.tzld.piaoquan.growth.common.utils.wecom.WxUtil; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; import java.util.Map; import java.util.UUID; import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.HNWQ; import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.YLQ; @Slf4j @RestController @RequestMapping("/wecom/server") public class TencentWeComController { @Autowired private WeComUserService weComUserService; @Autowired private WeComAutoReply weComAutoReply; @Autowired private HttpPoolClient httpPoolClient; @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); 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"); String welcomeCode = (String) suiteMap.get("WelcomeCode"); log.info("addStaffWithUser userId={} externalUserId={}", userId, externalUserId); weComUserService.addStaffWithUser(externalUserId, userId, HNWQ.getId()); weComAutoReply.AutoReplyMessage(welcomeCode, 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); weComUserService.delStaffWithUser(externalUserId, userId, HNWQ.getId(), System.currentTimeMillis()); } } } catch (Exception e) { log.error("verifyPost error", e); } String success = "success"; return success; } //优量圈验证接口 @GetMapping("/ylq/verify") public void ylqVerifyGet(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.YLQ_TOKEN, WeComServerConstant.YLQ_ENCODING_AES_KEY, WeComServerConstant.YLQ_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); } } //优量圈回调消息接口 @PostMapping(value = "/ylq/verify") public String ylqVerifyPost(HttpServletRequest request) { try { // 微信加密签名 String msg_signature = request.getParameter("msg_signature"); // 时间戳 String timestamp = request.getParameter("timestamp"); // 随机数 String nonce = request.getParameter("nonce"); String id = WeComServerConstant.YLQ_CORP_ID; WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeComServerConstant.YLQ_TOKEN, WeComServerConstant.YLQ_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); 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"); String welcomeCode = (String) suiteMap.get("WelcomeCode"); log.info("YLQ addStaffWithUser userId={} externalUserId={}", userId, externalUserId); weComUserService.addStaffWithUser(externalUserId, userId, YLQ.getId()); weComAutoReply.AutoReplyMessage(welcomeCode, externalUserId, userId, YLQ.getId()); } if (StringUtils.isNotEmpty(changeType) && changeType.equals("del_follow_user")) { String userId = (String) suiteMap.get("UserID"); String externalUserId = (String) suiteMap.get("ExternalUserID"); log.info("YLQ delStaffWithUser userId={} externalUserId={}", userId, externalUserId); weComUserService.delStaffWithUser(externalUserId, userId, YLQ.getId(), System.currentTimeMillis()); } } } catch (Exception e) { log.error("verifyPost error", e); } String success = "success"; return success; } @PostMapping("/send/post") public String sendPost(@RequestBody SendRequestParam sendRequestParam) throws IOException { log.info("sendPost sendRequestParam={}", sendRequestParam); return httpPoolClient.post(sendRequestParam.getUrl(), sendRequestParam.getParam()); } @PostMapping("/send/post/file") public String sendPostFile(@RequestParam String url, @RequestParam("media") MultipartFile multipartFile) throws IOException { String filePath = UUID.randomUUID() + ".jpg"; if (multipartFile.isEmpty()) { log.error("multipartFile is empty"); } File file = new File(filePath); // 将MultipartFile的内容传输到File中 multipartFile.transferTo(file); byte[] decodedBytes = Base64.getDecoder().decode(url); String originalString = new String(decodedBytes); log.info("sendPostFile url={}", originalString); String post = httpPoolClient.post(url, file); log.info("sendPostFile post={}", post); Files.delete(Paths.get(filePath)); return post; } @GetMapping("/send/get") public String sendGet(@RequestParam String url) throws IOException { byte[] decodedBytes = Base64.getDecoder().decode(url); String originalString = new String(decodedBytes); log.info("sendGet url={}", originalString); return httpPoolClient.get(originalString); } }