parent
774eb23637
commit
093d9908d5
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@
|
|||||||
|
$(document).on('turbolinks:load', function() {
|
||||||
|
if ($('.weapp-banner-setting-container').length > 0) {
|
||||||
|
var $form = $('#course_form');
|
||||||
|
|
||||||
|
$('.course.banner-item-bottom').on("change", 'input[type="file"]', function() {
|
||||||
|
var $fileInput = $(this);
|
||||||
|
var file = this.files[0];
|
||||||
|
var imageType = /image.*/;
|
||||||
|
if (file && file.type.match(imageType)) {
|
||||||
|
$form.ajaxSubmit()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var $shixunform = $('#shixun_form');
|
||||||
|
|
||||||
|
$('.shixun.banner-item-bottom').on("change", 'input[type="file"]', function() {
|
||||||
|
var $fileInput = $(this);
|
||||||
|
var file = this.files[0];
|
||||||
|
var imageType = /image.*/;
|
||||||
|
if (file && file.type.match(imageType)) {
|
||||||
|
$shixunform.ajaxSubmit()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
@ -1,80 +1,52 @@
|
|||||||
class Admins::WeappBannersController < Admins::BaseController
|
class Admins::WeappBannersController < Admins::BaseController
|
||||||
before_action :convert_file!, only: [:create]
|
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
@shixun = WeappSettings::ShixunBanner.first
|
||||||
|
@course = WeappSettings::CourseBanner.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
position = WeappSettings::Carousel.count + 1
|
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
carousel = WeappSettings::Carousel.create!(create_params.merge(position: position))
|
old_carouse = WeappSettings::CourseBanner.first
|
||||||
|
|
||||||
file_path = Util::FileManage.source_disk_filename(carousel)
|
if old_carouse.present?
|
||||||
|
old_carouse.destroy!
|
||||||
|
file_path = Util::FileManage.source_disk_filename(old_carouse)
|
||||||
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
|
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
|
||||||
Util.write_file(@file, file_path)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
flash[:success] = '保存成功'
|
@course = WeappSettings::CourseBanner.create!
|
||||||
redirect_to admins_weapp_carousels_path
|
save_image_file(params[:course_banner], @course)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
|
||||||
current_carousel.update!(update_params)
|
|
||||||
render_ok
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
|
||||||
|
def shixun_banner
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
current_carousel.destroy!
|
old_shixun = WeappSettings::ShixunBanner.first
|
||||||
# 前移
|
|
||||||
WeappSettings::Carousel.where('position > ?', current_carousel.position)
|
|
||||||
.update_all('position = position - 1')
|
|
||||||
|
|
||||||
file_path = Util::FileManage.source_disk_filename(current_carousel)
|
if old_shixun.present?
|
||||||
File.delete(file_path) if File.exist?(file_path)
|
old_shixun.destroy!
|
||||||
|
file_path = Util::FileManage.source_disk_filename(old_shixun)
|
||||||
|
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
|
||||||
|
end
|
||||||
|
|
||||||
|
@shixun = WeappSettings::ShixunBanner.create!
|
||||||
|
save_image_file(params[:shixun_banner], @shixun)
|
||||||
end
|
end
|
||||||
render_delete_success
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def drag
|
|
||||||
move = WeappSettings::Carousel.find_by(id: params[:move_id])
|
|
||||||
after = WeappSettings::Carousel.find_by(id: params[:after_id])
|
|
||||||
|
|
||||||
Admins::DragWeappCarouselService.call(move, after)
|
|
||||||
render_ok
|
|
||||||
rescue ApplicationService::Error => e
|
|
||||||
render_error(e.message)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def current_carousel
|
private
|
||||||
@_current_carousel ||= WeappSettings::Carousel.find(params[:id])
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_params
|
def save_image_file(file, model)
|
||||||
params.require(:weapp_settings_carousel).permit(:link)
|
return unless file.present? && file.is_a?(ActionDispatch::Http::UploadedFile)
|
||||||
end
|
|
||||||
|
|
||||||
def update_params
|
file_path = Util::FileManage.source_disk_filename(model)
|
||||||
params.permit(:link, :online)
|
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
|
||||||
|
Util.write_file(file, file_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def convert_file!
|
|
||||||
max_size = 10 * 1024 * 1024 # 10M
|
|
||||||
file = params.dig('weapp_settings_carousel', 'image')
|
|
||||||
if file.class == ActionDispatch::Http::UploadedFile
|
|
||||||
@file = file
|
|
||||||
render_error('请上传文件') if @file.size.zero?
|
|
||||||
render_error('文件大小超过限制') if @file.size > max_size
|
|
||||||
else
|
|
||||||
file = 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
|
|
||||||
end
|
end
|
@ -0,0 +1,12 @@
|
|||||||
|
class Weapps::BannersController < Weapps::BaseController
|
||||||
|
|
||||||
|
def index
|
||||||
|
shixun = WeappSettings::ShixunBanner.first
|
||||||
|
course = WeappSettings::CourseBanner.first
|
||||||
|
|
||||||
|
render json: {
|
||||||
|
shixun_img: shixun ? Util::FileManage.source_disk_file_url(shixun) : '',
|
||||||
|
course_img: course ? Util::FileManage.source_disk_file_url(course) : ''
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
class WeappSettings::CourseBanner < WeappSetting
|
||||||
|
default_scope { order(position: :asc) }
|
||||||
|
end
|
@ -0,0 +1,3 @@
|
|||||||
|
class WeappSettings::ShixunBanner < WeappSetting
|
||||||
|
default_scope { order(position: :asc) }
|
||||||
|
end
|
@ -0,0 +1 @@
|
|||||||
|
$("#course_img")[0].innerHTML = "<%= escape_javascript(image_tag Util::FileManage.source_disk_file_url(@course), class: 'banner-item-img course-banner-img') %>"
|
@ -0,0 +1 @@
|
|||||||
|
$("#shixun_img")[0].innerHTML = "<%= escape_javascript(image_tag Util::FileManage.source_disk_file_url(@shixun), class: 'banner-item-img course-banner-img') %>"
|
Loading…
Reference in new issue