手机注册功能
先过下实现方式:1.获取第三方支持【我常用聚合数据】;(注意 模板)
2.写一个函数,发送随机code给聚合 并且存储 session这个session 包括: 手机号、生code 和 过期时间;
老莫个人觉得,为了防止短信轰炸, 应该在发送前先去session 里查一下 有没有这个 手机号;
参考文献:https://blog.csdn.net/weixin_41793489/article/details/79661410
贴一下 乞丐未整理版 手机注册代码
<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;
use app\admin\model\Admins;
use app\admin\model\AdminGroups;
use app\admin\controller\Base;
use think\Session;
// 简化代码第一步
use think\config;
class Res extends Controller
{
// 渲染
public function index()
{
return $this->fetch();
}
// 注册
public function dores(Request $request)
{
// 非空判断
if(!$request->param('phone')){
exit( json($code=1,$msg='手机号不能为空'));
}
if(!$request->param('password')){
exit( json($code=1,$msg='密码不能为空'));
}
$where = function ($query) use($request){
$query->where([
'username' =>['=',$request->param('phone')],
]);
};
$lmb = admins::get($where);
//用户验证
if($lmb){
exit( json(1,$msg='该账号已注册'));
}
if(!Session::has($request->param('phone'))){
exit( json(1,$msg='您尚未发送短信验证码'));
}
if(Session::get($request->param('phone')) != $request->param('vercode')){
exit( json(1,'短信验证码不正确',$request->param('vercode')));
}
// 以手机号作为账户
$data = [
'username' => $request->param('phone'),
'truename' => $request->param('truename'),
'gid' => 5,
'password' =>md5( $request->param('password') ),
'add_time' => time(),
];
$lma = Admins::create($data);
if($lma){
exit( json(0,'添加成功',1,$data));
}else{
exit( json(1,'发生未知错误添加失败',1,$data));
}
}
// 发送验证码
public function sendtooken(Request $request)
{
// 这里缺个 手机号验证判断
$phone = $request->param('phone');
// 1.生成一个随机数 code
$code = rand(1024,9999);
// $url = Config::get('sendUrl').'?mobile='.$phone.'&tpl_id='.Config::get('smsConf')['tpl_id'].'&tpl_value=%23code%23%3D'.$code.'&key='.Config::get('smsConf')['key'];
$sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
$smsConf = array(
'key' => Config::get('smsConf')['key'], //您申请的APPKEY
'mobile' => $phone, //接受短信的用户手机号码
'tpl_id' =>Config::get('smsConf')['tpl_id'], //您申请的短信模板ID,根据实际情况修改
'tpl_value' =>'#code#='.$code , //您设置的模板变量,根据实际情况修改
);
$content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
if($error_code == 0){
//状态为0,说明短信发送成功
// 2.将手机号 和 code 值存起来
Session::set($phone,$code);
exit( json(0,'验证码发送成功' ));
}else{
//状态非0,说明失败
$msg = $result['reason'];
// echo "短信发送失败(".$error_code."):".$msg;
exit( json(1,'验证码发送失败,错误信息'. $result['reason']));
}
}else{
//返回内容异常,以下可根据业务逻辑自行修改
exit( json(1,'验证码发送失败' ));
}
}
// 聚合
public function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
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' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
}
页:
[1]