阿里大于发送短信 yii2扩展
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist zc/yii2-alisms "*"
or add
"zc/yii2-alisms": "*"
to the require section of your composer.json
file.
Once the extension is installed, simply use it in your code by :
<?= \zc\yii2Alisms\AutoloadExample::widget(); ?>
- 配置文件
'modules'=>[
"sms" => [
"class" => "zc\yii2Alisms\Module",
]
]
- 配置文件
'controllerMap' => [
'sms-api' => [
'class' => 'zc\yii2Alisms\controllers\ApiController',
'exitTime' => 300
],
]
-
发验证码api访问地址 http://example.dev/sms-api/get-code?mobile=15699999999&id=1&captcha=sdfwf 参数:
- mobile: 手机
- id:短信模板ID
- captcha:可选 ,在需要图形验证码的时候使用,是否需要图形验证码在后台设置
返回:
- status: true,
- code: "7025",
- hash1: "7025",原hash
- hash2: "7025",小写hash
- msg: "短信发送成功"
说明:
hash1、hash2可以用于客户端的验证,验证方法参考下面
/** 生成方法 * Generates a hash code that can be used for client-side validation. * @param string $code the CAPTCHA code * @return string a hash code generated from the CAPTCHA code * / public function generateValidationHash($code) { for ($h = 0, $i = strlen($code) - 1; $i >= 0; --$i) { $h += ord($code[$i]); } return $h; }
-
校验验证码api访问地址 (可以不用了) http://example.dev/sms-api/check-code?mobile=15699999999&code=1&id=1
- mobile: 手机
- id:短信模板ID
- code:验证码 校验验证码
在需要的model里面添加rule规则
['code', 'required'],
//['code', 'checkCode'],
['code', function ($attribute, $params) {
$smsType = 1;//跟前台访问验证码的id一致
if(!\zc\yii2Alisms\Sms::checkCode($this->mobile,$this->code,$smsType)){
$this->addError('code','手机验证码不正确');
return false;
}
}],