merge mail

This commit is contained in:
lovelyzhang 2017-01-05 22:13:58 +08:00
commit 9bf238a96e
17 changed files with 328 additions and 79 deletions

View File

@ -29,6 +29,50 @@ class UsersController < ApplicationController
end 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 private
def user_params def user_params
@ -36,4 +80,32 @@ class UsersController < ApplicationController
:password_confirmation) :password_confirmation)
end 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 end

View File

@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com' default from: "ucasguoren@163.com"
layout 'mailer' layout 'mailer'
end end

View File

@ -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

View File

@ -8,8 +8,8 @@
<%= form_tag(controller: "login", action: "login", method: "post") do %> <%= form_tag(controller: "login", action: "login", method: "post") do %>
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span><input class="form-control" <span class="input-group-addon"><i class="icon-user"></i></span><input class="form-control"
placeholder="请输入登陆账号" placeholder="请输入登陆邮箱"
name="email" name="email"
type="text"> type="text">
</div> </div>
@ -22,7 +22,7 @@
type="password"> type="password">
</div> </div>
</div> </div>
<a class="pull-right" href="#">忘记密码?</a> <a class="pull-right" href="/forgetpasswd">忘记密码?</a>
<div class="text-left"> <div class="text-left">
<label class="checkbox"><input type="checkbox" name="remember"><span>记住我</span></label> <label class="checkbox"><input type="checkbox" name="remember"><span>记住我</span></label>
</div> </div>

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<!-- 可选的Bootstrap主题文件一般不用引入 -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
您的验证码是: <%= @verify_code %>
</div>
</body>
</html>

View File

@ -0,0 +1,47 @@
<% provide(:title, '忘记密码') %>
<% provide(:body_class, 'login2') %>
<div class="login-wrapper">
<a href="#"><img width="210" height="70" src="images/logo/guoren.png"/></a>
<%= form_tag("/changepasswd", method: "post") do %>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span><input class="form-control"
placeholder="请输入邮箱地址"
name="email"
type="email">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope-alt"></i></span><input class="form-control"
placeholder="请输入邮箱验证码"
name="verify_code"
type="number">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"
placeholder="请输入新密码"
name="password"
type="password">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"
placeholder="请再次输入新密码"
name="password_confirm"
type="password">
</div>
</div>
<%= submit_tag "重置密码", class: "btn btn-primary btn-block" %>
<% end %>
</div>

View File

@ -0,0 +1,22 @@
<% provide(:title, '忘记密码') %>
<% provide(:body_class, 'login2') %>
<div class="login-wrapper">
<a href="#"><img width="210" height="70" src="images/logo/guoren.png"/></a>
<%= form_tag("/forgetpasswd", method: "get") do %>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span><input class="form-control"
placeholder="请输入邮箱地址"
name="email"
type="email">
</div>
</div>
<%= submit_tag "找回密码", class: "btn btn-primary btn-block" %>
<% end %>
</div>

33
code.bak Normal file
View File

@ -0,0 +1,33 @@
<%= form_for @user, url: {action: "changepasswd"} do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock"></i></span>
<input class="form-control" placeholder="请输入邮箱验证码" name="verify_code" type="text">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span>
<%= f.email_field :email, class: 'form-control', placeholder: "请输入邮箱" %>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password, class: 'form-control', placeholder: "请输入密码" %>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: "请再次输入密码" %>
</div>
</div>
<%= f.submit "重置密码", class: "btn btn-primary btn-block" %>
<% end %>

View File

@ -12,5 +12,15 @@ module GuorenPro
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.
config.time_zone = 'Beijing' config.time_zone = 'Beijing'
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => 25,
:user_name => "ucasguoren@163.com",
:password => "ucas123",
:authentication => "plain",
:enable_starttls_auto => true
}
end end
end end

View File

@ -51,4 +51,15 @@ Rails.application.configure do
# Use an evented file watcher to asynchronously detect changes in source code, # Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem. # routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => 25,
:user_name => "ucasguoren@163.com",
:password => "ucas123",
:authentication => "plain",
:enable_starttls_auto => true
}
end end

View File

@ -8,6 +8,9 @@ Rails.application.routes.draw do
get '/signup', to: 'users#new' get '/signup', to: 'users#new'
get '/profile', to: 'users#edit' get '/profile', to: 'users#edit'
get '/forgetpasswd', to: 'users#forgetpasswd'
get '/changepasswd', to: 'users#changepasswd'
post '/changepasswd', to: 'users#changepasswd'
get '/main', to: 'main#show' get '/main', to: 'main#show'
get '/activity', to: 'main#activity' get '/activity', to: 'main#activity'

Binary file not shown.

View File

@ -0,0 +1,5 @@
class AddVerifyCodeToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :verify_code, :string
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161227034933) do ActiveRecord::Schema.define(version: 20170102130110) do
create_table "comments", force: :cascade do |t| create_table "comments", force: :cascade do |t|
t.string "content" t.string "content"
@ -54,6 +54,7 @@ ActiveRecord::Schema.define(version: 20161227034933) do
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "password_digest" t.string "password_digest"
t.string "verify_code"
end end
end end

View File

@ -0,0 +1,4 @@
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class UserMailerTest < ActionMailer::TestCase
# test "the truth" do
# assert true
# end
end