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.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HTTP; 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; /** * @author wzy * @date 2020-06-17 **/ public class SmsSend { /** * 时间戳接口配置 */ public static final String TIMESTAMP = "https://api.mysubmail.com/service/timestamp"; 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 requestData = new TreeMap(); JSONObject vars = new JSONObject(); String project = "2CHnV3"; vars.put("code", code); vars.put("time", time); 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 requestData = new TreeMap(); 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 requestData) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); @SuppressWarnings("deprecation") ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8); for (Map.Entry entry : requestData.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (value instanceof String) { builder.addTextBody(key, String.valueOf(value), contentType); } } if (SIGN_TYPE.equals(TYPE_MD5) || SIGN_TYPE.equals(TYPE_SHA1)) { String timestamp = getTimestamp(); requestData.put("timestamp", timestamp); 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", SIGN_TYPE); builder.addTextBody("signature", RequestEncoder.encode(SIGN_TYPE, signStr), contentType); } else { builder.addTextBody("signature", APP_KEY, contentType); } HttpPost httpPost = new HttpPost(URL); httpPost.addHeader("charset", "UTF-8"); httpPost.setEntity(builder.build()); try { CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpResponse response = closeableHttpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); System.out.println(jsonStr); if ("success".equals(JSONObject.fromObject(jsonStr).getString("status"))) { return true; } } } catch (IOException e) { e.printStackTrace(); } return false; } /** * 获取时间戳 */ private static String getTimestamp() { CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpGet httpget = new HttpGet(TIMESTAMP); try { HttpResponse response = closeableHttpClient.execute(httpget); HttpEntity httpEntity = response.getEntity(); String jsonStr = EntityUtils.toString(httpEntity, "UTF-8"); if (jsonStr != null) { JSONObject json = JSONObject.fromObject(jsonStr); return json.getString("timestamp"); } closeableHttpClient.close(); } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { // sendVerifyCode("15773002834", "123456", 2); } }