Commit 894e1817 by 罗长华

调整路由回调请求逻辑

parent 9194ccbd
...@@ -3,6 +3,8 @@ package com.wecloud.im.service.impl; ...@@ -3,6 +3,8 @@ package com.wecloud.im.service.impl;
import io.geekidea.springbootplus.framework.shiro.signature.SignUtils; import io.geekidea.springbootplus.framework.shiro.signature.SignUtils;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -15,7 +17,6 @@ import org.springframework.web.client.RestTemplate; ...@@ -15,7 +17,6 @@ import org.springframework.web.client.RestTemplate;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.id.NanoId; import cn.hutool.core.lang.id.NanoId;
import cn.hutool.core.util.EscapeUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
...@@ -80,12 +81,10 @@ public class ImCallbackServiceImpl implements ImCallbackService { ...@@ -80,12 +81,10 @@ public class ImCallbackServiceImpl implements ImCallbackService {
if (StringUtils.isNotBlank(subscribeUrl)) { if (StringUtils.isNotBlank(subscribeUrl)) {
String appKey = application.getAppKey(); String appKey = application.getAppKey();
String appSecret = application.getAppSecret(); String appSecret = application.getAppSecret();
String callbackUrl = buildCallbackUrl(subscribeUrl, appKey, appSecret);
try { try {
ResponseEntity<Object> response = restTemplate.postForEntity(callbackUrl, buildMsgBody(conversation, ResponseEntity<Object> response = restTemplate.postForEntity(subscribeUrl,
message), buildMsgBody(conversation, message), Object.class, buildUriVariables(appKey, appSecret));
Object.class);
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步 // 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if (response.getStatusCode().equals(HttpStatus.OK)) { if (response.getStatusCode().equals(HttpStatus.OK)) {
// do nothing // do nothing
...@@ -126,8 +125,6 @@ public class ImCallbackServiceImpl implements ImCallbackService { ...@@ -126,8 +125,6 @@ public class ImCallbackServiceImpl implements ImCallbackService {
if (StringUtils.isNotBlank(subscribeUrl)) { if (StringUtils.isNotBlank(subscribeUrl)) {
String appKey = application.getAppKey(); String appKey = application.getAppKey();
String appSecret = application.getAppSecret(); String appSecret = application.getAppSecret();
String callbackUrl = buildCallbackUrl(subscribeUrl, appKey, appSecret);
ClientOnlineStatusChangeDto body = ClientOnlineStatusChangeDto.builder() ClientOnlineStatusChangeDto body = ClientOnlineStatusChangeDto.builder()
.userId(client.getClientId()) .userId(client.getClientId())
...@@ -137,7 +134,7 @@ public class ImCallbackServiceImpl implements ImCallbackService { ...@@ -137,7 +134,7 @@ public class ImCallbackServiceImpl implements ImCallbackService {
.clientIp(clientIp) .clientIp(clientIp)
.build(); .build();
try { try {
ResponseEntity<Object> response = restTemplate.postForEntity(callbackUrl, body, Object.class); ResponseEntity<Object> response = restTemplate.postForEntity(subscribeUrl, body, Object.class, buildUriVariables(appKey, appSecret));
// 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步 // 同步在线状态时需要接收服务提供应答,只要有 HTTP 应答码 200 即认为状态已经同步
if (response.getStatusCode().equals(HttpStatus.OK)) { if (response.getStatusCode().equals(HttpStatus.OK)) {
// do nothing // do nothing
...@@ -158,14 +155,19 @@ public class ImCallbackServiceImpl implements ImCallbackService { ...@@ -158,14 +155,19 @@ public class ImCallbackServiceImpl implements ImCallbackService {
* @param * @param
* @Return * @Return
*/ */
private String buildCallbackUrl(String subscribeUrl, String appKey, String appSecret) { private Map<String, String> buildUriVariables(String appKey, String appSecret) {
Map<String, String> uriVariables = new HashMap<>();
// 计算 Signature (数据签名) // 计算 Signature (数据签名)
String nonce = NanoId.randomNanoId(); String nonce = NanoId.randomNanoId();
String date = DateUtil.formatHttpDate(new Date()); String date = DateUtil.formatHttpDate(new Date());
String signature = SignUtils.buildSignature(appKey, appSecret, nonce, date); String signature = SignUtils.buildSignature(appKey, appSecret, nonce, date);
String finalUrl =
subscribeUrl + "?appKey=" + appKey + "&nonce=" + nonce + "&date=" + date + "&signature=" + EscapeUtil.escape(signature); uriVariables.put("appKey", appKey);
return finalUrl; uriVariables.put("nonce", nonce);
uriVariables.put("date", date);
uriVariables.put("signature", signature);
return uriVariables;
} }
private Boolean isNeedSync(ImConversation conversation, ImMessage message) { private Boolean isNeedSync(ImConversation conversation, ImMessage message) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment