This commit is contained in:
dute7liang
2024-01-01 03:52:14 +08:00
parent 248d0271b1
commit 3aa29bcefc
31 changed files with 407 additions and 92 deletions

View File

@@ -3,6 +3,8 @@ package com.ruoyi.cai.auth;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.secure.BCrypt;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.cai.domain.*;
@@ -242,6 +244,7 @@ public class CaiLoginManager {
add.setGender(user.getGender());
add.setCity(user.getCity());
add.setInviteId(user.getInviteId());
add.setImToken(IdUtil.simpleUUID());
userService.save(add);
String clientIP = ServletUtils.getClientIP();
UserInfo userInfo = new UserInfo();

View File

@@ -2,7 +2,7 @@ package com.ruoyi.cai.trd;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.cai.util.RestTemplateUtil;
import com.ruoyi.yunxin.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

View File

@@ -1,76 +0,0 @@
package com.ruoyi.cai.trd;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.cai.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class Yunxin {
@Autowired
private YunxinProperties yunxinProperties;
private final static String SEND_URL = "https://api.netease.im/nimserver/msg/sendMsg.action";
private final static String SEND_ATTR_URL = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
public void sendTo(Long toUid,Object data,Long fromUid){
Map<String,Object> option = new HashMap<>();
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
option.put("roam",false); // 该消息是否需要漫游需要app开通漫游消息功能
option.put("history",false); // 该消息是否存云端历史
option.put("sendersync",false); // 该消息是否需要发送方多端同步
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
Map<String,Object> bodyData = new HashMap<>();
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
bodyData.put("ope", 0);
bodyData.put("to", toUid);
bodyData.put("type", 100);
bodyData.put("body", JSON.toJSONString(data));
bodyData.put("option", JSON.toJSONString(option));
String nonce = UUID.fastUUID().toString();
String curTime = DateUtil.currentSeconds() + "";
HttpHeaders headers = new HttpHeaders();
headers.add("AppKey",yunxinProperties.getAppKey());
headers.add("Nonce", nonce);
headers.add("CurTime", curTime);
headers.add("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
HttpEntity httpEntity = new HttpEntity<>(bodyData, headers);
RestTemplateUtil.restTemplate.postForObject(SEND_URL,httpEntity, JSONObject.class);
}
public void sendAttachMsg(Long toUid,Object data,Long fromUid){
Map<String,Object> option = new HashMap<>();
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
option.put("roam",false); // 该消息是否需要漫游需要app开通漫游消息功能
option.put("history",false); // 该消息是否存云端历史
option.put("sendersync",false); // 该消息是否需要发送方多端同步
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
Map<String,Object> bodyData = new HashMap<>();
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
bodyData.put("ope", 0);
bodyData.put("to", toUid);
bodyData.put("type", 100);
bodyData.put("body", JSON.toJSONString(data));
bodyData.put("option", JSON.toJSONString(option));
String nonce = UUID.fastUUID().toString();
String curTime = DateUtil.currentSeconds() + "";
HttpHeaders headers = new HttpHeaders();
headers.add("AppKey",yunxinProperties.getAppKey());
headers.add("Nonce", nonce);
headers.add("CurTime", curTime);
headers.add("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
HttpEntity httpEntity = new HttpEntity<>(bodyData, headers);
RestTemplateUtil.restTemplate.postForObject(SEND_ATTR_URL,httpEntity, JSONObject.class);
}
}

View File

@@ -1,15 +0,0 @@
package com.ruoyi.cai.trd;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "yunxin")
public class YunxinProperties {
private String appKey;
private String appSecret;
private String defaultFromUid;
}

View File

@@ -1,31 +0,0 @@
package com.ruoyi.cai.util;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class RestTemplateUtil {
public static RestTemplate restTemplate;
static {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(3000);
requestFactory.setReadTimeout(3000);
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
//添加转换器
for (HttpMessageConverter<?> messageConverter : messageConverters) {
if (messageConverter instanceof StringHttpMessageConverter) {
StringHttpMessageConverter converter = (StringHttpMessageConverter) messageConverter;
converter.setDefaultCharset(StandardCharsets.UTF_8);
}
}
}
}

View File

@@ -6,7 +6,7 @@ import com.ruoyi.cai.domain.UserCall;
import com.ruoyi.cai.service.UserCallService;
import com.ruoyi.cai.trd.ImDataRes;
import com.ruoyi.cai.trd.ImMsgGen;
import com.ruoyi.cai.trd.Yunxin;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.constant.HangUpEnums;
@@ -46,7 +46,7 @@ public class CancelMessageHandler extends AbstractMessageHandle implements IMess
Long receiverId = room.getReceiverUserData().getId();
Long callerId = room.getCallUserData().getId();
ImDataRes imDataRes = ImMsgGen.callNotice(1, callerId, receiverId, 0);
yunxin.sendTo(receiverId,imDataRes,callerId);
yunxin.sendTo(receiverId,callerId,imDataRes);
// 更新房间状态
userCallService.update(Wrappers.lambdaUpdate(UserCall.class)
.eq(UserCall::getId,roomId)

View File

@@ -6,7 +6,7 @@ import com.ruoyi.cai.domain.UserCall;
import com.ruoyi.cai.service.UserCallService;
import com.ruoyi.cai.trd.ImDataRes;
import com.ruoyi.cai.trd.ImMsgGen;
import com.ruoyi.cai.trd.Yunxin;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.cache.RoomDataCache;
@@ -49,7 +49,7 @@ public class RefuseMessageHandler extends AbstractMessageHandle implements IMess
Long receiverId = room.getReceiverUserData().getId();
Long callerId = room.getCallUserData().getId();
ImDataRes imDataRes = ImMsgGen.callNotice(1, callerId, receiverId, 0);
yunxin.sendTo(receiverId,imDataRes,callerId);
yunxin.sendTo(receiverId,callerId,imDataRes);
// 更新房间状态
userCallService.update(Wrappers.lambdaUpdate(UserCall.class)

View File

@@ -5,7 +5,7 @@ import com.ruoyi.cai.domain.Account;
import com.ruoyi.cai.service.AccountService;
import com.ruoyi.cai.trd.ImDataRes;
import com.ruoyi.cai.trd.ImMsgGen;
import com.ruoyi.cai.trd.Yunxin;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.bean.RoomData;
import com.ruoyi.cai.ws.bean.UserData;
@@ -67,7 +67,7 @@ public class SettleService {
Long callUserId = room.getCallUserData().getId();
if(callTime > 0){
ImDataRes imDataRes = ImMsgGen.callNotice(4, receiverUserId, callUserId, callTime);
yunxin.sendTo(room.getCallUserData().getId(),imDataRes,room.getReceiverUserData().getId());
yunxin.sendTo(room.getCallUserData().getId(),room.getReceiverUserData().getId(),imDataRes);
}
// 收入通知
if(room != null){ // TODO修改数据