前言
聚合数据
,是一家综合性API数据流通服务商(integrated API-enabled data exchange service provider),提供基于API技术的标准化数据服务和定制化数字化解决方案
准备工作
- 聚合数据平台注册一个账号,准备好聚合账号的
API key
- 聚合数据平台创建短信模板,代码中提到的
SMS_CODE_TPID
就是这个模板的ID
- 代码使用的基础型Java Spring Boot项目,引入了第三方的
lombok
、fastjson
依赖
申请短信服务流程
注册并实名账号信息
这里一定要注意!!!想开通短信API服务,必须是企业认证,个人认证是无法开通短信服务的!!!!
购买短信API服务套餐
创建短信模板
聚合数据的短信模板分 固定模板、自定义模板
固定模板,内容可以选择日常最常用的短信内容
自定义模板,内容根据自己需求进行描述,前提是内容必须不违规
- 固定模板类型
自定义模板类型
等待审核
在国内,短信内容是受国家管控的,所以平台需要对短信内容进行审核。等平台审核完成,才能正常使用
记住创建的模板ID,就是后面代码中要用的 SMS_CODE_TPID
JAVA代码
代码已经分好层,换上自己的key和模板id,可以直接拿来用
工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map;
@Slf4j @Service public class JuHeUtils {
public static Map send(String phone, String smsCode, String tplId) { Map resultMap = new HashMap(); Map params = new HashMap(); params.put("mobile", phone); params.put("tpl_value", "#code#=" + smsCode); if (tplId != null && !"".equals(tplId)) { params.put("tpl_id", tplId); } params.put("key", JuHeConstants.SMA_PORT_KEY); try { String msg = net(JuHeConstants.SMA_CALL_URL, params, "GET"); JSONObject object = JSONObject.parseObject(msg); if (object.getIntValue("error_code") == 0) { log.info("短信发送结果:" + object.get("result")); resultMap.put("codeMsg", 0); resultMap.put("msg", "短信发送成功"); } else { resultMap.put("codeMsg", -1); resultMap.put("msg", "短信发送失败"); log.info("短信发送结果:" + object.get("error_code") + ":" + object.get("reason")); } } catch (Exception e) { resultMap.put("codeMsg", -1); resultMap.put("msg", "短信发送失败"); e.printStackTrace(); } return resultMap; }
public static String net(String strUrl, Map params, String method) throws Exception { HttpURLConnection conn = null; BufferedReader reader = null; String rs = null; try { StringBuffer sb = new StringBuffer(); if (method == null || method.equals("GET")) { strUrl = strUrl + "?" + urlencode(params); } URL url = new URL(strUrl); conn = (HttpURLConnection) url.openConnection(); if (method == null || method.equals("GET")) { conn.setRequestMethod("GET"); } else { conn.setRequestMethod("POST"); conn.setDoOutput(true); } conn.setRequestProperty("User-agent", JuHeConstants.USER_AGENT); conn.setUseCaches(false); conn.setConnectTimeout(JuHeConstants.DEF_CONN_TIMEOUT); conn.setReadTimeout(JuHeConstants.DEF_READ_TIMEOUT); conn.setInstanceFollowRedirects(false); conn.connect(); if (params != null && method.equals("POST")) { try { DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(urlencode(params)); } catch (Exception e) { e.printStackTrace(); } } InputStream is = conn.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, JuHeConstants.DEF_CHATSET)); String strRead = null; while ((strRead = reader.readLine()) != null) { sb.append(strRead); } rs = sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { reader.close(); } if (conn != null) { conn.disconnect(); } } return rs; }
public static String urlencode(Map<String, String> data) { StringBuilder sb = new StringBuilder(); for (Map.Entry i : data.entrySet()) { try { sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return sb.toString(); } }
|
实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import lombok.Data; import java.io.Serializable; import java.util.Date;
@Data public class SmsCode implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String codeType;
private String codeTypeName;
private Integer status;
private String mobilePhone;
private String code;
private String content;
private Date createTime; }
|
常量类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class JuHeConstants {
public static final String SMA_CALL_URL = "http://v.juhe.cn/sms/send";
public static final String SMA_PORT_KEY = "d4544a800c99ee467758aexxxxxxxxxx";
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"; }
|
业务逻辑测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.util.Map;
public class DemoMain {
public static Boolean sendSms(SmsCode smsCode) { String SMS_CODE_TPID = "172410"; Map resultMap = JuHeUtils.send(smsCode.getMobilePhone(), smsCode.getCode(), SMS_CODE_TPID); if (resultMap.containsKey("codeMsg")) { if (resultMap.get("codeMsg").equals(0)) { smsCode.setStatus(0); return true; } } return false; } public static void main(String[] args) { SmsCode smsCode = new SmsCode(); Boolean flag = DemoMain.sendSms(smsCode); System.out.println(flag); } }
|