微信小程序 实现通过验证码获取手机号登录

phon.wxml:

<form catchsubmit="formSubmit">

<view>请输入手机号</view>

<input type="text" name="phone" bindinput="bindinput"/>

<button bindtap="sendcode" type="warn" size="mini">验证码</button>

<view>请输入验证码</view>

<input type="text" name="code"/>

<button formType="submit">登录</button>

</form>

phone.js:

Page({

    /**

     * 页面的初始数据

     */

    data: {

       phone:"",

    },

    /**

     * 生命周期函数--监听页面加载

     */

    onLoad: function (options) {

    },

    bindinput(e){

        var phone = e.detail.value

        this.setData({

          phone:phone

        })

    },

    sendcode(e){

      var phone = this.data.phone

      console.log(phone)

       if(phone == ''){

           wx.showToast({

             title: '手机号不能为空',

             icon:"error"

           })

           return false;

       }

       var that = this

       wx.request({

        url: 'http://www.laravel.mouth.com/sendCode', //接口地址

        data: {

            phone:phone

        },

        header: {

          'content-type': 'application/json' // 默认值

        },

        success (res) {

          console.log(res.data)

          if(res.data.code==200){

              wx.showToast({

                title: '验证码发送成功',

              })

          }

          if(res.data.code==0){

            wx.showToast({

              title: '每天只能请求3次',

              icon:'error'

            })

        }

        }

      }) 

    },

    formSubmit:function(e){

      console.log(e)

      var phone = e.detail.value.phone

      var code = e.detail.value.code

      

     wx.request({

      url: 'http://www.laravel.mouth.com/doLogin', //接口地址

      data: {

          phone:phone,

          code:code

      },

      header: {

        'content-type': 'application/json' // 默认值

      },

      success (res) {

        console.log(res.data)

        if(res.data.code==200){

            wx.showToast({

              title: '登录成功',

            })

            wx.navigateTo({

              url: '/pages/course/course',

            })

        }

        if(res.data.code==201){

          wx.showToast({

            title: '手机号有误',

            icon:'error'

          })

      }

      if(res.data.code==202){

        wx.showToast({

          title: '验证码有误',

          icon:'error'

        })

    }

      }

    }) 

  },

})

php:

controller:

//通过手机号来获取验证码
public function sendCode(request $request)
{
    $phone = $request->get('phone');

    //限制恶意请求问题
    $count = Phone::where('phone',$phone)->count();
    if ($count >3){
        return ['code'=>0,'msg'=>'每个手机号每天只能请求3次','data'=>null];
    }

    $code = rand(1111,9999);

    $statusStr = array(
        "0" => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );
    $smsapi = "http://api.smsbao.com/";
    $user = "xxxxx"; //短信平台帐号
    $pass = md5("xxxxx"); //短信平台密码
    $content="您的验证码为.$code ,在1分钟内有效";//要发送的短信内容
    $phone = "$phone";//要发送短信的手机号码
    $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
    $result =file_get_contents($sendurl) ;
    $data = [
        'phone'=>$phone,
        'code'=>$code
    ];
    Phone::create($data,true);
    return ['code'=>200,'msg'=>$statusStr[$result],'data'=>$code] ;
}
//手机号登录
public function doLogin(request $request)
{
    $phone = $request->get('phone');
    $code  = $request->get('code');
    $data = Phone::getOne($phone);

    if ($data['phone']!=$phone){//验证手机号
        return ['code'=>201,'msg'=>'手机号有误','data'=>null];
    }
    if ($data['code']!=$code){//验证验证码
        return ['code'=>202,'msg'=>'验证码有误','data'=>null];
    }
    if ($data){
        return ['code'=>200,'msg'=>'登录成功','data'=>$data];
    }
}

model:

protected $guarded = [];
protected $table = 'phone';
public static function getOne($phone)
{
    return self::where('phone',$phone)->first();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值