From 392f3f9e19083ce352cc53d5a772bc0e2e3b47b2 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 30 Aug 2019 14:32:54 +0800 Subject: [PATCH] admins: precompile --- .../admins/modals/admin-upload-file-modal.js | 62 ++++++++++ .../{shixun_settings.js => index.js} | 7 ++ app/assets/stylesheets/admin.scss | 1 + .../stylesheets/admins/shixun_settings.scss | 34 +++--- app/assets/stylesheets/common.scss | 5 + app/controllers/admins/files_controller.rb | 54 +++++++++ .../_admin_common_refuse_modal.html.erb | 2 +- .../shared/modal/_upload_file_modal.html.erb | 32 ++++++ .../admins/shixun_settings/index.html.erb | 1 + .../shixun_settings/shared/_td.html.erb | 18 +-- app/views/colleges/shared/_navbar.html.erb | 2 +- config/routes.rb | 1 + ...fest-1c370772f16743f825981ab0e5c94237.json | 2 +- ...a0b607cc91c77f662b59489cd02ba0930fa5ab.js} | 69 ++++++++++++ ...607cc91c77f662b59489cd02ba0930fa5ab.js.gz} | Bin 327184 -> 327554 bytes ...da100f69d987c29092d00934e9ac11648749d.css} | 53 ++++++--- ...00f69d987c29092d00934e9ac11648749d.css.gz} | Bin 52564 -> 52608 bytes ...2cb573c222af5435c1a7c3792e60ccf95ce.css.gz | Bin 132807 -> 0 bytes ...645e87b536664cea8c6f7c6031c5c32cd17a.js.gz | Bin 934246 -> 0 bytes ...9e3b9aeaa6db572dec8b9f399f6ecef463efd7.js} | 69 ++++++++++++ ...3b9aeaa6db572dec8b9f399f6ecef463efd7.js.gz | Bin 0 -> 934580 bytes ...4aba155e9ef61ad3e2731096ca98b559babca.css} | 106 ++++++++++++------ ...ba155e9ef61ad3e2731096ca98b559babca.css.gz | Bin 0 -> 132876 bytes ...bfed6093f1ad56a3cc3cef00be02faedb0176.css} | 13 ++- ...d6093f1ad56a3cc3cef00be02faedb0176.css.gz} | Bin 43212 -> 43235 bytes 25 files changed, 446 insertions(+), 85 deletions(-) create mode 100644 app/assets/javascripts/admins/modals/admin-upload-file-modal.js rename app/assets/javascripts/admins/shixun_settings/{shixun_settings.js => index.js} (78%) create mode 100644 app/controllers/admins/files_controller.rb create mode 100644 app/views/admins/shared/modal/_upload_file_modal.html.erb rename public/assets/{admin-8d82c014df138d1f5f4c8d9e1311d45ab7e1f057c1982604abb5c48529896f1c.js => admin-09270f9cd6af4e44c0e843f307a0b607cc91c77f662b59489cd02ba0930fa5ab.js} (99%) rename public/assets/{admin-8d82c014df138d1f5f4c8d9e1311d45ab7e1f057c1982604abb5c48529896f1c.js.gz => admin-09270f9cd6af4e44c0e843f307a0b607cc91c77f662b59489cd02ba0930fa5ab.js.gz} (90%) rename public/assets/{admin-536370ad413cc83f6cc8d12548cdb754dd74450a1469d5b0041bfc2407d5131a.css => admin-9d5c4a373c243715692f9847445da100f69d987c29092d00934e9ac11648749d.css} (99%) rename public/assets/{admin-536370ad413cc83f6cc8d12548cdb754dd74450a1469d5b0041bfc2407d5131a.css.gz => admin-9d5c4a373c243715692f9847445da100f69d987c29092d00934e9ac11648749d.css.gz} (57%) delete mode 100644 public/assets/application-472497f1d0379456a4d31b301162d2cb573c222af5435c1a7c3792e60ccf95ce.css.gz delete mode 100644 public/assets/application-555b621615e9dae684fceecd0966645e87b536664cea8c6f7c6031c5c32cd17a.js.gz rename public/assets/{application-555b621615e9dae684fceecd0966645e87b536664cea8c6f7c6031c5c32cd17a.js => application-de4390096dd5da70c80c36f7479e3b9aeaa6db572dec8b9f399f6ecef463efd7.js} (99%) create mode 100644 public/assets/application-de4390096dd5da70c80c36f7479e3b9aeaa6db572dec8b9f399f6ecef463efd7.js.gz rename public/assets/{application-472497f1d0379456a4d31b301162d2cb573c222af5435c1a7c3792e60ccf95ce.css => application-fe7a1764ac518e837dfe9820e5e4aba155e9ef61ad3e2731096ca98b559babca.css} (99%) create mode 100644 public/assets/application-fe7a1764ac518e837dfe9820e5e4aba155e9ef61ad3e2731096ca98b559babca.css.gz rename public/assets/{college-58adc636bb1f4e5c1f312c875bf34402f8e02dbed9c440e0532baeddbd3b4c27.css => college-315d626b40fca41ae2670fb1700bfed6093f1ad56a3cc3cef00be02faedb0176.css} (99%) rename public/assets/{college-58adc636bb1f4e5c1f312c875bf34402f8e02dbed9c440e0532baeddbd3b4c27.css.gz => college-315d626b40fca41ae2670fb1700bfed6093f1ad56a3cc3cef00be02faedb0176.css.gz} (97%) 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/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 @@