dev_cs
cxt 6 years ago
parent 393627644a
commit 8ca84977a2

@ -0,0 +1,10 @@
class ShixunListsController < ApplicationController
def index
@results = ShixunSearchService.call(search_params)
end
private
def search_params
params.permit(:keyword, :type, :page, :per_page, :order, :type, :status, :diff)
end
end

@ -1,6 +1,7 @@
class ShixunsController < ApplicationController
include ShixunsHelper
include ApplicationHelper
include ElasticsearchAble
before_action :require_login, :check_auth, except: [:download_file, :index, :menus, :show, :show_right, :ranking_list,
:discusses, :collaborators, :fork_list, :propaedeutics]
@ -131,8 +132,16 @@ class ShixunsController < ApplicationController
offset = (page.to_i - 1) * (limit.to_i)
order = params[:order] || "desc"
## 搜索关键字创建者、实训名称、院校名称
keyword = params[:search].blank? ? "*" : params[:search]
@shixuns = Shixun.search keyword, where: {id: @shixuns.pluck(:id)}, order: {"myshixuns_count" => order}, limit: limit, offset: offset
keyword = params[:keyword].to_s.strip.presence || '*'
model_options = {
index_name: [Shixun],
model_includes: Shixun.searchable_includes
}
model_options.merge(where: { id: @shixuns.pluck(:id) }).merge(order: {"myshixuns_count" => order}).merge(limit: limit, offset: offset)
model_options.merge(default_options)
@shixuns = Searchkick.search(keyword, model_options)
# @shixuns = Shixun.search keyword, where: {id: @shixuns.pluck(:id)}, order: {"myshixuns_count" => order}, limit: limit, offset: offset
@total_count = @shixuns.total_count
end

@ -51,13 +51,12 @@ module Searchable::Shixun
visits_count: visits,
challenges_count: challenges_count,
study_count: myshixuns_count,
description: description
}
end
module ClassMethods
def searchable_includes
{ user: { user_extension: :school } }
[ :shixun_info, user: { user_extension: :school } ]
end
end
end

@ -28,9 +28,11 @@ module ElasticsearchAble
end
def body_options
{
min_score: EduSetting.get('es_min_score') || 10
}
hash = {}
hash[:min_score] = (EduSetting.get('es_min_score') || 10) if keyword != '*'
hash
end
def per_page

@ -0,0 +1,56 @@
class ShixunSearchService < ApplicationService
include ElasticsearchAble
attr_reader :params
def initialize(params)
@params = params
end
def call
# 全部实训/我的实训
type = params[:type] || "all"
# 状态:已发布/未发布
status = params[:status] || "all"
# 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭)
if type == "mine"
@shixuns = User.current.shixuns.none_closed
else
if User.current.admin? || User.current.business?
@shixuns = Shixun.none_closed.where(hidden: 0)
else
none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id)
@shixuns = Shixun.where.not(id: none_shixun_ids).none_closed.where(hidden: 0)
end
end
unless status == "all"
@shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1])
end
## 筛选 难度
if params[:diff].present? && params[:diff].to_i != 0
@shixuns = @shixuns.where(trainee: params[:diff])
end
Shixun.search(keyword, search_options)
end
private
def search_options
model_options = {
includes: Shixun.searchable_includes
}
model_options.merge!(where: { id: @shixuns.pluck(:id) })
model_options.merge!(order: {"myshixuns_count" => sort_str})
model_options.merge!(default_options)
model_options
end
def sort_str
params[:order] || "desc"
end
end

@ -1,4 +1,4 @@
# json.shixun_list @shixuns do |shixun|
# json.shixun_lists @shixuns do |shixun|
# json.shixun_identifier shixun.identifier
# json.name shixun.name
# json.creator shixun.user&.full_name

@ -0,0 +1,23 @@
json.shixuns_count @results.total_count
json.shixun_list do
json.array! @results.with_highlights(multiple: true) do |obj, highlights|
puts obj
json.merge! obj.to_searchable_json
json.challenge_names obj.challenges.pluck(:subject)
# 去除开头标点符号
reg = /^[,。?:;‘’!“”—……、]/
highlights[:description]&.first&.sub!(reg, '')
highlights[:content]&.first&.sub!(reg, '')
json.title highlights.delete(:name)&.join('...') || obj.searchable_title
json.description highlights[:description]&.join('...') || obj.description
json.content highlights
json.level level_to_s(obj.trainee)
json.subjects obj.subjects.uniq do |subject|
json.(subject, :id, :name)
end
end
end

@ -174,7 +174,10 @@ Rails.application.routes.draw do
end
end
resources :shixun_lists
resources :shixuns, param: :identifier do
collection do
get :menus
get :get_recommend_shixuns
@ -182,7 +185,7 @@ Rails.application.routes.draw do
get :get_mirror_script
post :apply_shixun_mirror
get :download_file
get :shixun_list
get :shixun_lists
end
member do

@ -502,9 +502,9 @@ class ShixunHomework extends Component{
// }).then((result)=>{
// if(result.status===200){
//
// let shixun_list=result.data.shixun_list;
// for(var i=0; i<shixun_list.length;i++){
// newshixunmodallists.push(shixun_list[i])
// let shixun_lists=result.data.shixun_lists;
// for(var i=0; i<shixun_lists.length;i++){
// newshixunmodallists.push(shixun_lists[i])
// }
// this.setState({
// shixunmodal:true,
@ -540,9 +540,9 @@ class ShixunHomework extends Component{
// }).then((result)=>{
// if(result.status===200){
//
// let shixun_list=result.data.subject_list;
// for(var i=0; i<shixun_list.length;i++){
// newshixunmodallists.push(shixun_list[i])
// let shixun_lists=result.data.subject_list;
// for(var i=0; i<shixun_lists.length;i++){
// newshixunmodallists.push(shixun_lists[i])
// }
// this.setState({
// shixunpath:true,

Loading…
Cancel
Save