zy:add sendgird send mail

This commit is contained in:
lovelyzhang 2017-01-14 15:43:34 +08:00
parent 9dad27c6da
commit 49138c748b
15 changed files with 147 additions and 52 deletions

3
.gitignore vendored
View File

@ -53,6 +53,7 @@ build-iPhoneSimulator/
# JetBrain的忽略文件 # JetBrain的忽略文件
.idea/* .idea/*
public/data/* public/data/*
public/userpics/*
# rails的忽略文件 # rails的忽略文件
@ -102,3 +103,5 @@ bower.json
db/*.sqlite3 db/*.sqlite3

View File

@ -27,6 +27,7 @@ gem 'jbuilder', '~> 2.5'
gem 'bcrypt', '~> 3.1.7' gem 'bcrypt', '~> 3.1.7'
# will_paginate # will_paginate
gem 'will_paginate', '~> 3.1.0' gem 'will_paginate', '~> 3.1.0'
gem 'sendgrid-ruby'
# Use Capistrano for deployment # Use Capistrano for deployment
# gem 'capistrano-rails', group: :development # gem 'capistrano-rails', group: :development

View File

@ -118,6 +118,7 @@ GEM
rb-fsevent (0.9.8) rb-fsevent (0.9.8)
rb-inotify (0.9.7) rb-inotify (0.9.7)
ffi (>= 0.5.0) ffi (>= 0.5.0)
ruby_http_client (3.0.0)
sass (3.4.22) sass (3.4.22)
sass-rails (5.0.6) sass-rails (5.0.6)
railties (>= 4.0.0, < 6) railties (>= 4.0.0, < 6)
@ -125,6 +126,8 @@ GEM
sprockets (>= 2.8, < 4.0) sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3) tilt (>= 1.1, < 3)
sendgrid-ruby (4.0.6)
ruby_http_client (~> 3.0.0)
spring (2.0.0) spring (2.0.0)
activesupport (>= 4.2) activesupport (>= 4.2)
spring-watcher-listen (2.0.1) spring-watcher-listen (2.0.1)
@ -177,6 +180,7 @@ DEPENDENCIES
rails (~> 5.0.0, >= 5.0.0.1) rails (~> 5.0.0, >= 5.0.0.1)
rake rake
sass-rails (~> 5.0) sass-rails (~> 5.0)
sendgrid-ruby
spring spring
spring-watcher-listen (~> 2.0.0) spring-watcher-listen (~> 2.0.0)
sqlite3 sqlite3

View File

@ -1,4 +1,8 @@
class UsersController < ApplicationController class UsersController < ApplicationController
before_action :logged_in_user,except: [:new,:create,:forgetpasswd,:changepasswd]
@@user_pics_dir = "public/userpics"
def new def new
@user = User.new @user = User.new
end end
@ -20,12 +24,6 @@ class UsersController < ApplicationController
def update def update
if @user.update_attributes(user_params)
flash[:success]="profile updated"
redirect_to @user
else
render 'edit'
end
end end
@ -72,7 +70,18 @@ class UsersController < ApplicationController
end end
def changeprofile def changeprofile
render html: "等待完成" @user = current_user
result, user, err_string = check_change_profile_params(params[:user])
if result == false
flash[:danger] = err_string
else
pic_url = save_user_pic params[:user][:pic]
if pic_url.nil? == false
user[:picurl] = pic_url
end
@user.update_attributes(user)
end
redirect_to profile_path
end end
private private
@ -82,6 +91,35 @@ class UsersController < ApplicationController
:password_confirmation) :password_confirmation)
end end
def check_change_profile_params user_params
if user_params.nil?
return false, nil, "修改参数为空"
end
if user_params[:username].length <= 0
return false, nil, "姓名错误"
end
user = Hash.new
user[:name] = user_params[:username]
user[:profession] = user_params[:profession]
user[:sexual] = user_params[:sexual]
return true, user, nil
end
def save_user_pic pic
if pic.nil?
return nil
end
if !(File.directory? @@user_pics_dir)
FileUtils.mkdir_p(@@user_pics_dir)
end
ext = File.extname(pic.original_filename)
timestamp = Time.now.to_s
picname = Digest::MD5::hexdigest(pic.original_filename + timestamp) + ext
File.open(File.join(@@user_pics_dir, picname), 'wb') { |f| f.write(pic.read) }
save_name = File.join("userpics", picname)
return save_name
end
def send_code user def send_code user
verify_code = rand(999999).to_s verify_code = rand(999999).to_s
result = UserMailer.send_verify_code(user.email, verify_code).deliver_now result = UserMailer.send_verify_code(user.email, verify_code).deliver_now

View File

@ -3,4 +3,9 @@ module UsersHelper
user = User.find_by(id: user_id) user = User.find_by(id: user_id)
return user.name if !user.nil? return user.name if !user.nil?
end end
def get_user_sexual user
user.sexual
end
end end

View File

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

View File

@ -1,5 +1,5 @@
class UserMailer < ApplicationMailer class UserMailer < ApplicationMailer
default from: "ucasguoren@163.com" default from: "ucasguoren@heroku.com"
def send_verify_code mail_address, code def send_verify_code mail_address, code
@verify_code = code @verify_code = code

View File

@ -1,9 +1,10 @@
class User < ApplicationRecord class User < ApplicationRecord
before_save :default_values before_save :default_values
before_save :default_values
def default_values def default_values
self.picurl = 'images/avatars/default/avatar.png' if self.picurl.nil?
self.picurl = 'images/avatars/default/avatar.png'
end
end end
has_many :send_messages, class_name: 'Message', foreign_key: 'send_user', dependent: :destroy has_many :send_messages, class_name: 'Message', foreign_key: 'send_user', dependent: :destroy

View File

@ -6,7 +6,7 @@
<%= render 'layouts/nav' %> <%= render 'layouts/nav' %>
<%= form_tag("/changeprofile", method: "post", class: "form-horizontal", id: "profile") do %> <%= form_tag("/changeprofile", method: "post", class: "form-horizontal", id: "profile", enctype: "multipart/form-data") do %>
<div class="row"> <div class="row">
<div class="widget-container"> <div class="widget-container">
@ -40,10 +40,7 @@
</span> </span>
<input type="file" name="user[pic]"> <input type="file" name="user[pic]">
</span> </span>
<a <a class="btn btn-default fileupload-exists" data-dismiss="fileupload">清除照片</a>
class="btn btn-default fileupload-exists" data-dismiss="fileupload"
href="#">清除照片
</a>
</div> </div>
</div> </div>
</div> </div>
@ -71,34 +68,23 @@
</div> </div>
</div> </div>
<div class="form-group">
<label class="control-label col-md-2">学院</label>
<div class="col-md-9">
<select class="form-control" name="user[academy]">
<option value="计算机与控制学院">计算机与控制学院</option>
<option value="数学与系统学院">数学与系统学院</option>
<option value="电子信息工程学院">电子信息工程学院</option>
<option value="生命科学院">生命科学院</option>
</select>
</div>
</div>
<div class="form-group"> <div class="form-group">
<label class="control-label col-md-2">性别</label> <label class="control-label col-md-2">性别</label>
<div class="col-md-9"> <div class="col-md-9">
<label class="radio-inline"><input name="user[sex]" type="radio" <label class="radio-inline"><input name="user[sexual]" type="radio" value="1"
value="1" checked="true"><span>男</span></label><label <% if get_user_sexual(@user) == true %>checked
class="radio-inline"><input name="user[sex]" <% end %>
type="radio" ><span>男</span></label>
value="0"><span>女</span></label> <label class="radio-inline"><input name="user[sexual]" type="radio" value="0"
<% if get_user_sexual(@user) == false %>checked
<% end %>
><span>女</span></label>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div class="col-md-12 text-center"> <div class="col-md-12 text-center">
<button class="btn btn-primary" type="submit">修改</button> <button class="btn btn-primary" type="submit">修改</button>
<button class="btn btn-default-outline">取消</button>
</div> </div>
</div> </div>

View File

@ -0,0 +1,44 @@
<% provide(:title, @user.name) %>
<div class="modal-shiftfix">
<div id="profile-container" class="container-fluid main-content">
<%= render 'layouts/nav' %>
<%= form_for(@user) 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-user"></i></span>
<%= f.text_field :name, 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 %>
</div>
</div>

View File

@ -12,15 +12,5 @@ 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

@ -18,7 +18,7 @@ Rails.application.configure do
config.cache_store = :memory_store config.cache_store = :memory_store
config.public_file_server.headers = { config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800' 'Cache-Control' => 'public, max-age=172800'
} }
else else
config.action_controller.perform_caching = false config.action_controller.perform_caching = false
@ -52,14 +52,18 @@ Rails.application.configure do
# 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.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
host = 'ucasguoren.herokuapp.com'
config.action_mailer.default_url_options = {host: host}
config.action_mailer.raise_delivery_errors = true config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = { config.action_mailer.smtp_settings = {
:address => "smtp.163.com", :address => "smtp.sendgrid.net",
:port => 25, :port => 587,
:user_name => "ucasguoren@163.com", :user_name => "app61271187@heroku.com",
:password => "ucas123", :password => "5cysx5kw9899",
:authentication => "plain", :authentication => "plain",
:domain => 'herokuapp.com',
:enable_starttls_auto => true :enable_starttls_auto => true
} }
end end

View File

@ -83,4 +83,19 @@ Rails.application.configure do
# Do not dump schema after migrations. # Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false config.active_record.dump_schema_after_migration = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = 'ucasguoren.herokuapp.com'
config.action_mailer.default_url_options = {host: host}
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => "plain",
:domain => 'herokuapp.com',
:enable_starttls_auto => true
}
end end

View File

@ -0,0 +1,5 @@
class RemoveAcademyFromUser < ActiveRecord::Migration[5.0]
def change
remove_column :users, :academy, :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: 20170102130110) do ActiveRecord::Schema.define(version: 20170114044144) do
create_table "comments", force: :cascade do |t| create_table "comments", force: :cascade do |t|
t.string "content" t.string "content"
@ -49,7 +49,6 @@ ActiveRecord::Schema.define(version: 20170102130110) do
t.string "email" t.string "email"
t.string "picurl" t.string "picurl"
t.string "profession" t.string "profession"
t.string "academy"
t.boolean "sexual" t.boolean "sexual"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false