diff --git a/app/assets/javascripts/admins/modals/admin-upload-file-modal.js b/app/assets/javascripts/admins/modals/admin-upload-file-modal.js new file mode 100644 index 000000000..cf1333381 --- /dev/null +++ b/app/assets/javascripts/admins/modals/admin-upload-file-modal.js @@ -0,0 +1,62 @@ +$(document).on('turbolinks:load', function() { + var $modal = $('.modal.admin-upload-file-modal'); + if ($modal.length > 0) { + var $form = $modal.find('form.admin-upload-file-form') + var $sourceIdInput = $modal.find('input[name="source_id"]'); + var $sourceTypeInput = $modal.find('input[name="source_type"]'); + + $modal.on('show.bs.modal', function(event){ + var $link = $(event.relatedTarget); + var sourceId = $link.data('sourceId'); + var sourceType = $link.data('sourceType'); + + $sourceIdInput.val(sourceId); + $sourceTypeInput.val(sourceType); + + $modal.find('.upload-file-input').trigger('click'); + }); + + $modal.find('.upload-file-input').on('change', function(e){ + var file = $(this)[0].files[0]; + + if(file){ + $modal.find('.file-names').html(file.name); + $modal.find('.submit-btn').trigger('click'); + } + }) + + var formValid = function(){ + if($form.find('input[name="file"]').val() == undefined || $form.find('input[name="file"]').val().length == 0){ + $form.find('.error').html('请选择文件'); + return false; + } + + return true; + }; + + $modal.on('click', '.submit-btn', function(){ + $form.find('.error').html(''); + + if (formValid()) { + var formDataString = $form.serialize(); + $.ajax({ + method: 'POST', + dataType: 'json', + url: '/admins/files?' + formDataString, + data: new FormData($form[0]), + processData: false, + contentType: false, + success: function(data){ + $.notify({ message: '上传成功' }); + $modal.trigger('upload:success', data); + $modal.modal('hide'); + }, + error: function(res){ + var data = res.responseJSON; + $form.find('.error').html(data.message); + } + }); + } + }); + } +}); \ No newline at end of file diff --git a/app/assets/javascripts/admins/shixun_settings/shixun_settings.js b/app/assets/javascripts/admins/shixun_settings/index.js similarity index 78% rename from app/assets/javascripts/admins/shixun_settings/shixun_settings.js rename to app/assets/javascripts/admins/shixun_settings/index.js index 9034ebe39..cff00a7c4 100644 --- a/app/assets/javascripts/admins/shixun_settings/shixun_settings.js +++ b/app/assets/javascripts/admins/shixun_settings/index.js @@ -28,6 +28,13 @@ $(document).on('turbolinks:load', function() { data: json }) }) + + $('.modal.admin-upload-file-modal').on('upload:success', function(e, data){ + var $imageElement = $('.shixun-image-' + data.source_id); + $imageElement.attr('src', data.url); + $imageElement.show(); + $imageElement.next().html('重新上传'); + }) } }); diff --git a/app/assets/stylesheets/admin.scss b/app/assets/stylesheets/admin.scss index e4b9cdbf3..d3a298dcf 100644 --- a/app/assets/stylesheets/admin.scss +++ b/app/assets/stylesheets/admin.scss @@ -19,6 +19,7 @@ body { align-items: stretch; font-size: 14px; background: #efefef; + overflow: hidden; } .simple_form { diff --git a/app/assets/stylesheets/admins/shixun_settings.scss b/app/assets/stylesheets/admins/shixun_settings.scss index c38fc0c6d..dcfc35650 100644 --- a/app/assets/stylesheets/admins/shixun_settings.scss +++ b/app/assets/stylesheets/admins/shixun_settings.scss @@ -1,14 +1,22 @@ -input[type="checkbox"]{ - font-size:18px; -} -.select2 input::-webkit-input-placeholder{ - color:#ccc; -} -.select2 .select2-selection__choice{ - border: 1px solid #eee !important; -} -.setting-chosen{ - font-weight: 400; - font-size: 10px; - color:#333; +.admins-shixun-settings-index-page { + input[type="checkbox"]{ + font-size:18px; + } + .select2 input::-webkit-input-placeholder{ + color:#ccc; + } + .select2 .select2-selection__choice{ + border: 1px solid #eee !important; + } + .setting-chosen{ + font-weight: 400; + font-size: 10px; + color:#333; + } + + .shixun-setting-image { + display: flex; + flex-direction: column; + align-items: center; + } } \ No newline at end of file diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index e2deb745d..3310ec828 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -20,6 +20,11 @@ label.error { input.form-control { font-size: 14px; } +.input-group-prepend { + .input-group-text { + font-size: 14px; + } +} .flex-1 { flex: 1; diff --git a/app/controllers/admins/files_controller.rb b/app/controllers/admins/files_controller.rb new file mode 100644 index 000000000..3c799ceba --- /dev/null +++ b/app/controllers/admins/files_controller.rb @@ -0,0 +1,54 @@ +class Admins::FilesController < Admins::BaseController + before_action :convert_file!, only: [:create] + + def create + File.delete(file_path) if File.exist?(file_path) # 删除之前的文件 + + Util.write_file(@file, file_path) + + render_ok(source_id: params[:source_id], source_type: params[:source_type].to_s, url: file_url) + rescue StandardError => ex + logger_error(ex) + render_error('上传失败') + end + + private + + def convert_file! + max_size = 10 * 1024 * 1024 # 10M + if params[:file].class == ActionDispatch::Http::UploadedFile + @file = params[:file] + render_error('请上传文件') if @file.size.zero? + render_error('文件大小超过限制') if @file.size > max_size + else + file = params[:file].to_s.strip + return render_error('请上传正确的图片') if file.blank? + @file = Util.convert_base64_image(file, max_size: max_size) + end + rescue Base64ImageConverter::Error => ex + render_error(ex.message) + end + + def file_path + @_file_path ||= begin + case params[:source_type].to_s + when 'Shixun' then + disk_filename('Shixun', params[:source_id]) + else + disk_filename(params[:source_type].to_s, params[:source_id].to_s) + end + end + end + + def disk_filename(type, id) + File.join(storage_path, type.to_s, id.to_s) + end + + def storage_path + @_storage_path ||= File.join(Rails.root, 'public', 'images', 'avatars') + end + + def file_url + File.join('/images/avatars/', params[:source_type].to_s, params[:source_id].to_s) + end +end \ No newline at end of file diff --git a/app/controllers/concerns/git_common.rb b/app/controllers/concerns/git_common.rb index 1752df7cb..93f5fecd7 100644 --- a/app/controllers/concerns/git_common.rb +++ b/app/controllers/concerns/git_common.rb @@ -46,9 +46,9 @@ module GitCommon # 为版本库添加文件 def add_file @path, message, content = params[:path].strip, params[:message], params[:content] - author_name, author_email = current_user.real_name, current_user.current_user.git_mail + author_name, author_email = current_user.real_name, current_user.git_mail @content = GitService.update_file(repo_path: @repo_path, - file_path: path, + file_path: @path, message: message, content: content, author_name: author_name, diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 50e349bab..1632813ea 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1238,7 +1238,8 @@ class CoursesController < ApplicationController end tip_exception("课堂所属单位不能为空!") if params[:school].blank? tip_exception("请至少添加一个课堂模块") if params[:course_module_types].blank? - @school = School.find_by!(name: params[:school].strip) + @school = School.find_by(name: params[:school].strip) + tip_exception("所属单位不存在") unless @school.present? end def validate_start_end_date diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 78e6cc0f0..e86c135e1 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -3,13 +3,16 @@ class ShixunsController < ApplicationController include ApplicationHelper before_action :require_login, :check_auth, except: [:download_file, :index, :menus, :show, :show_right, :ranking_list, - :discusses, :collaborators, :fork_list, :propaedeutics] + :discusses, :collaborators, :fork_list, :propaedeutics, :add_file] before_action :check_account, only: [:new, :create, :shixun_exec] + before_action :find_shixun, except: [:index, :new, :create, :menus, :get_recommend_shixuns, + :propaedeutics, :departments, :apply_shixun_mirror, + :get_mirror_script, :download_file] - before_action :find_shixun, :shixun_access_allowed, except: [:index, :new, :create, :menus, :get_recommend_shixuns, - :propaedeutics, :departments, :apply_shixun_mirror, - :get_mirror_script, :download_file] + before_action :shixun_access_allowed, except: [:index, :new, :create, :menus, :get_recommend_shixuns, :add_file, + :propaedeutics, :departments, :apply_shixun_mirror, + :get_mirror_script, :download_file] before_action :find_repo_name, only: [:repository, :commits, :file_content, :update_file, :shixun_exec, :copy] before_action :allowed, only: [:update, :close, :update_propaedeutics, :settings, :publish, diff --git a/app/controllers/users/question_banks_controller.rb b/app/controllers/users/question_banks_controller.rb index 1f51b701c..d2f111973 100644 --- a/app/controllers/users/question_banks_controller.rb +++ b/app/controllers/users/question_banks_controller.rb @@ -1,4 +1,5 @@ class Users::QuestionBanksController < Users::BaseController + before_action :require_login before_action :check_query_params! before_action :check_user_permission! @@ -62,12 +63,10 @@ class Users::QuestionBanksController < Users::BaseController end def check_user_permission! - return if User.current.admin? || (observed_logged_user? && read_question_bank_permission?) - - render_forbidden - end - - def read_question_bank_permission? - params[:type] == 'personal' ? User.current.is_teacher? : User.current.certification_teacher? + if params[:type] == 'publicly' + render_error("未通过职业认证") unless User.current.admin? || User.current.certification_teacher? + else + render_forbidden unless User.current.admin? || User.current.is_teacher? + end end end \ No newline at end of file diff --git a/app/services/homeworks_service.rb b/app/services/homeworks_service.rb index 64aec92c6..6659ec760 100644 --- a/app/services/homeworks_service.rb +++ b/app/services/homeworks_service.rb @@ -314,8 +314,8 @@ class HomeworksService if work.work_status != 0 if myshixun_endtime.present? - work.cost_time = myshixun_endtime.to_i - setting_time.publish_time.to_i - + work_cost_time = myshixun_endtime.to_i - setting_time.publish_time.to_i + work.cost_time = work_cost_time > 0 ? work_cost_time : games.select{|game| game.status == 2}.pluck(:cost_time).sum efficiency = (pass_consume_time == 0 ? 0 : Math.log((user_total_score / pass_consume_time.to_f) + 1.0)) work.efficiency = format("%.2f", efficiency) diff --git a/app/views/admins/shared/_admin_common_refuse_modal.html.erb b/app/views/admins/shared/_admin_common_refuse_modal.html.erb index a2daf7f0c..ee1b3177e 100644 --- a/app/views/admins/shared/_admin_common_refuse_modal.html.erb +++ b/app/views/admins/shared/_admin_common_refuse_modal.html.erb @@ -2,7 +2,7 @@