From 1e3eeec2d3a470d066d21900586b912dfef58c91 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期四, 17 四月 2025 15:07:41 +0800
Subject: [PATCH] 优化代码

---
 pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/captcha/CaptchaCtrl.java |  143 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/captcha/CaptchaCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/captcha/CaptchaCtrl.java
new file mode 100644
index 0000000..e11e3f3
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/captcha/CaptchaCtrl.java
@@ -0,0 +1,143 @@
+package com.dy.pipIrrApp.captcha;
+
+import com.dy.common.multiDataSource.DataSourceContext;
+import com.dy.common.webUtil.BaseResponse;
+import com.dy.common.webUtil.BaseResponseUtils;
+import com.dy.pipIrrApp.captcha.dto.CaptchaDomain;
+import com.dy.pipIrrGlobal.pojoBa.BaCaptcha;
+import jakarta.servlet.ServletOutputStream;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.imageio.ImageIO;
+import java.io.IOException;
+import java.util.Calendar;
+
+/**
+ * @author ZhuBaoMin
+ * @date 2024-09-11 13:45
+ * @LastEditTime 2024-09-11 13:45
+ * @Description
+ */
+
+@Slf4j
+@RestController
+@RequestMapping(path="captcha")
+public class CaptchaCtrl {
+    @Autowired
+    private CaptchaSV captchaSV;
+
+    /**
+     * 鑾峰彇楠岃瘉鐮�
+     * @param type锛屽浘鐗囩被鍨嬶細char-鏂囨湰锛宮ath-涓�浣嶆暟绠楀紡锛宮ath2-涓や綅鏁扮畻寮�
+     * @return
+     */
+    @GetMapping("/get")
+    @ResponseBody
+    public BaseResponse<CaptchaDomain> getCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false, defaultValue = "char") String type) {
+        // 閰嶇疆鏁版嵁婧�
+        HttpServletRequest httpRequest = (HttpServletRequest) request;
+        String wxDataSourceName = httpRequest.getHeader("tag");
+        if(wxDataSourceName != null && wxDataSourceName.trim().length() > 0){
+            log.info("寰俊寮�鍙戯紝璁剧疆鏁版嵁婧愬悕绉颁负:" + wxDataSourceName);
+            //鎶婄粍缁囧崟浣嶆爣绛句綔涓烘暟鎹簮鍚嶇О
+            DataSourceContext.set(wxDataSourceName);
+        } else {
+            log.info("鐢ㄦ埛鏈�夋嫨鏁版嵁婧�");
+        }
+
+        // 鐢熸垚楠岃瘉鐮佸疄浣�
+        CaptchaDomain captchaDomain = captchaSV.createGoogleCaptcha(type);
+        if (null != captchaDomain) {
+
+            // 楠岃瘉鐮佷繚瀛樿嚦redis
+            // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L);
+
+            // 楠岃瘉鐮佷繚瀛樿嚦session
+            //HttpSession session = (HttpSession) request.getSession();
+            //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode());
+
+            /**
+             * 楠岃瘉淇濆瓨鍒版暟鎹簱
+             * 鑾峰彇褰撳墠鏃堕棿鎴冲苟寤跺悗3鍒嗛挓
+             */
+            Long timestamp = System.currentTimeMillis();
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTimeInMillis(timestamp);
+            calendar.add(Calendar.SECOND, 180);
+
+            BaCaptcha baCaptcha = new BaCaptcha();
+            baCaptcha.setToken(captchaDomain.getToken());
+            baCaptcha.setCode(captchaDomain.getCode());
+            baCaptcha.setExpiration(calendar.getTimeInMillis());
+            Long rec = captchaSV.addCaptcha(baCaptcha);
+            //System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode());
+
+            // 鏃犵敤淇℃伅璁剧┖
+            captchaDomain.setText(null);
+            captchaDomain.setCode(null);
+            // 杩斿洖鍓嶇淇℃伅
+            return BaseResponseUtils.buildSuccess(captchaDomain);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * 鐩存帴杈撳嚭鍥剧墖
+     * @param type锛屽浘鐗囩被鍨嬶細char-鏂囨湰锛宮ath-涓�浣嶆暟绠楀紡锛宮ath2-涓や綅鏁扮畻寮�
+     */
+    @GetMapping("/get/image")
+    public void getCaptchaImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "type", required = false, defaultValue = "char") String type) {
+        CaptchaDomain captchaDomain = null;
+
+        // 鐢熸垚璋锋瓕楠岃瘉鐮佸疄浣�
+        captchaDomain = captchaSV.createGoogleCaptcha(type);
+
+        // 楠岃瘉鐮佷繚瀛樿嚦redis
+        // redisUtils.set(captchaDomain.getToken(), captchaDomain.getCode(), 300L);
+
+        // 楠岃瘉鐮佷繚瀛樿嚦session
+        //HttpSession session = (HttpSession) request.getSession();
+        //session.setAttribute(captchaDomain.getToken(), captchaDomain.getCode());
+
+        /**
+         * 楠岃瘉淇濆瓨鍒版暟鎹簱
+         * 鑾峰彇褰撳墠鏃堕棿鎴冲苟寤跺悗3鍒嗛挓
+         */
+        Long timestamp = System.currentTimeMillis();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTimeInMillis(timestamp);
+        calendar.add(Calendar.SECOND, 180);
+
+        BaCaptcha baCaptcha = new BaCaptcha();
+        baCaptcha.setToken(captchaDomain.getToken());
+        baCaptcha.setCode(captchaDomain.getCode());
+        baCaptcha.setExpiration(calendar.getTimeInMillis());
+        Long rec = captchaSV.addCaptcha(baCaptcha);
+        //System.out.println("token: " + captchaDomain.getToken() + "; code: " + captchaDomain.getCode());
+
+        // 浠ユ枃浠舵祦鐨勫舰寮忥紝杈撳嚭楠岃瘉鐮佸浘鐗�
+        ServletOutputStream out = null;
+        try {
+            response.setContentType("image/jpeg");
+            out = response.getOutputStream();
+            ImageIO.write(captchaDomain.getImage(), "jpg", out);
+            out.flush();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (out != null) {
+                    out.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+}

--
Gitblit v1.8.0