module EducodeSales class ApplicationController < ActionController::Base include ApplicationHelper protect_from_forgery with: :exception before_action :authenticate_request def is_commissioner_above?(clazz_type="business") return true if @current_admin.is_admin? case clazz_type when "business" can?(:all_business, EducodeSales::BusinessDeliverSubject) when "shixun" can?(:all_shixun, EducodeSales::BusinessDeliverSubject) when "subject" can?(:all_subject, EducodeSales::BusinessDeliverSubject) end end def subject_members common = Common.find_by(clazz: 'staff_type', name: '课程') @manages = Staff.joins(:user).where(job_type: common.id.to_i).where.not(role_id: 11).map { |d| {name: d.user.real_name, value: d.id}} end def subject_staffs common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| { value: d.id, name: d.name } } end def subject_url @url = "https://data.educoder.net" @admins_url = "https://data.educoder.net" end def render_success render json: { success: true } end def render_failure(msg) render json: { success: false, msg: msg.is_a?(String) ? msg : msg.errors.full_messages.join(",") }, status: 403 end def current_user @current_admin ||= Staff.find_by_id(session[:admin_id]) end def authenticate_request if current_user current_user.check_login_status(request) else redirect_to login_path end end def authenticate_admin unless current_user&.is_admin redirect_to no_permission_path end end def filter params[:check] = params[:check] == "true" ? 0:1 begin filter = Filter.find_or_create_by!(staff_id: @current_admin.id, clazz: params[:type]) filter.extras["#{params[:name]}"] = params[:check] filter.save! render json: {success: true ,hidden: params[:check]} rescue => e render_failure("操作失败") end end def paginate(relation) limit = params[:limit] || params[:per_page] limit = (limit.to_i.zero? || limit.to_i > 100) ? 20 : limit.to_i page = params[:page].to_i.zero? ? 1 : params[:page].to_i offset = (page - 1) * limit if relation.is_a?(Array) relation[offset, limit] else relation.limit(limit).offset(offset) end end rescue_from CanCan::AccessDenied do |exception| redirect_to no_permission_path end end end