?
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]);
}
}
}
~~~

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'=>'驗證碼發送失敗']);
}
}
~~~

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 '已過期';
}
}
}
~~~

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));
}
~~~

6.redis在config.php 里面的配置
~~~php
'cache' => [
// 驅動方式
// 'type' => 'File',
'type' => '\think\cache\driver\Redis',
// 緩存保存目錄
'path' => CACHE_PATH,
// 緩存前綴
'prefix' => '',
// 緩存有效期 0表示永久緩存
'expire' => 0,
],
~~~

7.可以在這個地方調整redis失效時間,目前設置的是兩個小時
~~~php
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'expire' => 7200,
'persistent' => false,
'prefix' => '',
];
~~~

點擊鏈接加入群聊【ThinkPHP56小功能】:[正在跳轉](https://jq.qq.com/?_wv=1027&k=Z0c641WP "正在跳轉")?
?
- 支付寶身份驗證接口踩坑實錄-PHP(基于ThinkPHP5)(第二版更新中)
- 抖音小程序開發之授權登錄+支付寶支付+微信支付(ThinkPHP5-第三版修訂中)
- TP5小知識點錦集(長期更新)
- PHP 二維碼生成+識別
- 高德地圖點聚合點擊事件以及內容渲染
- ThinkPhP5使用phpexcle 導出數據(復制粘貼就可使用)
- Fastadmin微信小程序授權登錄+獲取手機號插件
- PHP -AES-128-CBC位加密解密
- PHP-Rsa分段加密解密(ThinkPHP5)
- PHP大轉盤抽獎代碼片段
- Fastadmin 項目上線關閉調試模式注意事項(記一次require-table.js修改事件)
- ThinkPHP5條件查詢FIND_IN_SET正反使用
- ThinkPhP5整合微信小程序訂閱消息
- think-queue處理延時任務事件
- ThinkPHP5 生成二維碼
- Python3定時監控指定文件內容變換-(增加多行,遍歷每行進行邏輯分析處理)
- Python3開發聲光報警器監控觸發報警
- ThinkPHP5下載文件流到本地
- 百度鷹眼抽軌跡集合稀算法&縮放比例調整顯示靜態圖(ThinkPHP5)
- PHP 導出Excle
- Fastadmin 自定義Tab選項卡(B表的條件查詢A表的數據,在A表里面加B表的參數作為選項卡)
- Fastadmin 修改url組件跳轉為復制功能
- 微信H5分享好友跟朋友圈-基于Easywechat
- Python3抓取監控日志文件關鍵詞跟內容變化修正版
- ThinkPHP5上傳圖片壓縮處理-(解決IOS拍照上傳旋轉90度問題)最近更新2021年12月9日11:35:07
- 二維數組根據‘key’分組為新的三維數組
- ThinkPHP5 成功部署Workerman 運行示例
- Fastadmin框架TOKEN的使用
- ThinkPHP5 -微信小程序訂閱消息開發-插件(插件基于fastadmin開發)
- ThinkPHP5-文本轉義emoji表情
- ThinkPHP5 自定義命令行處理 監聽Redis key失效觸發回調事件,實現延遲任務 實現自動取消訂單,自動完成訂單
- Fastadmin插件Shopro商城里面短信插件修改為騰訊云短信插件步驟
- Fastadmin框架自定義搜索操作流程
- ThinkPHP5 處理 微信小程序內容安全審核
- Fastadmin自定義快捷搜索之模糊搜索關聯他表
- php根據年月獲取指定月份天數及日期數組的方法
- PHP構造函數使用校驗token
- 基于ThinkPHP5&Redis騰訊云短信驗證碼注冊登錄基礎業務邏輯代碼整合
- ThinkPHP 解決跨域問題
- 支付寶沙箱環境測試支付(好久沒做都忘了,寫個博客比較省事)
- ThinkPHP5生成抖音小程序帶參數二維碼
- ThinkPHP5導入Excle-簡單絲滑
- PHP生成帶參數的小程序二維碼
- ThinkPHP5成功調通IOS蘋果支付
- swoole寫聊天室,簡單粗暴
- 微信小程序內容安全鑒別的時候,不成功因為沒有轉碼
- Fastadmin 后臺Excle文件上傳(更新新增功能)
- Lnmp 配置thinkphp5 Nginx基礎設置,包含http+https配置
- 通過經緯度獲取數據庫信息自動計算地址距離遠近
- 二維數組根據某個字段排序
- PHP二維數組去重,最簡單的方法
- TP5微信redis列隊群發模板消息Sendmsg
- PHP檢測是否關注公眾號,親測可用
- 小程序推廣分享帶參數二維碼生成
- 基于ThinkPHP5微信H5授權登錄獲取用戶信息(改進版)
- php過濾微信昵稱中的表情
- Socket.io