FIX: 完善登录接口

master
陈古峰 3 years ago
parent 80eb69ff4a
commit fa92abf1ee

@ -2,34 +2,61 @@
class AccountsController < ApplicationController
def login
user_try_to_login = User.try_to_login(params[:login])
return info(-1,"用户账号不存在") if user_try_to_login.blank?
return info(-1,"用户账号未激活,请联系管理员") if user_try_to_login.invalid?
return info(-1,"用户账号已经被锁定") if user_try_to_login.locked?
return info(-1, "用户账号不存在") if user_try_to_login.blank?
return info(-1, "用户账号未激活,请联系管理员") if user_try_to_login.invalid?
return info(-1, "用户账号已经被锁定") if user_try_to_login.locked?
login_control = LimitForbidControl::UserLogin.new(user_try_to_login)
return info(-3, "登录密码出错已达上限,账号已被锁定, 请#{login_control.forbid_expires/60}分钟后重新登录或找回密码") if login_control.forbid?
return info(-3, "登录密码出错已达上限,账号已被锁定, 请#{login_control.forbid_expires / 60}分钟后重新登录或找回密码") if login_control.forbid?
check_password = user_try_to_login.check_password?(params[:password])
unless check_password
if login_control.remain_times-1 == 0
info(-3, "登录密码出错已达上限,账号已被锁定, 请#{login_control.forbid_expires/60}分钟后重新登录或找回密码")
if login_control.remain_times - 1 == 0
info(-3, "登录密码出错已达上限,账号已被锁定, 请#{login_control.forbid_expires / 60}分钟后重新登录或找回密码")
else
info(-3, "您已经输错密码#{login_control.error_times+1}次,还剩余#{login_control.remain_times-1}次机会")
info(-3, "您已经输错密码#{login_control.error_times + 1}次,还剩余#{login_control.remain_times - 1}次机会")
end
login_control.increment!
return
end
info(0,"登录成功")
info(0, "登录成功")
end
def register
begin
ActiveRecord::Base.transaction do
user_new = User.new(email: params[:email],
password: params[:password],
nickname: params[:nickname],
phone: params[:phone])
if user_new.valid?
user_new.login = generate_code(8)
user_new.save!
UserMailer.register_email(user_new).deliver_now
info(0, "保存成功")
else
info(-1, "注册错误", user_new.errors)
end
end
rescue Exception => e
logger_error(e.message)
info(-1, "注册错误", e.message)
end
end
def activation
ActiveRecord::Base.transaction do
id = params[:id]
user_find_by_login = User.find_by_login(id)
if user_find_by_login.present?
user_find_by_login.update!(status: 1)
info(0, "激活成功")
else
info(-1, "账号不存在")
end
end
end
end

@ -1,13 +1,17 @@
class ApplicationController < ActionController::Base
include LoggerHelper
def info(status = 0, message)
def generate_code(size)
%W(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m o p q r s t u v w x y z).sample(size).join
end
def info(status = 0, message, data)
case status
when 403
message = "您没有访问权限,请先获取相应权限~"
when 404
message = "您访问的页面不存在或已被删除"
end
render :json => { status: status, message: message }
render :json => { status: status, message: message,data: data}
end
end

@ -0,0 +1,14 @@
module LoggerHelper
# debug日志
def logger_dubug(message)
Rails.logger.debug("##dubug-- #{message}")
end
# 以用户id开始的日志定义
def logger_error(message)
Rails.logger.error("##:error --#{message}")
end
end

@ -0,0 +1,11 @@
class UserMailer < ApplicationMailer
# 注意:这个地方一定要和你的邮箱服务域名一致
default from: 'm13762945131@163.com'
# 用户注册验证码
def register_email(user)
@user = user
mail(to: @user.email, subject: '验证你的账号')
end
end

@ -2,12 +2,15 @@ require_relative '../../lib/edu/units'
class User < ApplicationRecord
# 邮箱验证
VALID_EMAIL_REGEX = /^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/i
VALID_EMAIL_REGEX = /\A[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+\z/i
# 手机号验证
VALID_PHONE_REGEX = /^1\d{10}$/
VALID_PHONE_REGEX = /\A1\d{10}\z/
# 身份证验证
VALID_NUMBER_REGEX = /(^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^([A-Z]\d{6,10}(\(\w{1}\))?)$)/
VALID_NUMBER_REGEX = /(\A[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^([A-Z]\d{6,10}(\(\w{1}\))?)\z)/
validates :email, format: { with: VALID_EMAIL_REGEX }
validates :password, length: { minimum: 5, maximum: 50 }
validates_uniqueness_of :email, :if => Proc.new { |user| user.email_changed? && user.email.present? }, case_sensitive: false
before_save :update_hashed_password
module USER_STATUS

@ -0,0 +1,53 @@
<html>
<head>
<meta charset="utf-8">
<title>验证码发送</title>
<style type="text/css">
/* 验证链接页面 */
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{ margin:0; padding:0;}
body,table,input,textarea,select,button { font-family: "微软雅黑","宋体"; font-size:12px;line-height:1.5; background:#eaebec;}
div,img,tr,td,table{ border:0;}
table,tr,td{border:0;}
ol,ul,li{ list-style-type:none}
.new_content{ background:#fff; width: 100%;}
.email-page-link{ }
.email-link-top{ }
.c_white{ color:#fff;}
.email-link-con{ }
.email-link-line{ }
.email-link-footer{ padding:15px; color:#333; line-height: 1.9; }
.c_grey02{ color: #888;}
.fb{ font-weight: normal;}
.f14{ }
</style>
</head>
<body style="background:#fff;">
<div class="new_content">
<div style="width: 598px; background:#fff; margin:20px auto; font-size:14px; ">
<div style="height:50px; width: 578px; background:#46484c; padding:9px 10px 6px;border:1px solid #ddd; border-bottom:none;">
<div style="clear:both; overflow:hidden;"></div>
</div>
<div style="width: 558px; border-left:1px solid #ddd;border-right:1px solid #ddd; background:#fff; padding:30px 20px; color:#333; line-height: 1.9;">
<p style="color:#333; font-size:16px; margin-bottom:15px;font-weight: bold">
您好!
</p>
<p style="color:#333;">
您正在注册Edu平台请点击下面链接激活账号如非你本人操作请忽略此邮件。
</p>
<div style="text-align: center;">
<div style="display:block; height: 45px; line-height:45px;padding:0 30px; width:200px; font-size: 20px; font-weight: bold; background:deepskyblue; color:white; margin:30px auto;">
<p> <a href="<%= 'http://localhost:3000/api/accounts/'+ @user.login+"/activation.json" %>">
点击验证您的账号
</a></p>
</div>
</div>
<p style="color:#666; margin-top:30px;">
如果您并未发过此请求,则可能是因为其他用户在注册时误输了您的邮件地址,而使您收到了这封邮件,那么您可以放心的忽略此邮件,无需进一步采取任何操作。
</p>
</div>
</div>
</div>
</body>
</html>

@ -64,4 +64,13 @@ 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.smtp_settings = {
address: "smtp.163.com",
port: 25,
user_name: "m13762945131@163.com",
password: "KSYNMPELGGZCOXBB",
authentication: "plain",
enable_starttls_auto: true
}
end

@ -5,6 +5,9 @@ scope '/api' do
post :login
post :register
end
member do
get :activation
end
end
end

Loading…
Cancel
Save