package com.library.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.google.gson.Gson;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 本类和MethodUtil差不多，但本类是MethodUtil的固定方法形态
 * 
 * @author Administrator
 * 
 */
public class MSMmethodUtil {
	public static String appkey = "23767269";
	public static String secret = "e58760ff89aa0d55344ce7eff1275506";

	public static void main(String[] args) {
//		 SendSMS("13826834070", "你是傻逼", "乐享电竞");
		SendSMSCode("15602901970",Code(), "乐享电竞");
	}

	/**
	 * 短信内容：你好，我们有一个通知给你，内容如下：${content}，本信息来自${from}
	 */
	public static void SendSMS(String ToPhone, String Content, String FromName) {
		String url = "http://gw.api.taobao.com/router/rest";

		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		// req.setExtend(Code);
		req.setSmsType("normal");
		req.setSmsFreeSignName("乐享电竞");
		req.setRecNum(ToPhone);
		req.setSmsTemplateCode("SMS_4270001");
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		map.put("content", Content);
		map.put("from", FromName);
		req.setSmsParam(gson.toJson(map, Map.class));// "{\"code\":\"1234\",\"product\":\"三维软件工作室\"}"
		AlibabaAliqinFcSmsNumSendResponse response;
		try {
			response = client.execute(req);
			System.out.println(response.getBody());
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 发送短信验证码 接收的电话号码，验证码，被注册的名称 短信内容：验证码${code}，您正在注册成为${product}用户，感谢您的支持！
	 */
	public static void SendSMSCode(String ToPhone, String Code, String FromName) {
		String url = "http://gw.api.taobao.com/router/rest";
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		// req.setExtend(Code);
		req.setSmsType("normal");
		req.setSmsFreeSignName("注册验证");
		req.setRecNum(ToPhone);
		req.setSmsTemplateCode("SMS_63010252");
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		map.put("code", Code);
		map.put("product", FromName);
		req.setSmsParam(gson.toJson(map, Map.class));// "{\"code\":\"1234\",\"product\":\"三维软件工作室\"}"
		AlibabaAliqinFcSmsNumSendResponse response;
		try {
			response = client.execute(req);
			System.out.println(response.getBody());
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * 生成随机的6位数验证码
	 */
	public static String Code() {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < 6; i++) {
			result += random.nextInt(10);
		}
		System.out.print(result);
		return result;
	}
}
