use apply realname auth && apply professional auth api

dev_course
p31729568 5 years ago
parent 8df9380c6a
commit 65f82eda89

@ -0,0 +1,39 @@
class Users::AuthAttachmentsController < Users::BaseAccountController
before_action :private_user_resources!
before_action :convert_image!, only: [:update]
def update
image_temp_path = auth_image_path + 'temp' # 上传文件保存至临时文件,提交申请时再移到正常目录
File.delete(image_temp_path) if File.exist?(image_temp_path) # 删除之前的临时文件
Util.write_file(@image, image_temp_path)
render_ok
rescue StandardError => ex
logger_error(ex)
render_error('上传失败')
end
private
def convert_image!
max_size = EduSetting.get('upload_avatar_max_size') || 10 * 1024 * 1024 # 10M
if params[:image].class == ActionDispatch::Http::UploadedFile
@image = params[:image]
render_error('请上传文件') if @image.size.zero?
render_error('文件大小超过限制') if @image.size > max_size
else
image = params[:image].to_s.strip
return render_error('请上传正确的图片') if image.blank?
@image = Util.convert_base64_image(image, max_size: max_size)
end
rescue Base64ImageConverter::Error => ex
render_error(ex.message)
end
def auth_image_path
url_method = params[:type] == 'professional' ? :disk_professional_auth_filename : :disk_real_name_auth_filename
ApplicationController.helpers.send(url_method, observed_user.id)
end
end

@ -0,0 +1,16 @@
class Users::AuthenticationAppliesController < Users::BaseAccountController
before_action :private_user_resources!
def create
Users::ApplyAuthenticationService.call(observed_user, create_params)
render_ok
rescue Users::ApplyAuthenticationService::Error => ex
render_error(ex.message)
end
private
def create_params
params.permit(:name, :id_number, :upload_image)
end
end

@ -0,0 +1,16 @@
class Users::ProfessionalAuthAppliesController < Users::BaseAccountController
before_action :private_user_resources!
def create
Users::ApplyProfessionalAuthService.call(observed_user, create_params)
render_ok
rescue Users::ApplyProfessionalAuthService::Error => ex
render_error(ex.message)
end
private
def create_params
params.permit(:school_id, :department_id, :identity, :extra, :upload_image)
end
end

@ -0,0 +1,8 @@
class Users::ApplyAuthenticationForm
include ActiveModel::Model
attr_accessor :name, :id_number, :upload_image
validates :name, presence: true
validates :id_number, presence: true
end

@ -0,0 +1,10 @@
class Users::ApplyProfessionalAuthForm
include ActiveModel::Model
attr_accessor :school_id, :department_id, :identity, :extra, :upload_image
validates :school_id, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :department_id, numericality: { only_integer: true, greater_than: 0 }, allow_blank: true
validates :identity, presence: true, inclusion: { in: %w(student teacher professional) }
validates :extra, presence: true
end

@ -145,6 +145,18 @@ module ApplicationHelper
File.join(storage_path, "#{source_type}", "#{source_id}") File.join(storage_path, "#{source_type}", "#{source_id}")
end end
def disk_auth_filename(source_type, source_id, type)
File.join(storage_path, "#{source_type}", "#{source_id}#{type}")
end
def disk_real_name_auth_filename(source_id)
disk_auth_filename('UserAuthentication', source_id, 'ID')
end
def disk_professional_auth_filename(source_id)
disk_auth_filename('UserAuthentication', source_id, 'PRO')
end
def shixun_url_to_avatar(shixun) def shixun_url_to_avatar(shixun)
if File.exist?(disk_filename(shixun.class, shixun.id)) if File.exist?(disk_filename(shixun.class, shixun.id))
File.join("images/#{relative_path}", "#{shixun.class}", "#{shixun.id}") File.join("images/#{relative_path}", "#{shixun.class}", "#{shixun.id}")

@ -4,7 +4,7 @@ class UserExtension < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :school belongs_to :school
belongs_to :department belongs_to :department, optional: true
def identity_text def identity_text
I18n.t("user.identity.#{identity}") I18n.t("user.identity.#{identity}")

