package com.codvision.auth.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.IdUtil;
import com.codvision.auth.entity.vo.CustomCaptchaVO;
import com.codvision.auth.service.ICustomCaptchaService;
import com.codvision.cachecore.utils.RedisUtil;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

import static com.codvision.auth.support.CacheContants.CUSTOM_CAPTCHA_CODE;


/**
 * @author hxl
 */
@Service
public class CustomCaptchaServiceImpl implements ICustomCaptchaService {

    @Override
    public CustomCaptchaVO getPicCaptcha() {
        String reqNo = IdUtil.fastSimpleUUID();
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(100, 38, 4, 8);
        // 将请求号作为key，验证码的值作为value放到redis用于校验，5分钟有效期
        RedisUtil.set(CUSTOM_CAPTCHA_CODE + reqNo, circleCaptcha.getCode(), 5, TimeUnit.MINUTES);
        return CustomCaptchaVO.builder().picBase64(circleCaptcha.getImageBase64Data()).reqNo(reqNo).build();
    }
}
