个人主页性能优化

dev_newv
huang 6 years ago
parent 17f9de4198
commit 4e6e724dca

@ -30,19 +30,30 @@ class WelcomeController < ApplicationController
require 'simple_xlsx_reader' require 'simple_xlsx_reader'
def index def index
images = PortalImage.where(status: true).order("position asc")
@images_url = []
images.each do |image|
@images_url << {path: image.link, image_url: "/images/avatars/PortalImage/#{image.id}"}
end
# 目录分级
repertoires = Repertoire.includes(sub_repertoires: :tag_repertoires).order("updated_at asc")
@rep_list = []
repertoires.each do |rep|
sub_rep_list = []
rep.sub_repertoires.each do |sub_rep|
tag_rep_list = []
sub_rep.tag_repertoires.each do |tag_rep|
tag_rep_list << {tag_id: tag_rep.id, tag_name: tag_rep.name}
end
sub_rep_list << {sub_rep_id: sub_rep.id, sub_rep_name: sub_rep.name, tag_rep_list: tag_rep_list}
end
@rep_list << {rep_id: rep.id, rep_name: rep.name, sub_rep_list: sub_rep_list}
end
@shixuns = Shixun.select([:id, :user_id, :homepage_show, :identifier, :status, :name, :challenges_count, :myshixuns_count, :use_scope, @shixuns = Shixun.select([:id, :user_id, :homepage_show, :identifier, :status, :name, :challenges_count, :myshixuns_count, :use_scope,
:trainee]).where(:homepage_show => 1).includes(:users, :shixun_tag_repertoires, challenges: :challenge_chooses).order("myshixuns_count desc").limit(8) :trainee, :averge_star]).where(homepage_show: 1).includes(:tag_repertoires, :challenges).order("myshixuns_count desc").limit(8)
@subjects = Subject.select([:id, :name, :homepage_show, :user_id, :visits, :stages_count, :score_count, :repertoire_id]).where(:homepage_show => 1).includes(:users, stages: :stage_shixuns) @subjects = Subject.where(homepage_show: 1).includes(:shixuns).limit(8)
@subjects.each do |subject| @tea_users = User.where(homepage_teacher: 1).includes(:user_extensions).limit(10).order("experience desc")
subject[:myshixun_member_count] = Myshixun.where(:shixun_id=>subject.stage_shixuns.map(&:shixun_id)).count @stu_users = User.includes(:user_extensions).where(user_extensions: {identity: 1}).limit(10).order("experience desc")
end
@subjects = @subjects.sort{|x,y| y[:myshixun_member_count] <=> x[:myshixun_member_count] }[0, 8]
@tea_users = User.where(:homepage_teacher => 1).includes(:user_extensions).limit(10).order("experience desc")
@eng_users = User.where(:homepage_engineer => 1).includes(:user_extensions).limit(10).order("experience desc")
@eng_users = User.find_by_sql("select u.* from users u join user_extensions ue on ue.user_id = u.id where ue.identity = 1 order by experience desc limit 10") if @eng_users.blank?
@repertoires = Repertoire.includes(sub_repertoires: [:tag_repertoires]).order("updated_at asc")
@images = PortalImage.where(:status => true).order("position asc")
logger.info("########images: #{@images.count}")
render :layout => 'educoder' render :layout => 'educoder'
end end

