Browse Source

扫码取车

guoshuai 1 day ago
parent
commit
72b27772a3

+ 14 - 6
src/main/java/com/qmrb/system/controller/CaSignController.java

@@ -2451,7 +2451,7 @@ public class CaSignController {
      * @return
      */
     @GetMapping("fetchVehicleQrcode")
-    public Result<?> fetchVehicleQrcode(){
+    public ResponseEntity<?> fetchVehicleQrcode(){
         if(SecurityUtils.getUserId() == null){
             return null;
         }
@@ -2461,13 +2461,21 @@ public class CaSignController {
         try {
             String key = "fetchVehicle";
             String redisKey = "pickUp" + RandomUtil.randomString(10);
-            String base64 = iUserService.getCouponQRCode(toolWxConfigService.findConf(),key,redisKey);
+            // String base64 = iUserService.getCouponQRCodePng(toolWxConfigService.findConf(),key,redisKey);
+            // //保存当前时间戳
+            // redisTemplate.opsForValue().set(redisKey,System.currentTimeMillis(),fetchVehicleTime,TimeUnit.SECONDS);
+            //
+            // map.put("data",base64);
+            //
+            // return Result.success(map);
+
+            byte[] imageBytes = iUserService.getCouponQRCodePng(toolWxConfigService.findConf(),key,redisKey);
             //保存当前时间戳
             redisTemplate.opsForValue().set(redisKey,System.currentTimeMillis(),fetchVehicleTime,TimeUnit.SECONDS);
-            
-            map.put("data",base64);
-            
-            return Result.success(map);
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.IMAGE_JPEG);
+            headers.setContentLength(imageBytes.length);
+            return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
         }catch (Exception e){
             log.error(e.getMessage(),e);
         }

+ 2 - 0
src/main/java/com/qmrb/system/service/UserService.java

@@ -35,4 +35,6 @@ public interface UserService extends IService<Order> {
     public String getUserPhoneNumber(ToolWxConfig wxConfig, String code);
 
     String getCouponQRCode(ToolWxConfig conf, String key, String redisKey);
+
+    byte[] getCouponQRCodePng(ToolWxConfig conf, String key, String redisKey);
 }

+ 75 - 0
src/main/java/com/qmrb/system/service/impl/UserServiceImpl.java

@@ -209,6 +209,81 @@ public class UserServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         return result;
     }
 
+    @Override
+    public byte[] getCouponQRCodePng(ToolWxConfig conf, String key, String redisKey) {
+        String token =  getWxAccessToken(conf);
+        
+        Map<String, Object> resultObj = new HashMap();
+        PrintWriter out = null;
+        InputStream in = null;
+        String result = "";
+        try {
+            URL realUrl = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token);
+            // 打开和URL之间的连接
+            URLConnection conn = realUrl.openConnection();
+            // 设置通用的请求属性
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            // 发送POST请求必须设置如下两行
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            // 获取URLConnection对象对应的输出流
+            out = new PrintWriter(conn.getOutputStream());
+            // 发送请求参数
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("scene", key + "=" + redisKey); //参数自定义
+            jsonObject.put("page","pages/index/index");//要生成小程序码的链接
+            jsonObject.put("width",500);
+            jsonObject.put("env_version", conf.getEnvVersion());
+            out.print(jsonObject);
+            // flush输出流的缓冲
+            out.flush();
+            in = conn.getInputStream();
+            byte[] data = null;
+            // 读取图片字节数组
+            try {
+                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
+                byte[] buff = new byte[100];
+                int rc = 0;
+                while ((rc = in.read(buff, 0, 100)) > 0) {
+                    swapStream.write(buff, 0, rc);
+                }
+                data = swapStream.toByteArray();
+            } catch (IOException e) {
+                log.error("获取领券二维码出错",e);
+            } finally {
+                if (in != null) {
+                    try {
+                        in.close();
+                    } catch (IOException e) {
+                        log.error("获取领券二维码出错",e);
+                    }
+                }
+            }
+            return data;
+        } catch (Exception e) {
+            System.out.println("发送 POST 请求出现异常!" + e);
+            log.error("发送 POST 请求出现异常!",e);
+        }
+        // 使用finally块来关闭输出流、输入流
+        finally {
+            try {
+                if (out != null) {
+                    out.close();
+                }
+                if (in != null) {
+                    in.close();
+                }
+            } catch (IOException ex) {
+                log.error("关闭输出流、输入流!",ex);
+            }
+        }
+//            resultObj.put("result",result);
+        return null;
+    }
+
     public UserInfo getMyInfo(UserParam userParam){
         UserInfo userInfo = new UserInfo();
         userInfo.setId("1");