add: platform person admin

This commit is contained in:
yystopf 2021-12-06 16:41:12 +08:00
parent 9648afb0bb
commit 4310cc9f26
11 changed files with 265 additions and 1 deletions

View File

@ -71,6 +71,23 @@ $(document).on('turbolinks:load', function(){
$('.admin-alert-container .alert.alert-danger').alert('close'); $('.admin-alert-container .alert.alert-danger').alert('close');
}, 5000); }, 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 () { $(document).on("turbolinks:before-cache", function () {

View File

@ -58,3 +58,75 @@ input.form-control {
position: absolute; 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;
}
}

View File

@ -3,7 +3,7 @@ class Admins::PlatformPeopleController < Admins::BaseController
def index def index
sort_by = PlatformPerson.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_at' 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' 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 = q.result(distinct: true).order("#{sort_by} #{sort_direction}")
@people = kaminari_paginate(people) @people = kaminari_paginate(people)
end end
@ -15,6 +15,7 @@ class Admins::PlatformPeopleController < Admins::BaseController
def create def create
@person = PlatformPerson.new(person_params) @person = PlatformPerson.new(person_params)
if @person.save if @person.save
save_image_file(params[:image], @person)
redirect_to admins_platform_people_path redirect_to admins_platform_people_path
flash[:success] = '创建论坛动态成功' flash[:success] = '创建论坛动态成功'
else else
@ -30,6 +31,7 @@ class Admins::PlatformPeopleController < Admins::BaseController
def update def update
@person.attributes = person_params @person.attributes = person_params
if @person.save if @person.save
save_image_file(params[:image], @person)
redirect_to admins_platform_people_path redirect_to admins_platform_people_path
flash[:success] = '更新论坛动态成功' flash[:success] = '更新论坛动态成功'
else else
@ -56,4 +58,12 @@ class Admins::PlatformPeopleController < Admins::BaseController
def person_params def person_params
params.require(:platform_person).permit! params.require(:platform_person).permit!
end 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 end

View File

@ -9,7 +9,19 @@
# content :text(65535) # content :text(65535)
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# fake_login :string(255)
# #
class PlatformPerson < ApplicationRecord 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 end

View File

@ -0,0 +1,57 @@
<div class="modal fade platform-person-change-modal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><%= type == "create" ? "新增" : "编辑" %></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<%= form_for @person, url: {controller: "platform_people", action: "#{type}"}, html: { enctype: 'multipart/form-data' } do |p| %>
<div class="modal-body">
<div class="form-group">
<label>
标题 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_field :name,class: "form-control input-lg",required: true%>
</div>
<div class="logo-item">
<% logo_img = @person.image %>
<div class="logo-item-left mr-3 <%= logo_img ? 'has-img' : '' %>">
<img class="logo-item-img nav-logo-img" src="<%= logo_img %>" style="<%= logo_img.present? ? '' : 'display: none' %>"/>
<%= file_field_tag(:image, accept: 'image/png,image/jpg,image/jpeg',style: "display: none", value: params[:image]) %>
<label for="image" class="logo-item-upload" data-toggle="tooltip" data-title="选择图片"></label>
</div>
<div class="logo-item-right">
<div class="logo-item-title flex-1">logo</div>
<div>格式PNG、JPG</div>
<div>尺寸高度38px以内宽等比例缩放</div>
</div>
</div>
<div class="form-group">
<label>
个人宣言 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_field :announcement,class: "form-control input-lg",required: true%>
</div>
<div class="form-group">
<label>
内容 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_area :content,class: "form-control input-lg",required: true%>
</div>
<div class="form-group">
<label>
用户标识 <span class="ml10 color-orange mr20">*</span>
</label>
<%= p.text_field :fake_login,class: "form-control input-lg",required: true%>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<%= p.submit "确认", class: "btn btn-primary submit-btn" %>
</div>
<% end %>
</div>
</div>
</div>

View File

@ -0,0 +1,35 @@
<table class="table table-hover text-center subject-list-table">
<thead class="thead-light">
<tr>
<th width="5%">序号</th>
<th width="20%">名称</th>
<th width="20%">头像</th>
<th width="20%">个人宣言</th>
<th width="20%">内容</th>
<th width="10%">用户标识</th>
<th width="25%">操作</th>
</tr>
</thead>
<tbody>
<% if people.present? %>
<% people.each_with_index do |p, index| %>
<tr class="platform-person-item-<%= p.id %>">
<td><%= list_index_no((params[:page] || 1).to_i, index) %></td>
<td><%= p.name %></td>
<td><img style="width:150px" src="<%= p.image %>" /></td>
<td><%= p.announcement %></td>
<td><%= p.content.truncate(50) %></td>
<td><%= p.fake_login %></td>
<td class="action-container">
<%= 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" %>
</td>
</tr>
<% end %>
<% else %>
<%= render 'admins/shared/no_data_for_table' %>
<% end %>
</tbody>
</table>
<%= render partial: 'admins/shared/paginate', locals: { objects: people } %>

View File

@ -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 {
}
});

View File

@ -0,0 +1,18 @@
<% define_admin_breadcrumbs do %>
<% add_admin_breadcrumb('论坛交流管理') %>
<% end %>
<div class="box search-form-container platform-person-list-form">
<%= 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': '搜索中...') %>
<input type="reset" class="btn btn-secondary clear-btn" value="清空"/>
<% end %>
<%= link_to "新增", new_admins_platform_person_path, remote: true, class: "btn btn-primary pull-right", "data-disabled-with":"...新增" %>
</div>
<div class="box admin-list-container platform-person-list-container">
<%= render partial: 'admins/platform_people/list', locals: { people: @people } %>
</div>
<div id="platform-person-modals">
</div>

View File

@ -0,0 +1 @@
$('.platform-person-list-container').html("<%= j( render partial: 'admins/platform_people/list', locals: { people: @people } ) %>");

View File

@ -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 {
}
});

View File

@ -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