WebSocket来单提醒和客户催单
import com.alibaba.fastjson.JSON;
- @Autowired
- private WebSocketServer webSocketServer;
- /**
- * 支付成功,修改订单状态
- *
- * @param outTradeNo
- */
- public void paySuccess(String outTradeNo) {
- // 当前登录用户id
- Long userId = BaseContext.getCurrentId();
-
- // 根据订单号查询当前用户的订单
- Orders ordersDB = orderMapper.getByNumberAndUserId(outTradeNo, userId);
-
- // 根据订单id更新订单的状态、支付方式、支付状态、结账时间
- Orders orders = Orders.builder()
- .id(ordersDB.getId())
- .status(Orders.TO_BE_CONFIRMED)
- .payStatus(Orders.PAID)
- .checkoutTime(LocalDateTime.now())
- .build();
-
- orderMapper.update(orders);
- //////////////////////////////////////////////
- Map map = new HashMap();
- map.put("type", 1);//消息类型,1表示来单提醒
- map.put("orderId", orders.getId());
- map.put("content", "订单号:" + outTradeNo);
-
- //通过WebSocket实现来单提醒,向客户端浏览器推送消息
- webSocketServer.sendToAllClient(JSON.toJSONString(map));
- ///////////////////////////////////////////////////
- }
复制代码
WebSocket服务
- package com.zidiu.websocket;
-
- import jakarta.websocket.server.ServerEndpoint;
- import org.springframework.stereotype.Component;
- import jakarta.websocket.OnClose;
- import jakarta.websocket.OnMessage;
- import jakarta.websocket.OnOpen;
- import jakarta.websocket.Session;
- import jakarta.websocket.server.PathParam;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * WebSocket服务
- */
- @Component
-
- @ServerEndpoint("/ws/{sid}")
- public class WebSocketServer {
-
- //存放会话对象
- private static Map<String, Session> sessionMap = new HashMap();
-
- /**
- * 连接建立成功调用的方法
- */
- @OnOpen
- public void onOpen(Session session, @PathParam("sid") String sid) {
- System.out.println("客户端:" + sid + "建立连接");
- sessionMap.put(sid, session);
- }
-
- /**
- * 收到客户端消息后调用的方法
- *
- * @param message 客户端发送过来的消息
- */
- @OnMessage
- public void onMessage(String message, @PathParam("sid") String sid) {
- System.out.println("收到来自客户端:" + sid + "的信息:" + message);
- }
-
- /**
- * 连接关闭调用的方法
- *
- * @param sid
- */
- @OnClose
- public void onClose(@PathParam("sid") String sid) {
- System.out.println("连接断开:" + sid);
- sessionMap.remove(sid);
- }
-
- /**
- * 群发
- *
- * @param message
- */
- public void sendToAllClient(String message) {
- Collection<Session> sessions = sessionMap.values();
- for (Session session : sessions) {
- try {
- //服务器向客户端发送消息
- session.getBasicRemote().sendText(message);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-
- }
复制代码 客户催单
- /**
- * 用户催单
- *
- * @param id
- * @return
- */
- @GetMapping("/reminder/{id}")
- @ApiOperation("用户催单")
- public Result reminder(@PathVariable("id") Long id) {
- orderService.reminder(id);
- return Result.success();
- }
复制代码
- /**
- * 用户催单
- * @param id
- */
- void reminder(Long id);
复制代码
- /**
- * 用户催单
- *
- * @param id
- */
- public void reminder(Long id) {
- // 查询订单是否存在
- Orders orders = orderMapper.getById(id);
- if (orders == null) {
- throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
- }
-
- //基于WebSocket实现催单
- Map map = new HashMap();
- map.put("type", 2);//2代表用户催单
- map.put("orderId", id);
- map.put("content", "订单号:" + orders.getNumber());
- webSocketServer.sendToAllClient(JSON.toJSONString(map));
- }
复制代码
- /**
- * 根据id查询订单
- * @param id
- */
- @Select("select * from orders where id=#{id}")
- Orders getById(Long id);
复制代码
|