Commit 956fbac3 by 王召彬

Merge branch 'ft-newSmscode'

parents 369fa719 fdc68b73
......@@ -9,7 +9,8 @@
],
"require": {
"qcloud/cos-sdk-v5": "^2.0",
"catfan/medoo": "^1.6"
"catfan/medoo": "^1.6",
"qcloudsms/qcloudsms_php": "0.1.*"
},
"autoload": {
"psr-4": {
......
<?php
namespace Hdll\Services\Common\Lib;
use Swoft\App;
use Hdll\Services\Common\Entity\User;
/**
* 手机验证码 工具类
* 发送验证码:
* $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号
* if($err) {
* throw .... // 错误处理
* }
* 校验验证码:
* $check = (new Smscode)->check($mobile,$smscode); // 前台传入的手机号和验证码
* if(!$check) {
* throw .... // 错误处理
* }
*
*/
class Smscode
{
protected $number;
public function __construct($number = null)
{
if($number !== null) {
$this->number = $number;
} else {
$this->number = App::getBean(User::class)->getId();
}
}
/**
* 发送验证码,可指定短信签名和使用哪个短信模块
*
* @param integer $mobile
* @param [type] $signName
* @param [type] $templateCode
* @return null|string 返回错误信息,正确时返回null
*/
public function send(int $mobile, $signName = null, $templateCode = null)
{
$key = $this->getKey($mobile);
$val = cache()->get($key);
if ($val) {
list(, $time, $count) = explode(',', $val);
if ($count >= 3) {
return '验证码发送次数超限,请稍后再试';
}
if (time() - $time < 60) {
return '发送验证码请求过于频繁';
}
$count += 1;
} else {
$count = 1;
}
$vcode = mt_rand(1000, 9999);
$alisms = new Alisms(
$signName ?? Alisms::SN_01,
$templateCode ?? Alisms::TPL_01,
['code' => $vcode]
);
$response = $alisms->send($mobile);
if ($response->Code == 'OK') {
$vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->number, $mobile);
$result = cache()->set($key, $vdata, 300); // 验证码有效期5分钟
if (!$result) {
return '验证码存储失败';
}
} else {
return '验证码发送失败:' . $response->Message;
}
}
/**
* 校验验证码
*
* @param integer $mobile
* @param integer $smscode
* @return bool 验证码正确时返回true
*/
public function check(int $mobile, int $smscode)
{
$key = $this->getKey($mobile);
$val = cache()->get($key);
if ($val) {
list($vcode,, ) = explode(',', $val);
if ($vcode == $smscode) {
return true;
}
}
return false;
}
private function getKey($mobile)
{
return 'smscode:' . md5($this->number . '|' . $mobile);
}
}
\ No newline at end of file
......@@ -2,10 +2,11 @@
namespace Hdll\Services\Common\Lib;
use Swoft\App;
use Qcloud\Sms\SmsSingleSender;
use Hdll\Services\Common\Entity\User;
/**
* 手机验证码 工具类
* (腾讯短信)手机验证码 工具类
* 发送验证码:
* $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号
* if($err) {
......@@ -21,15 +22,18 @@ use Hdll\Services\Common\Entity\User;
class Smscode
{
protected $number;
protected $uid;
protected $config;
public function __construct($number = null)
public function __construct($uid = null)
{
if($number !== null) {
$this->number = $number;
if($uid !== null) {
$this->uid = $uid;
} else {
$this->number = App::getBean(User::class)->getId();
$this->uid = App::getBean(User::class)->getId();
}
$commonConfigs = include dirname(__FILE__, 2).'/Config/config.php';
$this->config = $commonConfigs['tencent_sms'];
}
/**
......@@ -37,10 +41,10 @@ class Smscode
*
* @param integer $mobile
* @param [type] $signName
* @param [type] $templateCode
* @param [type] $templateId
* @return null|string 返回错误信息,正确时返回null
*/
public function send(int $mobile, $signName = null, $templateCode = null)
public function send(int $mobile, $signName='活动啦啦', $templateId=178822)
{
$key = $this->getKey($mobile);
......@@ -58,20 +62,22 @@ class Smscode
$count = 1;
}
$vcode = mt_rand(1000, 9999);
$alisms = new Alisms(
$signName ?? Alisms::SN_01,
$templateCode ?? Alisms::TPL_01,
['code' => $vcode]
);
$response = $alisms->send($mobile);
if ($response->Code == 'OK') {
$vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->number, $mobile);
$result = cache()->set($key, $vdata, 300); // 验证码有效期5分钟
if (!$result) {
return '验证码存储失败';
}
} else {
return '验证码发送失败:' . $response->Message;
$expireTime = 300; // 验证码有效期5分钟
$params = [
$vcode,
$expireTime / 60
];
$ssender = new SmsSingleSender($this->config['appid'], $this->config['appkey']);
$result = $ssender->sendWithParam("86", $mobile, $templateId, $params, $signName);
$result = json_decode($result, true);
if ($result['result'] !== 0) {
App::error("[发送腾讯云短信验证码失败]" . var_export($result, true) . "---param---{$mobile}," . json_encode($params));
return '服务商返回错误';
}
$vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->uid, $mobile);
$result = cache()->set($key, $vdata, $expireTime);
if (!$result) {
return '验证码存储失败';
}
}
......@@ -97,7 +103,7 @@ class Smscode
private function getKey($mobile)
{
return 'smscode:' . md5($this->number . '|' . $mobile);
return 'smscode:' . md5($this->uid . '|' . $mobile);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment