package cn.ysk.cashier.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取验证码相关工具
 * @author 12847
 */
@Component
public class ValidateCodeUtil {
    /**
     * 阿里云key
     */
    @Value("${aliyun.keyid}")
    private String ACCESSKEYID;
    /**
     * 阿里云secret
     */
    @Value("${aliyun.keysecret}")
    private String ACCESSKEYSECRET;
    /**
     * 十小时
     */
    protected static final long MILLIS_MINUTE =  10 *60 * 60 *1000;

    @Autowired
    private HttpServletRequest request;


    /**
     * 获取验证码（阿里云）
     */
    public SendSmsResponse requestValidateCodeAli(String phone, String checkCode,String templateCode) throws Exception {
        Client client = null;
        try {
            client = createClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 1.发送短信
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName("银收客")
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\":" +"'"+checkCode +"'"+"}")
                .setPhoneNumbers(phone);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            assert client != null;
            return client.sendSmsWithOptions(sendSmsRequest, runtime);
        } catch (Exception e) {
           throw new Exception(e.getMessage());
        }

    }
    /**
     * 发送短信(阿里云)
     *
     * @return
     * @throws Exception
     */
    public  Client createClient() throws Exception {
        Config config = new Config();
        config.accessKeyId = ACCESSKEYID;
        config.accessKeySecret = ACCESSKEYSECRET;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


}
