diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index 0cab0435..e5c7e662 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -71,6 +71,23 @@ $(document).on('turbolinks:load', function(){ $('.admin-alert-container .alert.alert-danger').alert('close'); }, 5000); } + + $('.logo-item-left').on("change", 'input[type="file"]', function () { + var $fileInput = $(this); + var file = this.files[0]; + var imageType = /image.*/; + if (file && file.type.match(imageType)) { + var reader = new FileReader(); + reader.onload = function () { + var $box = $fileInput.parent(); + $box.find('img').attr('src', reader.result).css('display', 'block'); + $box.addClass('has-img'); + }; + reader.readAsDataURL(file); + } else { + } + }); + }); $(document).on("turbolinks:before-cache", function () { diff --git a/app/assets/stylesheets/admin.scss b/app/assets/stylesheets/admin.scss index a401fc37..b11b0518 100644 --- a/app/assets/stylesheets/admin.scss +++ b/app/assets/stylesheets/admin.scss @@ -58,3 +58,75 @@ input.form-control { position: absolute; } +.logo-item { + display: flex; + + &-img { + display: block; + width: 80px; + height: 80px; + background: #e9ecef; + } + + &-upload { + cursor: pointer; + position: absolute; + top: 0; + width: 80px; + height: 80px; + background: #e9ecef; + border: 1px solid #ced4da; + + &::before { + content: ''; + position: absolute; + top: 27px; + left: 39px; + width: 2px; + height: 26px; + background: #495057; + } + + &::after { + content: ''; + position: absolute; + top: 39px; + left: 27px; + width: 26px; + height: 2px; + background: #495057; + } + } + + &-left { + position: relative; + width: 80px; + height: 80px; + + &.has-img { + .logo-item-upload { + display: none; + } + + &:hover { + .logo-item-upload { + display: block; + background: rgba(145, 145, 145, 0.8); + } + } + } + } + + &-right { + display: flex; + flex-direction: column; + justify-content: space-between; + color: #777777; + font-size: 0.8rem; + } + + &-title { + color: #23272B; + font-size: 1rem; + } +} \ No newline at end of file diff --git a/app/controllers/admins/platform_people_controller.rb b/app/controllers/admins/platform_people_controller.rb index 00af2af0..df56f83a 100644 --- a/app/controllers/admins/platform_people_controller.rb +++ b/app/controllers/admins/platform_people_controller.rb @@ -3,7 +3,7 @@ class Admins::PlatformPeopleController < Admins::BaseController def index sort_by = PlatformPerson.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_at' sort_direction = %w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc' - q = PlatformPerson.ransack(title_cont: params[:search]) + q = PlatformPerson.ransack(name_cont: params[:search]) people = q.result(distinct: true).order("#{sort_by} #{sort_direction}") @people = kaminari_paginate(people) end @@ -15,6 +15,7 @@ class Admins::PlatformPeopleController < Admins::BaseController def create @person = PlatformPerson.new(person_params) if @person.save + save_image_file(params[:image], @person) redirect_to admins_platform_people_path flash[:success] = '创建论坛动态成功' else @@ -30,6 +31,7 @@ class Admins::PlatformPeopleController < Admins::BaseController def update @person.attributes = person_params if @person.save + save_image_file(params[:image], @person) redirect_to admins_platform_people_path flash[:success] = '更新论坛动态成功' else @@ -56,4 +58,12 @@ class Admins::PlatformPeopleController < Admins::BaseController def person_params params.require(:platform_person).permit! end + + def save_image_file(file, topic) + return unless file.present? && file.is_a?(ActionDispatch::Http::UploadedFile) + + file_path = Util::FileManage.source_disk_filename(topic, 'image') + File.delete(file_path) if File.exist?(file_path) # 删除之前的文件 + Util.write_file(file, file_path) + end end \ No newline at end of file diff --git a/app/models/platform_person.rb b/app/models/platform_person.rb index 7ca91250..cddd34fb 100644 --- a/app/models/platform_person.rb +++ b/app/models/platform_person.rb @@ -9,7 +9,19 @@ # content :text(65535) # created_at :datetime not null # updated_at :datetime not null +# fake_login :string(255) # class PlatformPerson < ApplicationRecord + + def image + image_url('image') + end + + private + + def image_url(type) + return nil unless Util::FileManage.exists?(self, type) + Util::FileManage.source_disk_file_url(self, type) + end end diff --git a/app/views/admins/platform_people/_form_modal.html.erb b/app/views/admins/platform_people/_form_modal.html.erb new file mode 100644 index 00000000..e2c23eda --- /dev/null +++ b/app/views/admins/platform_people/_form_modal.html.erb @@ -0,0 +1,57 @@ + \ No newline at end of file diff --git a/app/views/admins/platform_people/_list.html.erb b/app/views/admins/platform_people/_list.html.erb new file mode 100644 index 00000000..27eda669 --- /dev/null +++ b/app/views/admins/platform_people/_list.html.erb @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + <% if people.present? %> + <% people.each_with_index do |p, index| %> + + + + + + + + + + <% end %> + <% else %> + <%= render 'admins/shared/no_data_for_table' %> + <% end %> + +
序号名称头像个人宣言内容用户标识操作
<%= list_index_no((params[:page] || 1).to_i, index) %><%= p.name %><%= p.announcement %><%= p.content.truncate(50) %><%= p.fake_login %> + <%= link_to "编辑", edit_admins_platform_person_path(p), remote: true, class: "action" %> + <%= link_to "删除", admins_platform_person_path(p), method: :delete, data:{confirm: "确认删除的吗?"}, class: "action" %> +
+ +<%= render partial: 'admins/shared/paginate', locals: { objects: people } %> \ No newline at end of file diff --git a/app/views/admins/platform_people/edit.js.erb b/app/views/admins/platform_people/edit.js.erb new file mode 100644 index 00000000..80643985 --- /dev/null +++ b/app/views/admins/platform_people/edit.js.erb @@ -0,0 +1,18 @@ +$("#platform-person-modals").html("<%= j render(partial: 'admins/platform_people/form_modal', locals: {type: 'update'}) %>") +$(".platform-person-change-modal").modal('show'); + +$('.logo-item-left').on("change", 'input[type="file"]', function () { + var $fileInput = $(this); + var file = this.files[0]; + var imageType = /image.*/; + if (file && file.type.match(imageType)) { + var reader = new FileReader(); + reader.onload = function () { + var $box = $fileInput.parent(); + $box.find('img').attr('src', reader.result).css('display', 'block'); + $box.addClass('has-img'); + }; + reader.readAsDataURL(file); + } else { + } +}); diff --git a/app/views/admins/platform_people/index.html.erb b/app/views/admins/platform_people/index.html.erb new file mode 100644 index 00000000..585250bb --- /dev/null +++ b/app/views/admins/platform_people/index.html.erb @@ -0,0 +1,18 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb('论坛交流管理') %> +<% end %> + +
+ <%= form_tag(admins_platform_people_path, method: :get, class: 'form-inline search-form flex-1', remote: true) do %> + <%= text_field_tag(:search, params[:search], class: 'form-control col-12 col-md-2 mr-3', placeholder: '名称检索') %> + <%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %> + + <% end %> + <%= link_to "新增", new_admins_platform_person_path, remote: true, class: "btn btn-primary pull-right", "data-disabled-with":"...新增" %> +
+ +
+ <%= render partial: 'admins/platform_people/list', locals: { people: @people } %> +
+
+
diff --git a/app/views/admins/platform_people/index.js.erb b/app/views/admins/platform_people/index.js.erb new file mode 100644 index 00000000..b6c99b4c --- /dev/null +++ b/app/views/admins/platform_people/index.js.erb @@ -0,0 +1 @@ +$('.platform-person-list-container').html("<%= j( render partial: 'admins/platform_people/list', locals: { people: @people } ) %>"); \ No newline at end of file diff --git a/app/views/admins/platform_people/new.js.erb b/app/views/admins/platform_people/new.js.erb new file mode 100644 index 00000000..80b8fea1 --- /dev/null +++ b/app/views/admins/platform_people/new.js.erb @@ -0,0 +1,18 @@ +$("#platform-person-modals").html("<%= j render(partial: 'admins/platform_people/form_modal', locals: {type: 'create'}) %>") +$(".platform-person-change-modal").modal('show'); + +$('.logo-item-left').on("change", 'input[type="file"]', function () { + var $fileInput = $(this); + var file = this.files[0]; + var imageType = /image.*/; + if (file && file.type.match(imageType)) { + var reader = new FileReader(); + reader.onload = function () { + var $box = $fileInput.parent(); + $box.find('img').attr('src', reader.result).css('display', 'block'); + $box.addClass('has-img'); + }; + reader.readAsDataURL(file); + } else { + } +}); diff --git a/db/migrate/20211206063120_add_fake_login_to_platform_people.rb b/db/migrate/20211206063120_add_fake_login_to_platform_people.rb new file mode 100644 index 00000000..fafb4b68 --- /dev/null +++ b/db/migrate/20211206063120_add_fake_login_to_platform_people.rb @@ -0,0 +1,6 @@ +class AddFakeLoginToPlatformPeople < ActiveRecord::Migration[5.2] + def change + add_column :platform_people, :fake_login, :string + remove_column :platform_people, :image_url + end +end