21/05
2018
Wallogit.com
2017 © Pedro Peláez
阿里大于发送短信 yii2扩展
阿里大于发送短信 yii2扩展, (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist zc/yii2-alisms "*"
or add, (*4)
"zc/yii2-alisms": "*"
to the require section of your composer.json file., (*5)
Once the extension is installed, simply use it in your code by :, (*6)
<?= \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 参数:, (*7)
返回:, (*8)
说明:
hash1、hash2可以用于客户端的验证,验证方法参考下面, (*9)
/** 生成方法
* 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, (*10)
在需要的model里面添加rule规则, (*11)
['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;
}
}],