From 92821cfd87f691fb052b57523b7eb1b643ae4efb Mon Sep 17 00:00:00 2001 From: lovelyzhang Date: Mon, 2 Jan 2017 22:57:19 +0800 Subject: [PATCH 1/2] zy: add verify code --- app/controllers/users_controller.rb | 44 ++++++ app/mailers/user_mailer.rb | 9 ++ app/views/login/init.html.erb | 8 +- .../user_mailer/send_verify_code.html.erb | 25 +++ app/views/users/changepasswd.html.erb | 82 ++++++++++ app/views/users/edit.html.erb | 146 +++++++++--------- app/views/users/forgetpasswd.html.erb | 22 +++ config/application.rb | 10 ++ config/environments/development.rb | 11 ++ config/routes.rb | 2 + db/development.sqlite3 | Bin 40960 -> 40960 bytes ...20170102130110_add_verify_code_to_users.rb | 5 + db/schema.rb | 3 +- test/mailers/previews/user_mailer_preview.rb | 4 + test/mailers/user_mailer_test.rb | 7 + 15 files changed, 300 insertions(+), 78 deletions(-) create mode 100644 app/mailers/user_mailer.rb create mode 100644 app/views/user_mailer/send_verify_code.html.erb create mode 100644 app/views/users/changepasswd.html.erb create mode 100644 app/views/users/forgetpasswd.html.erb create mode 100644 db/migrate/20170102130110_add_verify_code_to_users.rb create mode 100644 test/mailers/previews/user_mailer_preview.rb create mode 100644 test/mailers/user_mailer_test.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 74b46d2..10e093a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -29,6 +29,39 @@ class UsersController < ApplicationController end + def forgetpasswd + useremail = params[:email] + if useremail + @user = User.find_by(email: useremail) + if !@user.nil? + # send_code @user + flash[:success] = "验证码已发送至您的邮箱" + redirect_to changepasswd_path(email: @user.email) + else + flash[:danger] = "用户不存在" + render 'forgetpasswd' + end + else + render 'forgetpasswd' + end + end + + def changepasswd + if request.get? + user_email = params[:email] + if user_email + @user = User.find_by(email: user_email) + if !@user.nil? + render 'changepasswd' + end + end + else + render html: "hello" + end + + end + + private def user_params @@ -36,4 +69,15 @@ class UsersController < ApplicationController :password_confirmation) end + def send_code user + verify_code = rand(999999).to_s + result = UserMailer.send_verify_code(user.email, verify_code).deliver_now + p result + User.transaction do + user.verify_code = verify_code + user.save + end + + end + end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..bf1e057 --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,9 @@ +class UserMailer < ApplicationMailer + default from: "zy_lovestar@163.com" + + def send_verify_code mail_address, code + @verify_code = code + mail(to: mail_address, subject: "验证码") + end + +end diff --git a/app/views/login/init.html.erb b/app/views/login/init.html.erb index 3a5f294..d21febf 100644 --- a/app/views/login/init.html.erb +++ b/app/views/login/init.html.erb @@ -8,8 +8,8 @@ <%= form_tag(controller: "login", action: "login", method: "post") do %>
-
@@ -22,9 +22,9 @@ type="password">
- 忘记密码? + 忘记密码?
- +
<% end %> diff --git a/app/views/user_mailer/send_verify_code.html.erb b/app/views/user_mailer/send_verify_code.html.erb new file mode 100644 index 0000000..1d07ece --- /dev/null +++ b/app/views/user_mailer/send_verify_code.html.erb @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + +
+ 您的验证码是: <%= @verify_code %> +
+ + + diff --git a/app/views/users/changepasswd.html.erb b/app/views/users/changepasswd.html.erb new file mode 100644 index 0000000..efe2b45 --- /dev/null +++ b/app/views/users/changepasswd.html.erb @@ -0,0 +1,82 @@ +<% provide(:title, '忘记密码') %> +<% provide(:body_class, 'login2') %> + +
+ + + <%= form_for @user, url: {action: "changepasswd"} do |f| %> + <%= render 'shared/error_messages', object: f.object %> + +
+
+ + <%= text_field_tag("verif_code", class: 'form-control', placeholder: "请输入邮箱验证码") %> +
+
+ +
+
+ + <%= f.email_field :email, class: 'form-control', placeholder: "请输入邮箱" %> +
+
+ +
+
+ + <%= f.password_field :password, class: 'form-control', placeholder: "请输入密码" %> +
+
+ +
+
+ + <%= f.password_field :password_confirmation, class: 'form-control', placeholder: "请再次输入密码" %> +
+
+ + <%= f.submit "重置密码", class: "btn btn-primary btn-block" %> + <% end %> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index a33533e..23a14d4 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -2,33 +2,33 @@