523096025 1 vuosi sitten
vanhempi
commit
7139b2d077

+ 11 - 0
pom.xml

@@ -21,6 +21,17 @@
             <artifactId>spring-boot-starter-websocket</artifactId>
             <version>2.3.1.RELEASE</version>
         </dependency>
+<!--生成二维码-->
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.4.1</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>javase</artifactId>
+            <version>3.4.1</version>
+        </dependency>
 
         <dependency>
             <groupId>com.alibaba</groupId>

+ 12 - 0
src/main/java/com/huimv/wine/common/EventWsErrUtil.java

@@ -0,0 +1,12 @@
+package com.huimv.wine.common;
+
+
+import com.huimv.wine.entity.vo.WsEvent;
+
+public class EventWsErrUtil {
+
+    public static WsEvent getWsErr(String msg){
+        return new WsEvent("__Error_Event__",msg);
+    }
+
+}

+ 11 - 0
src/main/java/com/huimv/wine/entity/vo/QrcodeParam.java

@@ -0,0 +1,11 @@
+package com.huimv.wine.entity.vo;
+
+import lombok.Data;
+
+@Data
+public class QrcodeParam {
+   private int id;
+    private  int cell;
+    private  int weight;
+    private  int cash;
+}

+ 59 - 0
src/main/java/com/huimv/wine/utils/CodeImgUtil.java

@@ -0,0 +1,59 @@
+package com.huimv.wine.utils;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.WriterException;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.QRCodeWriter;
+import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.wine.utils
+ * @Description : TODO
+ * @Author : yuxuexuan
+ * @Create : 2024/4/18 0018 18:36
+ **/
+public class CodeImgUtil {
+    public  static String generateQRCode(String url,  int width, int height) {
+        QRCodeWriter qrCodeWriter = new QRCodeWriter();
+        Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<>();
+        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
+        BitMatrix bitMatrix = null;
+        try {
+            bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, width, height, hintMap);
+        } catch (WriterException e) {
+            e.printStackTrace();
+        }
+
+        if (bitMatrix == null) {
+            return null;
+        }
+
+        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
+            }
+        }
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        try {
+            ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        byte[] imageBytes = byteArrayOutputStream.toByteArray();
+        return Base64.getEncoder().encodeToString(imageBytes);
+
+
+    }
+}

+ 2 - 1
src/main/java/com/huimv/wine/utils/WebsocketSellerUtil.java

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.util.JSONPObject;
 import com.huimv.wine.entity.vo.WsEvent;
 import com.mysql.cj.xdevapi.JsonParser;
+import org.springframework.util.ObjectUtils;
 
 import javax.websocket.RemoteEndpoint;
 import javax.websocket.Session;
@@ -43,8 +44,8 @@ public class WebsocketSellerUtil {
     if(session == null){
       return;
     }
-    // 同步
     RemoteEndpoint.Async async = session.getAsyncRemote ();
+
     async.sendText (JSON.toJSONString(wsEvent));
   }
 

+ 82 - 33
src/main/java/com/huimv/wine/ws/SellerController.java

@@ -1,11 +1,13 @@
 package com.huimv.wine.ws;
 
-import com.fasterxml.jackson.databind.util.BeanUtil;
+import com.alibaba.fastjson.JSON;
+import com.huimv.wine.common.EventWsErrUtil;
 import com.huimv.wine.config.VipCofing;
 import com.huimv.wine.entity.Advertise;
 import com.huimv.wine.entity.Device;
 import com.huimv.wine.entity.Params;
 import com.huimv.wine.entity.Wine;
+import com.huimv.wine.entity.vo.QrcodeParam;
 import com.huimv.wine.entity.vo.WsEvent;
 import com.huimv.wine.mapper.AdvertiseMapper;
 import com.huimv.wine.mapper.DeviceMapper;
@@ -13,8 +15,7 @@ import com.huimv.wine.mapper.ParamsMapper;
 import com.huimv.wine.mapper.WineMapper;
 import com.huimv.wine.utils.SpringContextUtil;
 import com.huimv.wine.utils.WebsocketSellerUtil;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
+import lombok.extern.log4j.Log4j2;
 import org.springframework.stereotype.Component;
 import org.springframework.util.ObjectUtils;
 import org.springframework.web.socket.handler.TextWebSocketHandler;
@@ -23,15 +24,14 @@ import javax.websocket.*;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 /**
  * websocket接口处理类
  */
 @Component
 @ServerEndpoint(value = "/seller/{seq}")