@ -1,25 +1,26 @@
<% result = User.current.is_certification_teacher || User.current.admin? %>
<% shixuns.each do |shixun| %> <% shixuns.each do |shixun| %>
<div class="square-Item"> <div class="square-Item">
<% if shixun.tag_repertoires.first.present? %> <% if shixun.tag_repertoires.first.present? %>
<div class="tag-green"><span class="tag-name"><%= shixun.tag_repertoires.first.try(:name) %></span><img src="/images/educoder/tag2.png"></div> <div class="tag-green"><span class="tag-name"><%= shixun.tag_repertoires.first.try(:name) %></span><img src="/images/educoder/tag2.png"></div>
<% end %> <% end %>
<% if params[:controller] != "welcome" && shixun_view_allow(shixun, true) %> <% unless result %>
<div class="closeSquare"> <div class="closeSquare">
<img src="/images/educoder/icon/lockclose.svg" class="mt80 mb25"> <img src="/images/educoder/icon/lockclose.svg" class="mt80 mb25">
<p class="font-14 color-white">非试用内容,需要授权</p> <p class="font-14 color-white">暂未公开</p>
</div> </div>
<% end %> <% end %>
<a href="<%= shixun_path(shixun) %>" class="square-img" target="_blank"> <a href="<%= result ? shixun_path(shixun) : "javascript:void(0);" %>" class="square-img" target="_blank">
<%= image_tag(url_to_avatar(shixun)) %> <%= image_tag(url_to_avatar(shixun)) %>
</a> </a>
<div class="square-main"> <div class="square-main">
<p class="task-hide"> <p class="task-hide">
<a href="<%= shixun_path(shixun) %>" class="justify color-grey-name" title="<%= shixun.name %>" target="_blank"><%= shixun.name %></a> <a href="<%= result ? shixun_path(shixun) : "javascript:void(0);" %>" class="justify color-grey-name" title="<%= shixun.name %>" target="_blank"><%= shixun.name %></a>
</p> </p>
<!--五星评分--> <!--五星评分-->
<p class="clearfix mt3 mb3"><span class="rateYoStar fl" data-star="<%= shixun.shixun_preference %>" style="padding: 0px;width: 100%;height:20px;line-height:19px;width: 90px;"></span><span class="fl ml10 font-12 color-grey-9 lineh-12 mt3"><%= shixun.shixun_preference %>分</span></p> <p class="clearfix mt3 mb3"><span class="rateYoStar fl" data-star="<%= shixun.averge_star %>" style="padding: 0px;width: 100%;height:20px;line-height:19px;width: 90px;">
</span><span class="fl ml10 font-12 color-grey-9 lineh-12 mt3"><%= shixun.averge_star %>分</span></p>
<p class="clearfix mt8 font-12 color-grey-B4"> <p class="clearfix mt8 font-12 color-grey-B4">
<% if shixun.challenges_count > 0 %> <% if shixun.challenges_count > 0 %>
@ -32,9 +33,9 @@
<i class="iconfont icon-jingyan fl mr3" data-tip-down="经验值"></i><%= shixun.shixun_score %> <i class="iconfont icon-jingyan fl mr3" data-tip-down="经验值"></i><%= shixun.shixun_score %>
</span> </span>
<% end %> <% end %>
<% if shixun.myshixuns.count > 0 %> <% if shixun.myshixuns_count > 0 %>
<span class="mr10 fl squareIconSpan"> <span class="mr10 fl squareIconSpan">
<i class="iconfont icon-chengyuan fl mr3" data-tip-down="学习人数"></i><%= shixun.myshixuns.count %> <i class="iconfont icon-chengyuan fl mr3" data-tip-down="学习人数"></i><%= shixun.myshixuns_count %>
</span> </span>
<% end %> <% end %>
<span class="fr color-grey-B3 squareIconSpan"><%= shixun.shixun_level %></span> <span class="fr color-grey-B3 squareIconSpan"><%= shixun.shixun_level %></span>
@ -65,3 +66,75 @@
}); });
}) })
</script> </script>
<% if false %>
<% result = User.current.is_certification_teacher || User.current.admin? %>
<% shixuns.each do |shixun| %>
<div class="square-Item">
<% if shixun.tag_repertoires.first.present? %>
<div class="tag-green"><span class="tag-name"><%= shixun.tag_repertoires.first.try(:name) %></span><img src="/images/educoder/tag2.png"></div>
<% end %>
<% unless result %>
<div class="closeSquare">
<img src="/images/educoder/icon/lockclose.svg" class="mt80 mb25">
<p class="font-14 color-white">暂未公开</p>
</div>
<% end %>
<a href="<%= result ? shixun_path(shixun) : "javascript:void(0);" %>" class="square-img" target="_blank">
<%= image_tag(url_to_avatar(shixun)) %>
</a>
<div class="square-main">
<p class="task-hide">
<a href="<%= result ? shixun_path(shixun) : "javascript:void(0);" %>" class="justify color-grey-name" title="<%= shixun.name %>" target="_blank"><%= shixun.name %></a>
</p>
<!--五星评分-->
<p class="clearfix mt3 mb3"><span class="rateYoStar fl" data-star="<%= shixun.shixun_preference %>" style="padding: 0px;width: 100%;height:20px;line-height:19px;width: 90px;"></span><span class="fl ml10 font-12 color-grey-9 lineh-12 mt3"><%= shixun.shixun_preference %>分</span></p>
<p class="clearfix mt8 font-12 color-grey-B4">
<% if shixun.challenges_count > 0 %>
<span class="mr10 fl squareIconSpan">
<i class="iconfont icon-shixunguanqia fl mr3" data-tip-down="关卡"></i><%= shixun.challenges_count %>
</span>
<% end %>
<% if shixun.shixun_score > 0 %>
<span class="mr10 fl squareIconSpan">
<i class="iconfont icon-jingyan fl mr3" data-tip-down="经验值"></i><%= shixun.shixun_score %>
</span>
<% end %>
<% if shixun.myshixuns.count > 0 %>
<span class="mr10 fl squareIconSpan">
<i class="iconfont icon-chengyuan fl mr3" data-tip-down="学习人数"></i><%= shixun.myshixuns.count %>
</span>
<% end %>
<span class="fr color-grey-B3 squareIconSpan"><%= shixun.shixun_level %></span>
</p>
</div>
</div>
<% end %>
<script>
$(function(){
$(".rateYoStar").raty({
hints: ['', '', '', '', ''],
readOnly: true,
path:"/images/educoder",
starOff: 'star-off.png',
starHalf:'star-half.png',
starOn: 'star-on.png',
size:'14',
score:function(){
var s=$(this).attr("data-star");
var yu=(s*10)%10;
if(yu>0){
return parseFloat(Math.floor((s*10)/10)+".5");
}else{
return $(this).attr("data-star");
}
}
});
})
</script>
<% end %>

