This commit is contained in:
caishi 2019-10-22 17:55:55 +08:00
commit 70630df038
10 changed files with 135 additions and 95 deletions

View File

@ -61,6 +61,8 @@ simple_intruduce(requires): String, 新闻简介
remote_url(requires): String, 新闻访问地址
platform(requires): String, 该条数据的来源{educoder | trusite | ihub | gcc | openi |toschina | military | jiaotong_university}; 如educoder说明是来源educoder平台
返回值
```

View File

@ -41,6 +41,7 @@ module Mobile
requires :title, type: String
requires :simple_intruduce, type: String
requires :remote_url, type: String, regexp: Home::REG_URL_FORMAT
requires :platform, type: String, desc: "platform requires." , values: DynamicNew::PLATFORMS
end
put 'sync_news' do
authenticate_token!
@ -50,7 +51,8 @@ module Mobile
remote_url: params[:remote_url],
category_id: params[:category_id],
content: params[:content],
cover_remote_url: params[:cover_remote_url]
cover_remote_url: params[:cover_remote_url],
platform: params[:platform]
}
begin
DynamicNewsService.new.sync(news_params)

View File

@ -1,14 +1,14 @@
class DynamicNewsController < ApplicationController
layout 'admin', only: [:new, :index, :destroy]
layout 'admin', only: [:new, :destroy, :edit]
before_filter :require_admin, :only => [:new, :create, :destroy]
before_filter :find_dynamic_new, :only => [:show, :destroy]
before_filter :require_admin, :except => [:index, :show]
before_filter :find_dynamic_new, :only => [:destroy, :edit, :update]
def index
end
def new
@category_id = params[:category_id] || 0
@dynamic_new = DynamicNew.new
end
@ -16,20 +16,22 @@ class DynamicNewsController < ApplicationController
tmp_file = params[:cover]
@dynamic_new = DynamicNew.new(params[:dynamic_new])
begin
ActiveRecord::Base.transaction do
identity = SecureRandom.uuid
unless tmp_file.nil?
File.open(Rails.root.join('public', 'images', 'dynamic_new', identity.to_s+'.png'), 'wb') do |file|
file.write(tmp_file.read)
end
@dynamic_new.cover_remote_url = "/images/dynamic_new/#{identity}.png"
identity = SecureRandom.uuid
unless tmp_file.nil?
File.open(Rails.root.join('public', 'images', 'dynamic_new', identity.to_s+'.png'), 'wb') do |file|
file.write(tmp_file.read)
end
# TODO: 如下警告信息导致值写不进库
# WARNING: Can't mass-assign protected attributes: simple_intruduce,
@dynamic_new.simple_intruduce = params[:dynamic_new][:simple_intruduce]
@dynamic_new.save
@dynamic_new.cover_remote_url = "/images/dynamic_new/#{identity}.png"
end
redirect_to admin_dynamic_news_path
# TODO: 如下警告信息导致值写不进库
# WARNING: Can't mass-assign protected attributes: simple_intruduce,
@dynamic_new.simple_intruduce = params[:dynamic_new][:simple_intruduce]
if @dynamic_new.save!
redirect_to admin_dynamic_news_path
else
render :action => 'new'
end
rescue => e
logger.info e
render 'new'
@ -41,6 +43,14 @@ class DynamicNewsController < ApplicationController
redirect_to admin_dynamic_news_path
end
def edit
end
def update
@dynamic_new.update_attributes!(params[:dynamic_new])
redirect_to admin_dynamic_news_path
end
def show
end

View File

@ -1,4 +1,5 @@
class DynamicNew < ActiveRecord::Base
PLATFORMS = %w(educoder trusite ihub gcc openi toschina military jiaotong_university)
belongs_to :category
validates_presence_of :title, :simple_intruduce, :category_id

View File

@ -54,8 +54,8 @@
<%= format_time(dynamic_new.created_at) %>
</td>
<td class="buttons" style="vertical-align: middle;text-align:center">
<%= link_to("修改", upload_logo_school_path(dynamic_new.id,:school_name => @school_name), :class => 'icon icon-copy') %>
<%= link_to(l(:button_delete), dynamic_news_path(dynamic_new), :method => :delete,:confirm => l(:text_are_you_sure), :class => 'icon icon-del') %>
<%= link_to("修改", edit_dynamic_news_path(dynamic_new), :class => 'icon icon-copy') %>
<%= link_to(l(:button_delete), dynamic_news_path(dynamic_new), :method => :delete,:confirm => l(:text_are_you_sure), :class => 'icon icon-del') %>
</td>
</tr>
<% end %>
@ -64,12 +64,3 @@
<div class="pagination">
<%= pagination_links_full @obj_pages, @obj_count ,:per_page_links => true, :remote => true, :flag => true, :is_new => true %>
</div>
<script>
function delcfm() {
if (!confirm("删除项目会一并删除项目的关联信息,确认要删除吗?")) {
window.event.returnValue = false;
}
}
</script>

View File

@ -0,0 +1,70 @@
<%= form_for(@dynamic_new, :url=> { :action => params[:action] == 'new'? :create : :update }, :html => {:id =>'system_messages-form',:multipart => true }) do |f| %>
<li>
<%= image_tag(params[:action] == 'new' ? '' : @dynamic_new.cover_url, id: "avatar_image", :class=>"school_avatar dynamic_new_image" )%>
<a type="button" onclick="$('#file').click();" style="margin: 86px 0 0 15px;float: left;padding: 2px 5px;border: 1px solid #eaeaea;cursor: pointer;text-decoration: none;width: 55px;">上传封面</a>
<%= file_field_tag 'cover',:style => "display:none;", :id => "file", :onchange => "showPreview(this)"%>
<div style="clear: both;"></div>
</li>
<li>
<label><span class="">*</span>&nbsp;<%= l(:field_category) %>&nbsp;&nbsp;</label>
<%= f.select :category_id, options_for_select(Category.all.collect{|item| [item.name, item.id]}, @category_id), :class => "small" %>
<p id="title_notice_span" class="ml55"></p>
</li>
<li>
<label><span class="">*</span>&nbsp;<%= l(:field_title) %>&nbsp;&nbsp;</label>
<input type="text" name="dynamic_new[title]" class="hwork_input_news" id="dynamic_new_title" width="576px" onblur="regexTitle($(this));" maxlength="255" placeholder="255个字符以内" value="<%= @dynamic_new.title %>">
<p id="title_notice_span" class="ml55"></p>
</li>
<li>
<label><span class="">*</span>&nbsp;<%= l(:field_summary) %>&nbsp;&nbsp;</label>
<input type="textarea" name="dynamic_new[simple_intruduce]" class="hwork_input_news" id="dynamic_new_simple_introduce" width="576px" onblur="regexTitle($(this));" maxlength="255" placeholder="255个字符以内" value="<%= @dynamic_new.simple_intruduce %>">
<p id="title_notice_span" class="ml55"></p>
</li>
<li>
<label class="fl" >&nbsp;&nbsp;<span class="c_red"></span>&nbsp;<%= l(:field_content) %>&nbsp;&nbsp;</label>
<%= f.kindeditor :content,:width=>'87.5%',:editor_id=>'dynamic_new_editor' %>
<p id="content_notice_span" class="ml55"></p>
</li>
<div>
<p id="content_notice_span" class="ml55"></p>
</div>
<div class="cl"></div>
<div class="actions">
<%= link_to l(:label_submit), "javascript:void(0)", :class => "btn_message_free", :onclick => "dynamic_new_editor.sync();submit_message($(this));" %>
</div>
<% end %>
<script>
function system_message_length() {
var obj = dynamic_new_editor.html();
if (obj.length > 10000) {
$("#content_notice_span").text("内容过长超过10000个字符");
$("#content_notice_span").css('color', '#ff0000');
$("#content_notice_span").focus();
return false;
} else {
$("#content_notice_span").text("填写正确");
$("#content_notice_span").css('color', '#008000');
return true;
}
}
function regexTitle(obj) {
var title = obj.val();
if (title.length == 0) {
$("#title_notice_span").text("标题不能为空").css("color", "#ff0000").focus();
return false;
} else if (title.length > 255) {
$("#title_notice_span").text("标题长度过长不能超过255个字符").css("color", "#ff0000").focus();
return false;
} else {
$("#title_notice_span").text("填写正确").css("color", "#008000");
return true;
}
}
function submit_message(dom) {
if (system_message_length() && regexTitle($("#dynamic_new_title")) && regexTitle($("#dynamic_new_simple_introduce"))) {
$("#system_messages-form").submit();
}
}
</script>

View File

@ -0,0 +1,23 @@
<script>
function showPreview(source) {
var file = source.files[0];
if (window.FileReader) {
var fr = new FileReader();
fr.onloadend = function (e) {
document.getElementById("avatar_image").src = e.target.result;
};
fr.readAsDataURL(file);
}
}
</script>
<%= content_for(:header_tags) do %>
<%= import_ke(enable_at: false) %>
<% end %>
<h3 style="float: left">
<%=l(:label_new_dynamic_new)%>
</h3><br/>
<div style="padding-top: 20px; padding-left: 5px;">
<%= error_messages_for 'dynamic_new' %>
<%= render 'form' %>
</div>

View File

@ -18,70 +18,5 @@
<%=l(:label_new_dynamic_new)%>
</h3><br/>
<div style="padding-top: 20px; padding-left: 5px;">
<%= form_for(@dynamic_new, :url=> { :action => :create }, :html => {:id =>'system_messages-form',:multipart => true }) do |f| %>
<li>
<input type="hidden" value="<%= @school_name%>" name="school_name">
<%= image_tag('', id: "avatar_image", :class=>"school_avatar dynamic_new_image" )%>
<a type="button" onclick="$('#file').click();" style="margin: 86px 0 0 15px;float: left;padding: 2px 5px;border: 1px solid #eaeaea;cursor: pointer;text-decoration: none;width: 55px;">上传图片</a>
<%= file_field_tag 'cover',:style => "display:none;", :id => "file", :onchange => "showPreview(this)"%>
<div style="clear: both;"></div>
</li>
<li>
<label><span class="">*</span>&nbsp;<%= l(:field_title) %>&nbsp;&nbsp;</label>
<input type="text" name="dynamic_new[title]" class="hwork_input_news" id="system_message_subject" width="576px" onblur="regexTitle($(this));" maxlength="255" placeholder="255个字符以内" value="">
<p id="title_notice_span" class="ml55"></p>
</li>
<li>
<label><span class="">*</span>&nbsp;<%= l(:field_summary) %>&nbsp;&nbsp;</label>
<input type="textarea" name="dynamic_new[simple_intruduce]" class="hwork_input_news" id="system_message_subject" width="576px" onblur="regexTitle($(this));" maxlength="255" placeholder="255个字符以内" value="">
<p id="title_notice_span" class="ml55"></p>
</li>
<li>
<label class="fl" >&nbsp;&nbsp;<span class="c_red"></span>&nbsp;<%= l(:field_content) %>&nbsp;&nbsp;</label>
<%= f.kindeditor :content,:width=>'87.5%',:editor_id=>'system_message_editor' %>
<p id="content_notice_span" class="ml55"></p>
</li>
<div>
<p id="content_notice_span" class="ml55"></p>
</div>
<div class="cl"></div>
<div class="actions">
<%= link_to l(:label_submit), "javascript:void(0)", :class => "btn_message_free", :onclick => "system_message_editor.sync();submit_message($(this));" %>
</div>
<% end %>
<%= render 'form' %>
</div>
<script>
function system_message_length() {
var obj = system_message_editor.html();
if (obj.length > 10000) {
$("#content_notice_span").text("内容过长超过10000个字符");
$("#content_notice_span").css('color', '#ff0000');
$("#content_notice_span").focus();
return false;
} else {
$("#content_notice_span").text("填写正确");
$("#content_notice_span").css('color', '#008000');
return true;
}
}
function regexTitle(obj) {
var title = obj.val();
if (title.length == 0) {
$("#title_notice_span").text("标题不能为空").css("color", "#ff0000").focus();
return false;
} else if (title.length > 255) {
$("#title_notice_span").text("标题长度过长不能超过255个字符").css("color", "#ff0000").focus();
return false;
} else {
$("#title_notice_span").text("填写正确").css("color", "#008000");
return true;
}
}
function submit_message(dom) {
if (system_message_length() && regexTitle($("#system_message_subject"))) {
$("#system_messages-form").submit();
}
}
</script>

View File

@ -0,0 +1,5 @@
class AddPlatformToDynamicNews < ActiveRecord::Migration
def change
add_column :dynamic_news, :platform, :string
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20191021063319) do
ActiveRecord::Schema.define(:version => 20191022031808) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -958,6 +958,7 @@ ActiveRecord::Schema.define(:version => 20191021063319) do
t.datetime "updated_at", :null => false
t.string "simple_intruduce"
t.integer "category_id", :null => false
t.string "platform"
end
add_index "dynamic_news", ["title"], :name => "index_dynamic_news_on_title"