老莫的笔记本  
  
查看: 1276|回复: 2

手机注册功能

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-9-14 14:28:36 | 显示全部楼层 |阅读模式
先过下实现方式:
1.获取第三方支持【我常用聚合数据】;(注意 模板)
2.写一个函数,发送随机code给聚合   并且存储 session  这个session 包括: 手机号、生code 和 过期时间;

老莫个人觉得,为了防止短信轰炸, 应该在发送前先去session 里查一下 有没有这个 手机号;




参考文献:https://blog.csdn.net/weixin_41793489/article/details/79661410
回复

使用道具 举报

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
 楼主| 发表于 2018-9-20 00:10:27 | 显示全部楼层
贴一下 乞丐未整理版 手机注册代码
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Request;
  4. use think\Controller;
  5. use app\admin\model\Admins;
  6. use app\admin\model\AdminGroups;
  7. use app\admin\controller\Base;
  8. use think\Session;
  9. // 简化代码第一步
  10. use think\config;
  11. class Res extends Controller
  12. {
  13.     // 渲染
  14.     public function index()
  15.     {
  16.         return $this->fetch();
  17.     }
  18.     // 注册
  19.     public function dores(Request $request)
  20.     {
  21.         // 非空判断
  22.         if(!$request->param('phone')){
  23.             exit( json($code=1,$msg='手机号不能为空'));
  24.         }
  25.         if(!$request->param('password')){
  26.             exit( json($code=1,$msg='密码不能为空'));
  27.         }
  28.         $where = function ($query) use($request){
  29.             $query->where([
  30.                     'username' =>['=',$request->param('phone')],
  31.                 ]);
  32.         };
  33.         $lmb = admins::get($where);
  34.         //用户验证
  35.         if($lmb){
  36.             exit( json(1,$msg='该账号已注册'));
  37.         }
  38.         if(!Session::has($request->param('phone'))){
  39.             exit( json(1,$msg='您尚未发送短信验证码'));
  40.         }
  41.         if(Session::get($request->param('phone')) != $request->param('vercode')){
  42.             exit( json(1,'短信验证码不正确',$request->param('vercode')));
  43.         }
  44.         // 以手机号作为账户
  45.         $data = [
  46.             'username' => $request->param('phone'),
  47.             'truename' => $request->param('truename'),
  48.             'gid' => 5,
  49.             'password' =>  md5( $request->param('password') ),
  50.             'add_time' => time(),
  51.         ];
  52.         $lma = Admins::create($data);
  53.         if($lma){
  54.             exit( json(0,'添加成功',1,$data));
  55.         }else{
  56.             exit( json(1,'发生未知错误添加失败',1,$data));
  57.         }
  58.     }
  59.     // 发送验证码
  60.     public function sendtooken(Request $request)
  61.     {
  62.         // 这里缺个 手机号验证判断
  63.         $phone = $request->param('phone');
  64.         // 1.生成一个随机数 code
  65.         $code = rand(1024,9999);
  66.         // $url = Config::get('sendUrl').'?mobile='.$phone.'&tpl_id='.Config::get('smsConf')['tpl_id'].'&tpl_value=%23code%23%3D'.$code.'&key='.Config::get('smsConf')['key'];
  67.         $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
  68.   
  69.         $smsConf = array(
  70.             'key'   =>    Config::get('smsConf')['key'], //您申请的APPKEY
  71.             'mobile'    => $phone, //接受短信的用户手机号码
  72.             'tpl_id'    =>  Config::get('smsConf')['tpl_id'], //您申请的短信模板ID,根据实际情况修改
  73.             'tpl_value' =>'#code#='.$code , //您设置的模板变量,根据实际情况修改
  74.         );
  75.         
  76.         $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
  77.         
  78.         if($content){
  79.             $result = json_decode($content,true);
  80.             $error_code = $result['error_code'];
  81.             if($error_code == 0){
  82.                 //状态为0,说明短信发送成功
  83.                         // 2.将手机号 和 code 值存起来
  84.                 Session::set($phone,$code);
  85.                 exit( json(0,'验证码发送成功' ));
  86.             }else{
  87.                 //状态非0,说明失败
  88.                 $msg = $result['reason'];
  89.                 // echo "短信发送失败(".$error_code."):".$msg;
  90.                 exit( json(1,'验证码发送失败,错误信息'. $result['reason']));
  91.             }
  92.         }else{
  93.             //返回内容异常,以下可根据业务逻辑自行修改
  94.             exit( json(1,'验证码发送失败' ));
  95.         }
  96.     }
  97.     // 聚合
  98.     public function juhecurl($url,$params=false,$ispost=0){
  99.         $httpInfo = array();
  100.         $ch = curl_init();
  101.         curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  102.         curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
  103.         curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
  104.         curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
  105.         curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  106.         if( $ispost )
  107.         {
  108.             curl_setopt( $ch , CURLOPT_POST , true );
  109.             curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  110.             curl_setopt( $ch , CURLOPT_URL , $url );
  111.         }
  112.         else
  113.         {
  114.             if($params){
  115.                 curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  116.             }else{
  117.                 curl_setopt( $ch , CURLOPT_URL , $url);
  118.             }
  119.         }
  120.         $response = curl_exec( $ch );
  121.         if ($response === FALSE) {
  122.             //echo "cURL Error: " . curl_error($ch);
  123.             return false;
  124.         }
  125.         $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  126.         $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  127.         curl_close( $ch );
  128.         return $response;
  129.     }
  130. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表