package com.mjlogin.util.sms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;
import com.taurus.core.util.Utils;


/**
 * 短信验证    
 */
public class SMSVerification {
	
	private static Logger		logger				= Logger.getLogger(SMSVerification.class);

	/**短信API产品域名*/
	final static String domain = "dysmsapi.aliyuncs.com";
	/**你的accessKeyId*/
	final static String accessKeyId = "LTAI4FmrV5FnBy4EeRrk8oRh";
	/**你的accessKeySecret*/
	final static String accessKeySecret = "wdsbFdYXARh8riHg51KNw1KLjVzKmR";
	/**短信版本*/
	final static String version="2017-05-25";
	final static String regionId = "cn_beijing";
	/**action SendSms*/
	final static String action = "SendSms";
	/**短信模板*/
	final static String templateCode = "SMS_185575736";
	/**短信签名*/
	final static String signName = "找团长";
	
	public static String hidePhone(String phone) {
		String phone_4 = phone.substring(3, 7);
		phone = phone.replace(phone_4, "****");
		return phone;
	}
	
	/**
	 * 获取一个指定长度的N位随机数
	 */
	public static int getFixLenthString(int length) {
		int tempInt = Utils.rand.nextInt((int) Math.pow(10, length));
		if (tempInt < 100000) {
			return getFixLenthString(length);
		}
		return tempInt;
	}
	
	// 生成短信验证码
	private static String verificationCode() {
		return getFixLenthString(6)+"";
	}

	public static void main(String[] args) {
		// send("13267171517");template_id parameter_2
		System.out.println(isValidPhone("18610826573"));
	}

	public static String send(String phoneNumber) {
		DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        String code = verificationCode();
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            if(StringUtil.isNotEmpty(data)) {
            	ITObject tem = TObject.newFromJsonData(data);
            	String str_code = tem.getUtfString("Code");
            	if(str_code.equals("OK")) {
            		return code;
            	}
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
		return null;
	}

	

	/**
	 *  是否有效手机号码
	 */
	public static boolean isValidPhone(String phoneNumber) {
		boolean isValidPhone = false;
		String regex = "^((13[0-9])|(14[5|7|9])|(15([0-3]|[5-9]))|(17[01235678])|(18[0-9])|(19[189]))\\d{8}$";
		if (phoneNumber.length() == 11) {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phoneNumber);
			isValidPhone = m.matches();
		}
		return isValidPhone;
	}

}
