package com.fjs.scenic.utils;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 阿里短信推送
 *
 * @date 2017-12-31
 * @author
 * @version 3.0
 */
public class MessageByAli {

    private final static Log logger = LogFactory.getLog(MessageByAli.class);

    /** 短信API产品名称（短信产品名固定，无需修改） */
    private final static String product = "Dysmsapi";
    /** 短信API产品域名（接口地址固定，无需修改 */
    private final static String  domain = "dysmsapi.aliyuncs.com";

    /** 接口密钥 */
    public static String accessKeyId;
    public static String accessKeySecret;
    /** 短信签名 */
    public static String singName;
    /** 短信消息模板ID */
    public static String   captcha;
    /** 接入点 */
    private final static String    endpointName = "cn-hangzhou";
    private final static String        regionId = "cn-hangzhou";
    private final static String         timeOut = "10000";

    /**
     * 阿里短信服务发送短信接口
     *
     * @param phoneNumbers  接收短信手机号【多个用,分割】
     * @param templateCode  短信模板
     * @param templateParam 短信参数
     */
    public static boolean sendMsg(String phoneNumbers, String templateCode, String templateParam)  {
        // 设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", timeOut);
        System.setProperty("sun.net.client.defaultReadTimeout", timeOut);

        // 初始化ascClient,暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile(regionId,
                accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(endpointName, regionId, product,
                    domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 使用post提交
        request.setMethod(MethodType.POST);
        // 必填:待发送手机号
        request.setPhoneNumbers(phoneNumbers);
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName(singName);
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam(templateParam);
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if (sendSmsResponse.getCode() != null
                && sendSmsResponse.getCode().equals("OK")) {
            return true;
        }else{
            logger.info("短信发送失败，失败原因："+sendSmsResponse.getMessage()+"短信模版:"+templateCode+"，发送手机号："+phoneNumbers);
            return false;
        }
    }
    /**
     * 阿里短信服务发送短信接口
     *
     * @param phoneNumbers 接收短信手机号【多个用,分割】
     * @param templateCode 短信模板
     * @param paramMap     短信参数集合
     */
    public static boolean sendMsg(String phoneNumbers, String templateCode, Map<String, Object> paramMap) {
        // 设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", timeOut);
        System.setProperty("sun.net.client.defaultReadTimeout", timeOut);

        // 初始化ascClient,暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile(regionId,
                accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(endpointName, regionId, product,
                    domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 使用post提交
        request.setMethod(MethodType.POST);
        // 必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
        request.setPhoneNumbers(phoneNumbers);
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName(singName);
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        String templateParam = JSON.toJSONString(paramMap);
        request.setTemplateParam(templateParam);
        SendSmsResponse sendSmsResponse = new SendSmsResponse();
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        if (sendSmsResponse.getCode() != null
                && sendSmsResponse.getCode().equals("OK")) {
            return true;
        }else{
            logger.info("短信发送失败，失败原因："+sendSmsResponse.getMessage()+"，短信模版:"+templateCode+"，发送手机号："+phoneNumbers);
            return false;
        }
    }
}
