在项目中使用短信接口

手机短信验证,群发短信这些功能在项目中已经很常见,本人第一次写项目连接短信接口,分享一下经验。

首先就是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">&#xe63b;</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条,大家要是有别的第三方比较好的,也可以推荐。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值