@ -1,37 +1,29 @@
<% result = User.current.is_certification_teacher || User.current.admin? %>
<% subjects.each do |subject| %> <% subjects.each do |subject| %>
<div class="square-Item"> <div class="square-Item">
<% if subject.repertoire.present? %> <% if subject.repertoire.present? %>
<div class="tag-green"><span class="tag-name"><%= subject.repertoire.name %></span><img src="/images/educoder/tag2.png"></div> <div class="tag-green"><span class="tag-name"><%= subject.repertoire.name %></span><img src="/images/educoder/tag2.png"></div>
<% end %> <% end %>
<% if params[:controller] != "welcome" && subject.status < 2 && !User.current.member_of_subject?(subject) %> <% unless result %>
<div class="closeSquare"> <div class="closeSquare">
<img src="/images/educoder/icon/lockclose.svg" class="mt80 mb25"/> <img src="/images/educoder/icon/lockclose.svg" class="mt80 mb25"/>
<p class="font-14 color-white">非试用内容,需要授权</p> <p class="font-14 color-white">暂未开发</p>
</div> </div>
<% end %> <% end %>
<a href="<%= subject_path(subject) %>" class="square-img" target="_blank"><%= image_tag(url_to_avatar(subject)) %></a> <a href="<%= result ? subject_path(subject) : "javascript:void(0);" %>" class="square-img" target="_blank"><%= image_tag(url_to_avatar(subject)) %></a>
<div class="square-main"> <div class="square-main">
<p class="task-hide"> <p class="task-hide">
<%= link_to subject.name, subject_path(subject), :class => "justify color-grey-name", :target => "_blank" %> <%= link_to subject.name, "#{result ? subject_path(subject) : "javascript:void(0);"}", :class => "justify color-grey-name", :target => "_blank" %>
</p> </p>
<p class="clearfix mt8 font-12 color-grey-B4"> <p class="clearfix mt8 font-12 color-grey-B4">
<% if subject.stages_count > 0 %> <% if subject.stages_count > 0 %>
<span class="mr10 fl squareIconSpan"> <span class="mr10 fl squareIconSpan">
<i class="iconfont icon-shixun fl mr3" data-tip-down="章节"></i><%= subject.stages_count %></span> <i class="iconfont icon-shixun fl mr3" data-tip-down="章节"></i><%= subject.stages_count %></span>
<% end %> <% end %>
<% if params[:controller] == "welcome" %>
<span class="mr10 fl squareIconSpan">
<i class="iconfont icon-jingyan fl mr3" data-tip-down="经验值"></i><%= subject.subject_shixun_score + subject.subject_shixun_choose_score %></span> <% if subject.member_count > 0 %>
<% else %> <span class="mr10 fl squareIconSpan"><i class="iconfont icon-chengyuan fl mr3" data-tip-down="学习人数"></i><%= subject.member_count %></span>
<% if (subject.subject_shixun_score + subject.subject_shixun_choose_score) > 0 %>
<span class="mr10 fl squareIconSpan">
<i class="iconfont icon-jingyan fl mr3" data-tip-down="经验值"></i><%= subject.subject_shixun_score + subject.subject_shixun_choose_score %></span>
<% end %>
<% end %>
<% myshixun_member_count = subject.respond_to?("myshixun_member_count") ? subject.myshixun_member_count : Myshixun.where(:shixun_id=>subject.stage_shixuns.map(&:shixun_id)).count %>
<%# REDO这块的关联关系有问题所以这个统计的N+1问题没法避免。可以考虑改关联关系或者写单字段 %>
<% if myshixun_member_count > 0 %>
<span class="mr10 fl squareIconSpan"><i class="iconfont icon-chengyuan fl mr3" data-tip-down="学习人数"></i><%= myshixun_member_count %></span>
<% end %> <% end %>
</p> </p>
</div> </div>

