package com.kmall.api.util;

import com.kmall.manager.manager.common.CommonPropertiesBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangyaqin
 * @version 1.0
 * 2018-10-16 11:27
 */
public class SendMsgUtil {
    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";
    /**
     * 发送短信消息
     * 方法说明
     * @Discription:扩展说明
     * @param phones
     * @param text
     * @return
     * @return String
     */
    @SuppressWarnings("deprecation")
    public static String sendMsg(String phones,String text) {
        System.out.println(text);
        String apikey = CommonPropertiesBuilder.instance().getYunpianSmsApiKey();
        String url = CommonPropertiesBuilder.instance().getYunpianSmsUrl();
        if(apikey != null && url != null){
            Map<String, String> params = new HashMap<String, String>();
            params.put("apikey", apikey);
            //手机号码，多个号码使用英文逗号进行分割
            params.put("mobile", phones);
            //将短信内容进行URLEncoder编码
            params.put("text", text);

            return post(url, params);
        }else{
            return "";
        }
    }

    /**
     * 随机生成6位随机验证码
     * 方法说明
     * @Discription:扩展说明
     * @return
     * @return String
     */
    public static String createRandomVcode(){
        //验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }

    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url       提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return 提交响应
     */

    public static String post(String url, Map < String, String > paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry < String, String > param: paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(),
                            param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList,
                        ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity, ENCODING);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return responseText;
    }

    /**
     * 测试
     * 方法说明
     * @Discription:扩展说明
     * @param args
     * @return void
     */
    public static void main(String[] args) {
//      System.out.println(SendMsgUtil.createRandomVcode());
//      System.out.println("&ecb=12".substring(1));
        System.out.println(sendMsg("13530612313", "【前海电商】手机验证码为：" + SendMsgUtil.createRandomVcode()));
    }
}
