forked from Gitlink/forgeplus
ADD 邮箱、登录名、手机号验证api
This commit is contained in:
parent
9cd54b0ccb
commit
2719e60248
|
@ -323,7 +323,15 @@ class AccountsController < ApplicationController
|
|||
def phone_mail_type value
|
||||
value =~ /^1\d{10}$/ ? 1 : 0
|
||||
end
|
||||
|
||||
|
||||
# check user's login or email or phone is used
|
||||
# params[:value] 手机号或者邮箱号或者登录名
|
||||
# params[:type] 为事件类型 1:登录名(login) 2:email(邮箱) 3:phone(手机号)
|
||||
def check
|
||||
Register::CheckColumnsForm.new(check_params).validate!
|
||||
render_ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# type 事件类型 1:用户注册 2:忘记密码 3: 绑定手机 4: 绑定邮箱, 5: 验证手机号是否有效 # 如果有新的继续后面加
|
||||
|
@ -369,4 +377,8 @@ class AccountsController < ApplicationController
|
|||
def account_params
|
||||
params.require(:account).permit(:login, :password)
|
||||
end
|
||||
|
||||
def check_params
|
||||
params.permit(:type, :value)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
module Register
|
||||
class CheckColumnsForm < BaseForm
|
||||
attr_accessor :type, :value
|
||||
|
||||
validates :type, presence: true, numericality: true
|
||||
validates :value, presence: true
|
||||
validate :check!
|
||||
|
||||
def check!
|
||||
# params[:type] 为事件类型 1:登录名(login) 2:email(邮箱) 3:phone(手机号)
|
||||
case strip_type
|
||||
when 1 then check_login
|
||||
when 2 then check_mail
|
||||
when 3 then check_phone
|
||||
else raise("type值无效")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def check_login
|
||||
raise("登录名格式有误") unless strip_value =~ CustomRegexp::LOGIN
|
||||
|
||||
login_exist = Owner.exists?(login: strip_value) || ReversedKeyword.exists?(identifier: strip_value)
|
||||
raise('登录名已被使用') if login_exist
|
||||
end
|
||||
|
||||
def check_mail
|
||||
raise("邮件格式有误") unless strip_value =~ CustomRegexp::EMAIL
|
||||
|
||||
mail_exist = Owner.exists?(mail: strip_value)
|
||||
raise('邮箱已被使用') if mail_exist
|
||||
end
|
||||
|
||||
def check_phone
|
||||
raise("手机号格式有误") unless strip_value =~ CustomRegexp::PHONE
|
||||
|
||||
phone_exist = Owner.exists?(phone: strip_value)
|
||||
raise('手机号已被使用') if phone_exist
|
||||
end
|
||||
|
||||
def strip_value
|
||||
value&.strip
|
||||
end
|
||||
|
||||
def strip_type
|
||||
type&.strip.to_i
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,7 @@
|
|||
module CustomRegexp
|
||||
PHONE = /1\d{10}/
|
||||
EMAIL = /\A[a-zA-Z0-9]+([._\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+\z/
|
||||
LOGIN = /^(?!_)(?!.*?_$)[a-zA-Z0-9_-]+$/ #只含有数字、字母、下划线不能以下划线开头和结尾
|
||||
LASTNAME = /\A[a-zA-Z0-9\u4e00-\u9fa5]+\z/
|
||||
NICKNAME = /\A[\u4e00-\u9fa5_a-zA-Z0-9]+\z/
|
||||
PASSWORD = /\A[a-z_A-Z0-9\-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",'_<>~\·`\?:;|]{8,16}\z/
|
||||
|
|
|
@ -197,6 +197,7 @@ Rails.application.routes.draw do
|
|||
post :remote_login
|
||||
post :remote_password
|
||||
post :change_password
|
||||
post :check
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue