Commit 06b844fa authored by Shen's avatar Shen

1. 添加WebSocket对订单进行PC端的通知

2. Shiro开放WebSocket
3. 添加NewRestTemplate类对第三方接口的请求
4. 需要对websocket用户身份进行分别通知
parent 38e96398
package io.hmit.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @Description : WebSocket配置
* @Author : Shen Yuanfeng
* @Date : 2021/5/17 16:29
*/
@Component
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
package io.hmit.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @Description : websocket
* @Author : Shen Yuanfeng
* @Date : 2021/5/17 16:31
*/
@Slf4j
@Component
@ServerEndpoint("/webSocket")
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<>();
@OnOpen
public void opOpen(Session session) {
this.session = session;
webSocketSet.add(this);
log.info("【WebSocket】有一个新的连接,总数:{}", webSocketSet.size());
}
@OnClose
public void onClose() {
webSocketSet.remove(this);
log.info("【WebSocket】连接断开,总数:{}", webSocketSet.size());
}
@OnMessage
public void onMessage(String message) {
log.info("【WebSocket】收到客户端发来的消息:{}", message);
}
public void sendMessage(String message) {
for (WebSocket webSocket : webSocketSet) {
log.info("【WebSocket】广播消息,message={}", message);
try {
webSocket.session.getBasicRemote().sendText(message);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
...@@ -58,6 +58,7 @@ public class ShiroConfig { ...@@ -58,6 +58,7 @@ public class ShiroConfig {
Map<String, String> filterMap = new LinkedHashMap<>(); Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/static/**", "anon"); filterMap.put("/static/**", "anon");
filterMap.put("/webSocket/**", "anon");
filterMap.put("/webjars/**", "anon"); filterMap.put("/webjars/**", "anon");
filterMap.put("/druid/**", "anon"); filterMap.put("/druid/**", "anon");
filterMap.put("/login", "anon"); filterMap.put("/login", "anon");
......
package io.hmit.modules.sys.controller;
import io.hmit.common.utils.WebSocket;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.service.PensionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description : 通过websocket通知
* @Author : Shen Yuanfeng
* @Date : 2021/5/17 17:05
*/
@RestController
@RequestMapping("/webSocket")
public class WebSocketController {
@Autowired
private WebSocket webSocket;
@Autowired
private PensionOrderService pensionOrderService;
@GetMapping("notify")
public String doNotify(@RequestParam("orderId") Long orderId) {
PensionOrderDTO pensionOrderDTO = pensionOrderService.get(orderId);
webSocket.sendMessage("新订单-服务id:" + pensionOrderDTO.getServiceId());
return "success";
}
}
...@@ -5,6 +5,7 @@ import io.hmit.annotation.Login; ...@@ -5,6 +5,7 @@ import io.hmit.annotation.Login;
import io.hmit.annotation.LoginUser; import io.hmit.annotation.LoginUser;
import io.hmit.common.constant.Constant; import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData; import io.hmit.common.page.PageData;
import io.hmit.common.utils.NewRestTemplate;
import io.hmit.common.utils.Result; import io.hmit.common.utils.Result;
import io.hmit.common.validator.AssertUtils; import io.hmit.common.validator.AssertUtils;
import io.hmit.common.validator.ValidatorUtils; import io.hmit.common.validator.ValidatorUtils;
......
...@@ -6,6 +6,7 @@ import io.hmit.common.constant.Constant; ...@@ -6,6 +6,7 @@ import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData; import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl; import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils; import io.hmit.common.utils.ConvertUtils;
import io.hmit.common.utils.NewRestTemplate;
import io.hmit.entity.UserEntity; import io.hmit.entity.UserEntity;
import io.hmit.modules.serviceOrder.dao.PensionOrderDao; import io.hmit.modules.serviceOrder.dao.PensionOrderDao;
import io.hmit.modules.serviceOrder.dto.*; import io.hmit.modules.serviceOrder.dto.*;
...@@ -92,6 +93,7 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe ...@@ -92,6 +93,7 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe
PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO); PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO);
pensionOrderServiceService.save(orderServiceDTO); pensionOrderServiceService.save(orderServiceDTO);
pushMessage.sendWeChatMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid(), "oldman"); pushMessage.sendWeChatMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid(), "oldman");
NewRestTemplate.getNewOrder(pensionOrderDTO.getId().toString());
// pushMessage.sendSMSMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid()); // pushMessage.sendSMSMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
} }
......
package io.hmit.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
/**
* @Description : 内部请求第三方接口
* @Author : Shen Yuanfeng
* @Date : 2021/5/17 17:15
*/
@Slf4j
public class NewRestTemplate {
public static void getNewOrder(String orderId){
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:8088/hmit-admin/webSocket/notify?orderId=" + orderId;
String trans = restTemplate.getForObject(url,String.class);
log.info("trans = {}", trans);
}
}
...@@ -195,6 +195,10 @@ ...@@ -195,6 +195,10 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>${lombok.version}</version> <version>${lombok.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.github.binarywang</groupId> <groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId> <artifactId>weixin-java-mp</artifactId>
......
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