第一步登入网站 zz短信平台
导入pom依赖
<dependency>
<groupId>com.zhenzikj</groupId>
<artifactId>zhenzisms</artifactId>
<version>2.0.2</version>
</dependency>
第二步导入工具类MessageUtil
package kj15.util;
import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class MessageUtil {
private static String apiUrl;
private static String appId;
private static String appSecret;
static {
InputStream resourceAsStream = MessageUtil.class.getClassLoader().getResourceAsStream("messageUtil.properties");
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
apiUrl = properties.getProperty("apiUrl");
appId = properties.getProperty("appId");
appSecret = properties.getProperty("appSecret");
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean sendMessage(String number,String templateId,String randomCode,String timeDesc){
if(apiUrl==null||appId==null||appSecret==null){
return false;
}
// String apiUrl="https://sms_developer.zhenzikj.com";
// String appId="111566";//在控制台的 我的应用
// String appSecret="6831ecde-9823-40de-8354-bf33c7afd819";//在控制台的 我的应用
ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
Map<String, Object> params = new HashMap<String, Object>();
params.put("number",number);//你要发送的手机号
params.put("templateId",templateId);//短信模板里的
String[] templateParams = new String[2];//模板
templateParams[0] = randomCode;
templateParams[1] = timeDesc;
params.put("templateParams", templateParams);
String result = null;
try {
result = client.send(params);
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println(result);
JSONObject jsonObject = JSONObject.parseObject(result);
if("0".equals(jsonObject.get("code").toString())){
return true;
}else {
return false;
}
}
}
配置messageUtil.properties
apiUrl=https://sms_developer.zhenzikj.com
appId=111233
appSecret=6831ecde-9823-40de-8354-17afd831119
第三步ajax 模块
$("#send").click(function () {
// $('#captcha').empty();
jigsaw.init(document.getElementById('captcha'), function () {
alert("验证成功");
$('#captcha').empty();
sendMsg($("#send"));
});
});