一、flutter 端集成
1、我这边写的登录代码
2、我是把源码的这部分直接复制黏贴到我这个项目里面,原封不动
二、SpringBoot 端集成
1、几乎原封不动抄袭你家的登录IM 源码
`
@Override
public LoginImRespDTO loginIm(LoginImReqDTO imReqDTO) {
LoginImRespDTO imRespDTO = new LoginImRespDTO();
try {
//使用电话号码查询用户信息。
IMResult userResult = UserAdmin.getUserByMobile(imReqDTO.getPhoneCode() + imReqDTO.getPhoneNumber());
//如果用户信息不存在,创建用户
InputOutputUserInfo user;
boolean isNewUser = false;
if (userResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST) {
log.info("User not exist, try to create");
// 创建 IM 用户
user = new InputOutputUserInfo();
user.setUserId(imReqDTO.getUserId());
user.setName(imReqDTO.getNickname());
user.setDisplayName(imReqDTO.getNickname());
user.setPortrait(imReqDTO.getAvatar());
user.setMobile(imReqDTO.getPhoneCode() + imReqDTO.getPhoneNumber());
IMResult<OutputCreateUser> userIdResult = UserAdmin.createUser(user);
if (userIdResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
user.setUserId(userIdResult.getResult().getUserId());
isNewUser = true;
} else {
log.info("IM Server Create user failure {}", userIdResult.code);
return null;
}
} else if (userResult.getCode() != 0) {
log.error("IM Server Get user failure {}", userResult.code);
return null;
} else {
user = userResult.getResult();
}
imRespDTO.setImUserId(user.getUserId());
// 获取手机号获取 IM token
IMResult<OutputGetIMTokenData> tokenResult = UserAdmin.getUserToken(user.getUserId(), imReqDTO.getClientId(), imReqDTO.getPlatform());
if (tokenResult.getErrorCode() != ErrorCode.ERROR_CODE_SUCCESS) {
log.error("Get IM token failure {}", tokenResult.code);
return null;
}
OutputGetIMTokenData result = tokenResult.getResult();
imRespDTO.setImToken(result.getToken());
if (isNewUser) {
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getWelcomeForNewUser())) {
sendTextMessage(imConfig.adminUserId, user.getUserId(), imConfig.getWelcomeForNewUser());
}
if (imConfig.isNewUserRobotFriend() && !org.springframework.util.StringUtils.isEmpty(imConfig.getRobotFriendId())) {
RelationAdmin.setUserFriend(user.getUserId(), imConfig.getRobotFriendId(), true, null);
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getRobotWelcome())) {
sendTextMessage(imConfig.getRobotFriendId(), user.getUserId(), imConfig.getRobotWelcome());
}
}
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getNewUserSubscribeChannelId())) {
GeneralAdmin.subscribeChannel(imConfig.getBackUserSubscribeChannelId(), user.getUserId());
}
} else {
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getWelcomeForBackUser())) {
sendTextMessage(imConfig.adminUserId, user.getUserId(), imConfig.getWelcomeForBackUser());
}
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getBackUserSubscribeChannelId())) {
IMResult<OutputBooleanValue> booleanValueIMResult = GeneralAdmin.isUserSubscribedChannel(user.getUserId(), imConfig.getNewUserSubscribeChannelId());
if (booleanValueIMResult != null && booleanValueIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && !booleanValueIMResult.getResult().value) {
GeneralAdmin.subscribeChannel(imConfig.getBackUserSubscribeChannelId(), user.getUserId());
}
}
}
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getPromptText())) {
sendTextMessage(imConfig.adminUserId, user.getUserId(), imConfig.getPromptText());
}
if (!org.springframework.util.StringUtils.isEmpty(imConfig.getImageMsgUrl()) && !org.springframework.util.StringUtils.isEmpty(imConfig.getImageMsgBase64Thumbnail())) {
sendImageMessage(imConfig.adminUserId, user.getUserId(), imConfig.getImageMsgUrl(), imConfig.getImageMsgBase64Thumbnail());
}
} catch (Exception ex) {
log.error("Login IM Server fail" + ex);
return null;
}
return imRespDTO;
}
`
三、总结
在应用登录后,能成功连接上 IM 服务,最后出现如下问题(注:直接运行你家的flutter源码,后端服务,能正常显示)。麻烦看下,是哪个环节出了问题,谢谢