| package com.dy.pipIrrApp.captcha; | 
|   | 
| import com.dy.pipIrrApp.captcha.dto.CaptchaDomain; | 
| import com.dy.pipIrrGlobal.daoBa.BaCaptchaMapper; | 
| import com.dy.pipIrrGlobal.pojoBa.BaCaptcha; | 
| import com.google.code.kaptcha.Producer; | 
| import jakarta.annotation.Resource; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import javax.imageio.ImageIO; | 
| import java.io.ByteArrayOutputStream; | 
| import java.util.Base64; | 
| import java.util.UUID; | 
|   | 
| /** | 
|  * @author ZhuBaoMin | 
|  * @date 2024-09-11 13:46 | 
|  * @LastEditTime 2024-09-11 13:46 | 
|  * @Description | 
|  */ | 
|   | 
| @Slf4j | 
| @Service | 
| public class CaptchaSV { | 
|     @Autowired | 
|     private BaCaptchaMapper baCaptchaMapper; | 
|   | 
|     @Resource(name = "captchaProducer") | 
|     private Producer captchaProducer; | 
|   | 
|     @Resource(name = "captchaProducerMathOne") | 
|     private Producer captchaProducerMathOne; | 
|   | 
|     @Resource(name = "captchaProducerMathTwo") | 
|     private Producer captchaProducerMathTwo; | 
|   | 
|     private static final String TYPE_CHAR = "char"; | 
|     private static final String TYPE_MATH_ONE = "math"; | 
|     private static final String TYPE_MATH_TWO = "math2"; | 
|   | 
|     /** | 
|      * Kaptcha生成验证码实体 | 
|      * @param type,char - 字符(缺省) | math - 一位数算式 | math2 - 两位数算式 | 
|      * @return | 
|      */ | 
|     public CaptchaDomain createGoogleCaptcha(String type) { | 
|         // 定义验证码实体 | 
|         CaptchaDomain captchaDomain = new CaptchaDomain(); | 
|         // 一位数加减乘除 | 
|         if (TYPE_MATH_ONE.equals(type)) { | 
|             // 生成文本 | 
|             String producerText = captchaProducerMathOne.createText(); | 
|             // 设置验证码字符 | 
|             captchaDomain.setText(producerText.substring(0, producerText.indexOf("@"))); | 
|             // 设置验证码答案码 | 
|             captchaDomain.setCode(producerText.substring(producerText.indexOf("@") + 1)); | 
|             // 设置验证码图片 | 
|             captchaDomain.setImage(captchaProducerMathOne.createImage(captchaDomain.getText())); | 
|         } | 
|         // 两位数加减乘除 | 
|         else if (TYPE_MATH_TWO.equals(type)) { | 
|             String producerText = captchaProducerMathTwo.createText(); | 
|             captchaDomain.setText(producerText.substring(0, producerText.indexOf("@"))); | 
|             captchaDomain.setCode(producerText.substring(producerText.indexOf("@") + 1)); | 
|             captchaDomain.setImage(captchaProducerMathTwo.createImage(captchaDomain.getText())); | 
|         } | 
|         // 缺省情况:字符 | 
|         else { | 
|             captchaDomain.setText(captchaProducer.createText()); | 
|             captchaDomain.setCode(captchaDomain.getText()); | 
|             captchaDomain.setImage(captchaProducer.createImage(captchaDomain.getText())); | 
|         } | 
|         // 生成base64 | 
|         try { | 
|             // 定义字节数组输出流 | 
|             ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | 
|             // 将图像以 jpg 的形式,写到字节数组输出流中 | 
|             ImageIO.write(captchaDomain.getImage(), "jpg", outputStream); | 
|   | 
|             // 写入base64格式 | 
|             captchaDomain.setBase64("data:image/jpg;base64," + Base64.getEncoder().encodeToString(outputStream.toByteArray())); | 
|             // 写入唯一Token | 
|             captchaDomain.setToken(UUID.randomUUID().toString()); | 
|             // 返回结果 | 
|             return captchaDomain; | 
|         } catch (Exception e) { | 
|             System.out.println(e.getMessage()); | 
|             return null; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 添加图片验证记录 | 
|      * @param po | 
|      * @return | 
|      */ | 
|     public Long addCaptcha(BaCaptcha po) { | 
|         baCaptchaMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
| } |