+@Log4j2
 public class SellerController extends TextWebSocketHandler {
 
 
@@ -73,10 +73,38 @@ public class SellerController extends TextWebSocketHandler {
 
 
     @OnMessage
-    public void onMessage( Session session, String message) {
+    public void onMessage(@PathParam(value = "seq") String seq,  Session session, String message) {
 //        RemoteEndpoint.Async asyncRemote = session.getAsyncRemote();
+        if ("".equals(seq)){
+            WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("device seq is required"));
+            return;
+        }
+        //获取设备
+        DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
+        Device device =  deviceMapper.getByDeviceId(seq);
+        if (ObjectUtils.isEmpty(device)){
+            WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("device not fount"));
+            return;
+        }
+        if (ObjectUtils.isEmpty(message)){
+            WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("data not fount"));
+            return;
+        }
 
-        System.out.println(message);
+
+        WsEvent wsEvent = JSON.parseObject(message, WsEvent.class);
+        String event = wsEvent.getEvent();
+        Object data = wsEvent.getData();
+
+        switch (event){
+            case "pin":
+                keepAlive(session,wsEvent.getData());
+                break;
+            case "getQrcode":
+                getQrcode(device, session, data);
+            default:
+                WebsocketSellerUtil.sendMessage(session,new WsEvent("__Error_Event__","unrecognized event"));
+        }
         //下发数据
 //        asyncRemote.sendText(message);
 
@@ -85,22 +113,36 @@ public class SellerController extends TextWebSocketHandler {
 
     }
 
-    /**
-     * 处理用户活连接异常
-     *
-     * @param session
-     * @param throwable
-     */
-    @OnError
-    public void onError(Session session, Throwable throwable) {
-        try {
-            session.close();
-        } catch (IOException e) {
-            e.printStackTrace();
+    //生成支付码
+    private void getQrcode(Device device, Session session, Object data) {
+        WineMapper wineMapper = SpringContextUtil.getBean(WineMapper.class);
+        QrcodeParam qrcodeParam =   (QrcodeParam) data;
+        int weight = qrcodeParam.getWeight();
+        int id = qrcodeParam.getId();
+        int cash = qrcodeParam.getCash();
+        int cell = qrcodeParam.getCell();
+
+        Wine wine = wineMapper.selectWineById(id);
+        if (ObjectUtils.isEmpty(wine)){
+            WebsocketSellerUtil.sendMessage(session,EventWsErrUtil.getWsErr(String.format("no such wine: %d", qrcodeParam.getId())));
+            return;
         }
-        throwable.printStackTrace();
+        int nowCash = weight * wine.getPrice();
+        if (nowCash != cash){
+            WebsocketSellerUtil.sendMessage(session,EventWsErrUtil.getWsErr(String.format("got a wrong cash[you: %d, real: %d]",cash,nowCash)));
+            return;
+        }
+
+
+
+
+    }
+
+    private void keepAlive(Session session, Object data) {
+        WebsocketSellerUtil.sendMessage(session,new WsEvent("advResult",data));
     }
 
+
     public void   infoOfDevice(Device device,Session session, DeviceMapper deviceMapper) {
 
         WineMapper wineMapper = SpringContextUtil.getBean(WineMapper.class);
@@ -125,19 +167,26 @@ public class SellerController extends TextWebSocketHandler {
         WebsocketSellerUtil.sendMessage(session,new WsEvent("runParamResult",paramsList));
 
         //生成二维码
-        String   url = vipCofing.getVipScanApi() + "&device="+device.getId()+"&callback=" +vipCofing.getVipScanApi();
-        System.out.println("url---->"+url);
+//        String codeBase = CodeImgUtil.generateQRCode(vipCofing.getVipScanApi() + "&device=" + device.getId() + "&callback=" + vipCofing.getVipScanApi(), 200, 200);
+//        WebsocketSellerUtil.sendMessage(session,new WsEvent("vipQrcodeResult",codeBase));
+        WebsocketSellerUtil.sendMessage(session,new WsEvent("initFinish",null));
+    }
 
+    /**
+     * 处理用户活连接异常
+     *
+     * @param session
+     * @param throwable
+     */
+    @OnError
+    public void onError(Session session, Throwable throwable) {
+        try {
+            session.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        throwable.printStackTrace();
     }
-    //url := utils.Format("%s?device=%s&callback=%s", utils.VipScanApi, device.Id, utils.VipCallback)
-    //	img, err := qrcode.Encode(url, qrcode.Medium, 512)
-    //	if err != nil {
-    //		utils.Logger.Println(err)
-    //		_ = conn.WriteJSON(utils.WsError("Can't Access to Vip Qrcode"))
-    //		return
-    //	}
-    //	_ = conn.WriteJSON(utils.WsEvent("vipQrcodeResult", img))
-    //
-    //	_ = conn.WriteJSON(utils.WsEvent("initFinish", nil))
+
 
 }

+ 1 - 1
src/main/resources/application-dev.yml

@@ -33,7 +33,7 @@ wx:
   api_cert_path: "./certs/apiclient_key.pem"
   api_v3_key: "3HangZhou2GuiZhou1LiQuanGuNiang0"
 vip:
-  scan_api: "http://192.168.1.10:3080/seller/vip-test"
+  scan_api: "http://127.0.0.1:3080/seller/vip-test"
   pay_api: "http://192.168.1.10:3080/seller/vip-pay"
   callback: "http://192.168.1.10:3080/seller/current_user"