Commit 1798496c by 王召彬

发送腾讯云验证码

parent 85425b05
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
namespace Hdll\Services\Common\Lib; namespace Hdll\Services\Common\Lib;
use Swoft\App; use Swoft\App;
use Qcloud\Sms\SmsSingleSender;
use Hdll\Services\Common\Entity\User; use Hdll\Services\Common\Entity\User;
/** /**
* (阿里短信)手机验证码 工具类 * (腾讯短信)手机验证码 工具类
* 发送验证码: * 发送验证码:
* $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号 * $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号
* if($err) { * if($err) {
...@@ -21,15 +22,18 @@ use Hdll\Services\Common\Entity\User; ...@@ -21,15 +22,18 @@ use Hdll\Services\Common\Entity\User;
class Smscode class Smscode
{ {
protected $number; protected $uid;
protected $config;
public function __construct($number = null) public function __construct($uid = null)
{ {
if($number !== null) { if($uid !== null) {
$this->number = $number; $this->uid = $uid;
} else { } 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 ...@@ -37,10 +41,10 @@ class Smscode
* *
* @param integer $mobile * @param integer $mobile
* @param [type] $signName * @param [type] $signName
* @param [type] $templateCode * @param [type] $templateId
* @return null|string 返回错误信息,正确时返回null * @return null|string 返回错误信息,正确时返回null
*/ */
public function send(int $mobile, $signName = null, $templateCode = null) public function send(int $mobile, $signName = null, $templateId = null)
{ {
$key = $this->getKey($mobile); $key = $this->getKey($mobile);
...@@ -58,20 +62,22 @@ class Smscode ...@@ -58,20 +62,22 @@ class Smscode
$count = 1; $count = 1;
} }
$vcode = mt_rand(1000, 9999); $vcode = mt_rand(1000, 9999);
$alisms = new Alisms( $expireTime = 300; // 验证码有效期5分钟
$signName ?? Alisms::SN_01, $params = [
$templateCode ?? Alisms::TPL_01, $vcode,
['code' => $vcode] $expireTime / 60
); ];
$response = $alisms->send($mobile); $ssender = new SmsSingleSender($this->config['appid'], $this->config['appkey']);
if ($response->Code == 'OK') { $result = $ssender->sendWithParam("86", $mobile, $templateId, $params, $signName);
$vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->number, $mobile); $result = json_decode($result, true);
$result = cache()->set($key, $vdata, 300); // 验证码有效期5分钟 if ($result['result'] !== 0) {
if (!$result) { App::error("[发送腾讯云短信验证码失败]" . var_export($result, true) . "---param---{$mobile}," . json_encode($params));
return '验证码存储失败'; return '服务商返回错误';
} }
} else { $vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->uid, $mobile);
return '验证码发送失败:' . $response->Message; $result = cache()->set($key, $vdata, $expireTime);
if (!$result) {
return '验证码存储失败';
} }
} }
...@@ -97,7 +103,7 @@ class Smscode ...@@ -97,7 +103,7 @@ class Smscode
private function getKey($mobile) private function getKey($mobile)
{ {
return 'smscode:' . md5($this->number . '|' . $mobile); return 'smscode:' . md5($this->uid . '|' . $mobile);
} }
} }
\ No newline at end of file
...@@ -2,11 +2,10 @@ ...@@ -2,11 +2,10 @@
namespace Hdll\Services\Common\Lib; namespace Hdll\Services\Common\Lib;
use Swoft\App; use Swoft\App;
use Qcloud\Sms\SmsSingleSender;
use Hdll\Services\Common\Entity\User; use Hdll\Services\Common\Entity\User;
/** /**
* (腾讯短信)手机验证码 工具类 * 手机验证码 工具类
* 发送验证码: * 发送验证码:
* $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号 * $err = (new Smscode)->send($mobile); // $mobile 接收验证码的手机号
* if($err) { * if($err) {
...@@ -19,21 +18,18 @@ use Hdll\Services\Common\Entity\User; ...@@ -19,21 +18,18 @@ use Hdll\Services\Common\Entity\User;
* } * }
* *
*/ */
class TencSmscode class Smscode
{ {
protected $uid; protected $number;
protected $config;
public function __construct($uid = null) public function __construct($number = null)
{ {
if($uid !== null) { if($number !== null) {
$this->uid = $uid; $this->number = $number;
} else { } else {
$this->uid = App::getBean(User::class)->getId(); $this->number = App::getBean(User::class)->getId();
} }
$commonConfigs = include dirname(__FILE__, 2).'/Config/config.php';
$this->config = $commonConfigs['tencent_sms'];
} }
/** /**
...@@ -41,10 +37,10 @@ class TencSmscode ...@@ -41,10 +37,10 @@ class TencSmscode
* *
* @param integer $mobile * @param integer $mobile
* @param [type] $signName * @param [type] $signName
* @param [type] $templateId * @param [type] $templateCode
* @return null|string 返回错误信息,正确时返回null * @return null|string 返回错误信息,正确时返回null
*/ */
public function send(int $mobile, $signName = null, $templateId = null) public function send(int $mobile, $signName = null, $templateCode = null)
{ {
$key = $this->getKey($mobile); $key = $this->getKey($mobile);
...@@ -62,22 +58,20 @@ class TencSmscode ...@@ -62,22 +58,20 @@ class TencSmscode
$count = 1; $count = 1;
} }
$vcode = mt_rand(1000, 9999); $vcode = mt_rand(1000, 9999);
$expireTime = 300; // 验证码有效期5分钟 $alisms = new Alisms(
$params = [ $signName ?? Alisms::SN_01,
$vcode, $templateCode ?? Alisms::TPL_01,
$expireTime / 60 ['code' => $vcode]
]; );
$ssender = new SmsSingleSender($this->config['appid'], $this->config['appkey']); $response = $alisms->send($mobile);
$result = $ssender->sendWithParam("86", $mobile, $templateId, $params, $signName); if ($response->Code == 'OK') {
$result = json_decode($result, true); $vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->number, $mobile);
if ($result['result'] !== 0) { $result = cache()->set($key, $vdata, 300); // 验证码有效期5分钟
App::error("[发送腾讯云短信验证码失败]" . var_export($result, true) . "---param---{$mobile}," . json_encode($params)); if (!$result) {
return '服务商返回错误'; return '验证码存储失败';
} }
$vdata = sprintf("%d,%d,%d,%d,%d", $vcode, time(), $count, $this->uid, $mobile); } else {
$result = cache()->set($key, $vdata, $expireTime); return '验证码发送失败:' . $response->Message;
if (!$result) {
return '验证码存储失败';
} }
} }
...@@ -103,7 +97,7 @@ class TencSmscode ...@@ -103,7 +97,7 @@ class TencSmscode
private function getKey($mobile) private function getKey($mobile)
{ {
return 'smscode:' . md5($this->uid . '|' . $mobile); return 'smscode:' . md5($this->number . '|' . $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