package cn.sh.stc.sict.cloud.common.data.util;

import cn.hutool.core.util.StrUtil;
import cn.sh.stc.sict.cloud.common.core.constant.RedisCacheConstant;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Description 验证码校验工具类
 * @Author
 * @Date
 */
public class ValidateCodeUtil {

    /**
     *  从redis取出验证码并验证
     */
    public static boolean validateCode(RedisTemplate redisTemplate, String code, String phone) {
        String key = RedisCacheConstant.SICT_PHONE_CODE_KEY + phone;
        if (!redisTemplate.hasKey(key)) {
            return false;
        }
        Object codeObj = redisTemplate.opsForValue().get(key);

        if (codeObj == null) {
            return false;
        }
        String savedCode = codeObj.toString();
        if (StrUtil.isBlank(savedCode)) {
            redisTemplate.delete(key);
            return false;
        }
        if (!StrUtil.equals(savedCode, code)) {
            return false;
        }
        return true;
    }
}
