zainali5120
2020-09-25 286ea89f5f6cade73276f865effa5dcd2b19406d
src/main/java/com/xcong/excoin/utils/mail/SmsSend.java
@@ -1,9 +1,9 @@
package com.xcong.excoin.utils.mail;
import cn.hutool.core.date.DateUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
@@ -14,6 +14,8 @@
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
@@ -29,30 +31,68 @@
    public static final String TYPE_MD5 = "md5";
    public static final String TYPE_SHA1 = "sha1";
    private static final String APP_ID = "51258";
    private static final String APP_KEY = "a9b6e0758dc40d3c346887fc0e154642";
    private static final String SIGN_TYPE = "";
    /**
     * API 请求接口配置
     */
    private static final String URL = "https://api.mysubmail.com/message/xsend";
    /**
     * 发送验证码
     *
     * @param telphone 手机号
     * @param code     验证码
     * @param time     超时时间
     * @return
     */
    public static boolean sendVerifyCode(String telphone, String code, int time) {
        TreeMap<String, Object> requestData = new TreeMap<String, Object>();
        JSONObject vars = new JSONObject();
        String appid = "51258";
        String appkey = "a9b6e0758dc40d3c346887fc0e154642";
        String project = "E98Tx3";
        String signtype = "";
        String project = "2CHnV3";
        vars.put("code", code);
        vars.put("time", time);
        requestData.put("appid", appid);
        requestData.put("appid", APP_ID);
        requestData.put("project", project);
        requestData.put("to", telphone);
        if (!vars.isEmpty()) {
            requestData.put("vars", vars.toString());
        }
        return requestSend(requestData);
    }
    /**
     * 发送充值成功消息
     *
     * @param phone   手机号
     * @param time    充值时间
     * @param amount  金额
     * @param orderNo 订单号
     */
    public static void sendRechargeMsg(String phone, String time, String amount, String orderNo) {
        TreeMap<String, Object> requestData = new TreeMap<String, Object>();
        JSONObject vars = new JSONObject();
        vars.put("time", time);
        vars.put("price", amount);
        vars.put("orderNo", orderNo);
        String project = "Cqky91";
        requestData.put("appid", APP_ID);
        requestData.put("project", project);
        requestData.put("to", phone);
        if (!vars.isEmpty()) {
            requestData.put("vars", vars.toString());
        }
        requestSend(requestData);
    }
    private static boolean requestSend(TreeMap<String, Object> requestData) {
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        @SuppressWarnings("deprecation")
        ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
@@ -63,17 +103,17 @@
                builder.addTextBody(key, String.valueOf(value), contentType);
            }
        }
        if (signtype.equals(TYPE_MD5) || signtype.equals(TYPE_SHA1)) {
        if (SIGN_TYPE.equals(TYPE_MD5) || SIGN_TYPE.equals(TYPE_SHA1)) {
            String timestamp = getTimestamp();
            requestData.put("timestamp", timestamp);
            requestData.put("sign_type", signtype);
            String signStr = appid + appkey + RequestEncoder.formatRequest(requestData) + appid + appkey;
            requestData.put("sign_type", SIGN_TYPE);
            String signStr = APP_ID + APP_KEY + RequestEncoder.formatRequest(requestData) + APP_ID + APP_KEY;
            builder.addTextBody("timestamp", timestamp);
            builder.addTextBody("sign_type", signtype);
            builder.addTextBody("signature", RequestEncoder.encode(signtype, signStr), contentType);
            builder.addTextBody("sign_type", SIGN_TYPE);
            builder.addTextBody("signature", RequestEncoder.encode(SIGN_TYPE, signStr), contentType);
        } else {
            builder.addTextBody("signature", appkey, contentType);
            builder.addTextBody("signature", APP_KEY, contentType);
        }
        HttpPost httpPost = new HttpPost(URL);
@@ -98,8 +138,6 @@
    /**
     * 获取时间戳
     *
     * @return
     */
    private static String getTimestamp() {
        CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
@@ -121,6 +159,6 @@
    }
    public static void main(String[] args) {
        sendVerifyCode("15773002834", "123456", 2);
//        sendVerifyCode("15773002834", "123456", 2);
    }
}