/** * 忘了密码 */ public function forgot() { if ($this->validate()) { $passwordReminder = new PasswordReminder(); $passwordReminder->email = $this->email; $passwordReminder->token = Str::random(40); $passwordReminder->created_at = new Carbon(); $passwordReminder->save(false); Yii::$app->mail->compose('forgotpassword', ['token' => $passwordReminder->token])->setTo($this->email)->setSubject(Yii::$app->id . ' 重置密码邮件')->send(); return true; } return false; }
/** * 重置密码 * @param $token * @return string * @throws \yii\web\HttpException */ public function actionResetPassword($token) { $passwordReminder = PasswordReminder::findOne(['token' => $token]); if (is_null($passwordReminder)) { throw new NotFoundHttpException('请求页面不存在'); } $model = new SignUpForm(); $model->scenario = 'reset'; if ($model->load(Yii::$app->request->post()) && $model->resetPassword()) { //清掉$passwordReminder $passwordReminder->delete(); unset($passwordReminder); return $this->goHome(); } return $this->render('reset-password', ['model' => $model]); }