<?php
namespace app\modules\api\models;
use app\models\enum\CacheKeyEnum;
use app\models\Option;
use app\models\SmsSetting;
use Opening\Sms\Exceptions\SmsException;
use Opening\Sms\Messages\VerificationCodeMessage;
use Opening\Sms\Senders\AliyunSender;
class SmsForm extends ApiModel
{
public $mobile;
public $sms_code;
public $store_id = 0;
const SMS_CODE = 'SMS_CODE';
const SMS_CODE_VALIDATE_COUNT = 'SMS_CODE_VALIDATE_COUNT';
public function rules()
{
return [
[['sms_code',], 'trim'],
[['sms_code'], 'required'],
/*[['sms_code',], function ($attribute, $params) {
//校验用户验证短信验证码的次数
$max_validate_count = 10;
$validate_count = \Yii::$app->session->get(CacheKeyEnum::SMS_CODE. $this->mobile, 0);
if ($validate_count >= $max_validate_count) {
$this->addError($attribute, '您输入的短信验证码错误次数过多,请刷新页后面重试。');
return;
}
}],*/
[['sms_code',], function ($attribute, $params) {
//校验用户的短信验证码
$sms_code = \Yii::$app->cache->get(CacheKeyEnum::SMS_CODE. $this->mobile);
if (!$sms_code) {
$this->addError($attribute, '未查询到验证码发送记录,请刷新页后面重试。');
return;
}
if (strval($this->attributes[$attribute]) !== strval($sms_code)) {
$this->addError($attribute, '短信验证码错误。');
return;
}
}],
];
}
/**
* 发送短信验证码
* @return array|bool
*/
public function sendCode()
{
$sms_code = mt_rand(100000, 999999);
//获取商户短信配置
$smsSetting = SmsSetting::findOne(['is_delete' => 0, 'store_id' => $this->store_id]);
if ($smsSetting) {
$tpl = json_decode($smsSetting->tpl_code, true);
$ind_sms['aliyun'] = [
'access_key_id' => $smsSetting->AccessKeyId,
'access_key_secret' => $smsSetting->AccessKeySecret,
'sign' => $smsSetting->sign,
'tpl_id' => $tpl['tpl'],
];
if (!is_array($tpl) || !$tpl['tpl']) {
$ind_sms = Option::get('ind_sms', 0, 'admin');
}
}else{
//获取总后台短信配置
$ind_sms = Option::get('ind_sms', 0, 'admin');
}
if (!$ind_sms) {
return [
'code' => 1,
'msg' => '发送失败,短信尚未配置。',
];
}
if (!$ind_sms['aliyun'] || !$ind_sms['aliyun']['access_key_id'] || !$ind_sms['aliyun']['access_key_secret'] || !$ind_sms['aliyun']['sign'] || !$ind_sms['aliyun']['tpl_id']) {
return [
'code' => 1,
'msg' => '发送失败,短信尚未配置。',
];
}
$limitKey = CacheKeyEnum::SMS_LIMIT. $this->mobile. date('d');
$codeLimit = \Yii::$app->cache->get($limitKey);
if ($codeLimit >= 5) {
return [
'code' => 1,
'msg' => '每个手机号每天最多能发5条短信',
];
}
$sender = new AliyunSender($ind_sms['aliyun']['access_key_id'], $ind_sms['aliyun']['access_key_secret']);
$message = new VerificationCodeMessage([
'sender' => $sender,
'sign' => $ind_sms['aliyun']['sign'],
'tplId' => $ind_sms['aliyun']['tpl_id'],
'tplParams' => [
'code' => $sms_code,
],
'phoneNumber' => $this->mobile,
]);
try {
$message->send();
} catch (SmsException $e) {
return [
'code' => 1,
'msg' => '发送失败,' . $e->getMessage(),
];
}
\Yii::$app->cache->set(CacheKeyEnum::SMS_CODE. $this->mobile, $sms_code, 300);
\Yii::$app->cache->set($limitKey, $codeLimit + 1, 86400);
return [
'code' => 0,
'msg' => 'success'
];
}
/**
* 认证手机号短信验证码
* @return \app\opening\ValidationErrorResponse
*/
public function checkSmsCode()
{
if (!$this->validate()) {
return $this->getErrorResponse();
}
\Yii::$app->cache->delete(CacheKeyEnum::SMS_CODE. $this->mobile);
}
}