¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.captcha; |
| | | |
| | | import cn.hutool.captcha.generator.CodeGenerator; |
| | | import cn.hutool.core.math.Calculator; |
| | | import cn.hutool.core.util.CharUtil; |
| | | import cn.hutool.core.util.RandomUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | |
| | | /** |
| | | * æ 符å·è®¡ç®çæå¨ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class UnsignedMathGenerator implements CodeGenerator { |
| | | |
| | | private static final long serialVersionUID = -5514819971774091076L; |
| | | |
| | | private static final String operators = "+-*"; |
| | | |
| | | /** |
| | | * åä¸è®¡ç®æ°åæå¤§é¿åº¦ |
| | | */ |
| | | private final int numberLength; |
| | | |
| | | /** |
| | | * æé |
| | | */ |
| | | public UnsignedMathGenerator() { |
| | | this(2); |
| | | } |
| | | |
| | | /** |
| | | * æé |
| | | * |
| | | * @param numberLength åä¸è®¡ç®æå¤§æ°å使° |
| | | */ |
| | | public UnsignedMathGenerator(int numberLength) { |
| | | this.numberLength = numberLength; |
| | | } |
| | | |
| | | @Override |
| | | public String generate() { |
| | | final int limit = getLimit(); |
| | | int min = RandomUtil.randomInt(limit); |
| | | int max = RandomUtil.randomInt(min, limit); |
| | | String number1 = Integer.toString(max); |
| | | String number2 = Integer.toString(min); |
| | | number1 = StrUtil.padAfter(number1, this.numberLength, CharUtil.SPACE); |
| | | number2 = StrUtil.padAfter(number2, this.numberLength, CharUtil.SPACE); |
| | | |
| | | return number1 + RandomUtil.randomChar(operators) + number2 + '='; |
| | | } |
| | | |
| | | @Override |
| | | public boolean verify(String code, String userInputCode) { |
| | | int result; |
| | | try { |
| | | result = Integer.parseInt(userInputCode); |
| | | } catch (NumberFormatException e) { |
| | | // ç¨æ·è¾å
¥éæ°å |
| | | return false; |
| | | } |
| | | |
| | | final int calculateResult = (int) Calculator.conversion(code); |
| | | return result == calculateResult; |
| | | } |
| | | |
| | | /** |
| | | * è·åéªè¯ç é¿åº¦ |
| | | * |
| | | * @return éªè¯ç é¿åº¦ |
| | | */ |
| | | public int getLength() { |
| | | return this.numberLength * 2 + 2; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®é¿åº¦è·ååä¸è®¡ç®æ°åæå¤§å¼ |
| | | * |
| | | * @return æå¤§å¼ |
| | | */ |
| | | private int getLimit() { |
| | | return Integer.parseInt("1" + StrUtil.repeat('0', this.numberLength)); |
| | | } |
| | | } |