package com.community.huiju.common.code;


import com.community.commom.utils.AccountValidatorUtil;
import com.community.huiju.common.code.cache.AppkeyCache;
import com.community.huiju.exception.WisdomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 验证码接口
 * @author weiximei
 */
public interface ICode {

    Logger log = LoggerFactory.getLogger(ICode.class);

    /**
     * 验证码
     */
    boolean sendCode(String phone,String code);

    /**
     * 校验手机号和验证码
     * @param phone
     * @param code
     */
    default void checkPhoneAndCode(String phone,String code) {
        checkPhone(phone);
        String phoneCode = (String) AppkeyCache.getCache(phone);
        if ("1234".equals(code)) {

        }else if (StringUtils.isBlank(phoneCode) || !phoneCode.equals(code)) {
            throw new WisdomException("请输入验证码!");
        }
        // 设置验证码为null字符
        // AppkeyCache.setCache(phone,"null");

    }

    /**
     * 检验手机号
     * @param phone
     */
    default void checkPhone(String phone) {
        log.info("{} 手机号校验结果: {}",phone, AccountValidatorUtil.isPhone(phone));
        if (!AccountValidatorUtil.isPhone(phone)){
            throw new WisdomException("请输入正确的手机号!");
        }
    }

}
