CaptchaController.java 2.21 KB
package com.order.erp.controller;

import com.order.erp.common.annotation.AnonymousAccess;
import com.order.erp.common.constant.ServerResult;
import com.order.erp.common.utils.ImgCaptchaUtils;
import com.order.erp.common.utils.SmsUtils;
import com.order.erp.domain.vo.CaptchaMessageVO;
import com.wf.captcha.ArithmeticCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Member)验证码
 *
 * @author makejava
 * @since 2023-08-22 17:02:39
 */
@RestController
@Api(tags = "验证码")
@RequestMapping("/order/erp/captcha")
public class CaptchaController {

    @Resource
    private SmsUtils smsUtils;

    @Resource
    private ImgCaptchaUtils imgCaptchaUtils;

    /**
     * 获取验证码
     *
     * @param msgVo 查询条件
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("获取验证码")
    @PostMapping("/send_captcha_code")
    public ServerResult sendCaptchaCode(@RequestBody @Validated CaptchaMessageVO msgVo) {
        return smsUtils.sendCaptchaCode(msgVo);
    }

    /**
     * 获取图片验证码
     *
     * @return 查询结果
     */
    @AnonymousAccess
    @ApiOperation("获取图片验证码")
    @PostMapping("/get_img_captcha_code")
    public ServerResult getImgCaptchaCode() {
        // 算术类型 https://gitee.com/whvse/EasyCaptcha
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(111, 36);
        // 几位数运算,默认是两位
        captcha.setLen(2);
        // 获取运算的结果
        String result = captcha.text();
        String uuid = imgCaptchaUtils.setImgCaptcha4uuid(result);
        // 验证码信息
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {{
            put("img", captcha.toBase64());
            put("uuid", uuid);
        }};
        return ServerResult.success(imgResult);
    }
}