/**
  * Send mobile captcha.
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/mobile/send-captcha<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for send mobile captcha.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     mobile: string, phone number<br/>
  *     unionId: string<br/>
  *     language: 'zh_cn' or 'en_us', This param is just for update mobile<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     message: OK or Fail
  *     data: string, if success, It is verification code<br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *  "message": "OK",
  *  "data": "456787"
  * }
  * </pre>
  */
 public function actionSendCaptcha()
 {
     $params = $this->getParams();
     if (empty($params['type']) || empty($params['mobile']) || empty($params['codeId']) || empty($params['code'])) {
         throw new BadRequestHttpException('Missing params');
     }
     $type = $params['type'];
     $mobile = $params['mobile'];
     if (in_array($type, [self::CAPTCHA_TYPE_COMPANY_INFO, self::CAPTCHA_TYPE_EXCHANGE])) {
         $params['accountId'] = $this->getAccountId();
     } else {
         if (!in_array($type, [self::CAPTCHA_TYPE_BIND, self::CAPTCHA_TYPE_SIGNUP])) {
             throw new BadRequestHttpException('Invalid type');
         }
     }
     $this->attachBehavior('CaptchaBehavior', new CaptchaBehavior());
     $companyInfo = $this->{$type}($params);
     $company = $companyInfo['company'] === null ? self::DEFAULT_COMPANY : $companyInfo['company'];
     $accountId = $companyInfo['accountId'];
     //limit captcha send by ip
     $ip = Yii::$app->request->userIp;
     $captcha = Captcha::getByIP($ip);
     $now = time();
     if (!empty($captcha)) {
         $sendTimeInt = MongodbUtil::MongoDate2TimeStamp($captcha->createdAt);
         $nextTime = $sendTimeInt + Yii::$app->params['captcha_send_interval'];
         if ($nextTime > $now) {
             throw new InvalidParameterException(['phone' => Yii::t('common', 'send_too_frequently')]);
         } else {
             $captcha->isExpired = true;
             $captcha->save();
         }
     }
     //get random string, length = 6, charlist = '0123456789'
     $code = StringUtil::rndString(6, 0, '0123456789');
     $text = str_replace('#code#', $code, Yii::$app->params['mobile_message_text']);
     $text = str_replace('#company#', $company, $text);
     $captcha = new Captcha();
     $captcha->ip = $ip;
     $captcha->code = $code;
     $captcha->mobile = $mobile;
     $captcha->isExpired = false;
     $captcha->accountId = $accountId;
     if (MessageUtil::sendMobileMessage($mobile, $text) && $captcha->save()) {
         MessageUtil::recoreMessageCount('omni_record_message_' . $type);
         $result = ['message' => 'OK', 'data' => ''];
     } else {
         $result = ['message' => 'Error', 'data' => 'unknow error'];
     }
     return $result;
 }
 /**
  * Check message captch
  * @param string $mobile
  * @param string $code
  * @throws InvalidParameterException
  */
 public function checkCaptcha($mobile, $code)
 {
     $now = time();
     //get available captcha by mobile
     $captcha = Captcha::getByMobile($mobile);
     if (!empty($captcha)) {
         $sendTimeInt = MongodbUtil::MongoDate2TimeStamp($captcha->createdAt);
         $availabTime = $sendTimeInt + Yii::$app->params['captcha_availab_time'];
         if ($captcha['code'] != $code) {
             throw new InvalidParameterException(['captcha' => Yii::t('common', 'captcha_error')]);
         }
         $captcha->isExpired = true;
         $captcha->save(true, ['isExpired']);
         if ($now > $availabTime) {
             throw new InvalidParameterException(['captcha' => Yii::t('common', 'captcha_expired')]);
         }
     } else {
         throw new InvalidParameterException(['phone' => Yii::t('common', 'mobile_error')]);
     }
 }