Commit 85425b05 by 王召彬

腾讯云短信验证码

parent b3f9579a
......@@ -2,6 +2,7 @@
namespace Hdll\Services\Common\Lib;
use Swoft\App;
use Qcloud\Sms\SmsSingleSender;
use Hdll\Services\Common\Entity\User;
/**
......@@ -22,6 +23,7 @@ class TencSmscode
{
protected $uid;
protected $config;
public function __construct($uid = null)
{
......@@ -30,6 +32,8 @@ class TencSmscode
} else {
$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 TencSmscode
*
* @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 = null, $templateId = null)
{
$key = $this->getKey($mobile);
......@@ -58,8 +62,23 @@ class TencSmscode
$count = 1;
}
$vcode = mt_rand(1000, 9999);
$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 '验证码存储失败';
}
}
/**
......
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