public function validate($model) { $mobileField = $this->getOption('mobileField') ?: 'mobile'; $captchaField = $this->getOption('captchaField') ?: 'captcha'; if (!Verification::factory($model->{$mobileField}, 'sms', 'new_mobile')->verify($model->{$captchaField})) { $messageStr = $this->getOption('message') ?: '手机号验证码不正确'; $this->appendMessage($messageStr, $captchaField, 'MobileCaptcha'); return false; } return true; }
/** * 修改手机号 * * @param string $newMobile 新手机号 * @param string $newCaptcha 新手机号验证码 * @param string $oldCaptcha 旧手机号验证码 * @return bool * @throws Exception\InvalidArgumentException * @throws Exception\ResourceConflictException */ public function changeMobile($newMobile, $newCaptcha, $oldCaptcha = null) { if (!Verification::factory($newMobile, 'sms', 'new_mobile')->verify($newCaptcha)) { throw new InvalidArgumentException('新手机号码验证码不正确'); } if (self::checkMobileExistence($newMobile, $this->id)) { throw new Exception\ResourceConflictException('ERR_MOBILE_HAS_BEEN_TAKEN'); } // 如果之前的手机号码未激活,在现在是激活手机号的操作; if ($this->mobileStatus == 'inactive') { $this->mobile = $newMobile; $this->mobileStatus = 'active'; return $this->save(); // 已激活的手机号修改必须先验证原来的手机验证码 } else { if (!Verification::factory($this->mobile, 'sms')->verify($oldCaptcha)) { throw new InvalidArgumentException('当前手机号码验证码不正确'); } $this->mobile = $newMobile; $this->mobileStatus = 'active'; return $this->save(); } }