package com.rndchina.aisin.util.sms;

import java.text.SimpleDateFormat;
import java.util.TreeMap;

import open189.sign.ParamsSign;

import com.rndchina.aisin.util.Util;

import emp.tool.json.JSONObject;

/**
 * <code>SendSms.java<code/>
 * <p>
 * 功能：发送短信验证码(电信短信通道)
 * <p>
 * Copyright 神州锐达 2014 All right reserved.
 * 
 * @Author: Brain.Hou,2014年3月11日下午3:56:15
 */
public class SendSmsUtil {

	private static String app_id = "168541270000034849";
	private static String app_secret = "d2610ec2322fc205c4a6d7669617c628";
	private static String access_token = "4a963f075afae4fce634e6b9bf059ba81397182603901";
	private static String postUrl = "http://api.189.cn/v2/dm/randcode/sendSms";

	/**
	 * 给指定号码发送验证码短信
	 * 
	 * @param token
	 * @param userPhone
	 * @return
	 */
	public static String sendSms(String userPhone) {
		String result = "";
		try {
			// 获取信任码
			// 拼时间字符串
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String timestamp = Util.getCurrDate(dateFormat);
			// 创建参数集合
			TreeMap<String, String> paramsMap = new TreeMap<String, String>();
			paramsMap.put("app_id", app_id);
			paramsMap.put("access_token", access_token);
			paramsMap.put("timestamp", timestamp);
			// 访问地址（get方式）
			String getUrl = "http://api.189.cn/v2/dm/randcode/token?app_id="
					+ app_id + "&access_token=" + access_token + "&timestamp="
					+ timestamp + "&sign="
					+ ParamsSign.value(paramsMap, app_secret);
			// 发送get请求获取响应
			String resJson = HttpInvoker.httpGet(getUrl);
			// 打印获取的信任码
			System.out.println(resJson);
			JSONObject json = new JSONObject(resJson);
			String rescode = json.get("res_code").toString();
			String token = "";
			if ("0".equals(rescode)) {
				token = json.get("token").toString();

				/* ------发送验证短信------ */
				// 拼接参数
				TreeMap<String, String> paramsMap1 = new TreeMap<String, String>();
				// 生成6位随机数
				String randcode = Util.getRandom();
				paramsMap1.put("app_id", app_id);
				paramsMap1.put("access_token", access_token);
				paramsMap1.put("token", token);
				paramsMap1.put("phone", userPhone);
				paramsMap1.put("randcode", randcode);
				paramsMap1.put("exp_time", "2");
				paramsMap1.put("timestamp", timestamp);

				// 拼接参数签名
				String postEntity = "app_id=" + app_id + "&access_token="
						+ access_token + "&token=" + token + "&phone="
						+ userPhone + "&randcode=" + randcode + "&timestamp="
						+ timestamp + "&exp_time=2" + "&sign="
						+ ParamsSign.value(paramsMap1, app_secret);

				String resJson1 = HttpInvoker.httpPost(postUrl, null,
						postEntity);
				net.sf.json.JSONObject jo = net.sf.json.JSONObject
						.fromObject(resJson1);
				int res_code = Integer.parseInt(jo.get("res_code").toString());
				switch (res_code) {
				case 0:// 成功
					result = randcode;
					break;
				case 1:// 未知错误
					result = "1";
					break;
				case 2:// 服务暂不可用
					result = "2";
					break;
				case 3:// 未知的方法
					result = "3";
					break;
				case 4:// 接口调用次数已达到设定的上限
					result = "4";
					break;
				case 5:// 请求来自未经授权的IP地址
					result = "5";
					break;
				default:
					break;
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
