多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
​ 1.腾讯云短信验证码扩展包,直接下载就可以了: 链接:https://pan.baidu.com/s/16kQ5ejt2YxMbQCDeGCyyBg  提取码:fmue 2.封装一个类用于调用发送短信接口: ~~~php <?php /** * Created by PhpStorm. * User: cwwx0 * Date: 2020/4/15 * Time: 11:58 */ namespace app\index\controller; use sms\SmsSingleSender; class Smsmsg { public function sendcode($phoneNumbers='18888888888',$params=1234){ // [rand(1000, 9999)] //腾讯短信验证码 // 短信应用 SDK AppID $appid = 1400**0687; // SDK AppID 以1400开头 // 短信应用 SDK AppKey $appkey = "7922a5ec4****4220941e8f16e1a1615"; // 需要发送短信的手机号码 // $phoneNumbers = '15956137456'; // $phoneNumbers = input("phone"); // 短信模板 ID,需要在短信控制台中申请 $templateId = 57**70; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 $smsSign = "**赳"; // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请 try { $ssender = new SmsSingleSender($appid, $appkey); // $params = [rand(1000, 9999)];//生成随机数 $result = $ssender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", ""); $rsp = json_decode($result); return json(["result"=>$rsp->result,"code"=>$params]); } catch(\Exception $e) { // echo var_dump($e); // return false; return json(["result"=>1,"code"=>$params]); } } public function sendmsg($phoneNumbers='18855662233',$params=array()){ //腾讯短信验证码 // 短信应用 SDK AppID $appid = 140***0687; // SDK AppID 以1400开头 // 短信应用 SDK AppKey $appkey = "7922a5****3144220941e8f16e1a1615"; // 需要发送短信的手机号码 // $phoneNumbers = '18855662233'; // $phoneNumbers = input("phone"); // 短信模板 ID,需要在短信控制台中申请 $templateId = 57**71; // NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 $smsSign = "**赳"; // NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台申请 try { $ssender = new SmsSingleSender($appid, $appkey); // $params = array('猪猪侠');//生成随机数 // $params = [rand(1000, 9999)];//生成随机数 $result = $ssender->sendWithParam("86", $phoneNumbers, $templateId, $params, $smsSign, "", ""); $rsp = json_decode($result); return json(["result"=>$rsp->result,"code"=>$params]); } catch(\Exception $e) { // echo var_dump($e); return json(["result"=>1,"code"=>$params]); } } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 3.短信发送接口调用方法  在控制器顶部加上 ~~~php /*注册发送短信*/ public function sendmsg(){ $request=request()->param(); if(!$request['phone']){ return json(['code'=>400,'msg'=>'手机号不能为空']); } $cs= new Smsmsg(); $code=rand(1000, 9999); $ret=$cs->sendcode($request['phone'],[$code]); $ck = json_decode($ret->getContent(),true); if($ck['result']==0){ /*短信存储*/ Db::name('sms')->insert(array('event'=>'注册验证码','mobile'=>$request['phone'],'code'=>$code,'ip'=>request()->ip(),'createtime'=>time())); return json(['code'=>200,'msg'=>'验证码发送成功']); }else{ return json(['code'=>400,'msg'=>'验证码发送失败']); } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 4.完整版登录、注册、找回密码、验证码登录、退出登录 ~~~php <?php /** * Created by PhpStorm. * User: cwwx0 * Date: 2020/6/30 * Time: 10:36 */ namespace app\webapi\controller; use fast\Random; use think\Controller; use think\Db; use think\Request; use think\cache\driver\Redis; class Login extends Controller { /*注册发送短信*/ public function sendmsg(){ $request=request()->param(); if(!$request['phone']){ return json(['code'=>400,'msg'=>'手机号不能为空']); } $cs= new Smsmsg(); $code=rand(1000, 9999); $type=$request['type']; if($type==1){ $templateId=648267; $msg='注册验证码'; }else if($type==2){ $templateId=563119; $msg='找回密码验证码'; }else if($type==3){ $templateId=648270; $msg='登录验证码'; } $ret=$cs->sendcode($request['phone'],[$code],$templateId); $ck = json_decode($ret->getContent(),true); if($ck['result']==0){ /*短信存储*/ Db::name('sms')->insert(array('event'=>$msg,'mobile'=>$request['phone'],'code'=>$code,'ip'=>request()->ip(),'createtime'=>time())); return json(['code'=>200,'msg'=>'验证码发送成功']); }else{ return json(['code'=>400,'msg'=>'验证码发送失败']); } } /** * @return mixed|\think\response\Json * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * 立即注册 */ public function register(){ if (Request::instance()->isPost()) { $data = Request::instance()->param(); $ck=Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->find(); if($ck){ if($ck['times']==0){ Db::startTrans(); try { $cks=Db::name('pcnewuser')->where(array('phone'=>$data['phone']))->find(); Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->setInc('times'); if($cks){ // return json(['code'=>400,'msg'=>'账号已存在']); Db::name('pcnewuser')->where(array('phone'=>$data['phone']))->update(array('password'=>md5($data['password']),'createtime'=>time())); return json(['code'=>200,'msg'=>'账号已存在,密码更新成功']); }else{ /*新增一条用户信息*/ Db::name('pcnewuser')->insertGetId(array('phone'=>$data['phone'],'password'=>md5($data['password']),'createtime'=>time())); } Db::commit(); // session('userinfo',$infoint); 是否登录用cookie来判断 参照 蓝牙打印 return json(['code'=>200,'msg'=>'注册成功']); } catch (Exception $e) { Db::rollback(); } }else{ return json(['code'=>400,'msg'=>'验证码失效']); } }else{ return json(['code'=>400,'msg'=>'验证码错误']); } } return $this->fetch(); } /** * @return string * @throws \think\Exception * 找回密码 */ public function findpassword() { if (Request::instance()->isPost()) { $data = Request::instance()->param(); $ck=Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->find(); if($ck){ if($ck['times']==0){ $cks=Db::name('pcnewuser')->where(array('phone'=>$data['phone']))->find(); Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->setInc('times'); if($cks){ Db::name('pcnewuser')->where(array('phone'=>$data['phone']))->update(array('password'=>md5($data['password']),'createtime'=>time())); return json(['code'=>200,'msg'=>'密码修改成功']); }else{ return json(['code'=>400,'msg'=>'用户不存在']); } }else{ return json(['code'=>400,'msg'=>'验证码失效']); } }else{ return json(['code'=>400,'msg'=>'验证码错误']); } } // return $this->view->fetch(); } /*验证码登录*/ public function codelogon(){ /*有就登录,没有就注册*/ if (Request::instance()->isPost()) { $data = Request::instance()->param(); $ck=Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->find(); if($ck){ if($ck['times']==0){ $cks=Db::name('pcnewuser')->where(array('phone'=>$data['phone']))->find(); Db::name('sms')->where(array('mobile'=>$data['phone'],'code'=>$data['code']))->setInc('times'); /*存redis*/ $redis=new Redis(); $token=Random::build('alnum',30); $redis->set('token'.$data['phone'],$token); if(!$cks){ // 没有就新建用户并且写redis并且登录成功,返回token跟手机号 Db::name('pcnewuser')->insert(array('phone'=>$data['phone'],'createtime'=>time())); } return json(['code'=>200,'msg'=>'登录成功','data'=>['token'=>$token,'phone'=>$data['phone']]]); }else{ return json(['code'=>400,'msg'=>'验证码失效']); } }else{ return json(['code'=>400,'msg'=>'验证码错误']); } } } /** * @return mixed|\think\response\Json * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * 登录 */ public function login(){ if (Request::instance()->isPost()) { $data = Request::instance()->param(); $cks=Db::name('pcnewuser')->where(array('phone'=>$data['phone'],'password'=>md5($data['password'])))->find(); if($cks){ $redis=new Redis(); $token=Random::build('alnum',30); $redis->set('token'.$data['phone'],$token); return json(['code'=>200,'msg'=>'登录成功','data'=>['token'=>$token,'phone'=>$data['phone']]]); }else{ /*新增一条用户信息*/ return json(['code'=>400,'msg'=>'账号或密码错误']); } } return $this->fetch(); } /*退出登录*/ public function logout(){ if (Request::instance()->isPost()) { $data = Request::instance()->param(); $phone=$data['phone']; $redis=new Redis(); $redis->rm('token'.$phone); return json(['code'=>200,'msg'=>'退出成功']); } } /*测试↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ public function testset(){ $redis=new Redis(); $token=Random::build('alnum',30); $redis->set('token18855662233',$token); } public function testget(){ $redis=new Redis(); $token=$redis->has('token18855662233'); if($token){ return $redis->get('token18855662233'); }else{ return '已过期'; } } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 5.底层代码改写记录  delete改成del ~~~php C:\phpStudy\PHPTutorial\WWW\numbersell\thinkphp\library\think\session\driver\Redis.php /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name) { return $this->handler->del($this->getCacheKey($name)); // return $this->handler->delete($this->getCacheKey($name)); } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 6.redis在config.php 里面的配置 ~~~php 'cache' => [ // 驱动方式 // 'type' => 'File', 'type' => '\think\cache\driver\Redis', // 缓存保存目录 'path' => CACHE_PATH, // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 0, ], ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 7.可以在这个地方调整redis失效时间,目前设置的是两个小时 ~~~php protected $options = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 7200, 'persistent' => false, 'prefix' => '', ]; ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 点击链接加入群聊【ThinkPHP56小功能】:[正在跳转](https://jq.qq.com/?_wv=1027&k=Z0c641WP "正在跳转")  ​