1、前端部分:
<script type="text/javascript">
function runReg()
{
var name=$.trim($("#username").val());
var pwd = $.trim($("#pwd").val());
var repwd = $.trim($("#repwd").val());
var telphone=$('#telphone').val();
var code=$('#rcode').val();
var myreg = /^1\d{10}$/;
if(name == "")
{
alert("请填写用户名!");
return false;
}
else if(pwd == "")
{
alert('请填写密码!');
return false;
}
else if(repwd == "")
{
alert('请填写重复密码!');
return false;
}
else if(pwd != repwd)
{
alert("两次密码不一致,请重新输入!");
$("#username").val("");
$("#username").focus();
return false;
}
else if(telphone.length!=11)
{
alert('请输入正确的11位电话号码!');
return false;
}
else if(!myreg.test(telphone))
{
alert('请输入有效的手机号码!');
return false;
}
else if(code.length!=4)
{
alert('请输入正确的4位验证码!');
return false;
}
else
{
var urlstr = "/Index/Index/runregist";
$.ajax({
type: "POST",
url: urlstr,
data: "name="+name+"&pwd="+pwd+"&telphone="+telphone+"&code="+code,
dataType: 'json',
contentType: "application/x-www-form-urlencoded",
async:false,
success: function(msg){
if(msg.result == "ok")
{
alert("恭喜您注册成功,请进入登入页面登入");
location.href='/userlogin.html';
}
else if(msg.result == "re")
{
alert("此用户名已被占用,请重新填写!");
}
else if(msg.result == "err1")
{
alert("您输入的手机号不匹配,请重新输入!");
}
else if(msg.result == "err2")
{
alert("您输入的验证码已过时!");
}
else if(msg.result == "err3")
{
alert("您输入的验证码不正确,请重新输入!");
}
else if(msg.result == "err4")
{
alert("此手机号已被注册,请重新填写!");
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
return true;
}
}
//倒计时
var times = 60;
function waitTime() {
if (times == 0) {
$("#codeButton").removeAttr("disabled").val("重新获取验证码");
times = 60;
} else {
$("#codeButton").attr("disabled", true).val("在(" + times + ")秒后重新发送");
times--;
setTimeout(function() {waitTime()},1000)
}
}
//发送验证码
function codeCheck(){
var telphone=$('#telphone').val();
var urlstr = "/Index/Index/registCode";
if(telphone.length!=11){
alert('请输入正确的11位电话号码!');
return true;
}
var myreg = /^1\d{10}$/;
if(!myreg.test(telphone))
{
alert('请输入有效的手机号码!');
return true;
}
$.ajax({
type: "GET",
url: urlstr,
data: {telphone:telphone},
success: function(data){
waitTime();
}
});
}
</script>
<div class="registdiv"><div class="registname">用户注册</div><table width="500" height="138" border="0"> <tr> <td width="100" align="right">用户名:</td> <td colspan="2" width="300"><input type="text" class="retest" id="username" name="username" /></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <tr> <td align="right">密码:</td> <td colspan="2"><input type="password" class="retest" id="pwd" name="pwd" /></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td align="right">重复密码:</td> <td colspan="2"><input type="password" class="retest" id="repwd" name="repwd" /></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td align="right">手机号码:</td> <td width="200"><input type="text" class="retest" id="telphone" name="telphone" /></td> <td width="200"><input type="button" onClick="codeCheck();" id="codeButton" style="width:130px; margin:0 auto; height:30px; line-height:30px; display:block;" value="获取验证码" /></td> </tr> <tr> <td colspan="3"> </td> </tr> <tr> <td align="right">验证码:</td> <td colspan="2"><input type="text" class="retest" id="rcode" name="rcode" size="6" /></td> </tr></table><p><input type="button" id="regist" name="regist" value="注册" class="btnregist" onClick="runReg()"/></p></div>2、SDK下载下来之后修改名字为【alidayu】,放在我们的项目的核心目录, \ThinkPHP\Extend\Vendor 文件夹下面
文件只保留如下2个文件夹和2个文件其他删除即可
3、代码部分:
######**********阿里大鱼*******########
//阿里大鱼发送短信函数
public function sendAli($telphone,$randomCode){
vendor('alidayu.top.TopClient');
vendor('alidayu.top.ResultSet');
vendor('alidayu.top.RequestCheckUtil');
vendor('alidayu.top.TopLogger');
vendor('alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
include "TopSdk.php";
date_default_timezone_set('Asia/Shanghai');
$c = new \TopClient;
$c->appkey = '23524266';
$c->secretKey = 'b421d9dd6deeb0a0e903e4a94b3e5d38';
$req = new \AlibabaAliqinFcSmsNumSendRequest;
$req ->setExtend( "123456" );//固定参数
$req ->setSmsType( "normal" );//固定参数
$req ->setSmsFreeSignName( "XXXX网" );//来源于配置短信签名 下面列表中有签名名称
$number = $randomCode;
$time = 10;
$req ->setSmsParam( "{number:'{$number}',time:'{$time}'}" ); //变量来源于 配置短信模板 点击列表中的详情 模板内容的变量
$req ->setRecNum($telphone); //手机号
$req ->setSmsTemplateCode("SMS_123456"); //配置短信模板 列表中有模板id
$resp = $c ->execute( $req );
//$content = 您将要绑定本号码作为您在XXXX网中的手机号码。手机验证码为:${number},此验证码${time}分钟以内有效!; //内容
//object(SimpleXMLElement)#8 (2) { ["result"]=> object(SimpleXMLElement)#7 (3) { ["err_code"]=> string(1) "0" ["model"]=> string(26) "104469829648^1106082240893" ["success"]=> string(4) "true" } ["request_id"]=> string(13) "10fk2epu4bw4l" }
if($resp->result->success && !$resp->result->code){
//发送成功
return 1;
}else if($resp->code || $resp->msg =="Remote service error"){
//发送失败
return 0;
}else{
//未知情况
return 0;
}
}
//追加手机验证码
public function registCode()
{
$telphone=$_GET['telphone'];
$randomCode=$this->randCode(4,1);
$res = $this->sendAli($telphone , $randomCode);
if($res){
$_SESSION['registTelphone']=$telphone;
$_SESSION['registCode']=$randomCode;
$_SESSION['time']=time();
$this->success('短信已成功发送至您的手机,请注意查收!');
$this->assign('jumpUrl',U('/Index/regist'));
}else{
$this->assign('jumpUrl',U('/Index/regist'));
$this->error('发送失败!');
}
}
/**
+----------------------------------------------------------
* 生成随机字符串
+----------------------------------------------------------
* @param int $length 要生成的随机字符串长度
* @param string $type 随机码类型:0,数字+大写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function randCode($length = 5, $type = 0) {
$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
if ($type == 0) {
array_pop($arr);
$string = implode("", $arr);
} else if ($type == "-1") {
$string = implode("", $arr);
} else {
$string = $arr[$type];
}
$count = strlen($string) - 1;
for ($i = 0; $i < $length; $i++) {
$str[$i] = $string[rand(0, $count)];
$code .= $str[$i];
}
return $code;
}
/**
+----------------------------------------------------------
* 生成随机字符串
+----------------------------------------------------------
* @param int $length 要生成的随机字符串长度
* @param string $type 随机码类型:0,数字+大写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
######**********阿里大鱼*******########
//注册
public function runregist()
{
########*******追加手机注册******############
$telphone=$_POST['telphone'];
$code=$_POST['code'];
$reg_error=1;
if($telphone!=$_SESSION['registTelphone']){
$reg_error=2;
}
if((time()-intval($_SESSION['time']))>10*60){
$reg_error=3;
}
if($code!=$_SESSION['registCode']){
$reg_error=4;
}
$rgtel = M('member')->where(array('tel'=>$telphone))->find();
if(count($rgtel) >0){
$reg_error=5;
}
$info['tel']=$telphone;
########*******追加手机注册******############
$info['username'] = $_POST['name'];
$info['password'] = I('pwd','','md5');
$info['schoolid']= I('post.schoolid');
$info['school']=I('post.schoolname');
$user = M('member')->where(array('username'=>I('post.name')))->find();
$result = 'no';
if(count($user) >0){
$result = 're';
}else{
if($reg_error==1){
M('member')->add($info);
$result = 'ok';
}else if($reg_error==2){
$result = 'err1';
}else if($reg_error==3){
$result = 'err2';
}else if($reg_error==4){
$result = 'err3';
}else if($reg_error==5){
$result = 'err4';
}
}
print json_encode(array('reg_error'=>$reg_error,'result'=>$result));
}
注意:阿里大鱼注册、应用创建、配置管理【验证码】、下载API接口等自行看指南