competitions
cxt 5 years ago
commit b7e109cd7c

@ -1,5 +1,7 @@
$(document).on('turbolinks:load', function() {
if ($('body.admins-carousels-index-page').length > 0) {
var laboratoryId = $('#carousels-container').data('laboratoryId');
// ------------ 保存链接 -----------
$('.carousels-card').on('click', '.save-data-btn', function(){
var $link = $(this);
@ -13,7 +15,7 @@ $(document).on('turbolinks:load', function() {
$link.attr('disabled', true);
$.ajax({
url: '/admins/carousels/' + id,
url: '/admins/laboratories/' + laboratoryId + '/carousels/' + id,
method: 'PATCH',
dataType: 'json',
data: { link: link, name: name },
@ -34,7 +36,7 @@ $(document).on('turbolinks:load', function() {
$checkbox.attr('disabled', true);
$.ajax({
url: '/admins/carousels/' + id,
url: '/admins/laboratories/' + laboratoryId + '/carousels/' + id,
method: 'PATCH',
dataType: 'json',
data: { status: checked },
@ -60,7 +62,7 @@ $(document).on('turbolinks:load', function() {
var insertId = $(sibling).data('id') || '';
$.ajax({
url: '/admins/carousels/drag',
url: '/admins/laboratories/' + laboratoryId + '/carousels/drag',
method: 'POST',
dataType: 'json',
data: { move_id: moveId, after_id: insertId },

@ -48,9 +48,15 @@
}
.action-container {
.action {
& > .action {
padding: 0 3px;
}
.more-action-dropdown {
.dropdown-item {
font-size: 14px;
}
}
}
/* 分页 */

@ -1,15 +1,17 @@
class Admins::CarouselsController < Admins::BaseController
before_action :convert_file!, only: [:create]
helper_method :current_laboratory
def index
@images = PortalImage.order(position: :asc)
@images = current_laboratory.portal_images.order(position: :asc)
end
def create
position = PortalImage.count + 1
position = current_laboratory.portal_images.count + 1
ActiveRecord::Base.transaction do
image = PortalImage.create!(create_params.merge(position: position))
image = current_laboratory.portal_images.create!(create_params.merge(position: position))
file_path = Util::FileManage.disk_filename('PortalImage', image.id)
File.delete(file_path) if File.exist?(file_path) # 删除之前的文件
@ -17,7 +19,7 @@ class Admins::CarouselsController < Admins::BaseController
end
flash[:success] = '保存成功'
redirect_to admins_carousels_path
redirect_to admins_laboratory_carousels_path(current_laboratory)
end
def update
@ -29,7 +31,7 @@ class Admins::CarouselsController < Admins::BaseController
ActiveRecord::Base.transaction do
current_image.destroy!
# 前移
PortalImage.where('position > ?', current_image.position)
current_laboratory.portal_images.where('position > ?', current_image.position)
.update_all('position = position - 1')
file_path = Util::FileManage.disk_filename('PortalImage', current_image.id)
@ -39,10 +41,10 @@ class Admins::CarouselsController < Admins::BaseController
end
def drag
move = PortalImage.find_by(id: params[:move_id])
after = PortalImage.find_by(id: params[:after_id])
move = current_laboratory.portal_images.find_by(id: params[:move_id])
after = current_laboratory.portal_images.find_by(id: params[:after_id])
Admins::DragPortalImageService.call(move, after)
Admins::DragPortalImageService.call(current_laboratory, move, after)
render_ok
rescue Admins::DragPortalImageService::Error => e
render_error(e.message)
@ -50,8 +52,12 @@ class Admins::CarouselsController < Admins::BaseController
private
def current_laboratory
@_current_laboratory ||= Laboratory.find(params[:laboratory_id])
end
def current_image
@_current_image ||= PortalImage.find(params[:id])
@_current_image ||= current_laboratory.portal_images.find(params[:id])
end
def create_params

@ -2,6 +2,7 @@ module LaboratoryHelper
extend ActiveSupport::Concern
included do
helper_method :current_laboratory
helper_method :default_setting
end

@ -2,10 +2,10 @@ class HomeController < ApplicationController
def index
# banner图
images = PortalImage.where(status: true).order("position asc")
images = current_laboratory.portal_images.only_online.order(position: :asc)
@images_url = []
images.each do |image|
@images_url << {path: image.link, image_url: Util::FileManage.disk_file_url('PortalImage', image.id)}
@images_url << {path: image.link, image_url: Util::FileManage.source_disk_file_url(image)}
end
# 目录分级

@ -1,3 +1,4 @@
# 基于Redis实现热门搜索关键字
class HotSearchKeyword
class << self
def add(keyword)

@ -6,6 +6,8 @@ class Laboratory < ApplicationRecord
has_one :laboratory_setting, dependent: :destroy
has_many :portal_images, dependent: :destroy
validates :identifier, uniqueness: { case_sensitive: false }, allow_nil: true
def site

@ -1,4 +1,8 @@
class PortalImage < ApplicationRecord
belongs_to :laboratory
scope :only_online, -> { where(status: true) }
def online?
status?
end

@ -1,9 +1,10 @@
class Admins::DragPortalImageService < ApplicationService
Error = Class.new(StandardError)
attr_reader :move, :after
attr_reader :laboratory, :move, :after
def initialize(move, after)
def initialize(laboratory, move, after)
@laboratory = laboratory
@move = move
@after = after # 移动后下一个位置的元素
end
@ -11,7 +12,7 @@ class Admins::DragPortalImageService < ApplicationService
def call
return if move.position + 1 == after&.position # 未移动
images = PortalImage.all
images = laboratory.portal_images
ActiveRecord::Base.transaction do
if after.blank? || move.id == after.id # 移动至末尾
@ -31,5 +32,4 @@ class Admins::DragPortalImageService < ApplicationService
end
end
end
end

@ -1,5 +1,6 @@
<%
define_admin_breadcrumbs do
add_admin_breadcrumb('云上实验室', admins_laboratories_path)
add_admin_breadcrumb('轮播图')
end
%>
@ -9,7 +10,7 @@
<span class="flex-1">首页轮播图<span class="text-secondary font-12">(拖动排序)</span></span>
<%= javascript_void_link '添加', class: 'btn btn-primary btn-sm add-btn', data: { toggle: 'modal', target: '.admin-add-carousel-modal' } %>
</div>
<div class="card-body row" id="carousels-container">
<div class="card-body row" id="carousels-container" data-laboratory-id="<%= current_laboratory.id %>">
<% @images.each_with_index do |image, index| %>
<div class="col-12 custom-carousel-item custom-carousel-item-<%= image.id %>" data-id="<%= image.id %>">
<div class="border rounded relative p-3 mb-3 drag row align-items-center <%= image.online? ? '' : 'not_active' %>">
@ -28,7 +29,7 @@
</div>
<div class="col-2 col-md-1 operate-box">
<%= check_box_tag(:online, 1, image.online?, id: nil, class: 'online-check-box', data: { id: image.id, toggle: 'tooltip', title: '首页展示' }) %>
<%= delete_link '删除', admins_carousel_path(image, element: ".custom-carousel-item-#{image.id}", not_refresh: true), class: 'delete-btn' do %>
<%= delete_link '删除', admins_laboratory_carousel_path(image, laboratory_id: image.laboratory_id, element: ".custom-carousel-item-#{image.id}", not_refresh: true), class: 'delete-btn' do %>
<i class="fa fa-trash-o" data-id="<%= image.id %>"></i>
<% end %>
</div>
@ -39,5 +40,5 @@
</div>
<%= render partial: 'admins/carousels/shared/add_carousel_modal' %>
<%= render partial: 'admins/carousels/shared/add_carousel_modal', locals: { laboratory_id: current_laboratory } %>
<%= render partial: 'admins/shared/modal/upload_file_modal' %>

@ -8,7 +8,7 @@
</button>
</div>
<div class="modal-body">
<%= simple_form_for(PortalImage.new, url: admins_carousels_path, html: { class: 'admin-add-carousel-form', enctype: 'multipart/form-data' }) do |f| %>
<%= simple_form_for(PortalImage.new, url: admins_laboratory_carousels_path(laboratory_id: laboratory_id), html: { class: 'admin-add-carousel-form', enctype: 'multipart/form-data' }) do |f| %>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">

@ -30,11 +30,18 @@
</td>
<td><%= laboratory.created_at.strftime('%Y-%m-%d %H:%M') %></td>
<td class="action-container">
<%= link_to '定制', admins_laboratory_laboratory_setting_path(laboratory) %>
<%= link_to '定制', admins_laboratory_laboratory_setting_path(laboratory), class: 'action' %>
<% if school.present? && laboratory.id != 1 %>
<%= javascript_void_link '添加管理员', class: 'action', data: { laboratory_id: laboratory.id, toggle: 'modal', target: '.admin-add-laboratory-user-modal' } %>
<% end %>
<%= javascript_void_link('更多', class: 'action dropdown-toggle', 'data-toggle': 'dropdown', 'aria-haspopup': true, 'aria-expanded': false) %>
<div class="dropdown-menu more-action-dropdown">
<%= link_to '轮播图', admins_laboratory_carousels_path(laboratory), class: 'dropdown-item' %>
<%= delete_link '删除', admins_laboratory_path(laboratory, element: ".laboratory-item-#{laboratory.id}"), class: 'delete-laboratory-action' %>
<% if school.present? && laboratory.id != 1 %>
<%= delete_link '删除', admins_laboratory_path(laboratory, element: ".laboratory-item-#{laboratory.id}"), class: 'dropdown-item delete-laboratory-action' %>
<% end %>
</div>
</td>

@ -33,7 +33,6 @@
<%= sidebar_item_group('#schools-submenu', '单位管理', icon: 'building') do %>
<li><%= sidebar_item(admins_schools_path, '单位列表', icon: 'university', controller: 'admins-schools') %></li>
<li><%= sidebar_item(admins_departments_path, '部门列表', icon: 'sitemap', controller: 'admins-departments') %></li>
<li><%= sidebar_item(admins_laboratories_path, '云上实验室', icon: 'cloud', controller: 'admins-laboratories') %></li>
<% end %>
</li>
@ -67,10 +66,10 @@
</li>
<li><%= sidebar_item(admins_competitions_path, '竞赛', icon: 'trophy', controller: 'admins-competitions') %></li>
<li><%= sidebar_item(admins_laboratories_path, '云上实验室', icon: 'cloud', controller: 'admins-laboratories') %></li>
<li>
<%= sidebar_item_group('#setting-submenu', '网站建设', icon: 'cogs') do %>
<li><%= sidebar_item(admins_carousels_path, '轮播图', icon: 'image', controller: 'admins-carousels') %></li>
<li><%= sidebar_item(edit_admins_about_path, '关于我们', icon: 'smile-o', controller: 'admins-abouts') %></li>
<li><%= sidebar_item(edit_admins_contact_us_path, '联系我们', icon: 'commenting-o', controller: 'admins-contact_us') %></li>
<li><%= sidebar_item(admins_cooperatives_path, '合作伙伴', icon: 'handshake-o', controller: 'admins-cooperatives') %></li>

@ -1,2 +1,3 @@
admins-mirror_scripts: 'admins-mirror_repositories'
admins-laboratory_settings: 'admins-laboratories'
admins-carousels: 'admins-laboratories'

@ -985,12 +985,13 @@ Rails.application.routes.draw do
post :drag, on: :collection
post :replace_image_url, on: :member
end
resources :carousels, only: [:index, :create, :update, :destroy] do
post :drag, on: :collection
end
resources :laboratories, only: [:index, :create, :destroy] do
resource :laboratory_setting, only: [:show, :update]
resource :laboratory_user, only: [:create, :destroy]
resources :carousels, only: [:index, :create, :update, :destroy] do
post :drag, on: :collection
end
end
resources :competitions, only: [:index, :destroy] do

@ -0,0 +1,8 @@
class AddLaboratoryIdToPortalImages < ActiveRecord::Migration[5.2]
def change
add_column :portal_images, :laboratory_id, :bigint
add_index :portal_images, :laboratory_id
execute 'UPDATE portal_images SET laboratory_id = 1'
end
end
Loading…
Cancel
Save