|
|
@ -17,11 +17,12 @@ import io.swagger.annotations.ApiModel; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
@ -54,37 +55,77 @@ public class WeixinController { |
|
|
|
* 数据回调URL |
|
|
|
*/ |
|
|
|
@GetMapping("userChangeNotice") |
|
|
|
public String userChangeNotice(MessageDto dto) throws Exception{ |
|
|
|
public String userChangeNoticeGet(MessageDto dto) throws Exception{ |
|
|
|
log.info("数据回调请求参数:{}", dto); |
|
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinConstant.token, WeiXinConstant.encodingAESKey, WeiXinConstant.corpID); |
|
|
|
String verifyURL = wxcpt.VerifyURL(dto.getMsg_signature(), dto.getTimestamp(), dto.getNonce(), dto.getEchostr()); |
|
|
|
log.info("数据回调解析结果:{}", verifyURL); |
|
|
|
return verifyURL; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 指令回调URL |
|
|
|
* 指令回调get |
|
|
|
* @param dto |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@GetMapping("authorizationChangeNotice") |
|
|
|
public String authorizationChangeNotice(MessageDto dto) throws Exception{ |
|
|
|
public String authorizationChangeNoticeGet(MessageDto dto) throws Exception { |
|
|
|
log.info("请求参数:{}", dto); |
|
|
|
|
|
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinConstant.token, WeiXinConstant.encodingAESKey, WeiXinConstant.corpID); |
|
|
|
String verifyURL = wxcpt.VerifyURL(dto.getMsg_signature(), dto.getTimestamp(), dto.getNonce(), dto.getEchostr()); |
|
|
|
log.info("解析结果:{}", verifyURL); |
|
|
|
if (StrUtil.isBlank(verifyURL) || !(verifyURL.startsWith("<xml") || verifyURL.startsWith("<XML"))){ |
|
|
|
return verifyURL; |
|
|
|
} |
|
|
|
return verifyURL; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 数据回调URL post |
|
|
|
*/ |
|
|
|
@PostMapping("userChangeNotice") |
|
|
|
public String userChangeNoticePost(HttpServletRequest request, MessageDto dto) throws Exception{ |
|
|
|
log.info("数据回调请求参数:{}", dto); |
|
|
|
String body = getBody(request); |
|
|
|
String xmlStr = JacksonUtil.xmlToJson(body); |
|
|
|
log.info("数据回到xml转换:{}", xmlStr); |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(xmlStr); |
|
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinConstant.token, WeiXinConstant.encodingAESKey, WeiXinConstant.corpID); |
|
|
|
String verifyURL = wxcpt.VerifyURL(dto.getMsg_signature(), dto.getTimestamp(), dto.getNonce(), jsonObject.getString("Encrypt")); |
|
|
|
log.info("数据回调解析结果:{}", verifyURL); |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 指令回调URL |
|
|
|
*/ |
|
|
|
@PostMapping("authorizationChangeNotice") |
|
|
|
public String authorizationChangeNotice(HttpServletRequest request, MessageDto dto) throws Exception{ |
|
|
|
log.info("指令回调请求参数:{}", dto); |
|
|
|
String listString = getBody(request); |
|
|
|
log.info("指令回调body:{}", listString); |
|
|
|
String xmlStr = JacksonUtil.xmlToJson(listString); |
|
|
|
log.info("指令回到xml转换:{}", xmlStr); |
|
|
|
JSONObject jsonObject = JSONObject.parseObject(xmlStr); |
|
|
|
log.info("jsonObject:{}", jsonObject); |
|
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinConstant.token, WeiXinConstant.encodingAESKey, WeiXinConstant.suiteID); |
|
|
|
String verifyURL = wxcpt.VerifyURL(dto.getMsg_signature(), dto.getTimestamp(), dto.getNonce(), jsonObject.getString("Encrypt")); |
|
|
|
log.info("解析结果:{}", verifyURL); |
|
|
|
|
|
|
|
String jsonStr = JacksonUtil.xmlToJson(verifyURL); |
|
|
|
JSONObject json = JSONObject.parseObject(jsonStr); |
|
|
|
log.info("指令回调业务参数:{}", json); |
|
|
|
switch (json.getString("InfoType")){ |
|
|
|
case "suite_ticket" : |
|
|
|
MessageDto.Ticket ticket = JSONObject.parseObject(jsonStr, MessageDto.Ticket.class); |
|
|
|
constantService.saveConstant(WeiXinConstant.getSuiteTicket(ticket.getSuiteId()), ticket.getSuiteTicket()); |
|
|
|
|
|
|
|
//获取第三方应用凭证
|
|
|
|
getSuiteAccessToken(ticket.getSuiteId(),ticket.getSuiteTicket()); |
|
|
|
break; |
|
|
|
case "create_auth" : |
|
|
|
MessageDto.Grant grant = JSONObject.parseObject(jsonStr, MessageDto.Grant.class); |
|
|
|
constantService.saveConstant(WeiXinConstant.getTempAuthCodeKey(grant.getSuiteId()), grant.getAuthCode()); |
|
|
|
getPermanentCode(grant.getAuthCode()); |
|
|
|
//获取永久授权
|
|
|
|
getPermanentCode(grant.getSuiteId(), grant.getAuthCode()); |
|
|
|
break; |
|
|
|
case "change_auth" : |
|
|
|
//TODO 变更授权通知
|
|
|
@ -94,7 +135,33 @@ public class WeixinController { |
|
|
|
break; |
|
|
|
//TODO 成员变更和部门变更通知
|
|
|
|
} |
|
|
|
return verifyURL; |
|
|
|
return "success"; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 异步获取suite_access_token |
|
|
|
* @param suite_id |
|
|
|
* @param suite_ticket |
|
|
|
*/ |
|
|
|
@Async |
|
|
|
public void getSuiteAccessToken(String suite_id, String suite_ticket) { |
|
|
|
weiXinService.getSuiteToken(suite_id, suite_ticket); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 读取body |
|
|
|
* @param request |
|
|
|
* @return |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
private String getBody(HttpServletRequest request) throws IOException { |
|
|
|
BufferedReader br = request.getReader(); |
|
|
|
String str = ""; |
|
|
|
String listString = ""; |
|
|
|
while ((str = br.readLine()) != null) { |
|
|
|
listString += str; |
|
|
|
} |
|
|
|
return listString; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -102,14 +169,19 @@ public class WeixinController { |
|
|
|
* @param authCode |
|
|
|
*/ |
|
|
|
@Async |
|
|
|
public void getPermanentCode(String authCode){ |
|
|
|
weiXinService.savePermanentCode(authCode); |
|
|
|
// TODO 读取部门信息和成员信息
|
|
|
|
public void getPermanentCode(String suiteId, String authCode){ |
|
|
|
String suiteTicket = constantService.getByKey(WeiXinConstant.getSuiteTicket(suiteId)); |
|
|
|
String suiteToken = weiXinService.getSuiteToken(suiteId, suiteTicket); |
|
|
|
String accessToken = weiXinService.savePermanentCode(authCode, suiteToken); |
|
|
|
if (StrUtil.isNotBlank(accessToken)) { |
|
|
|
weiXinService.initDepartment(accessToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 系统验证 |
|
|
|
*/ |
|
|
|
@GetMapping("reciveSysMsg") |
|
|
|
@RequestMapping(value = "reciveSysMsg",method = {RequestMethod.POST, RequestMethod.GET}) |
|
|
|
public String reciveSysMsg(MessageDto dto) throws Exception{ |
|
|
|
log.info("请求参数:{}", dto); |
|
|
|
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(WeiXinConstant.token, WeiXinConstant.encodingAESKey, WeiXinConstant.corpID); |
|
|
|