@ -0,0 +1,52 @@
class Users::ApplyAuthenticationService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
Users::ApplyAuthenticationForm.new(params).validate!
raise Error, '您已经申请过实名认证了' if ApplyUserAuthentication.real_name_auth.processing.exists?(user_id: user.id)
user.lastname = params[:name].to_s.strip
user.firstname = ''
user.ID_number = params[:id_number].to_s.strip.presence
ActiveRecord::Base.transaction do
user.authentication = false
user.save!
user.apply_user_authentication.create!(auth_type: 1, status: 0)
move_image_file! unless params[:upload_image].to_s == 'false'
sms_notify_admin
end
user
end
private
def move_image_file!
image_url = ApplicationController.helpers.disk_real_name_auth_filename(user.id)
temp_image_url = image_url + 'temp'
FileUtils.mv(temp_image_url, image_url, force: true) if File.exist?(temp_image_url)
rescue RuntimeError => ex
Util.logger_error(ex)
raise Error, '申请失败'
ensure
File.delete(temp_image_url) if File.exist?(temp_image_url)
end
def sms_notify_admin
Educoder::Sms.notify_admin(send_type: 'apply_auth')
rescue => ex
Util.logger_error(ex)
end
end

@ -0,0 +1,62 @@
class Users::ApplyProfessionalAuthService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
Users::ApplyProfessionalAuthForm.new(params).validate!
raise Error, '您已经申请过职业认证了' if ApplyUserAuthentication.professional_auth.processing.exists?(user_id: user.id)
user.professional_certification = false
extension = user.user_extension
extension.school_id = params[:school_id]
extension.department_id = params[:department_id]
extension.identity = params[:identity]
extra = params[:extra].to_s.strip.presence
if extension.identity.to_s == 'student'
extension.technical_title = nil
extension.student_id = extra
else
extension.technical_title = extra
extension.student_id = nil
end
ActiveRecord::Base.transaction do
user.save!
extension.save!
user.apply_user_authentication.create!(auth_type: 2, status: 0)
move_image_file! unless params[:upload_image].to_s == 'false'
sms_notify_admin
end
end
private
def move_image_file!
image_url = ApplicationController.helpers.disk_professional_auth_filename(user.id)
temp_image_url = image_url + 'temp'
FileUtils.mv(temp_image_url, image_url, force: true) if File.exist?(temp_image_url)
rescue RuntimeError => ex
Util.logger_error(ex)
raise Error, '申请失败'
ensure
File.delete(temp_image_url) if File.exist?(temp_image_url)
end
def sms_notify_admin
Educoder::Sms.notify_admin(send_type: 'apply_pro_certification')
rescue => ex
Util.logger_error(ex)
end
end

@ -0,0 +1,7 @@
'zh-CN':
activemodel:
attributes:
users/apply_authentication_form:
name: 姓名
id_number: 身份证号

@ -0,0 +1,9 @@
'zh-CN':
activemodel:
attributes:
users/apply_professional_auth_form:
school_id: 学校/单位
department_id: 学院/部门
identity: 职业
extra: 职称/学号

@ -74,6 +74,9 @@ Rails.application.routes.draw do
resource :email_bind, only: [:create] resource :email_bind, only: [:create]
resource :password, only: [:update] resource :password, only: [:update]
resource :avatar, only: [:update] resource :avatar, only: [:update]
resource :auth_attachment, only: [:update]
resource :authentication_apply, only: [:create]
resource :professional_auth_apply, only: [:create]
end end
end end
end end

@ -21,7 +21,7 @@ module Educoder
def self.notify_admin(opt) def self.notify_admin(opt)
opt[:name] = '管理员' opt[:name] = '管理员'
opt[:mobile] = ENV['NOTIDY_ADMIN_PHONE'] || '17680641960' opt[:mobile] = ENV['NOTIFY_ADMIN_PHONE'] || EduSetting.get('notify_admin_phone') || '17680641960'
send(opt) send(opt)
end end

Loading…
Cancel
Save