From f74e2cd231c6af19047cfef12a571bdaddbcd4ad Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 19 Sep 2019 11:22:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BC=80=E6=94=BE=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 11 +++++++++++ app/models/user.rb | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2ce52d405..8797c4074 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,7 @@ class ApplicationController < ActionController::Base #before_action :check_account DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + OPENKEY = "79e33abd4b6588941ab7622aed1e67e8" helper_method :current_user @@ -246,6 +247,16 @@ class ApplicationController < ActionController::Base User.current = find_current_user uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) + # 开放课程通过链接访问的用户 + if !User.current.logged? && !params[:chinaoocTimestamp].blank? && !params[:websiteName].blank? && !params[:chinaoocKey].blank? + content = "#{OPENKEY}#{params[:websiteName]}#{params[:chinaoocTimestamp]}" + if Digest::MD5.hexdigest(content) == params[:chinaoocKey] + user = User.open_class_user + start_user_session(user) if user + User.current = user + end + end + if !User.current.logged? && Rails.env.development? User.current = User.find 1 end diff --git a/app/models/user.rb b/app/models/user.rb index 12759ea4d..ebf68cce6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -610,6 +610,29 @@ class User < ApplicationRecord admin? || business? end + # 149课程的评审用户数据创建(包含创建课堂学生) + def open_class_user + user = User.find_by(login: "OpenClassUser") + unless user + ActiveRecord::Base.transaction do + user_params = {status: 1, login: "OpenClassUser", lastname: "开放课程", + nickname: "开放课程", professional_certification: 1, certification: 1, grade: 0, + password: "12345678", phone: "11122223333", profile_completed: 1} + user = User.create!(user_params) + + UserExtension.create!(user_id: user.id, gender: 0, school_id: 117, :identity => 1, :student_id => "openclassuser") + + subject = Subject.find_by(id: 149) + if subject + subject.courses.each do |course| + CourseMember.create!(course_id: course.id, role: 4, user_id: user.id) if !course.course_students.exists?(user_id: user.id) + end + end + end + end + user + end + protected def validate_password_length # 管理员的初始密码是5位 From 78a6273e53e336ab09bab08802bc05e31fb20933 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 19 Sep 2019 13:41:50 +0800 Subject: [PATCH 2/2] admins: school list && destory school --- .../javascripts/admins/schools/index.js | 4 ++ app/assets/stylesheets/admins/schools.scss | 3 ++ app/controllers/admins/schools_controller.rb | 30 ++++++++++++ app/libs/util/file_manage.rb | 8 ++++ app/models/department.rb | 2 +- app/models/school.rb | 1 + app/queries/admins/school_query.rb | 23 +++++++++ app/views/admins/schools/index.html.erb | 16 +++++++ app/views/admins/schools/index.js.erb | 1 + .../admins/schools/shared/_list.html.erb | 48 +++++++++++++++++++ app/views/admins/shared/_sidebar.html.erb | 1 + config/routes.rb | 1 + 12 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/admins/schools/index.js create mode 100644 app/assets/stylesheets/admins/schools.scss create mode 100644 app/controllers/admins/schools_controller.rb create mode 100644 app/queries/admins/school_query.rb create mode 100644 app/views/admins/schools/index.html.erb create mode 100644 app/views/admins/schools/index.js.erb create mode 100644 app/views/admins/schools/shared/_list.html.erb diff --git a/app/assets/javascripts/admins/schools/index.js b/app/assets/javascripts/admins/schools/index.js new file mode 100644 index 000000000..20ea080fb --- /dev/null +++ b/app/assets/javascripts/admins/schools/index.js @@ -0,0 +1,4 @@ +$(document).on('turbolinks:load', function() { + if ($('body.admins-schools-index-page').length > 0) { + } +}); \ No newline at end of file diff --git a/app/assets/stylesheets/admins/schools.scss b/app/assets/stylesheets/admins/schools.scss new file mode 100644 index 000000000..8dff1eaff --- /dev/null +++ b/app/assets/stylesheets/admins/schools.scss @@ -0,0 +1,3 @@ +.admins-schools-index-page { + +} \ No newline at end of file diff --git a/app/controllers/admins/schools_controller.rb b/app/controllers/admins/schools_controller.rb new file mode 100644 index 000000000..8c4f1d59e --- /dev/null +++ b/app/controllers/admins/schools_controller.rb @@ -0,0 +1,30 @@ +class Admins::SchoolsController < Admins::BaseController + def index + params[:sort_by] ||= 'created_at' + params[:sort_direction] ||= 'desc' + + schools = Admins::SchoolQuery.call(params) + + @schools = paginate schools + + school_ids = @schools.map(&:id) + @department_count = Department.where(school_id: school_ids).group(:school_id).count + end + + def destroy + users = User.joins(:user_extension).where(user_extensions: { school_id: current_school.id }) + + ActiveRecord::Base.transaction do + users.update_all(profile_completed: false) + current_school.destroy! + end + + render_delete_success + end + + private + + def current_school + @_current_school ||= School.find(params[:id]) + end +end \ No newline at end of file diff --git a/app/libs/util/file_manage.rb b/app/libs/util/file_manage.rb index f592ca4e7..b6cd79e57 100644 --- a/app/libs/util/file_manage.rb +++ b/app/libs/util/file_manage.rb @@ -18,10 +18,18 @@ module Util::FileManage File.exist?(disk_filename(source_type, source_id)) end + def exists?(source) + File.exist?(disk_filename(source.class, source.id)) + end + def disk_file_url(source_type, source_id) File.join('/images', relative_path, "#{source_type}", "#{source_id}") end + def source_disk_file_url(source) + File.join('/images', relative_path, "#{source.class}", "#{source.id}") + end + def disk_auth_filename(source_type, source_id, type) File.join(storage_path, "#{source_type}", "#{source_id}#{type}") end diff --git a/app/models/department.rb b/app/models/department.rb index 3b0672cce..2c537fd91 100644 --- a/app/models/department.rb +++ b/app/models/department.rb @@ -5,7 +5,7 @@ class Department < ApplicationRecord has_many :member_users, through: :department_members, source: :user has_many :user_extensions, dependent: :nullify - has_many :apply_add_departments + has_many :apply_add_departments, dependent: :destroy scope :without_deleted, -> { where(is_delete: false) } diff --git a/app/models/school.rb b/app/models/school.rb index 24de02496..387b6b5d8 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -17,6 +17,7 @@ class School < ApplicationRecord has_many :partners, dependent: :destroy has_many :apply_add_departments, dependent: :destroy + has_many :user_extensions, dependent: :nullify # 学校管理员 def manager?(user) diff --git a/app/queries/admins/school_query.rb b/app/queries/admins/school_query.rb new file mode 100644 index 000000000..7361588c4 --- /dev/null +++ b/app/queries/admins/school_query.rb @@ -0,0 +1,23 @@ +class Admins::SchoolQuery < ApplicationQuery + include CustomSortable + + attr_reader :params + + sort_columns :users_count, :created_at, default_by: :created_at, default_direction: :desc + + def initialize(params) + @params = params + end + + def call + schools = School.all + + keyword = strip_param(:keyword) + schools = schools.where('schools.name LIKE ?', "%#{keyword}%") if keyword + + schools = schools.joins(:user_extensions).group(:id) + schools = schools.select('schools.*, COUNT(*) AS users_count') + + custom_sort schools, params[:sort_by], params[:sort_direction] + end +end \ No newline at end of file diff --git a/app/views/admins/schools/index.html.erb b/app/views/admins/schools/index.html.erb new file mode 100644 index 000000000..0dd5b0bc5 --- /dev/null +++ b/app/views/admins/schools/index.html.erb @@ -0,0 +1,16 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb('单位列表') %> +<% end %> + +
+ <%= form_tag(admins_schools_path, method: :get, class: 'form-inline search-form flex-1', remote: true) do %> + <%= text_field_tag(:keyword, params[:keyword], class: 'form-control col-sm-2 ml-3', placeholder: '部门名称检索') %> + <%= submit_tag('搜索', class: 'btn btn-primary ml-3', 'data-disable-with': '搜索中...') %> + <% end %> + + <%#= javascript_void_link '新建单位', class: 'btn btn-primary', data: { toggle: 'modal', target: '.admin-create-school-modal' } %> +
+ +
+ <%= render partial: 'admins/schools/shared/list', locals: { schools: @schools } %> +
\ No newline at end of file diff --git a/app/views/admins/schools/index.js.erb b/app/views/admins/schools/index.js.erb new file mode 100644 index 000000000..41a2454be --- /dev/null +++ b/app/views/admins/schools/index.js.erb @@ -0,0 +1 @@ +$('.school-list-container').html("<%= j(render partial: 'admins/schools/shared/list', locals: { schools: @schools }) %>"); \ No newline at end of file diff --git a/app/views/admins/schools/shared/_list.html.erb b/app/views/admins/schools/shared/_list.html.erb new file mode 100644 index 000000000..74df29cee --- /dev/null +++ b/app/views/admins/schools/shared/_list.html.erb @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + <% if schools.present? %> + <% schools.each do |school| %> + + + + + + + + + + + + + + <% end %> + <% else %> + <%= render 'admins/shared/no_data_for_table' %> + <% end %> + +
IDLOGO标识码单位名称地区城市详细地址<%= sort_tag('用户数', name: 'users_count', path: admins_schools_path) %>部门数<%= sort_tag('创建时间', name: 'created_at', path: admins_schools_path) %>操作
<%= school.id %> + <% if Util::FileManage.exists?(school) %> + <%= image_tag(Util::FileManage.source_disk_file_url(school).to_s + "?#{Time.now.to_i}", width: 40, height: 40, class: 'preview-image') %> + <% else %> + <%= content_tag(:span, '--', class: 'text-secondary') %> + <% end %> + <%= display_text school.identifier %><%= school.name %><%= school.province %><%= school.city %><%= school.address %><%= school.users_count %><%= @department_count.fetch(school.id, 0) %><%= school.created_at&.strftime('%Y-%m-%d %H:%M') %> + <%= delete_link '删除', admins_school_path(school, element: ".school-item-#{school.id}"), class: 'delete-school-action' %> +
+ +<%= render partial: 'admins/shared/paginate', locals: { objects: schools } %> \ No newline at end of file diff --git a/app/views/admins/shared/_sidebar.html.erb b/app/views/admins/shared/_sidebar.html.erb index da65099ca..2d9461f45 100644 --- a/app/views/admins/shared/_sidebar.html.erb +++ b/app/views/admins/shared/_sidebar.html.erb @@ -31,6 +31,7 @@
  • <%= sidebar_item_group('#schools-submenu', '单位管理', icon: 'building') do %> +
  • <%= sidebar_item(admins_schools_path, '单位列表', icon: 'university', controller: 'admins-schools') %>
  • <%= sidebar_item(admins_departments_path, '部门列表', icon: 'sitemap', controller: 'admins-departments') %>
  • <% end %> diff --git a/config/routes.rb b/config/routes.rb index ba851b2f2..e26016ab8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -926,6 +926,7 @@ Rails.application.routes.draw do resources :mirror_scripts, only: [:index, :new, :create, :edit, :update, :destroy] end resources :choose_mirror_repositories, only: [:new, :create] + resources :schools, only: [:index, :destroy] resources :departments, only: [:index, :create, :edit, :update, :destroy] do resource :department_member, only: [:create, :update, :destroy] post :merge, on: :collection