作者 shenhailong

修改payfees 类型 添加websocket 通信

@@ -53,6 +53,18 @@ @@ -53,6 +53,18 @@
53 <version>2.8.6</version> 53 <version>2.8.6</version>
54 </dependency> 54 </dependency>
55 55
  56 + <!--websocket 依赖-->
  57 + <dependency>
  58 + <groupId>org.springframework.boot</groupId>
  59 + <artifactId>spring-boot-starter-websocket</artifactId>
  60 + </dependency>
  61 + <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-log -->
  62 + <dependency>
  63 + <groupId>cn.hutool</groupId>
  64 + <artifactId>hutool-log</artifactId>
  65 + <version>5.2.5</version>
  66 + </dependency>
  67 +
56 68
57 <dependency> 69 <dependency>
58 <groupId>org.springframework.boot</groupId> 70 <groupId>org.springframework.boot</groupId>
1 package com.sunyo.energy.location.controller; 1 package com.sunyo.energy.location.controller;
2 2
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONObject;
3 import com.sunyo.energy.location.controller.response.ResultJson; 5 import com.sunyo.energy.location.controller.response.ResultJson;
4 import com.sunyo.energy.location.dao.PayResponseMapper; 6 import com.sunyo.energy.location.dao.PayResponseMapper;
5 import com.sunyo.energy.location.model.PayResponse; 7 import com.sunyo.energy.location.model.PayResponse;
  8 +import com.sunyo.energy.location.websocket.WebSocketServer;
6 import io.swagger.annotations.ApiOperation; 9 import io.swagger.annotations.ApiOperation;
7 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Controller; 11 import org.springframework.stereotype.Controller;
@@ -11,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping; @@ -11,8 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.bind.annotation.RequestParam;
12 import org.springframework.web.bind.annotation.ResponseBody; 15 import org.springframework.web.bind.annotation.ResponseBody;
13 16
  17 +import java.io.IOException;
14 import java.math.BigDecimal; 18 import java.math.BigDecimal;
15 import java.util.Date; 19 import java.util.Date;
  20 +import java.util.HashMap;
  21 +import java.util.Map;
