package com.cirle.scientific.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.JsonObject;

/**
 * 阿里云短信发送工具类
 */
public class SmsUtils {

	private static final String accessKeyId = "LTAI5tHTG5eNAC7pmoqUDghi";
	private static final String accessKeySecret = "K8aZK7RWhqw4LdkP9MtwiC7Rq0avwt";

	/**
	 * 使用AK&SK初始化账号Client
	 * @return Client
	 * @throws Exception
	 */
	public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
		Config config = new Config()
				// 您的AccessKey ID
				.setAccessKeyId(accessKeyId)
				// 您的AccessKey Secret
				.setAccessKeySecret(accessKeySecret);
		// 访问的域名
		config.endpoint = "dysmsapi.aliyuncs.com";
		return new com.aliyun.dysmsapi20170525.Client(config);
	}

	/**
	 * 发送短信
	 *
	 * @param phoneNumber
	 * @param templateCode
	 * @param templateParams
	 * @return
	 */
	public static boolean send(String phoneNumber, String templateCode, JsonObject templateParams) {
		try {
			com.aliyun.dysmsapi20170525.Client client = SmsUtils.createClient();
			SendSmsRequest sendSmsRequest = new SendSmsRequest()
					.setPhoneNumbers(phoneNumber)
					.setTemplateCode(templateCode)
					.setSignName("全科界")
					.setTemplateParam(templateParams.toString());

			SendSmsResponse resp = client.sendSms(sendSmsRequest);
			if("OK".equals(resp.getBody().getCode())){
				return true;
			}else{
				return false;
			}
		}catch (Exception e){
			e.printStackTrace();
			return  false;
		}
	}


	public static void main(String[] args_) throws Exception {
		JsonObject templateParams = new JsonObject();
		templateParams.addProperty("code","123456");
		SmsUtils.send("13439984083", "SMS_219905183", templateParams);
	}
}
