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,33 +2,33 @@
<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' %>
<%= render 'layouts/nav' %>
<form action="/baidu" class="form-horizontal" id="profile" method="post">
<div class="row">
<div class="widget-container">
<div class="heading">
<i class="icon-user"></i>个人设置
<form action="/baidu" class="form-horizontal" id="profile" method="post">
<div class="row">
<div class="widget-container">
<div class="heading">
<i class="icon-user"></i>个人设置
</div>
</div>
</div>
</div>
<div class="row">
<div class="widget-container">
<div class="heading">
<i class="icon-user"></i>用户图像
</div>
<div class="col-md-12 text-center">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new img-thumbnail" style="width: 200px; height: 200px;">
<!--原照片-->
<img src="/images/avatars/default/avatar.png">
</div>
<div class="fileupload-preview fileupload-exists img-thumbnail"
style="width: 200px; max-height: 200px">
</div>
<div class="padded">
<div class="row">
<div class="widget-container">
<div class="heading">
<i class="icon-user"></i>用户图像
</div>
<div class="col-md-12 text-center">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new img-thumbnail" style="width: 200px; height: 200px;">
<!--原照片-->
<img src="/images/avatars/default/avatar.png">
</div>
<div class="fileupload-preview fileupload-exists img-thumbnail"
style="width: 200px; max-height: 200px">
</div>
<div class="padded">
<span class="btn btn-default btn-file">
<span
class="fileupload-new">选择照片
@ -39,70 +39,70 @@
</span>
<input type="file" name="image_file">
</span>
<a
class="btn btn-default fileupload-exists" data-dismiss="fileupload"
href="#">清除照片
</a>
<a
class="btn btn-default fileupload-exists" data-dismiss="fileupload"
href="#">清除照片
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="widget-container fluid-height clearfix">
<div class="heading">
<i class="icon-reorder"></i>基本信息
</div>
<div class="padded">
<div class="form-group">
<label class="control-label col-md-2">姓名</label>
<div class="col-md-9">
<input class="form-control" placeholder="Text" type="text">
</div>
<div class="row">
<div class="widget-container fluid-height clearfix">
<div class="heading">
<i class="icon-reorder"></i>基本信息
</div>
<div class="form-group">
<label class="control-label col-md-2">专业</label>
<div class="col-md-9">
<input class="form-control" placeholder="Text" type="text">
<div class="padded">
<div class="form-group">
<label class="control-label col-md-2">姓名</label>
<div class="col-md-9">
<input class="form-control" placeholder="Text" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">学院</label>
<div class="col-md-9">
<select class="form-control">
<option value="Category 1">Option 1</option>
<option value="Category 2">Option 2</option>
<option value="Category 3">Option 3</option>
<option value="Category 4">Option 4</option>
</select>
<div class="form-group">
<label class="control-label col-md-2">专业</label>
<div class="col-md-9">
<input class="form-control" placeholder="Text" type="text">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">性别</label>
<div class="col-md-9">
<label class="radio-inline"><input name="optionsRadios" type="radio"
value="1"><span>男</span></label><label
class="radio-inline"><input name="optionsRadios"
type="radio"
value="0"><span>女</span></label>
<div class="form-group">
<label class="control-label col-md-2">学院</label>
<div class="col-md-9">
<select class="form-control">
<option value="Category 1">Option 1</option>
<option value="Category 2">Option 2</option>
<option value="Category 3">Option 3</option>
<option value="Category 4">Option 4</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button class="btn btn-primary" type="submit">修改</button>
<button class="btn btn-default-outline">取消</button>
<div class="form-group">
<label class="control-label col-md-2">性别</label>
<div class="col-md-9">
<label class="radio-inline"><input name="optionsRadios" type="radio"
value="1"><span>男</span></label><label
class="radio-inline"><input name="optionsRadios"
type="radio"
value="0"><span>女</span></label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button class="btn btn-primary" type="submit">修改</button>
<button class="btn btn-default-outline">取消</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</form>
</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