toplan / laravel-sms

:iphone::heavy_check_mark:A phone number validation solution based on laravel
MIT License
839 stars 174 forks source link

请问博主有没有完整的例子? #11

Closed followtheart closed 9 years ago

followtheart commented 9 years ago

如题

toplan commented 9 years ago

按照readme.md中的【快速上手】里面的1,2,3步就可以在你的程序里实现短信发送了。。完整demo暂时还没有呢,我是直接应用到项目的,比如 http://kiteme.cn/signup

followtheart commented 9 years ago

已经实现了1,2,3步总是有500跨站的错误.token也是新的...能否共享下那个页面form部分的和js的代码,多谢

toplan commented 9 years ago

你好,能否帖上你的代码和错误的信息?前端部分,给发送按钮绑定 sms 方法:

<button id='sendVerifySmsButton'>获取验证码</button>
<script>
     $('#sendVerifySmsButton').sms({
        //token value
        token          : "{{csrf_token()}}",
        //定义如何获取mobile的值
        mobileSelector : 'input[name="mobile"]',
        //定义手机号的检测规则,当然你还可以到配置文件中自定义你想要的任何规则
        mobileRule     : 'check_mobile_unique',
        //定义服务器有消息返回时如何展示,默认为alert
        alertMsg       :  function (msg) {
            alert(msg);
        },
     });
</script>
followtheart commented 9 years ago

数据库中users表是否需要一个mobile字段?

toplan commented 9 years ago

不是,你改一下配置文件,配置文件里默认要求检测users表的mobile值时唯一,你可以按你的需求进行修改

'rules' => [
       //唯一性检测规则
       'check_mobile_unique' => 'unique:users,mobile',//适用于注册
       //存在性检测规则
       'check_mobile_exists' => 'exists:users',//适用于找回密码和系统内业务验证
   ]
toplan commented 9 years ago

你可以先吧check_mobile_unique 设置为空字符串嘛

'rules' => [
     ...
     'check_mobile_unique' => ''
     ...
]
followtheart commented 9 years ago

多谢,设置似乎没有问题; 暂时找不到csrf不能通过的原因,在App\Http\Middleware\VerifyCsrfToken.php中 对sms/*禁用了csrf保护.现在已经能收到短信了


namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        "sms/*"
    ];
}
toplan commented 9 years ago

你看下请求提交的token值和服务器端session里的token是否一致,通过不了 csrf 肯定是值不一致。。不知道你前端请求的写法,可以帖出来看看

followtheart commented 9 years ago

这个是表单,发送按钮本来是button,参考kiteme改成了a标签

    <form class="input-group">
        <input type="text" name="mobile" placeholder="用于登录与找回密码" autofocus required  />       
       <a class="btn btn-block btn-positive mgt50" id="sendVerifySmsButton" href="javascript:void(0);">获取短信校验码</a>
        <input id="protocol" type="checkbox" name="check" class="check_switch" checked="checked" title="0">
    </form>
    <script type="text/javascript">
    // $(document).ready(function(){
      $('#sendVerifySmsButton').sms({
            //token value
            token          : " {{ csrf_token() }} ",
            //定义如何获取mobile的值
            mobileSelector : 'input[name="mobile"]',
            //定义手机号的检测规则,当然你还可以到配置文件中自定义你想要的任何规则
            mobileRule     : 'check_mobile_unique',
            //是否请求语音验证码
            voice          : false,
            //定义服务器有消息返回时如何展示,默认为alert
            alertMsg       :  function (msg) {
                alert(msg);
            },
         });
    //  });
     </script>

另外在<header>里面有

<html>
  <head>
    <meta charset="utf-8">
    <meta name="_token" content="{{csrf_token() }}"/> 
    ....
toplan commented 9 years ago

@followtheart js中,token那个双引号前后各有一个空格,导致token值会前后多个空格符,把空格去掉试试:

..
token : "{{ csrf_token() }}"
..
followtheart commented 9 years ago

好像找到问题了.原因不知道; 不管到哪儿网站都产生固定的xsrf-token和laravel_session的cookie值. 那个空格似乎不影响大局

toplan commented 9 years ago

我刚刚按照你的写法实验了下,有空格是肯定通过不了csrf的。毕竟服务器端没有trim字符串。。你的写法是:

token : " {{ csrf_token() }} ",

在双引号和大括号之间有空格,导致token值错误,比如本来值为abc123,前后有空格就成了<空格符>abc123<空格符>

followtheart commented 9 years ago

It works!!!牛!我是直接看的请求提交的数据,看见了值,就没在意这个,我擦。