第三方短信接口服务商—动力思维乐信,赠送 100 条免费测试短信。
步骤一、到
http://www.lx598.com/ 注册一个账号,这个账号和密码后期接入过程中要用到。
步骤二、登录账户,完善企业信息,申请 sdk 使用。
步骤三、阅读“短信接口 api ”文档,和下载对于开发语言的的么,下面示例是用 java 开发语言实现的,就贴出来 java 的短信接口 demo (
http://www.lx598.com/javaCode.html )
下面是实现图片验证和发送短信的 js 代码:
//手机号格式验证
function isPhoneNum(str) {
var reg = /^0?(13[0-9]|15[012356789]|18[012356789]|14[57]|17[0-9]|199)[0-9]{8}$/;
return reg.test(str);
}
//判断图形验证码填写是否正确
var canCaptcha = false;
function getRegCode() {
if($.trim($('#inputCaptcha').val()) == ''){
$('#imgRs').html("图形验证码不能为空");
$('#inputCaptcha').select();
return;
}
if (!isPhoneNum($('#phoneRe').val())) {
document.getElementById('phoneReInfo').innerHTML = '<font color="red">请填写有效的 11 位手机号码</font>';
} else {
document.getElementById('phoneReInfo').innerHTML = '注册后用手机号码进行登录';
//判断手机号是否注册及发送验证码
$.ajax({
url : "${path}/account/checkMob",
type : "POST",
data : "account.ACCMOB=" + $('#phoneRe').val(),
contentType : "application/x-www-form-urlencoded;charset=utf-8",
async : false,
success : function(data) {
res = data;
if (data == 1) {
document.getElementById('phoneReInfo').innerHTML = '<font color="red">该手机号已被注册</font>';
refreshYzm();
} else {
document.getElementById('phoneReInfo').innerHTML = '<font color="green">该手机号可用</font>';
$.ajax({
url : "${path}/account/reAimcodeGetVeCode",
type : "POST",
data : "account.ACCMOB="
+ $('#phoneRe').val()
+ "&fromSource=4&smsCount="+$('#smsCount').val()
+"&searchName="+$.trim($('#inputCaptcha').val()),
contentType : "application/x-www-form-urlencoded;charset=utf-8",
async : false,
success : function(data) {
myArray = data.split("&");
if (myArray[0] == '发送成功!') {
canCaptcha = true;
document.getElementById('phoneReInfo').innerHTML = '<font color="green">验证码已发送,请注意查收!</font>';
accountFID = myArray[1];
}else if(data == '限制申请'){
document.getElementById('phoneReInfo').innerHTML = '<font color="red">一个手机号码一天最多只能申请 3 次!</font>';
refreshYzm();
}else if(data == '验证码错误'){
document.getElementById('phoneReInfo').innerHTML = '<font color="red">验证码错误!</font>';
refreshYzm();
}
},
error : function() {
alert('异常,内部验证出错!'+data);
}
});
}
},
error : function() {
alert('异常,验证出错!');
}
});
}
//验证码申请次数
var smsCount=parseInt($('#smsCount').val()); smsCount=smsCount<3?smsCount+1:3;
$('#smsCount').val(smsCount);
}
//重置申请次数
function resetCount(){
$('#smsCount').val(1);
}
//点击刷新图片验证码
function refreshYzm(){
$('#inputCaptcha').val('');
$('#validImg').attr('src','${path }/sys/loginCaptcha?tm='+Math.random());
}
//密码强度检测
function CharMode(iN) {
if (iN >= 48 && iN <= 57) //数字
return 1;
if (iN >= 65 && iN <= 90) //大写字母
return 2;
if (iN >= 97 && iN <= 122) //小写
return 4;
else
return 8; //特殊字符
}
//计算出当前密码当中一共有多少种模式
function bitTotal(num) {
modes = 0;
for ( var i = 0; i < 4; i++) {
if (num & 1)
modes++;
num >>>= 1;
}
return modes;
}
//checkStrong 函数
//返回密码的强度级别
function checkStrong(sPW) {
if (sPW.length <= 4)
return 0; //密码太短
Modes = 0;
for ( var i = 0; i < sPW.length; i++) {
//测试每一个字符的类别并统计一共有多少种模式
Modes |= CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}
//pwStrength 函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwStrength(pwd) {
checkPassLong(0);
var O_color = "#eeeeee";
var L_color = "#FF0000";
var M_color = "#FF9900";
var H_color = "#33CC00";
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
} else {
var S_level = checkStrong(pwd);
switch (S_level) {
case 0:
Lcolor = Mcolor = Hcolor = O_color;
case 1:
Lcolor = L_color;
Mcolor = Hcolor = O_color;
break;
case 2:
Lcolor = Mcolor = M_color;
Hcolor = O_color;
break;
default:
Lcolor = Mcolor = Hcolor = H_color;
}
}
document.getElementById("strength_L").style.background = Lcolor;
document.getElementById("strength_M").style.background = Mcolor;
document.getElementById("strength_H").style.background = Hcolor;
return;
}
function pwStrength2(pwd) {
checkPassLong(1);
var O_color = "#eeeeee";
var L_color = "#FF0000";
var M_color = "#FF9900";
var H_color = "#33CC00";
if (pwd == null || pwd == '') {
Lcolor = Mcolor = Hcolor = O_color;
} else {
var S_level = checkStrong(pwd);
switch (S_level) {
case 0:
Lcolor = Mcolor = Hcolor = O_color;
case 1:
Lcolor = L_color;
Mcolor = Hcolor = O_color;
break;
case 2:
Lcolor = Mcolor = M_color;
Hcolor = O_color;
break;
default:
Lcolor = Mcolor = Hcolor = H_color;
}
}
document.getElementById("strength_L2").style.background = Lcolor;
document.getElementById("strength_M2").style.background = Mcolor;
document.getElementById("strength_H2").style.background = Hcolor;
return;
}
function checkPassLong(flag) {
var passStr = $('#pass').val();
if (flag == 1) {
passStr = $('#pass2').val();
}
passStr = passStr.replace(/(\s*$)/g, "");
if (passStr.length == 0 || passStr.length < 6) {
if (flag == 0) {
document.getElementById('pass1Info').innerHTML = '<font color="red">密码长度大于 6 位,不能为空格</font>';
} else {
document.getElementById('pass1Info2').innerHTML = '<font color="red">密码长度大于 6 位,不能为空格</font>';
}
return true;
} else {
if (flag == 0) {
document.getElementById('pass1Info').innerHTML = '由 6-16 位字符组成,请使用英文字母、符号或数字。';
} else {
document.getElementById('pass1Info2').innerHTML = '由 6-16 位字符组成,请使用英文字母、符号或数字。';
}
return false;
}
}
//密码强度验证 end
后台 action 方法,Account 为用户类
// 注册新用户
@
Action(value = "reAimcodeGetVeCode")
public void reAimcodeGetVeCode() {
PrintWriter out;
String result = "验证码申请失败!请重试!";
try {
smsUnit = new SmsUnit(ConfUtil.getProperty("sys_sms_server"));
if (null != account.getACCMOB() && !account.getACCMOB().equals("")) {
account.setACCSTATUS(new BigDecimal(1));//设置使用状态未用
String verifyCode = String
.valueOf(new Random().nextInt(899999) + 100000);//生成短信验证码
account.setFSECURITYCODE(verifyCode);
account.setACCCREATEDATE(new Date());
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, 1); // 设置验证码失效时间为 24 小时
account.setFREGISTERSOURCE(fromSource);
// 判断该手机是否获取过验证码
AccountCriteria accountCriteria = new AccountCriteria();
accountCriteria.createCriteria().andACCMOBEqualTo(
account.getACCMOB());
List<Account> accs = accountService
.selectByExample(accountCriteria);
int re = 0;
Integer cishu = 0;
if (accs == null || accs.isEmpty()) {
cishu = 1;
account.setSDKURL("1");
account.setFSECURITYOUTTIME(c.getTime());
BigDecimal accid=accountService.getPrimaryKey();
account.setFID(accid);
re = accountService.insertSelective(account,IPUtil.getRealIP(request));
Cookie cookie=new Cookie("id" , accid.toString());
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
} else {
Account ac = accs.get(0);
account.setFID(ac.getFID());
Date date = new Date();
// 如果是新的一天则使用次数修改为 1
if (date.getDate() == ac.getFSECURITYOUTTIME().getDate()) {
account.setSDKURL("1");
} else {
Integer count = Integer.parseInt(ac.getSDKURL());
account.setSDKURL(count + 1 + "");
}
cishu = Integer.parseInt(account.getSDKURL());
account.setFSECURITYOUTTIME(c.getTime());
if (cishu <= 3)
re = accountService
.updateByPrimaryKeySelective(account);
}
if (re > 0 && cishu <= 3) {
request.getSession().removeAttribute(ConstValues.WEB_SESSION_PROMOTE);
AccountCriteria ac = new AccountCriteria();
ac.createCriteria().andACCMOBEqualTo(account.getACCMOB());
List<Account> acList = new ArrayList<Account>();
acList = accountService.selectByExample(ac);
if (acList != null && acList.size() > 0) {
// 这里执行短信发送
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
String bizID = df.format(new Date());
SendSmsReply sendSmsReply = smsUnit.sendSms("","","您的验证码为:" + verifyCode+",该码有效期为 24 小时,该码只能使用一次! [短信签名] ", bizID,account.getACCMOB(), "", 1 + "", 1 + "","","","" , "");
result = sendSmsReply.getReplyMsg() + "&"
+ acList.get(0).getFID() + "&"
+ acList.get(0).getSDKURL();
}
} else if (cishu > 3) {
result = "限制申请";
}
}
} catch (Exception e) {
logger.error("获取验证码失败", e);
} finally {
try {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
out = response.getWriter();
out.write(result);
} catch (IOException e) {
logger.error("", e);
}
}
}
SmsUnit 短信发送类代码:
// 发送短信
public SendSmsReply sendSms(String acname, String acpwdmd5,
String smsContent, String batchnumber, String mobiles,
String schTime, String serialPkgNumber, String countPkgNum,
String sendType,String pid,String reno) {
httppost = new HttpPost("www.lx198.com/lxDlsms/sms/sendSms"); List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("sendSms.acName", acname));// 用户名
formparams.add(new BasicNameValuePair("sendSms.pwd", acpwdmd5));// 密码
formparams
.add(new BasicNameValuePair("sendSms.smsContent", smsContent));// 短信内容
formparams.add(new BasicNameValuePair("sendSms.mobiles", mobiles));// 电话号码
formparams.add(new BasicNameValuePair("sendSms.schTime", schTime));
formparams.add(new BasicNameValuePair("sendSms.batchNum",
batchnumber));
formparams.add(new BasicNameValuePair("sendSms.pkgNum",
serialPkgNumber));
formparams.add(new BasicNameValuePair("sendSms.pkgCount",
countPkgNum));
formparams.add(new BasicNameValuePair("sendSms.sendType",
sendType));
formparams.add(new BasicNameValuePair("sendSms.pid",
pid));
formparams.add(new BasicNameValuePair("sendSms.reno",
reno));
//private String reno="";
try {
String replyString = doPost(formparams);
return (SendSmsReply) XmlReplyUnit.fromXml(replyString,new SendSmsReply());
} catch (ParseException e) {
logger.error("",e);
} catch (Exception e) {
logger.error("",e);
}
return null;
}