Browse Source

发送短信验证码接口2

master
wanghongjun 2 years ago
parent
commit
df0e398371
  1. 23
      app/controller/wechat/Wechat.php
  2. 60
      app/model/WechatSms.php
  3. 75
      app/util/AuthCodeUtil.php

23
app/controller/wechat/Wechat.php

@ -4,13 +4,14 @@ namespace app\controller\wechat;
use app\model\WechatPucode;
use app\model\WechatSms;
use app\model\WechatUser;
use app\service\user\SmsService;
use app\service\webService\FeeService;
use app\util\AuthCodeUtil;
use app\validate\WechatUser as WechatUserValidate;
use app\service\wechat\WechatService;
use app\util\ReturnCode;
use think\cache\driver\Redis;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;
@ -44,28 +45,20 @@ class Wechat extends Base
{
$param = $request->param();
$wechat_user_id = $request->wechat_user_id;
try {
validate(WechatUserValidate::class)->scene('smsCode')->check($param);
$create_time = WechatSms::getValidUser($wechat_user_id);
if ($create_time) {
$time = time() - $create_time;
throw new \Exception('请在' . $time . '秒后再次发送');
}
$AuthCodeUtil = new AuthCodeUtil(new Redis());
$code = WechatSms::getCode($wechat_user_id);
$addResult = WechatSms::addSmsCode($request->wechat_user_id,$code);
if (!$addResult) {
throw new \Exception('发送失败');
}
$AuthCodeUtil->setRate($param['mobile']);
$code = $AuthCodeUtil->setCode($param['mobile']);
$SmsService = new SmsService();
$SmsService->sendSms($param['mobile'],$SmsService->smsCodeTemplate($code));
return $this->buildSuccess();
return $this->buildSuccess([],'短信已发送');
} catch (\Exception $e) {
return $this->buildFailed(ReturnCode::INVALID,$e->getMessage());
}
@ -88,7 +81,7 @@ class Wechat extends Base
//$defaultSnsCode = '439764';
try {
validate(WechatUserValidate::class)->scene('savePuCode')->check($param);
$res = WechatSms::getValidCode($param['snsCode'],$wechat_user_id);
$res = (new AuthCodeUtil(new Redis()))->verifyCode($param['mobile'],$param['snsCode']);
if (!$res) {
throw new \Exception('短信验证码错误');
}

60
app/model/WechatSms.php

@ -1,60 +0,0 @@
<?php
namespace app\model;
use think\Model;
class WechatSms extends Model
{
/**
* @return int
*/
public static function getCode($wechat_user_id)
{
$code = rand(000000,999999);
$result = self::getValidCode($code,$wechat_user_id);
if ($result) {
return self::getCode($wechat_user_id);
}
return $code;
}
public static function getValidCode($code,$wechat_user_id)
{
$where = [
['code', '=', $code],
['wechat_user_id', '=', $wechat_user_id],
['create_time', '>=', time() - (10*60)],
];
return (new self())->where($where)->value('wechat_user_id');
}
public static function getValidUser($wechat_user_id)
{
$where = [
['wechat_user_id', '=', $wechat_user_id],
['create_time', '>=', time() - 60],
];
return (new self())->where($where)->value('create_time');
}
public static function addSmsCode($wechat_user_id,$code): bool
{
$data = [
'wechat_user_id' => $wechat_user_id,
'code' => $code,
'create_time' => time()
];
return (new self())->save($data);
}
}

75
app/util/AuthCodeUtil.php

@ -0,0 +1,75 @@
<?php
namespace app\util;
use fast\FuncException;
use think\facade\Cache;
class AuthCodeUtil
{
//redis连接
private $cache;
//验证码的保存秒数,600秒,即:10分钟
private $ttl = 600;
//构造
public function __construct($cache)
{
$this->cache = $cache;
}
//生成code保存到redis,并返回code
public function setCode($mobile)
{
$key = "Auth:" . $mobile;
$code = $this->newAuthCode();
$this->cache->set($key, $code, $this->ttl);
return $code;
}
/**
* 校对验证码
* @param $mobile
* @param $code
* @return bool
*/
public function verifyCode($mobile, $code): bool
{
$key = "Auth:" . $mobile;
$cacheCode = $this->cache->get($key);
if ($cacheCode == $code) {
return true;
} else {
return false;
}
}
/**
*
* @return int
*/
public function newAuthCode(): int
{
return rand(100000, 999999);
}
/**
* @param $mobile
* @return bool
* @throws FuncException
*/
public function setRate($mobile): bool
{
$key = "Auth:" . $mobile;
$cacheCode = $this->cache->get($key);
if ($cacheCode) {
$ttl = Cache::store('redis')->ttl($key);
if (($this->ttl - $ttl) < 60) {
throw new FuncException('请超过60秒之后再发送短信');
}
}
return true;
}
}
Loading…
Cancel
Save