diff --git a/app/controllers/users/auth_attachments_controller.rb b/app/controllers/users/auth_attachments_controller.rb new file mode 100644 index 000000000..86c3e70ef --- /dev/null +++ b/app/controllers/users/auth_attachments_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/users/authentication_applies_controller.rb b/app/controllers/users/authentication_applies_controller.rb new file mode 100644 index 000000000..d406b33d3 --- /dev/null +++ b/app/controllers/users/authentication_applies_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/users/professional_auth_applies_controller.rb b/app/controllers/users/professional_auth_applies_controller.rb new file mode 100644 index 000000000..b2603f642 --- /dev/null +++ b/app/controllers/users/professional_auth_applies_controller.rb @@ -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 \ No newline at end of file diff --git a/app/forms/users/apply_authentication_form.rb b/app/forms/users/apply_authentication_form.rb new file mode 100644 index 000000000..986ba6223 --- /dev/null +++ b/app/forms/users/apply_authentication_form.rb @@ -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 \ No newline at end of file diff --git a/app/forms/users/apply_professional_auth_form.rb b/app/forms/users/apply_professional_auth_form.rb new file mode 100644 index 000000000..c5de9700c --- /dev/null +++ b/app/forms/users/apply_professional_auth_form.rb @@ -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 \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fd72b5651..cdd647ebd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -145,6 +145,18 @@ module ApplicationHelper File.join(storage_path, "#{source_type}", "#{source_id}") 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) if File.exist?(disk_filename(shixun.class, shixun.id)) File.join("images/#{relative_path}", "#{shixun.class}", "#{shixun.id}") diff --git a/app/models/user_extension.rb b/app/models/user_extension.rb index 3d8457ce3..49fca7c71 100644 --- a/app/models/user_extension.rb +++ b/app/models/user_extension.rb @@ -4,7 +4,7 @@ class UserExtension < ApplicationRecord belongs_to :user belongs_to :school - belongs_to :department + belongs_to :department, optional: true def identity_text I18n.t("user.identity.#{identity}") diff --git a/app/services/users/apply_authentication_service.rb b/app/services/users/apply_authentication_service.rb new file mode 100644 index 000000000..f31bb335c --- /dev/null +++ b/app/services/users/apply_authentication_service.rb @@ -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 \ No newline at end of file diff --git a/app/services/users/apply_professional_auth_service.rb b/app/services/users/apply_professional_auth_service.rb new file mode 100644 index 000000000..2d8279317 --- /dev/null +++ b/app/services/users/apply_professional_auth_service.rb @@ -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 \ No newline at end of file diff --git a/config/locales/forms/apply_authentication_form.zh-CN.yml b/config/locales/forms/apply_authentication_form.zh-CN.yml new file mode 100644 index 000000000..cfc2e5b39 --- /dev/null +++ b/config/locales/forms/apply_authentication_form.zh-CN.yml @@ -0,0 +1,7 @@ +'zh-CN': + activemodel: + attributes: + users/apply_authentication_form: + name: 姓名 + id_number: 身份证号 + diff --git a/config/locales/forms/apply_professional_auth_form.zh-CN.yml b/config/locales/forms/apply_professional_auth_form.zh-CN.yml new file mode 100644 index 000000000..bbba03238 --- /dev/null +++ b/config/locales/forms/apply_professional_auth_form.zh-CN.yml @@ -0,0 +1,9 @@ +'zh-CN': + activemodel: + attributes: + users/apply_professional_auth_form: + school_id: 学校/单位 + department_id: 学院/部门 + identity: 职业 + extra: 职称/学号 + diff --git a/config/routes.rb b/config/routes.rb index 1a81f1175..e09808f6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -74,6 +74,9 @@ Rails.application.routes.draw do resource :email_bind, only: [:create] resource :password, 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 diff --git a/lib/educoder/sms.rb b/lib/educoder/sms.rb index e79678ad3..237b270d5 100644 --- a/lib/educoder/sms.rb +++ b/lib/educoder/sms.rb @@ -21,7 +21,7 @@ module Educoder def self.notify_admin(opt) opt[:name] = '管理员' - opt[:mobile] = ENV['NOTIDY_ADMIN_PHONE'] || '17680641960' + opt[:mobile] = ENV['NOTIFY_ADMIN_PHONE'] || EduSetting.get('notify_admin_phone') || '17680641960' send(opt) end