手机短信验证,群发短信这些功能在项目中已经很常见,本人第一次写项目连接短信接口,分享一下经验。
首先就是DOM,在html中创建按钮或者是加载,触发js并进入后台。
html中代码:
短信内容:<input type="text" class="layui-btn layui-btn-primary magin" id="words" />
<button class="layui-btn layui-btn-primary magin" id="allSent" οnclick="MessageSend()"><i class="layui-icon blue"></i>短信群发</button>
js中代码:
//短信群发功能
function MessageSend(){
var ids="";
$("input[name=sels]:checked").each(function(){
var id=$(this).val();
ids += id+",";
});
layer.confirm('是否要发送短信?', {
btn: ['确认', '取消'],
// 按钮,
shade: [0.1, '#fff'],
area: ['290px', '180px'],
title: "发送提醒",
}, function() {
$.ajax({
type: "post",
url: basePath + "/message/getMobById",
data: {
id: ids,
},
async: true,
//crossDomain: true == !(document.all),
success: function(data) {
mob = eval("(" + data + ")")
sendMes(mob);
}
});
}, function() {
layer.msg('已取消操作', {
icon: 2,
time: 1000
});
});
}
function sendMes(mob){
var word=$("#words").val();
if(mob!=null&&mob!=""){
for(var i=0;i<mob.length;i++){
console.log(mob[i][0][0]);
console.log(mob[i][0][1]);
$.ajax({
type:"post",
url:basePath + "/message/send",
data:{
mobs:mob[i][0][0],
name:mob[i][0][1],
word:word,
},
async: true,
success: function(data){
console.log(data);
if(data == 'true') {
layer.msg('已成功发送,1秒后将自动刷新页面。', {
icon: 1,
time: 1000
});
setTimeout("window.location.reload();", 1000);
} else {
layer.msg('发送信息失败,该信息可能已经被删除,请稍后重试', {
icon: 2,
time: 1000
});
}
}
});
}
}
}
后台java代码:
Controller层:
@RequestMapping("getMobById")
public void getMobById(HttpServletResponse response,String id){
List list=this.messageSendService.getMobById(id);
String[] exclude={""};
JsonUtils.printJsonStringFromArrayObjectWithExclude(response, list, exclude);
}
@RequestMapping("send")
public void sendMes(HttpServletResponse response,String mobs,String name,String word) throws IOException{
System.out.println(mobs);
SendMsg.SendMes(mobs,name,word);
JsonUtils.printCommonObject(response, "true");
}
service层:
public List getMobById(String id){
String[] idn=id.split(",");
List mobList=new ArrayList();
for(int i=0;i<idn.length;i++){
String sql="select mTel,mName from MemberList where mId= "+Long.parseLong(idn[i])+"";
List mob=this.baseDao.exeSqlQuery(sql);
mobList.add(mob);
}
return mobList;
}
短信接口代码:(来源于第三方)
public class SendMsg {
public static void SendMes(String mobs,String name,String word) throws IOException{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://api.sms.cn/sms/");
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//在头文件中设置转码
NameValuePair[] data ={ new NameValuePair("ac", "send"),
new NameValuePair("uid","aaaaaa"),
newNameValuePair("pwd","dad60b11"),
newNameValuePair("mobile",mobs),
new NameValuePair("encode","utf8"),
new NameValuePair("template","421241"),
new NameValuePair("content","{\"name\":\""+name+"\",\"word\":\""+word+"\"}")};
post.setRequestBody(data);client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:"+statusCode);
for(Header h : headers){
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
System.out.println(result); //打印返回消息状态
post.releaseConnection();
}
本人使用的是云信使的第三方软件,免费短信只有15条,大家要是有别的第三方比较好的,也可以推荐。