package com.tourism.webadmin.back.service.impl;

import com.tourism.common.core.constant.ApplicationConstant;
import com.tourism.common.core.util.RsaUtil;
import com.tourism.webadmin.back.service.SmsService;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Slf4j
@Service("smsService")
public class SmsServiceImpl implements SmsService {

    /**
     * 校验短信验证码
     * @param smsCode
     * @return
     */
    @Override
    public Boolean validateSmsCode(String smsCode,String phone) {
        RedisClient client = RedisClient.create("redis://localhost");
        // 创建连接
        StatefulRedisConnection<String, String> connection = client.connect();
        // 创建命令对象
        RedisCommands<String, String> commands = connection.sync();
        // 通过键获取值
        String value = commands.get(phone);


        if (ObjectUtils.isEmpty(value) || !value.equals(smsCode)){
            return false;
        }
        // 关闭连接
        connection.close();
        client.shutdown();
        return true;
    }
}
