修改消息服务 忽略过滤名单

master
ninftao 3 years ago
parent e5a5c047fa
commit 57884a9527

@ -0,0 +1,23 @@
package com.baiye.exception;
/**
* @author Enzo
* @date: 2022/1/6
* @Description
*/
import lombok.NoArgsConstructor;
/**
* @author Enzo
* @date : 2022/1/9
*/
@NoArgsConstructor
public class CallException extends RuntimeException {
private static final long serialVersionUID = 9022008349767066965L;
public CallException(String message) {
super(message);
}
}

@ -16,6 +16,7 @@
package com.baiye.exception.handler;
import com.baiye.exception.BadRequestException;
import com.baiye.exception.CallException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -46,6 +47,16 @@ public class GlobalExceptionHandler {
return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
}
/**
* open
*/
@ExceptionHandler(value = CallException.class)
public ResponseEntity<ApiError> callException(CallException e) {
// 打印堆栈信息
log.error(getStackTrace(e));
return buildResponseEntity(ApiError.error(e.getMessage()));
}
/**
*
*/

@ -1,6 +1,8 @@
package com.baiye.feign;
import com.baiye.constant.SecurityConstants;
import com.baiye.exception.CallException;
import com.baiye.http.ResponseCode;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
@ -22,11 +24,11 @@ public class FeignConfiguration implements RequestInterceptor {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
String uri = request.getRequestURI();
// HeaderConstants.TOKEN_HEADER_NAME 替换为自己的请求头名称,下同
String token = request.getHeader(SecurityConstants.AUTHORIZATION);
if(token == null){
log.info("--请求中未携带token.......");
return;
if (token == null && !FeignProvider.getDefaultSkipUrl().contains(uri)) {
throw new CallException(ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
requestTemplate.header(SecurityConstants.AUTHORIZATION, token);
}

@ -0,0 +1,28 @@
package com.baiye.feign;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author Chill
*/
public class FeignProvider {
private static final List<String> DEFAULT_SKIP_URL = new ArrayList<>();
static {
DEFAULT_SKIP_URL.add("/swagger/**");
}
/**
* API
*/
public static List<String> getDefaultSkipUrl() {
return DEFAULT_SKIP_URL;
}
}

@ -31,6 +31,8 @@ public enum ResponseCode {
*/
EMPTY_ARGUMENT(1,"请求参数为空"),
/**
*
*/

@ -0,0 +1,16 @@
package com.baiye.model.dto;
import lombok.Data;
/**
* @author Enzo
* @date : 2022/1/9
*/
@Data
public class ChangeMessageDTO {
private Boolean flag;
private Long messageId;
}

@ -11,14 +11,30 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum WebSocketEnums {
/**
*
*/
TOP(1,"top"),
/**
*
*/
WITHDRAW(2, "withdraw"),
/**
*
*/
READ(3, "read"),
FILE(4, "file");
/**
*
*/
FILE(4, "file"),
/**
*
*/
CREATE(5,"create");
private final Integer value;
private final String type;

@ -2,7 +2,9 @@ package com.baiye.feign;
import com.baiye.http.CommonResponse;
import com.baiye.model.dto.SendWebSocketDTO;
import com.baiye.model.entity.BaseTimeTask;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -30,4 +32,20 @@ public interface SendMessageClient {
*/
@PostMapping(API_PREFIX + "/websocket/save")
CommonResponse<Object> saveMessage(@RequestBody SendWebSocketDTO sendWebSocketDTO);
/**
*
* @param timeTask
* @return
*/
@PostMapping(API_PREFIX + "/time/add")
CommonResponse<Object> createTimedMessages(@RequestBody BaseTimeTask timeTask);
/**
*
* @param timeTask
* @return
*/
@GetMapping(API_PREFIX + "/time/update")
CommonResponse<Object> changeTaskTime(@RequestBody BaseTimeTask timeTask);
}

@ -2,6 +2,7 @@ package com.baiye.feign;
import com.baiye.http.CommonResponse;
import com.baiye.model.dto.SendWebSocketDTO;
import com.baiye.model.entity.BaseTimeTask;
import org.springframework.stereotype.Component;
/**
@ -19,4 +20,14 @@ public class SendMessageFallback implements SendMessageClient {
public CommonResponse<Object> saveMessage(SendWebSocketDTO sendWebSocketDTO) {
return null;
}
@Override
public CommonResponse<Object> createTimedMessages(BaseTimeTask timeTask) {
return null;
}
@Override
public CommonResponse<Object> changeTaskTime(BaseTimeTask timeTask) {
return null;
}
}

@ -60,9 +60,14 @@ public class MessageNotification implements Serializable {
private Boolean isEnable;
@Column(name = "is_top")
@ApiModelProperty(value = "是否使用")
@ApiModelProperty(value = "是否置顶")
private Boolean isTop;
@Column(name = "is_reuse")
@ApiModelProperty(value = "是否重复使用")
private Boolean isReuse;
@Column(name = "type")
@ApiModelProperty(value = "类型 1公告 2自提醒")
private Integer type;

@ -36,6 +36,16 @@ public interface UserMessageRepository extends JpaRepository<UserMessage, Long>,
* @return
*/
@Modifying
@Query(value = "update UserMessage set type = ?1 where messageId = ?1")
@Query(value = "update UserMessage set type = ?1 where messageId = ?2")
Boolean deleteUserMessageByMessageId(Integer num, Long messageId);
/**
*
* @param aTrue
* @param aLong
* @return
*/
@Modifying
@Query(value = "update UserMessage set isRead = ?1 where id = ?2")
Boolean updateUserMessageIsReadById(Boolean aTrue, Long aLong);
}

@ -65,10 +65,10 @@ public class MessageNotificationController {
@PutMapping
@PutMapping("changeMessage")
@ApiOperation("修改message")
public ResponseEntity<Object> update(@Validated @RequestBody MessageNotificationDto messageNotificationDto){
messageNotificationService.update(messageNotificationDto);
public ResponseEntity<Object> update(@RequestBody String body){
messageNotificationService.update(body);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@ -79,9 +79,5 @@ public class MessageNotificationController {
return new ResponseEntity<>(HttpStatus.OK);
}
@GetMapping("/changeMessage")
@ApiOperation("修改消息服务")
public ResponseEntity<Object> changeMessage(Boolean flag,Long messageId){
return new ResponseEntity<>(messageNotificationService.withdrawMessage(flag,messageId),HttpStatus.OK);
}
}

@ -46,7 +46,7 @@ public class UserMessageController {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@ApiOperation("新增角色")
@ApiOperation("创建小")
@PostMapping("/createMessage")
public CommonResponse<Boolean> create(String body) {
if (StringUtils.isEmpty(body)) {
@ -55,5 +55,13 @@ public class UserMessageController {
return userMessageService.createUserMessage(body);
}
@ApiOperation("已读")
@PostMapping("/read")
public CommonResponse<Boolean> read(String body) {
if (StringUtils.isEmpty(body)) {
return CommonResponse.createByErrorMessage(ResponseCode.ERROR.getDesc());
}
return userMessageService.read(body);
}
}

@ -65,7 +65,7 @@ public interface MessageNotificationService {
*
* @param resources /
*/
void update(MessageNotificationDto resources);
void update(String resources);
/**
*
@ -82,11 +82,10 @@ public interface MessageNotificationService {
void download(List<MessageNotificationDto> all, HttpServletResponse response) throws IOException;
/**
*
*
* @param flag
* @param messageId
* body
* @param body
* @return
*/
Boolean withdrawMessage(Boolean flag, Long messageId);
Boolean withdrawMessage(String body);
}

@ -107,4 +107,11 @@ public interface UserMessageService {
* @return
*/
CommonResponse<Boolean> createUserMessage(String body);
/**
*
* @param body
* @return
*/
CommonResponse<Boolean> read(String body);
}

@ -39,7 +39,7 @@ public class MessageNotificationDto implements Serializable {
/** 消息内容 */
private String messageContext;
/** 1超级管理员 2管理员 3组长消息 4自己消息*/
/** 1超级管理员 2 管理员 3 组长消息 4 自己消息*/
private Integer level;
/** 1公告 2提醒 */

@ -18,6 +18,9 @@ package com.baiye.modules.system.service.impl;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.feign.SendMessageClient;
import com.baiye.model.dto.ChangeMessageDTO;
import com.baiye.model.entity.BaseTimeTask;
import com.baiye.modules.system.domain.MessageNotification;
import com.baiye.modules.system.repository.MessageNotificationRepository;
import com.baiye.modules.system.service.MessageNotificationService;
@ -62,6 +65,8 @@ public class MessageNotificationServiceImpl implements MessageNotificationServic
private final UserMessageService userMessageService;
private final SendMessageClient sendMessageClient;
private final UserService userService;
@ -122,10 +127,28 @@ public class MessageNotificationServiceImpl implements MessageNotificationServic
@Override
@Transactional(rollbackFor = Exception.class)
public void update(MessageNotificationDto resources) {
MessageNotification messageNotification = messageNotificationRepository.findById(resources.getId()).orElseGet(MessageNotification::new);
ValidationUtil.isNull(messageNotification.getId(), "MessageNotification", "id", resources.getId());
messageNotificationRepository.save(Convert.convert(MessageNotification.class, resources));
public void update(String resources) {
MessageNotificationDto messageNotificationDto = JSONUtil.toBean(resources, MessageNotificationDto.class);
if (messageNotificationDto != null) {
MessageNotification messageNotification = messageNotificationRepository.findById(messageNotificationDto.getId()).orElseGet(MessageNotification::new);
ValidationUtil.isNull(messageNotification.getId(), "MessageNotification", "id", messageNotificationDto.getId());
messageNotificationRepository.save(Convert.convert(MessageNotification.class, resources));
if (messageNotification.getIsEnable() != null && !messageNotification.getIsEnable()) {
userMessageService.deleteAllByMessageId(DefaultNumberConstants.MINUS_ONE_NUMBER, messageNotificationDto.getId());
}
if (messageNotification.getType() != null && messageNotification.getType() == DefaultNumberConstants.ONE_NUMBER) {
BaseTimeTask baseTimeTask = new BaseTimeTask();
// TODO 增加消息id
baseTimeTask.setMessage(messageNotification.getMessageContext());
baseTimeTask.setUserId(SecurityUtils.getCurrentUserId());
baseTimeTask.setIsRepeat(messageNotification.getIsReuse() ?
DefaultNumberConstants.ONE_NUMBER : DefaultNumberConstants.ZERO_NUMBER);
sendMessageClient.changeTaskTime(baseTimeTask);
}
}
}
@Override
@ -155,11 +178,17 @@ public class MessageNotificationServiceImpl implements MessageNotificationServic
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean withdrawMessage(Boolean flag, Long messageId) {
public Boolean withdrawMessage(String body) {
ChangeMessageDTO changeMessageDTO = JSONUtil.toBean(body, ChangeMessageDTO.class);
if (changeMessageDTO == null) {
return Boolean.FALSE;
}
Boolean flag = changeMessageDTO.getFlag();
messageNotificationRepository
.updateMessageEnableByMessage(flag, messageId);
.updateMessageEnableByMessage(flag == null ? Boolean.FALSE : flag, changeMessageDTO.getMessageId());
return userMessageService.deleteAllByMessageId(Boolean.TRUE.equals(flag)
? DefaultNumberConstants.ONE_NUMBER : DefaultNumberConstants.MINUS_ONE_NUMBER, messageId);
? DefaultNumberConstants.ONE_NUMBER : DefaultNumberConstants.MINUS_ONE_NUMBER, changeMessageDTO.getMessageId());
}
}

@ -1,26 +1,28 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baiye.modules.system.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.feign.SendMessageClient;
import com.baiye.http.CommonResponse;
import com.baiye.model.dto.SendWebSocketDTO;
import com.baiye.model.entity.BaseTimeTask;
import com.baiye.modules.system.domain.MessageNotification;
import com.baiye.modules.system.domain.UserMessage;
import com.baiye.modules.system.repository.UserMessageRepository;
@ -47,11 +49,11 @@ import java.util.List;
import java.util.Map;
/**
* @website https://el-admin.vip
* @description
* @author Enzo
* @date 2021-12-30
**/
* @author Enzo
* @website https://el-admin.vip
* @description
* @date 2021-12-30
**/
@Service
@RequiredArgsConstructor
public class UserMessageServiceImpl implements UserMessageService {
@ -64,14 +66,14 @@ public class UserMessageServiceImpl implements UserMessageService {
@Override
public Map<String,Object> queryAll(UserMessageQueryCriteria criteria, Pageable pageable){
public Map<String, Object> queryAll(UserMessageQueryCriteria criteria, Pageable pageable) {
Page<UserMessage> page = userMessageRepository.findAll(
(root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
(root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
return PageUtil.toPage(page.map(userMessageMapper::toDto));
}
@Override
public List<UserMessageDto> queryAll(UserMessageQueryCriteria criteria){
public List<UserMessageDto> queryAll(UserMessageQueryCriteria criteria) {
Sort sort = Sort.by(Sort.Direction.ASC, "level").
and(Sort.by(Sort.Direction.DESC, "isTop")).
and(Sort.by(Sort.Direction.DESC, "isRead")).
@ -84,7 +86,7 @@ public class UserMessageServiceImpl implements UserMessageService {
@Transactional(rollbackFor = Exception.class)
public UserMessageDto findById(Long id) {
UserMessage userMessage = userMessageRepository.findById(id).orElseGet(UserMessage::new);
ValidationUtil.isNull(userMessage.getId(),"UserMessage","id",id);
ValidationUtil.isNull(userMessage.getId(), "UserMessage", "id", id);
return userMessageMapper.toDto(userMessage);
}
@ -98,7 +100,7 @@ public class UserMessageServiceImpl implements UserMessageService {
@Transactional(rollbackFor = Exception.class)
public void update(UserMessage resources) {
UserMessage userMessage = userMessageRepository.findById(resources.getId()).orElseGet(UserMessage::new);
ValidationUtil.isNull( userMessage.getId(),"UserMessage","id",resources.getId());
ValidationUtil.isNull(userMessage.getId(), "UserMessage", "id", resources.getId());
userMessage.copy(resources);
userMessageRepository.save(userMessage);
}
@ -114,7 +116,7 @@ public class UserMessageServiceImpl implements UserMessageService {
public void download(List<UserMessageDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (UserMessageDto userMessageDto : all) {
Map<String,Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("是否已读", userMessageDto.getIsRead());
map.put("开始时间", userMessageDto.getStartTime());
map.put("结束时间", userMessageDto.getEndTime());
@ -143,7 +145,17 @@ public class UserMessageServiceImpl implements UserMessageService {
userMessageRepository.save(userMessage);
}
}
// TODO发送消息
if (messageNotification.getType() == DefaultNumberConstants.ONE_NUMBER) {
BaseTimeTask baseTimeTask = new BaseTimeTask();
// TODO 增加消息id
baseTimeTask.setMessage(messageNotification.getMessageContext());
baseTimeTask.setUserId(longList.get(DefaultNumberConstants.ZERO_NUMBER));
baseTimeTask.setIsRepeat(messageNotification.getIsReuse() ?
DefaultNumberConstants.ONE_NUMBER : DefaultNumberConstants.ZERO_NUMBER);
sendMessageClient.createTimedMessages(baseTimeTask);
return Boolean.TRUE;
}
SendWebSocketDTO sendWebSocketDTO = new SendWebSocketDTO();
sendWebSocketDTO.setUserIds(longList);
SendWebSocketDTO.SendMessage sendMessage = new SendWebSocketDTO.SendMessage();
@ -165,4 +177,12 @@ public class UserMessageServiceImpl implements UserMessageService {
return null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public CommonResponse<Boolean> read(String body) {
Long aLong = JSONUtil.toBean(body, Long.class);
userMessageRepository.updateUserMessageIsReadById(Boolean.TRUE,aLong);
return CommonResponse.createBySuccess();
}
}

@ -42,12 +42,27 @@ public interface ConnectManageFeign {
* @param body
* @return
*/
@ApiOperation("新增消息服务")
@ApiOperation("新增消息模板")
@PostMapping(API_PREFIX + "/messageNotification/createMessage")
ResponseEntity<Object> createMessage(@RequestBody String body);
/**
*
* @param body
* @return
*/
@ApiOperation("修改消息模板")
@PostMapping(API_PREFIX + "/messageNotification/changeMessage")
ResponseEntity<Object> changeMessage(@RequestBody String body);
/**
*
* @param message
* @return
*/
@ApiOperation("修改消息服务")
@PostMapping(API_PREFIX + "/userMessage/changeMessage")
ResponseEntity<Object> userMessageRead(@RequestBody String message);
}

@ -27,6 +27,15 @@ public class ConnectManageFeignFallBack implements ConnectManageFeign {
return null;
}
@Override
public ResponseEntity<Object> changeMessage(String body) {
return null;
}
@Override
public ResponseEntity<Object> userMessageRead(String message) {
return null;
}
}

@ -0,0 +1,42 @@
package com.baiye.filter;
import com.baiye.constant.SecurityConstants;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Enzo
* @date : 2022/1/9
*/
@Order(1)
@Component
@WebFilter(filterName = "WebsocketFilter",urlPatterns = "/task/prosperous")
public class WebsocketFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
String token = ((HttpServletRequest) request).getHeader("Sec-WebSocket-Protocol");
response.setHeader("Sec-WebSocket-Protocol",token);
if (token != null) {
request.setAttribute(SecurityConstants.AUTHORIZATION,token);
}
chain.doFilter(request, servletResponse);
}
@Override
public void destroy() {
}
}

@ -1,6 +1,8 @@
package com.baiye.modules.elsaticjob.api;
import cn.hutool.core.bean.BeanUtil;
import com.baiye.http.CommonResponse;
import com.baiye.model.entity.BaseTimeTask;
import com.baiye.modules.elsaticjob.entity.AutoReminder;
import com.baiye.modules.elsaticjob.service.AutoReminderService;
import io.swagger.annotations.Api;
@ -29,8 +31,9 @@ public class AutoReminderController {
*/
@PostMapping("/add")
@ApiOperation("新增")
public CommonResponse<Object> addTimeTask(@RequestBody AutoReminder timeTask) {
return timeTaskService.addTimeTask(timeTask);
public CommonResponse<Object> addTimeTask(@RequestBody BaseTimeTask timeTask) {
AutoReminder autoReminder = new AutoReminder();
BeanUtil.copyProperties(timeTask, autoReminder);
return timeTaskService.addTimeTask(autoReminder);
}
}

@ -95,14 +95,14 @@ public class WebSocketServer {
webSocketServer.reportServiceImpl.reportHour();
}
switch (WebSocketEnums.find(jsonObject.getStr(type))) {
case DefaultNumberConstants.ONE_NUMBER:
connectManageFeign.createMessage(message);
break;
case DefaultNumberConstants.TWO_NUMBER:
connectManageFeign.changeMessage(message);
break;
case DefaultNumberConstants.THREE_NUMBER:
// connectManageFeign.changeMessage(jsonObject.get(""));
connectManageFeign.userMessageRead(message);
break;
case DefaultNumberConstants.FIVE_NUMBER:
connectManageFeign.createMessage(message);
break;
default:
break;

Loading…
Cancel
Save