weapp: verification code api

dev_sync_trustie
p31729568 5 years ago
parent 625c7fd7b5
commit 146001a070

@ -1,24 +0,0 @@
class Weapps::RegisterCodesController < Weapps::BaseController
before_action :require_wechat_login!
def create
login = params[:login].to_s.strip
if login =~ /^[a-zA-Z0-9]+([._\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
user = User.find_by(mail: login)
return render_error('该邮箱已注册') if user.present?
elsif login =~ /^1\d{10}$/
user = User.find_by(phone: params[:login])
return render_error('该手机号已注册') if user.present?
else
return render_error('请输入正确的邮箱或手机号')
end
code = %W(0 1 2 3 4 5 6 7 8 9)
verification_code = code.sample(6).join
send_type = login =~ /^1\d{10}$/ ? 1 : 8
# 记录验证码
check_verification_code(verification_code, send_type, login)
render_ok
end
end

@ -0,0 +1,52 @@
class Weapps::VerificationCodesController < Weapps::BaseController
before_action :require_wechat_login!
def create
params[:type] == 'register' ? check_can_register : check_can_reset_password
end
private
def check_can_register
login = params[:login].to_s.strip
if login =~ /^[a-zA-Z0-9]+([._\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
user = User.find_by(mail: login)
return render_error('该邮箱已注册') if user.present?
elsif login =~ /^1\d{10}$/
user = User.find_by(phone: params[:login])
return render_error('该手机号已注册') if user.present?
else
return render_error('请输入正确的邮箱或手机号')
end
code = %W(0 1 2 3 4 5 6 7 8 9)
verification_code = code.sample(6).join
send_type = login =~ /^1\d{10}$/ ? 1 : 8
# 记录验证码
check_verification_code(verification_code, send_type, login)
render_ok
end
def check_can_reset_password
login = params[:login].to_s.strip
if login =~ /^[a-zA-Z0-9]+([._\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
user = User.find_by(mail: login)
return render_error('该邮箱尚未注册') if user.blank?
elsif login =~ /^1\d{10}$/
user = User.find_by(phone: login)
return render_error('该手机号尚未注册') if user.blank?
else
user = User.find_by(login: login)
return render_error('该账号尚未注册') if user.blank?
end
code = %W(0 1 2 3 4 5 6 7 8 9)
verification_code = code.sample(6).join
send_type = login =~ /^1\d{10}$/ ? 2 : 3
# 记录验证码
check_verification_code(verification_code, send_type, login)
render_ok
end
end

@ -852,7 +852,7 @@ Rails.application.routes.draw do
resource :home, only: [:show]
resource :session, only: [:create]
resource :register, only: [:create]
resource :register_code, only: [:create]
resource :verification_code, only: [:create]
resource :code_session, only: [:create]
resource :verify, only: [:create]
resource :check_account, only: [:create]

Loading…
Cancel
Save