diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 74b46d2..6636045 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -29,6 +29,50 @@ 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' + else + flash[:danger] = "用户不存在" + redirect_to login_path + end + else + redirect_to login_path + end + else + result = check_change_params(params[:email], params[:verify_code], params[:password], params[:password_confirm]) + if result + flash[:success]="密码更新成功" + else + flash[:danger]="密码更新失败" + end + redirect_to login_path + end + + end + + private def user_params @@ -36,4 +80,32 @@ 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 + + def check_change_params(email, verify_code, password, password_confirmation) + if password != password_confirmation + return false + end + user = User.find_by(email: email) + if user.nil? + return false + end + if user.verify_code != verify_code + return false + end + hold = Hash.new + hold[:password] = password + hold[:password_confirmation] = password_confirmation + user.update_attributes(hold) + return true + end + end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 286b223..0937a52 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: 'from@example.com' + default from: "ucasguoren@163.com" layout 'mailer' end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb new file mode 100644 index 0000000..afb5a78 --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,9 @@ +class UserMailer < ApplicationMailer + default from: "ucasguoren@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..243d518 --- /dev/null +++ b/app/views/users/changepasswd.html.erb @@ -0,0 +1,47 @@ +<% provide(:title, '忘记密码') %> +<% provide(:body_class, 'login2') %> + +
+ + + <%= form_tag("/changepasswd", method: "post") do %> + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ + <%= submit_tag "重置密码", 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 @@