zy: add verify code

This commit is contained in:
lovelyzhang 2017-01-02 22:57:19 +08:00
parent 2c21dc9142
commit 92821cfd87
15 changed files with 300 additions and 78 deletions

View File

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

View File

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

View File

@ -8,8 +8,8 @@
<%= form_tag(controller: "login", action: "login", 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="请输入登陆账号"
<span class="input-group-addon"><i class="icon-user"></i></span><input class="form-control"
placeholder="请输入登陆邮箱"
name="email"
type="text">
</div>
@ -22,9 +22,9 @@
type="password">
</div>
</div>
<a class="pull-right" href="#">忘记密码?</a>
<a class="pull-right" href="/forgetpasswd">忘记密码?</a>
<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>
<input class="btn btn-lg btn-primary btn-block" type="submit" value="登陆">
<% end %>

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,82 @@
<% provide(:title, '忘记密码') %>
<% provide(:body_class, 'login2') %>
<div class="login-wrapper">
<a href="#"><img width="210" height="70" src="images/logo/guoren.png"/></a>
<%= 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-ok"></i></span>
<%= text_field_tag("verif_code", class: 'form-control', placeholder: "请输入邮箱验证码") %>
</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 %>
<!--<%= 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

@ -2,7 +2,7 @@
<div class="modal-shiftfix">
<div id="profile-container" class="container-fluid main-content">
<div id="profile-container" class="container-fluid main-content">
<%= render 'layouts/nav' %>
@ -104,5 +104,5 @@
</div>
</form>
</div>
</div>
</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>

View File

@ -12,5 +12,15 @@ module GuorenPro
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
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 => "zy_lovestar@163.com",
:password => "zy_lovestar+=*",
:authentication => "plain",
:enable_starttls_auto => true
}
end
end

View File

@ -51,4 +51,15 @@ Rails.application.configure do
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
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 => "zy_lovestar@163.com",
:password => "zy_lovestar+=*",
:authentication => "plain",
:enable_starttls_auto => true
}
end

View File

@ -8,6 +8,8 @@ Rails.application.routes.draw do
get '/signup', to: 'users#new'
get '/profile', to: 'users#edit'
get '/forgetpasswd', to: 'users#forgetpasswd'
get '/changepasswd', to: 'users#changepasswd'
get '/main', to: 'main#show'
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.
ActiveRecord::Schema.define(version: 20161227034933) do
ActiveRecord::Schema.define(version: 20170102130110) do
create_table "comments", force: :cascade do |t|
t.string "content"
@ -54,6 +54,7 @@ ActiveRecord::Schema.define(version: 20161227034933) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "verify_code"
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