From 2719e6024861c1b0dbbbad509c9a8733b097bf93 Mon Sep 17 00:00:00 2001 From: jasder Date: Wed, 27 Oct 2021 15:15:06 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E9=82=AE=E7=AE=B1=E3=80=81=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=90=8D=E3=80=81=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=AA=8C?= =?UTF-8?q?=E8=AF=81api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 14 ++++++- app/forms/register/check_columns_form.rb | 49 ++++++++++++++++++++++++ app/libs/custom_regexp.rb | 1 + config/routes.rb | 1 + 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/forms/register/check_columns_form.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 740a68ea7..dcffea5ca 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -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 diff --git a/app/forms/register/check_columns_form.rb b/app/forms/register/check_columns_form.rb new file mode 100644 index 000000000..24283b810 --- /dev/null +++ b/app/forms/register/check_columns_form.rb @@ -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 diff --git a/app/libs/custom_regexp.rb b/app/libs/custom_regexp.rb index c7b5e7a1a..bbc061250 100644 --- a/app/libs/custom_regexp.rb +++ b/app/libs/custom_regexp.rb @@ -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/ diff --git a/config/routes.rb b/config/routes.rb index 35fea39c7..20ce1e0a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -197,6 +197,7 @@ Rails.application.routes.draw do post :remote_login post :remote_password post :change_password + post :check end end