68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Constants\HttpEnumCode;
|
|
use Extend\Alibaba\Dysms;
|
|
use Hyperf\Redis\Redis;
|
|
use Hyperf\Snowflake\IdGeneratorInterface;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
/**
|
|
* 验证码
|
|
*/
|
|
class CodeService extends BaseService
|
|
{
|
|
/**
|
|
* 获取登陆验证码
|
|
* 5分钟内同一手机号请求三次
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function getLoginPhoneCode(): array
|
|
{
|
|
$phone = $this->request->input('phone');
|
|
|
|
$redis = $this->container->get(Redis::class);
|
|
|
|
$redis_key = "login_code_count" . $phone;
|
|
$result = $redis->get($redis_key);
|
|
if (!empty($result)){
|
|
if ( $result > 3){
|
|
// 超出规定时间内最大获取次数
|
|
return fail(HttpEnumCode::PHONE_MAXIMUM);
|
|
}
|
|
}
|
|
|
|
$generator = $this->container->get(IdGeneratorInterface::class);
|
|
|
|
$template_code = "SMS_243055263";
|
|
|
|
$template_param = array();
|
|
$template_param['code'] = (int)substr($generator->generate(),-4);
|
|
|
|
// 发送短信
|
|
Dysms::sendSms($phone,$template_param,$template_code,1);
|
|
|
|
if (empty($result)){
|
|
$redis_value = 1;
|
|
}else{
|
|
$redis_value = $result + 1;
|
|
}
|
|
|
|
$res = $redis->set($redis_key,$redis_value,60*5);
|
|
if (!$res){
|
|
return fail(HttpEnumCode::SERVER_ERROR);
|
|
}
|
|
|
|
// 设置有效期
|
|
$res = $redis->set("login_code" . $phone,$template_param['code'],60*30);
|
|
if (!$res){
|
|
return fail(HttpEnumCode::SERVER_ERROR);
|
|
}
|
|
|
|
return success();
|
|
}
|
|
} |