16 import java.util.UUID; 22 import java.util.UUID;
17 23
18 @Controller 24 @Controller
@@ -62,7 +68,7 @@ public class PayRseponeController { @@ -62,7 +68,7 @@ public class PayRseponeController {
62 @RequestParam(value = "USRMSG", required = false) String USRMSG, 68 @RequestParam(value = "USRMSG", required = false) String USRMSG,
63 @RequestParam(value = "USRINFO", required = false) String USRINFO, 69 @RequestParam(value = "USRINFO", required = false) String USRINFO,
64 @RequestParam(value = "PAYTYPE", required = false) String PAYTYPE, 70 @RequestParam(value = "PAYTYPE", required = false) String PAYTYPE,
65 - @RequestParam(value = "SIGN", required = false) String SIGN){ 71 + @RequestParam(value = "SIGN", required = false) String SIGN) throws IOException {
66 72
67 PayResponse payResponse = new PayResponse(); 73 PayResponse payResponse = new PayResponse();
68 payResponse.setId(UUID.randomUUID().toString()); 74 payResponse.setId(UUID.randomUUID().toString());
@@ -87,6 +93,12 @@ public class PayRseponeController { @@ -87,6 +93,12 @@ public class PayRseponeController {
87 int i = payResponseMapperl.insertSelective(payResponse); 93 int i = payResponseMapperl.insertSelective(payResponse);
88 if (i>0){ 94 if (i>0){
89 msg=1; 95 msg=1;
  96 + WebSocketServer webSocketServer = new WebSocketServer();
  97 +
  98 + JSONObject jsonObject = new JSONObject();
  99 + jsonObject.put("orderNumber", ORDERID);
  100 + jsonObject.put("success", SUCCESS);
  101 + webSocketServer.broadcast(jsonObject.toJSONString());
90 } 102 }
91 return i==1? new ResultJson("200","支付回执信息接受成功"):new ResultJson("500","支付回执信息接受失败"); 103 return i==1? new ResultJson("200","支付回执信息接受成功"):new ResultJson("500","支付回执信息接受失败");
92 } 104 }
1 package com.sunyo.energy.location.model; 1 package com.sunyo.energy.location.model;
2 2
  3 +import java.math.BigDecimal;
3 import java.util.Date; 4 import java.util.Date;
4 5
5 public class PayRecords { 6 public class PayRecords {
6 private Integer id; 7 private Integer id;
7 8
8 - private Long payfees; 9 + private BigDecimal payfees;
9 10
10 private Integer payuserid; 11 private Integer payuserid;
11 12
@@ -43,11 +44,11 @@ public class PayRecords { @@ -43,11 +44,11 @@ public class PayRecords {
43 this.id = id; 44 this.id = id;
44 } 45 }
45 46
46 - public Long getPayfees() { 47 + public BigDecimal getPayfees() {
47 return payfees; 48 return payfees;
48 } 49 }
49 50
50 - public void setPayfees(Long payfees) { 51 + public void setPayfees(BigDecimal payfees) {
51 this.payfees = payfees; 52 this.payfees = payfees;
52 } 53 }
53 54
@@ -177,17 +177,20 @@ public class WaterMeterServiceImp implements WaterMeterService { @@ -177,17 +177,20 @@ public class WaterMeterServiceImp implements WaterMeterService {
177 // 成功生成水费订单 177 // 成功生成水费订单
178 PayRecords payRecords = payRecords(payFees, wmId, userId, realName); 178 PayRecords payRecords = payRecords(payFees, wmId, userId, realName);
179 // 请求返回二维码lujing 179 // 请求返回二维码lujing
180 - String s = QrDemo.orUrl(payRecords.getOrdernumber(), payFees); 180 + String url = QrDemo.orUrl(payRecords.getOrdernumber(), payFees);
181 181
  182 + if(!"".equals(url)){
  183 + payRecords.setReamke2(url);
  184 + // 返回map 包含订单号 二维码url
  185 + Map<String, Object> map = new HashMap<>();
  186 + map.put("url",url);
  187 + map.put("orderNumber", payRecords.getOrdernumber());
  188 + resultJson.setData(map);
  189 + }
182 int i = payRecordsMapper.insertSelective(payRecords); 190 int i = payRecordsMapper.insertSelective(payRecords);
183 if(i>0){ 191 if(i>0){
184 resultJson.setCode("200"); 192 resultJson.setCode("200");
185 -  
186 } 193 }
187 - if (!"".equals(s)){  
188 - resultJson.setData(s);  
189 - }  
190 -  
191 return resultJson; 194 return resultJson;
192 } 195 }
193 196
@@ -230,7 +233,7 @@ public class WaterMeterServiceImp implements WaterMeterService { @@ -230,7 +233,7 @@ public class WaterMeterServiceImp implements WaterMeterService {
230 233
231 PayRecords payRecords = new PayRecords(); 234 PayRecords payRecords = new PayRecords();
232 payRecords.setOrdernumber(AllUtils.getOrderIdByTime()); 235 payRecords.setOrdernumber(AllUtils.getOrderIdByTime());
233 - payRecords.setPayfees(Long.valueOf(payFees)); 236 + payRecords.setPayfees(new BigDecimal(payFees));
234 payRecords.setPaystatus(false); 237 payRecords.setPaystatus(false);
235 payRecords.setPaytype(false); 238 payRecords.setPaytype(false);
236 payRecords.setReamke1("0"); 239 payRecords.setReamke1("0");
  1 +package com.sunyo.energy.location.websocket;
  2 +
  3 +import java.nio.charset.Charset;
  4 +import java.util.ArrayList;
  5 +import java.util.List;
  6 +
  7 +import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  8 +import org.springframework.context.annotation.Bean;
  9 +import org.springframework.context.annotation.Configuration;
  10 +import org.springframework.http.MediaType;
  11 +import org.springframework.http.converter.StringHttpMessageConverter;
  12 +
  13 +import com.alibaba.fastjson.support.config.FastJsonConfig;
  14 +import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  15 +
  16 +/**
  17 + * 添加fastjson的转换
  18 + */
  19 +@Configuration
  20 +public class FastjsonConverter {
  21 +
  22 + @Bean
  23 + public HttpMessageConverters customConverters() {
  24 + // 定义一个转换消息的对象
  25 +
  26 + FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  27 +
  28 + // 添加fastjson的配置信息 比如 :是否要格式化返回的json数据
  29 + FastJsonConfig fastJsonConfig = new FastJsonConfig();
  30 + //fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  31 +
  32 + List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
  33 +
  34 + // 处理中文乱码问题1
  35 + // 处理中文乱码问题2
  36 + fastJsonConfig.setCharset(Charset.forName("UTF-8"));
  37 + fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  38 + //fastMediaTypes.add(MediaType.valueOf("text/plain;charset=UTF-8"));
  39 + //fastMediaTypes.add(MediaType.valueOf("text/html;charset=UTF-8"));
  40 + fastConverter.setSupportedMediaTypes(fastMediaTypes);
  41 + // 在转换器中添加配置信息
  42 + fastConverter.setFastJsonConfig(fastJsonConfig);
  43 +
  44 + StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
  45 + stringConverter.setDefaultCharset(Charset.forName("UTF-8"));
  46 + stringConverter.setSupportedMediaTypes(fastMediaTypes);
  47 +
  48 + // 将转换器添加到converters中
  49 + return new HttpMessageConverters(stringConverter,fastConverter);
  50 + }
  51 +}
  1 +package com.sunyo.energy.location.websocket;
  2 +
  3 +import org.springframework.context.annotation.Bean;
  4 +import org.springframework.context.annotation.Configuration;
  5 +import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  6 +
  7 +@Configuration
  8 +public class WebSocketConfig {
  9 +
  10 + @Bean
  11 + public ServerEndpointExporter serverEndpointExporter() {
  12 + return new ServerEndpointExporter();
  13 + }
  14 +
  15 +}
  1 +package com.sunyo.energy.location.websocket;
  2 +
  3 +
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.JSONObject;
  6 +import org.apache.commons.lang.StringUtils;
  7 +import cn.hutool.log.Log;
  8 +import cn.hutool.log.LogFactory;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import javax.websocket.*;
  12 +import javax.websocket.server.PathParam;
  13 +import javax.websocket.server.ServerEndpoint;
  14 +import java.io.IOException;
  15 +import java.util.concurrent.ConcurrentHashMap;
  16 +
  17 +
  18 +/**
  19 + * WebSocketServer
  20 + * @author zhengkai.blog.csdn.net
  21 + */
  22 +@ServerEndpoint("/imserver")
  23 +@Component
  24 +public class WebSocketServer {
  25 +
  26 + static Log log= LogFactory.get(WebSocketServer.class);
  27 + /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
  28 + private static int onlineCount = 0;
  29 + /**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
  30 + private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
  31 + /**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
  32 + private Session session;
  33 + /**接收userId*/
  34 + private String orderNum="";
  35 +
  36 + /**
  37 + * 连接建立成功调用的方法*/
  38 + @OnOpen
  39 + public void onOpen(Session session) {
  40 + this.session = session;
  41 + this.orderNum=orderNum;
  42 + if(webSocketMap.containsKey(orderNum)){
  43 + webSocketMap.remove(orderNum);
  44 + webSocketMap.put(orderNum,this);
  45 + //加入set中
  46 + }else{
  47 + webSocketMap.put(orderNum,this);
  48 + //加入set中
  49 + addOnlineCount();
  50 + //在线数加1
  51 + }
  52 +
  53 + log.info("用户连接:"+orderNum+",当前在线人数为:" + getOnlineCount());
  54 +
  55 + try {
  56 + sendMessage("连接成功");
  57 + } catch (IOException e) {
  58 + log.error("用户:"+orderNum+",网络异常!!!!!!");
  59 + }
  60 + }
  61 +
  62 + /**
  63 + * 连接关闭调用的方法
  64 + */
  65 + @OnClose
  66 + public void onClose() {
  67 + if(webSocketMap.containsKey(orderNum)){
  68 + webSocketMap.remove(orderNum);
  69 + //从set中删除
  70 + subOnlineCount();
  71 + }
  72 + log.info("用户退出:"+orderNum+",当前在线人数为:" + getOnlineCount());
  73 + }
  74 +
  75 + /**
  76 + * 收到客户端消息后调用的方法
  77 + *
  78 + * @param message 客户端发送过来的消息*/
  79 +// @OnMessage
  80 +// public void onMessage(String message, Session session) {
  81 +// log.info("用户消息:"+orderNum+",报文:"+message);
  82 +// //可以群发消息
  83 +// //消息保存到数据库、redis
  84 +// if(StringUtils.isNotBlank(message)){
  85 +// try {
  86 +//// //解析发送的报文
  87 +//// JSONObject jsonObject = JSON.parseObject(message);
  88 +//// //追加发送人(防止串改)
  89 +//// jsonObject.put("fromOrderNum",this.orderNum);
  90 +//// String toUserId=jsonObject.getString("toUserId");
  91 +//// //传送给对应toUserId用户的websocket
  92 +//// if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){
  93 +//// webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString());
  94 +//// }else{
  95 +//// log.error("请求的userId:"+toUserId+"不在该服务器上");
  96 +//// //否则不在这个服务器上,发送到mysql或者redis
  97 +//// }
  98 +// }catch (Exception e){
  99 +// e.printStackTrace();
  100 +// }
  101 +// }
  102 +// }
  103 +
  104 + /**
  105 + * 群发消息
  106 + * @param content 要广播的内容
  107 + */
  108 + public void broadcast(String content){
  109 + for (ConcurrentHashMap.Entry<String,WebSocketServer> entry : webSocketMap.entrySet()) {
  110 + WebSocketServer webSocketServerE= entry.getValue();
  111 + try {
  112 + webSocketServerE.sendMessage(content);
  113 + }catch (IOException e){
  114 + e.printStackTrace();
  115 + }
  116 +
  117 + System.out.println(entry.getKey() + ":" + entry.getValue());
  118 + }
  119 + }
  120 +
  121 + /**
  122 + *
  123 + * @param session
  124 + * @param error
  125 + */
  126 + @OnError
  127 + public void onError(Session session, Throwable error) {
  128 + log.error("用户错误:"+this.orderNum+",原因:"+error.getMessage());
  129 + error.printStackTrace();
  130 + }
  131 + /**
  132 + * 实现服务器主动推送
  133 + */
  134 + public void sendMessage(String message) throws IOException {
  135 + this.session.getBasicRemote().sendText(message);
  136 + }
  137 +
  138 +
  139 + /**
  140 + * 发送自定义消息
  141 + * */
  142 + public static void sendInfo(String message,@PathParam("orderNum") String orderNum) throws IOException {
  143 + log.info("发送消息到:"+orderNum+",报文:"+message);
  144 + if(StringUtils.isNotBlank(orderNum)&&webSocketMap.containsKey(orderNum)){
  145 + webSocketMap.get(orderNum).sendMessage(message);
  146 + }else{
  147 + log.error("用户"+orderNum+",不在线!");
  148 + }
  149 + }
  150 +
  151 + public static synchronized int getOnlineCount() {
  152 + return onlineCount;
  153 + }
  154 +
  155 + public static synchronized void addOnlineCount() {
  156 + WebSocketServer.onlineCount++;
  157 + }
  158 +
  159 + public static synchronized void subOnlineCount() {
  160 + WebSocketServer.onlineCount--;
  161 + }
  162 +}