十八、使用短信宝

1. 接入短信宝

    if($this->input->post('mobile')){
        $smsapi = "http://api.smsbao.com/";
        $user = "XXX"; //短信平台帐号
        $pass = md5("XXX"); //短信平台密码
        $radom_number = $this->random(4,1);
        $content= "【XXXX】您的验证码是:".$radom_number."。请不要把验证码泄露给其他人。";//要发送的短信内容
        $phone = $this->input->post('mobile');//要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;
        //var_dump($result);
        if($result==0){
            $_SESSION['check_code'] = $radom_number;
            $info = array();
            $info['code'] = $radom_number;
            $info['msg'] = 'ok';
            exit(json_encode($info));
            // echo $radom_number;
            // echo '提交成功';
            // exit;
        }
    }

2. 界面:

                        $.post('index.php/ajax/sendCodeXfj',{mobile:phone}, function(msg) {

                            if(msg=='同一手机号验证码短信发送超出5条'){
                                alert("同一手机号验证码短信发送超出5条");
                                $('#getcode').addClass('am-disabled');
                                //$("#submit").removeClass('am-disabled');
                            }else{
                                var info = jQuery.parseJSON(msg);
                                if(info.msg =='ok'){
                                    $("#remote_code").val(info.code);
                                    mui.alert("验证码已发送");
                                    RemainTime();
                                }
                            }
                        });

3. 倒计时

function RemainTime(){
    $('#getcode').addClass('am-disabled');
    var iSecond,sSecond="",sTime="";
    if (iTime >= 0){
        iSecond = parseInt(iTime%60);
        iMinute = parseInt(iTime/60);
        if (iSecond >= 0){
            if(iMinute>0){
                sSecond = iMinute + "分" + iSecond + "秒";
            }else{
                sSecond = iSecond + "秒";
            }
        }
        sTime=sSecond;
        if(iTime==0){
            clearTimeout(Account);
            sTime='获取';
            iTime = 59;
            $("#getcode").removeClass('am-disabled');
        }else{
            Account = setTimeout("RemainTime()",1000);
            iTime=iTime-1;
        }
    }else{
        sTime='没有倒计时';
    }
    $("#getcode").html(sTime);
}

你可能感兴趣的:(十八、使用短信宝)