@ -7,9 +7,9 @@
<div class="banner"> <div class="banner">
<ul class="img"> <ul class="img">
<!--<li><a href="https://www.educoder.net/courses/1410" target="_blank"><img src="/images/educoder/recom_1.jpg" id="b-img-1"/></a></li>--> <!--<li><a href="https://www.educoder.net/courses/1410" target="_blank"><img src="/images/educoder/recom_1.jpg" id="b-img-1"/></a></li>-->
<% if @images.present? %> <% if @images_url.present? %>
<% @images.each do |image| %> <% @images_url.each do |image| %>
<li><a href="<%= image.link %>" target="_blank"><img src="/images/avatars/PortalImage/<%= image.id %>" id="b-img-1"/></a></li> <li><a href="<%= image[:path] %>" target="_blank"><img src="<%= image[:image_url] %>" id="b-img-1"/></a></li>
<% end %> <% end %>
<% else %> <% else %>
<li><a href="<%= competition_path(:id => "gcc-dev-2018") %>" target="_blank"><img src="/images/educoder/recom_1.jpg" id="b-img-1"/></a></li> <li><a href="<%= competition_path(:id => "gcc-dev-2018") %>" target="_blank"><img src="/images/educoder/recom_1.jpg" id="b-img-1"/></a></li>
@ -28,15 +28,17 @@
<div class="user_navlist"> <div class="user_navlist">
<div class="user_navlist_black"> <div class="user_navlist_black">
<ul class="black_nav_list"> <ul class="black_nav_list">
<% @repertoires.each do |rep| %> <% @rep_list.each do |rep| %>
<li class="welcome_shixun_index" data-id="<%= rep.id %>" data-type="rep"> <li class="welcome_shixun_index" data-id="<%= rep[:rep_id] %>" data-type="rep">
<span class="black_nav_span clearfix font-12"><%= rep.name %><img src="/images/educoder/icon/downarrowsmall-right.svg" class="fr mt15 transform-90"></span> <span class="black_nav_span clearfix font-12"><%= rep[:rep_name] %><img src="/images/educoder/icon/downarrowsmall-right.svg" class="fr mt15 transform-90"></span>
<ul class="user_navlist_white"> <ul class="user_navlist_white">
<% rep.sub_repertoires.each do |sub| %> <% rep[:sub_rep_list].each do |sub| %>
<div class="clearfix mt20 mb5 pb10 navlistpanel-line"> <div class="clearfix mt20 mb5 pb10 navlistpanel-line">
<p class="little-title clearfix welcome_shixun_index" data-id="<%= sub.id %>" data-rep-id="<%= rep.id %>" data-type="sub"><%= sub.name %></p> <p class="little-title clearfix welcome_shixun_index" data-id="<%= sub[:sub_rep_id] %>"
<% sub.tag_repertoires.each do |tag| %> data-rep-id="<%= rep[:id] %>" data-type="sub"><%= sub[:sub_rep_name] %></p>
<a href="javascript:void(0)" class="welcome_shixun_index" data-id="<%= tag.id %>" data-rep-id="<%= rep.id %>" data-sub-id="<%= sub.id %>" data-type="tag"><%= tag.name %></a> <% sub[:tag_rep_list].each do |tag| %>
<a href="javascript:void(0)" class="welcome_shixun_index" data-id="<%= tag[:tag_id] %>"
data-rep-id="<%= rep[:id] %>" data-sub-id="<%= sub[:sub_rep_id] %>" data-type="tag"><%= tag[:tag_name] %></a>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
@ -55,7 +57,7 @@
<p class="color-dark edu-txt-center font-24" style="line-height: 30px">精选实训</p> <p class="color-dark edu-txt-center font-24" style="line-height: 30px">精选实训</p>
<p class="color-grey-cd font-12">SELECTED TRAINING</p> <p class="color-grey-cd font-12">SELECTED TRAINING</p>
</div> </div>
<a href="<%= shixuns_path() %>" class="moreitem">更多<i class="fa fa-angle-right ml5"></i></a> <a href="<%= shixuns_path %>" class="moreitem">更多<i class="fa fa-angle-right ml5"></i></a>
<div class="square-list clearfix"> <div class="square-list clearfix">
<%= render :partial => "shixuns/shixun_item", :locals => {:shixuns => @shixuns} %> <%= render :partial => "shixuns/shixun_item", :locals => {:shixuns => @shixuns} %>
</div> </div>
@ -133,35 +135,35 @@
</div> </div>
<div class="ranking clearfix"> <div class="ranking clearfix">
<ul class="grade color-dark"> <ul class="grade color-dark">
<% if @eng_users[1] %> <% if @stu_users[1] %>
<li class="mt35 pr"> <li class="mt35 pr">
<img src="/images/educoder/huangguan-two.png" class="huangguan"/> <img src="/images/educoder/huangguan-two.png" class="huangguan"/>
<a href="/users/<%= @eng_users[1].try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(@eng_users[1])) %> <a href="/users/<%= @stu_users[1].try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(@stu_users[1])) %>
<span class="task-hide rankName"><%= @eng_users[1].try(:show_name) %></span> <span class="task-hide rankName"><%= @stu_users[1].try(:show_name) %></span>
</a> </a>
</li> </li>
<% end %> <% end %>
<% if @eng_users[0] %> <% if @stu_users[0] %>
<li class="pr"> <li class="pr">
<img src="/images/educoder/huangguan.png" class="huangguan"/> <img src="/images/educoder/huangguan.png" class="huangguan"/>
<a href="/users/<%= @eng_users[0].try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(@eng_users[0])) %> <a href="/users/<%= @stu_users[0].try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(@stu_users[0])) %>
<span class="task-hide rankName"><%= @eng_users[0].try(:show_name) %></span> <span class="task-hide rankName"><%= @stu_users[0].try(:show_name) %></span>
</a> </a>
</li> </li>
<% end %> <% end %>
<% if @eng_users[2] %> <% if @stu_users[2] %>
<li class="mt35 pr"> <li class="mt35 pr">
<img src="/images/educoder/huangguan-three.png" class="huangguan"/> <img src="/images/educoder/huangguan-three.png" class="huangguan"/>
<a href="/users/<%= (@eng_users[2].try(:login)) %>" class="color-dark"><%= image_tag(url_to_avatar(@eng_users[2])) %> <a href="/users/<%= (@stu_users[2].try(:login)) %>" class="color-dark"><%= image_tag(url_to_avatar(@stu_users[2])) %>
<span class="task-hide rankName"><%= @eng_users[2].try(:show_name) %></span> <span class="task-hide rankName"><%= @stu_users[2].try(:show_name) %></span>
</a> </a>
</li> </li>
<% end %> <% end %>
</ul> </ul>
<br/> <br/>
<ul class="grade mt30"> <ul class="grade mt30">
<% if @eng_users.size > 3 %> <% if @stu_users.size > 3 %>
<% @eng_users[3..@eng_users.size].each do |e_u| %> <% @stu_users[3..@stu_users.size].each do |e_u| %>
<li> <li>
<a href="/users/<%= e_u.try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(e_u)) %> <a href="/users/<%= e_u.try(:login) %>" class="color-dark"><%= image_tag(url_to_avatar(e_u)) %>
<span class="task-hide rankName"><%= e_u.try(:show_name) %></span> <span class="task-hide rankName"><%= e_u.try(:show_name) %></span>

Loading…
Cancel
Save