/**
  * 刪除逾期的EmailCheckCode
  * 3小時跑一次
  */
 public function actionClearEmailCheckcode()
 {
     $expireTime = time() - Yii::$app->params["checkCodeExpired"];
     \common\models\entities\EmailCheckCodes::deleteAll("createtime < {$expireTime}");
     echo Yii::$app->controller->action->id . " Done!! \n\r";
     exit;
 }
 public function actionForgetPwdConfirm($checkCode)
 {
     if (isset($checkCode) && !empty($checkCode)) {
         $emailModel = EmailCheckCodes::findOne(array("type" => EmailCheckCodes::TYPE_FORGET_PASSWORD, "check_code" => $checkCode));
         if (!$emailModel) {
             throw new \yii\web\HttpException(400, "認證碼錯誤", $this->errorLayout);
         } else {
             $model = Member::findOne(array('id' => $emailModel->member_id));
             if (!$model) {
                 throw new \yii\web\HttpException(400, "查無對應帳號", $this->errorLayout);
             }
             $model->password = md5($emailModel->other);
             if (!$model->save()) {
                 throw new \yii\web\HttpException(400, Yii::$app->tool->formatErrorMsg($model->getErrors()), $this->errorLayout);
             } else {
                 EmailCheckCodes::deleteAll("member_id = '{$emailModel->member_id}'");
             }
         }
     } else {
         throw new \yii\web\HttpException(400, "認證碼錯誤", $this->errorLayout);
     }
     $this->layout = false;
     Yii::$app->getSession()->setFlash('alert', "您的密碼已更新, 請使用新密碼進行登入");
     return $this->redirect(["user/login"]);
 }
 public function sendUserEmailCheckCode($user, $type = "regist", $other = null)
 {
     $emailModel = new \common\models\entities\EmailCheckCodes();
     $checkCode = $this->generatorRandomString();
     $emailModel->deleteAll("member_id = '{$user->id}'");
     $emailModel->attributes = array("member_id" => $user->id, "check_code" => $checkCode, 'email' => $user->email, 'type' => $type, 'other' => $other);
     if (!$emailModel->save()) {
         throw new \yii\web\HttpException(400, $this->formatErrorMsg($emailModel->getErrors()), Yii::$app->controller->errorLayout);
     }
     if ($type == EmailCheckCodes::TYPE_REGIST) {
         $subject = Yii::$app->params["mailCheckCodeTitle"]["register"];
         $view = "emailcheck";
         $url = $this->toBaseUrl(["user/regist-email-confirm", "checkCode" => $checkCode], true);
     } elseif ($type == EmailCheckCodes::TYPE_FORGET_PASSWORD) {
         $subject = Yii::$app->params["mailCheckCodeTitle"]["forgetPwd"];
         $view = "emailforgetpwd";
         $url = $this->toBaseUrl(["user/forget-pwd-confirm", "checkCode" => $checkCode], true);
     } else {
         throw new \yii\web\HttpException(400, "認證碼類型有誤", Yii::$app->controller->errorLayout);
     }
     return $this->sendMail($user->email, $user->email, $subject, array("user" => $user, "url" => $url, "other" => $other), $view);
 }