Commit c9550ee2 by giaogiao

创建会话,并添加成员

parent debfa697
...@@ -32,7 +32,7 @@ public class SignTest { ...@@ -32,7 +32,7 @@ public class SignTest {
} }
public static void main(String[] args) throws JsonProcessingException { public static void main(String[] args) throws JsonProcessingException {
String clientId = "hahah_30"; String clientId = "hahah_31";
String appKey = "elLwpel1gWCHDqZy"; String appKey = "elLwpel1gWCHDqZy";
String appSecret = "68809bb5a9077a83631aeb0b17b5965d6b2302faf2ab3737"; String appSecret = "68809bb5a9077a83631aeb0b17b5965d6b2302faf2ab3737";
String timestemp = String.valueOf(new Date().getTime()); String timestemp = String.valueOf(new Date().getTime());
......
package com.wecloud.im.controller; package com.wecloud.im.controller;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.param.add.ImConversationCreate;
import com.wecloud.im.service.ImConversationService; import com.wecloud.im.service.ImConversationService;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController; import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog; import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType; import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -25,7 +24,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -25,7 +24,7 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/imConversation") @RequestMapping("/conversation")
@Api(value = "会话表API", tags = {"会话表"}) @Api(value = "会话表API", tags = {"会话表"})
public class ImConversationController extends BaseController { public class ImConversationController extends BaseController {
...@@ -33,17 +32,28 @@ public class ImConversationController extends BaseController { ...@@ -33,17 +32,28 @@ public class ImConversationController extends BaseController {
private ImConversationService imConversationService; private ImConversationService imConversationService;
/** /**
* 添加会话表 * 创建会话
*/ */
@PostMapping("/add") @PostMapping("/create")
@OperationLog(name = "添加会话表", type = OperationLogType.ADD) @OperationLog(name = "创建会话", type = OperationLogType.ADD)
@ApiOperation(value = "添加会话表") @ApiOperation(value = "创建会话")
public ApiResult<Boolean> addImConversation(@Validated(Add.class) @RequestBody ImConversation imConversation) throws Exception { public ApiResult<ImConversation> createImConversation(@RequestBody ImConversationCreate imConversationCreate) throws Exception {
boolean flag = imConversationService.saveImConversation(imConversation); ApiResult<ImConversation> imConversation = imConversationService.createImConversation(imConversationCreate);
return ApiResult.result(flag); return imConversation;
} }
// /** // /**
// * 添加会话表
// */
// @PostMapping("/add")
// @OperationLog(name = "添加会话表", type = OperationLogType.ADD)
// @ApiOperation(value = "添加会话表")
// public ApiResult<Boolean> addImConversation(@Validated(Add.class) @RequestBody ImConversation imConversation) throws Exception {
// boolean flag = imConversationService.saveImConversation(imConversation);
// return ApiResult.result(flag);
// }
// /**
// * 修改会话表 // * 修改会话表
// */ // */
// @PostMapping("/update") // @PostMapping("/update")
......
package com.wecloud.im.controller; package com.wecloud.im.controller;
import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.service.ImConversationMembersService; import com.wecloud.im.service.ImConversationMembersService;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController; import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -32,16 +23,17 @@ public class ImConversationMembersController extends BaseController { ...@@ -32,16 +23,17 @@ public class ImConversationMembersController extends BaseController {
@Autowired @Autowired
private ImConversationMembersService imConversationMembersService; private ImConversationMembersService imConversationMembersService;
/**
* 添加会话成员表 // /**
*/ // * 添加会话成员表
@PostMapping("/add") // */
@OperationLog(name = "添加会话成员表", type = OperationLogType.ADD) // @PostMapping("/add")
@ApiOperation(value = "添加会话成员表") // @OperationLog(name = "添加会话成员表", type = OperationLogType.ADD)
public ApiResult<Boolean> addImConversationMembers(@Validated(Add.class) @RequestBody ImConversationMembers imConversationMembers) throws Exception { // @ApiOperation(value = "添加会话成员表")
boolean flag = imConversationMembersService.saveImConversationMembers(imConversationMembers); // public ApiResult<Boolean> addImConversationMembers(@Validated(Add.class) @RequestBody ImConversationMembers imConversationMembers) throws Exception {
return ApiResult.result(flag); // boolean flag = imConversationMembersService.saveImConversationMembers(imConversationMembers);
} // return ApiResult.result(flag);
// }
// //
// /** // /**
// * 修改会话成员表 // * 修改会话成员表
......
package com.wecloud.im.param.add;
import io.geekidea.springbootplus.framework.common.entity.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 会话表
*
* @author wei
* @since 2021-05-07
*/
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "ImConversationCreate")
public class ImConversationCreate extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty("可选 对话的名字,可为群组命名。")
private String name;
@ApiModelProperty("可选 自定义属性,供开发者扩展使用。")
private Object attributes;
@ApiModelProperty("可选 邀请加入会话的客户端,如创建单聊,则填入对方的clientId")
private List<String> clientIds;
}
...@@ -3,6 +3,8 @@ package com.wecloud.im.service; ...@@ -3,6 +3,8 @@ package com.wecloud.im.service;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.param.ImConversationPageParam; import com.wecloud.im.param.ImConversationPageParam;
import com.wecloud.im.param.ImConversationQueryVo; import com.wecloud.im.param.ImConversationQueryVo;
import com.wecloud.im.param.add.ImConversationCreate;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.BaseService; import io.geekidea.springbootplus.framework.common.service.BaseService;
import io.geekidea.springbootplus.framework.core.pagination.Paging; import io.geekidea.springbootplus.framework.core.pagination.Paging;
...@@ -23,6 +25,9 @@ public interface ImConversationService extends BaseService<ImConversation> { ...@@ -23,6 +25,9 @@ public interface ImConversationService extends BaseService<ImConversation> {
*/ */
boolean saveImConversation(ImConversation imConversation) throws Exception; boolean saveImConversation(ImConversation imConversation) throws Exception;
ApiResult<ImConversation> createImConversation(ImConversationCreate imConversationCreate);
/** /**
* 修改 * 修改
* *
......
...@@ -88,6 +88,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService { ...@@ -88,6 +88,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService {
if (imClient == null) { if (imClient == null) {
log.debug("client不存在,先走注册流程"); log.debug("client不存在,先走注册流程");
imClient = new ImClient();
imClient.setId(new Snowflake(1L, 1L).nextId()); imClient.setId(new Snowflake(1L, 1L).nextId());
imClient.setFkAppid(imApplication.getId()); imClient.setFkAppid(imApplication.getId());
imClient.setClientId(imTokenVerify.getClientId()); imClient.setClientId(imTokenVerify.getClientId());
...@@ -98,7 +99,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService { ...@@ -98,7 +99,7 @@ public class ImClientLoginServiceImpl implements ImClientLoginService {
String generateToken = JwtUtil.generateToken(imTokenVerify.getClientId(), imTokenVerify.getAppKey(), secret, null); String generateToken = JwtUtil.generateToken(imTokenVerify.getClientId(), imTokenVerify.getAppKey(), secret, null);
// 保存redis // 保存redis
redisTemplate.opsForValue().set("client:" + imApplication.getAppKey() + ":" + imTokenVerify.getClientId(), generateToken); // redisTemplate.opsForValue().set("client:" + imApplication.getAppKey() + ":" + imTokenVerify.getClientId(), generateToken);
JwtToken jwtToken = new JwtToken() JwtToken jwtToken = new JwtToken()
.build(generateToken, secret, jwtProperties.getExpireSecond(), imClient.getClientId(), imTokenVerify.getAppKey()); .build(generateToken, secret, jwtProperties.getExpireSecond(), imClient.getClientId(), imTokenVerify.getAppKey());
......
package com.wecloud.im.service.impl; package com.wecloud.im.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem; import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImConversation; import com.wecloud.im.entity.ImConversation;
import com.wecloud.im.entity.ImConversationMembers;
import com.wecloud.im.mapper.ImConversationMapper; import com.wecloud.im.mapper.ImConversationMapper;
import com.wecloud.im.param.ImConversationPageParam; import com.wecloud.im.param.ImConversationPageParam;
import com.wecloud.im.param.ImConversationQueryVo; import com.wecloud.im.param.ImConversationQueryVo;
import com.wecloud.im.param.add.ImConversationCreate;
import com.wecloud.im.service.ImApplicationService;
import com.wecloud.im.service.ImClientService;
import com.wecloud.im.service.ImConversationMembersService;
import com.wecloud.im.service.ImConversationService; import com.wecloud.im.service.ImConversationService;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl; import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo; import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging; import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
/** /**
* 会话表 服务实现类 * 会话表 服务实现类
* *
...@@ -29,12 +43,81 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap ...@@ -29,12 +43,81 @@ public class ImConversationServiceImpl extends BaseServiceImpl<ImConversationMap
@Autowired @Autowired
private ImConversationMapper imConversationMapper; private ImConversationMapper imConversationMapper;
@Autowired
private ImConversationMembersService imConversationMembersService;
@Autowired
private ImConversationService imConversationService;
@Autowired
private ImClientService imClientService;
@Autowired
private ImApplicationService imApplicationService;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public boolean saveImConversation(ImConversation imConversation) throws Exception { public boolean saveImConversation(ImConversation imConversation) throws Exception {
return super.save(imConversation); return super.save(imConversation);
} }
@Override
public ApiResult<ImConversation> createImConversation(ImConversationCreate imConversationCreate) {
JwtToken jwtToken = (JwtToken) SecurityUtils.getSubject().getPrincipal();
Long imConversationId = SnowflakeUtil.getId();
// 根据appKey查询appid
ImApplication imApplication = imApplicationService.getOne(
new QueryWrapper<ImApplication>().lambda().eq(ImApplication::getAppKey, jwtToken.getAppKey())
);
Long fkAppid = imApplication.getId();
ImClient client = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
.eq(ImClient::getFkAppid, fkAppid)
.eq(ImClient::getClientId, jwtToken.getClientId()));
Long creator = client.getId();
// 创建会话
ImConversation imConversation = new ImConversation();
imConversation.setId(imConversationId);
imConversation.setCreateTime(new Date());
imConversation.setLastMessage(null);
imConversation.setFkAppid(fkAppid);
imConversation.setCreator(creator);
imConversation.setName(imConversationCreate.getName());
imConversation.setAttributes(imConversationCreate.getAttributes().toString());
imConversation.setSystem(false);
imConversationService.save(imConversation);
// 将自己添加到会话
Long imConversationMembersId = SnowflakeUtil.getId();
ImConversationMembers imConversationMembers = new ImConversationMembers();
imConversationMembers.setId(imConversationMembersId);
imConversationMembers.setCreateTime(new Date());
imConversationMembers.setFkAppid(fkAppid);
imConversationMembers.setFkConversationId(imConversationId);
imConversationMembers.setFkClientId(creator);
imConversationMembersService.save(imConversationMembers);
// 将他人添加到会话
for (String id : imConversationCreate.getClientIds()) {
ImClient client2 = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
.eq(ImClient::getFkAppid, fkAppid)
.eq(ImClient::getClientId, id));
Long imConversationMembersId2 = SnowflakeUtil.getId();
ImConversationMembers imConversationMembers2 = new ImConversationMembers();
imConversationMembers2.setId(imConversationMembersId2);
imConversationMembers2.setCreateTime(new Date());
imConversationMembers2.setFkAppid(fkAppid);
imConversationMembers2.setFkConversationId(imConversationId);
imConversationMembers2.setFkClientId(client2.getId());
imConversationMembersService.save(imConversationMembers2);
}
return ApiResult.ok(imConversation);
}
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public boolean updateImConversation(ImConversation imConversation) throws Exception { public boolean updateImConversation(ImConversation imConversation) throws Exception {
......
...@@ -3,8 +3,6 @@ package com.wecloud.im.tillo.netty.handler; ...@@ -3,8 +3,6 @@ package com.wecloud.im.tillo.netty.handler;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT; import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.service.ImApplicationService; import com.wecloud.im.service.ImApplicationService;
import com.wecloud.im.service.ImClientService; import com.wecloud.im.service.ImClientService;
import com.wecloud.im.tillo.app_ws.WsHandler; import com.wecloud.im.tillo.app_ws.WsHandler;
...@@ -12,12 +10,16 @@ import com.wecloud.im.tillo.app_ws.model.WsConstants; ...@@ -12,12 +10,16 @@ import com.wecloud.im.tillo.app_ws.model.WsConstants;
import com.wecloud.im.tillo.app_ws.service.MangerChannelService; import com.wecloud.im.tillo.app_ws.service.MangerChannelService;
import com.wecloud.im.tillo.app_ws.utils.FullHttpRequestUtils; import com.wecloud.im.tillo.app_ws.utils.FullHttpRequestUtils;
import io.geekidea.springbootplus.config.constant.CommonConstant; import io.geekidea.springbootplus.config.constant.CommonConstant;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import io.geekidea.springbootplus.framework.shiro.service.ShiroLoginService;
import io.geekidea.springbootplus.framework.shiro.util.JwtUtil; import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -36,6 +38,9 @@ import java.util.Map; ...@@ -36,6 +38,9 @@ import java.util.Map;
@Slf4j @Slf4j
public class NettyApiRequest { public class NettyApiRequest {
@Autowired
private ShiroLoginService shiroLoginService;
@Resource @Resource
private MangerChannelService appUserChannelsService; private MangerChannelService appUserChannelsService;
...@@ -99,18 +104,38 @@ public class NettyApiRequest { ...@@ -99,18 +104,38 @@ public class NettyApiRequest {
String clientId = (String) jsonObject.get(CommonConstant.CLIENT_ID); String clientId = (String) jsonObject.get(CommonConstant.CLIENT_ID);
// 验签token // 验签token
ImApplication imApplication = imApplicationService.getOne(new QueryWrapper<ImApplication>().lambda() // ImApplication imApplication = imApplicationService.getOne(new QueryWrapper<ImApplication>().lambda()
.eq(ImApplication::getAppKey, appKey)); // .eq(ImApplication::getAppKey, appKey));
if (!JwtUtil.verifyToken(token, imApplication.getAppSecret())) { // if (!JwtUtil.verifyToken(token, imApplication.getAppSecret())) {
log.debug("验签token不通过"); // log.debug("验签token不通过");
// }
//
// String redisKey = "client:" + appKey + ":" + clientId;
// String redisToken = redisTemplate.opsForValue().get(redisKey);
//
// // 判断token和redis是否一致
// if (!token.equals(redisToken)) {
// log.debug("token和redis不一致");
// }
if (StringUtils.isBlank(token)) {
throw new AuthenticationException("token不能为空");
} }
if (JwtUtil.isExpired(token)) {
throw new AuthenticationException("JWT Token已过期,token:" + token);
}
// 验签token
JwtToken jwtToken = shiroLoginService.getTJwtTokenForRedis(token);
String redisKey = "client:" + appKey + ":" + clientId; if (jwtToken == null) {
String redisToken = redisTemplate.opsForValue().get(redisKey); log.error("jwtToken == null ,token和redis不一致");
return;
}
// 判断token和redis是否一致 if ((!jwtToken.getClientId().equals(clientId)) || (!jwtToken.getAppKey().equals(appKey))) {
if (!token.equals(redisToken)) { log.error("clientId appKey 不一致");
log.debug("token和redis不一致"); return;
} }
// 设置uri前缀 // 设置uri前缀
......
...@@ -50,7 +50,6 @@ public class ApiResult<T> implements Serializable { ...@@ -50,7 +50,6 @@ public class ApiResult<T> implements Serializable {
* 响应码 * 响应码
*/ */
private int code; private int code;
//
// /** // /**
// * 是否成功 // * 是否成功
// */ // */
...@@ -66,15 +65,8 @@ public class ApiResult<T> implements Serializable { ...@@ -66,15 +65,8 @@ public class ApiResult<T> implements Serializable {
*/ */
private T data; private T data;
// /**
// * 响应时间
// */
// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
// private Date time;
public ApiResult() { public ApiResult() {
// time = new Date();
} }
public static ApiResult<Boolean> result(boolean flag) { public static ApiResult<Boolean> result(boolean flag) {
......
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