第一次版本

master
daiao 6 years ago
commit 7b1f61a514

48
.gitignore vendored

@ -0,0 +1,48 @@
*.swp
/.project
/.idea/*
/.bundle
*.swp
/config/database.yml
/config/configuration.yml
/config/additional_environment.rb
/config/oneapm.yml
/files/*
/log/*
/public/tmp/*
/tmp/*
/public/cache/*
/config/newrelic.yml
/public/images/avatars/*
/db/schema.rb
/Gemfile.lock
/lib/plugins/acts_as_versioned/test/debug.log
.rbenv-gemsets
.DS_Store
public/api_doc/
/.metadata
vendor/cache
/files
/public/images/avatars
/public/files
/tags
/config/initializers/gitlab_config.rb
public/javascripts/wechat/node_modules/
.ruby-version
.access_token
tmux*.log
config/wechat.yml
config/oneapm.yml
/public/react/node_modules/
/public/react/public/react/build/static/js/*.map
/public/react-mobile/node_modules/
/public/react-mobile7/node_modules/
/public/react-gatsby/node_modules/
/public/react-gatsby/.cache/
/public/react-mobile7/build/.project
/public/npm-debug.log
/vendor
/workspace/*

@ -0,0 +1,2 @@
--color
--require spec_helper

Binary file not shown.

@ -0,0 +1,132 @@
source 'http://gems.ruby-china.com'
unless RUBY_PLATFORM =~ /w32/
# unix-like only
gem 'iconv'
if RUBY_PLATFORM =~ /darwin/
#gem "rmagick", "= 2.15.4" ## osx must be this version
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
elsif RUBY_PLATFORM =~ /linux/
gem "rmagick", "~> 2.13.1" ## centos yum install ImageMagick-devel
gem 'pdfkit'
gem 'wkhtmltopdf-binary'
gem 'rqrcode', '~> 0.10.1'
gem 'rqrcode_png'
gem 'roo-xls'
gem 'newrelic_rpm'
end
gem 'certified'
gem 'net-ssh', '2.9.1'
gem 'jenkins_api_client'
gem 'nokogiri'
end
gem 'seventeen_mon'
gem 'mobinfo'
gem 'simple_xlsx_reader'
gem 'wechat',path: 'lib/wechat'
gem 'grack', path:'lib/grack'
gem 'gitlab', path: 'lib/gitlab-cli'
gem 'rest-client'
gem "mysql2", "= 0.3.18"
gem 'redis-rails'
gem 'rubyzip'
gem 'delayed_job_active_record'#, :group => :production
gem 'daemons'
gem 'grape', '~> 0.15.0'
gem 'grape-entity', '~> 0.6.0'
gem 'rack-cors', :require => 'rack/cors'
gem 'seems_rateable', '~> 1.0.13'
gem 'rails', '~> 3.2'
gem "jquery-rails", "~> 2.0.2"
gem "i18n", "~> 0.6.0"
gem 'coderay', '~> 1.1.0'
gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
gem "builder", "3.0.0"
gem 'acts-as-taggable-on', '2.4.1'
gem 'spreadsheet'
gem 'ruby-ole'
gem 'rails_kindeditor',path:'lib/rails_kindeditor'
gem 'binding_of_caller'
gem 'chinese_pinyin'
# gem 'sunspot_rails', '~> 1.3.3'
# gem 'sunspot_solr'
# gem 'sunspot'
# gem 'progress_bar'
gem 'ansi'
gem 'kaminari'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
#Ruby 2.2+ has removed test/unit from the core library.
if RUBY_VERSION>='2.2'
gem 'test-unit', '~> 3.0'
end
### profile
# gem 'oneapm_rpm'
# gem 'therubyracer'
group :development do
gem 'grape-swagger', path: "./lib/grape-swagger-0.25.3"
gem 'better_errors', '~> 1.1.0'
# gem "query_reviewer"
gem 'rack-mini-profiler', '~> 0.9.3'
if RUBY_PLATFORM =~ /w32/
gem 'win32console'
end
if RUBY_PLATFORM =~ /darwin/
gem 'puma'
end
end
group :development, :test do
if RUBY_VERSION >= '2.0.0'
gem 'pry-byebug'
# gem "test-unit", "~>3.0"
end
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
# Optional gem for LDAP authentication
group :ldap do
gem "net-ldap", "~> 0.3.1"
end
# Optional gem for OpenID authentication
group :openid do
gem "ruby-openid", "~> 2.1.4", :require => "openid"
gem "rack-openid"
end
database_file = File.join(File.dirname(__FILE__), "config/database.yml")
if File.exist?(database_file)
else
warn("Please configure your config/database.yml first")
end
# Load plugins' Gemfiles
Dir.glob File.expand_path("../plugins/*/Gemfile", __FILE__) do |file|
puts "Loading #{file} ..." if $DEBUG # `ruby -d` or `bundle -v`
instance_eval File.read(file)
end

@ -0,0 +1,40 @@
# More info at https://github.com/guard/guard#readme
# guard 'rails' do
# watch('Gemfile.lock')
# watch(%r{^(config|lib)/.*})
# end
#
guard 'spork', :test_unit => true, :test_unit_env => {'RAILS_ENV' => 'test'}, :wait => 60 do
watch(%r{app/models/.+\.rb})
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.rb')
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard :test, :drb => true, :all_after_pass => false, :all_on_start => false do
watch(%r{^test/.+_test\.rb$})
watch('test/test_helper.rb') { 'test' }
# Non-rails
watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
# Rails 4
# watch(%r{^app/(.+)\.rb}) { |m| "test/#{m[1]}_test.rb" }
# watch(%r{^app/controllers/application_controller\.rb}) { 'test/controllers' }
# watch(%r{^app/controllers/(.+)_controller\.rb}) { |m| "test/integration/#{m[1]}_test.rb" }
# watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
# watch(%r{^lib/(.+)\.rb}) { |m| "test/lib/#{m[1]}_test.rb" }
# Rails < 4
watch(%r{^app/models/(.+)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
watch(%r{^app/controllers/(.+)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
watch(%r{^app/views/(.+)/.+\.erb$}) { |m| "test/functional/#{m[1]}_controller_test.rb" }
watch(%r{^app/views/.+$}) { 'test/integration' }
watch('app/controllers/application_controller.rb') { ['test/functional', 'test/integration'] }
end

@ -0,0 +1,7 @@
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
RedmineApp::Application.load_tasks

@ -0,0 +1,171 @@
#coding=utf-8
# status值
# 0成功-1500错误403无权限404页面不存在
module Mobile
# require_relative 'middleware/error_handler'
# require_relative 'apis/auth'
# require_relative 'apis/users'
# require_relative 'apis/courses'
# require_relative 'apis/watches'
# require_relative 'apis/upgrade'
# require_relative 'apis/homeworks'
# require_relative 'apis/comments'
# require_relative 'apis/issues'
# require_relative 'apis/activities'
# require_relative 'apis/whomeworks'
# require_relative 'apis/newss'
# require_relative 'apis/journal_for_messages'
# require_relative 'apis/messages'
# require_relative 'apis/blog_comments'
# require_relative 'apis/new_comment'
# require_relative 'apis/praise'
# require_relative 'apis/resources'
# require_relative 'apis/syllabuses'
# require_relative 'apis/projects'
# require_relative 'apis/games'
# require_relative 'apis/shixuns'
# require_relative 'apis/discusses'
# require_relative 'apis/memos'
# require_relative 'apis/sources'
# require_relative 'apis/careers'
class API < Grape::API
version 'v1', using: :path
format :json
content_type :json, "application/json;charset=UTF-8"
# use ActionDispatch::Session::CookieStore
use Mobile::Middleware::ErrorHandler
helpers do
def logger
Rails.logger
end
def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
def private_auth
Rails.logger.info("#########{params[:private_token]}")
error!('401 Unauthorized', 401) if params[:private_token] != "hriEn3UwXfJs3PmyXnSG"
end
# 有一些接口没登录也能查看数据
def career_authenticate!
pass = request.path.include?("introduction") || request.path.include?("get_published_careers")|| request.path.include?("get_current_user")
unless pass
error!('401 Unauthorized', 401) unless current_user
end
end
def memo_authenticate!
pass = (request.path.match(/memos\/\d+/).present? && !request.path.include?("reply")) ||
request.path.include?("get_memos_list") ||
request.path.include?("memos?page=") || request.path.match(/memos$/).present?
unless pass
error!('401 Unauthorized', 401) unless current_user
end
end
def discusses_authenticate!
pass = request.path.match(/discusses$/).present? || request.path.include?("discusses?page=")
unless pass
error!('401 Unauthorized', 401) unless current_user
end
end
# def manager_of_game
# myshixun_id = Game.where(:identifier => params[:identifier]).pluck(:myshixun_id).first
# myshixun = Myshixun.find(myshixun_id)
# unless (current_user.admin? || myshixun.user_id == current_user.id)
# return {}
# end
# end
#
def session
env['rack.session']
end
def current_user
openid = session[:wechat_openid]
if openid
uw = UserWechat.find_by_openid(openid)
return uw.user if uw
end
token = ApiKey.where(access_token: params[:token]).first
if token && !token.expired?
return User.find(token.user_id)
end
#
Rails.logger.info("########### host is #{request.host}")
if (Rails.env.development? && session[:user_id].blank?) || (session[:user_id].blank? && request.host.include?("testbdweb")) || params[:action] == "privateGit"
session[:user_id] = 12 #116
end
Rails.logger.info("####### session user_id is #{session[:user_id]}")
if session[:user_id]
user = (User.active.find(session[:user_id]) rescue nil)
else
autologin_cookie = Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'
if cookies[autologin_cookie] && Setting.autologin?
user = User.try_to_autologin(cookies[autologin_cookie])
if user
# reset_session
# start_user_session(user)
session[:user_id] = user.id
session[:ctime] = Time.now.utc.to_i
session[:atime] = Time.now.utc.to_i
end
user
end
end
return user if user
nil
end
end
mount Apis::Auth
mount Apis::Users
mount Apis::Courses
mount Apis::Watches
mount Apis::Upgrade
mount Apis::Homeworks
mount Apis::Comments
mount Apis::Issues
mount Apis::Activities
mount Apis::Whomeworks
mount Apis::Newss
mount Apis::JournalForMessages
mount Apis::Messages
mount Apis::BlogComments
mount Apis::NewComment
mount Apis::Praise
mount Apis::Resources
mount Apis::Syllabuses
mount Apis::Projects
mount Apis::Games
mount Apis::Shixuns
mount Apis::Discusses
mount Apis::Memos
mount Apis::Sources
mount Apis::Careers
mount Apis::Assets
# add_swagger_documentation ({host: 'educoder.0bug.info', api_version: 'v1', base_path: '/api'}) if Rails.env.development?
add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development?
end
end

@ -0,0 +1,69 @@
#coding=utf-8
module Mobile
module Apis
class Activities< Grape::API
resources :activities do
desc "get user activities"
params do
requires :page, type: Integer
requires :token, type: String
requires :container_type, type: String
end
post do
begin
authenticate!
user = current_user
container_type = params[:container_type] ? params[:container_type] : "All"
if container_type == "All" || container_type == "Project"
shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id)
user_project_ids = (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).join(",") + ")"
project_types = "('Message','Issue','Project')"
end
if container_type == "All" || container_type == "Course"
shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id)
user_course_ids = (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")"
course_types = "('Message','News','HomeworkCommon','Poll','Course')"
end
page = params[:page] ? params[:page] : 0
principal_types = "JournalsForMessage"
watched_user_ids = User.watched_by(user.id).count == 0 ? " " : ("," + User.watched_by(user.id).map{|u| u.id.to_s }.join(','))
user_ids = "(" + user.id.to_s + watched_user_ids + ")"
watched_user_blog_ids = Blog.select("id").where("author_id in #{user_ids}").count == 0 ? "-1" :Blog.select("id").where("author_id in #{user_ids}").map { |blog| blog.id}.join(",")
blog_ids = "(" + watched_user_blog_ids + ")"
if container_type == "Course"
activities = UserActivity.where("(container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})").order('updated_at desc')
elsif container_type == "Project"
activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})").order('updated_at desc')
else
activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" +
"or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+
"or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " +
"or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc')
end
all_count = activities.count
activities = activities.limit(10).offset(page * 10)
count = activities.count
present :data, activities, with: Mobile::Entities::Activity,user: user
present :all_count, all_count
present :count, count
present :page, page
present :status, 0
present :container_type, container_type
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,40 @@
#coding=utf-8
module Mobile
module Apis
class Assets< Grape::API
resources :assets do
desc "upload file"
params do
requires :avatar, type: File
requires :token, type: String
optional :compress, type: Boolean #是否需要服务器压缩
end
post 'upload' do
authenticate!
# params[:avatar][:filename] # => 'avatar.png'
# params[:avatar][:type] # => 'image/png'
# params[:avatar][:tempfile] # => #<File>
attachment = ::Attachment.new(:file => params[:avatar][:tempfile])
attachment.author = current_user
attachment.filename = params[:avatar][:filename].presence || Redmine::Utils.random_hex(16)
saved = attachment.save
if saved
present :data, attachment, with: Mobile::Entities::Attachment
present :status, 0
else
present :status, -1
present :message, '上传失败'
end
end
end
end
end
end

@ -0,0 +1,56 @@
#coding=utf-8
module Mobile
module Apis
class Auth < Grape::API
desc '认证相关'
resource :auth do
desc "用户登录"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post do
user,last_logon = ::User.try_to_login(params[:login], params[:password])
if user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
api_user = ::UsersService.new.show_user({id:user.id})
present :data, {token: key.access_token, user: api_user}, with: Mobile::Entities::Auth
present :status, 0
else
present :message, "无效的用户名或密码"
present :status,1
end
end
desc "用户登出"
params do
requires :token, type: String
end
delete do
authenticate!
::ApiKey.delete_all(user_id: current_user.id)
{status: 0}
end
desc "忘记密码"
params do
requires :mail,type: String
end
post 'lost_password' do
us = UsersService.new
message = us.lost_password params
present :message, message
present :status, 0
end
end
end
end
end

@ -0,0 +1,31 @@
#coding=utf-8
module Mobile
module Apis
class BlogComments< Grape::API
resources :blog_comments do
desc "get special topic"
post ':id' do
begin
user = current_user
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
blog = BlogComment.find params[:id]
present :data, blog, with: Mobile::Entities::BlogComment,user: user,type: type,page: page
present :type, type
present :page, page
present :status, 0
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,118 @@
# encoding=utf-8
module Mobile
module Apis
class Careers < Grape::API
before {career_authenticate!}
content_type :json, 'application/json;charset=UTF-8'
resources :careers do
desc "创建职业路径课程介绍"
params do
requires :name, type: String, desc: "职业路径名称"
requires :video_name, type: String, desc: "视频名称"
end
post 'create' do
CareersService.new.create params, current_user
end
desc "编辑职业路径"
params do
requires :id, type: Integer, desc: "职业路径id"
end
route_param :id do
get 'edit' do
CareersService.new.edit params
end
end
desc '添加实训路径弹框和弹框搜索'
get 'get_paths_info' do
CareersService.new.get_paths_info params
end
desc "实训路径弹框选择路径"
params do
requires :id, type: Array, desc: "实训路径的id"
end
post 'add_paths' do
CareersService.new.add_paths params
end
desc "创建职业路径内容安排"
params do
requires :id, type: Integer, desc: "职业路径id"
requires :career_stages, type: Array, desc: '阶段数据'
end
route_param :id do
post 'create_career_stages' do
CareersService.new.create_career_stages params
end
end
desc "课程介绍"
params do
requires :id, type: Integer, desc: "职业路径id"
end
route_param :id do
get 'introduction' do
CareersService.new.introduction params
end
end
desc "更新课程介绍"
params do
requires :id, type: Integer, desc: "职业路径id"
requires :name, type: String, desc: '课程名称'
requires :video_name, type: String, desc: '视频名称'
end
route_param :id do
post 'update_introduction' do
CareersService.new.update_introduction params
end
end
desc "内容安排"
params do
requires :id, type: Integer, desc: "职业路径id"
end
route_param :id do
get 'contents' do
CareersService.new.contents params, current_user
end
end
desc "更新内容安排"
params do
requires :id, type: Integer, desc: "职业路径id"
requires :career_stages, type: Array, desc: "阶段内容(数组)"
end
route_param :id do
post 'update_contents' do
CareersService.new.update_contents params
end
end
# api/v1/careers/get_published_careers
desc "获取已发布的职业路径"
get 'get_published_careers' do
CareersService.new.get_published_careers params
end
# 公共接口
desc "获取当前用户的信息"
get 'get_current_user' do
CareersService.new.get_current_user params, current_user
end
# 工程认证头部url
desc "获取已发布的职业路径"
get 'get_navigation_url' do
CareersService.new.get_navigation_url
end
end
end
end
end

@ -0,0 +1,133 @@
#coding=utf-8
module Mobile
module Apis
class Comments < Grape::API
include ApplicationHelper
resource :comments do
desc '课程通知评论'
params do
requires :token, type: String
requires :comments, type: String
end
post ':id' do
cs = CommentService.new
cs_params = {
id: params[:id],
comment: params.reject{|k,v| [:id].include?(k)}}
comments = cs.news_comments cs_params,current_user
raise "create comments failed #{comments.errors.full_messages}" if comments.new_record?
present :data, comments, with: Mobile::Entities::Comment
present :status, 0
end
desc '作业留言(教师布置的作业)'
params do
requires :token, type: String
requires :message,type: String, desc: '留言'
#optional :reference_content, type: String ,desc: '引用的内容'
#optional :reference_user_id, type: Integer,desc: '被引用的人'
end
post ':id/create_homework_message' do
cs_params = {
id: params[:id],
token: params[:token],
reference_content: params[:reference_content],
bid_message: params.reject{|k,v| [:id,:token,:reference_content].include?(k)}}
cs = CommentService.new
message = cs.homework_message cs_params,current_user
present :data, message, with: Mobile::Entities::Jours
present :status, 0
end
desc '课程留言'
params do
requires :token, type: String
requires :course_message,type: String, desc: '留言'
end
post ':id/leave_course_message' do
cs_params = {
id: params[:id],
token: params[:token],
new_form: params.reject{|k,v| [:id,:token].include?(k)}}
cs = CommentService.new
message = cs.leave_course_message cs_params,current_user
present :data, message, with: Mobile::Entities::Jours
present :status, 0
end
desc '回复留言'
params do
requires :token, type: String
requires :reference_id, type: Integer,desc: '所属留言树的根留言id最顶层的非回复的留言,留言对象中的m_parent_id'
requires :reference_user_id,type: Integer ,desc: '被回复的留言的作者id'
#requires :reference_message_id,type: Integer,desc: '被回复的留言的id'
requires :user_notes,type: String,desc: '留言的内容'
requires :jour_type,type: String,desc: '等于父留言的jour_type'
requires :jour_id,type:Integer, desc: '等于父留言的jour_id'
end
post ':reference_message_id/create_reply'do
cs = CommentService.new
message = cs.create_reply params,current_user
raise "create reply failed #{message.errors.full_messages}" if message.new_record?
present :data, message, with: Mobile::Entities::Jours
present :status, 0
end
desc ' 意见反馈'
params do
requires :token, type: String
requires :subject,type: String,desc: '意见'
end
post do
cs_params = {
memo: {:subject => '该贴来自手机App意见反馈' ,:content => params[:subject]},
}
cs = CommentService.new
memo,message = cs.create_feedback cs_params, current_user
raise message if memo.new_record?
present :status, 0
end
desc '课程留言列表'
params do
optional :token, type: String
optional :page,type:Integer,desc:'页数'
end
get ':id/course_message' do
cs = CommentService.new
jours = cs.course_messages params,(current_user.nil? ? User.find(2):current_user)
present :data, jours, with: Mobile::Entities::Jours
present :status, 0
end
desc '留言详情'
params do
requires :token, type: String
requires :comment_parent_id,type:Integer,desc:'留言id'
optional :course_id,type:Integer,desc:'课程id'
end
get ':comment_parent_id/comment_details' do
cs = CommentService.new
jour = cs.comment_detail params,current_user
present :data, jour, with: Mobile::Entities::Jours
present :status, 0
end
desc '通知评论列表'
params do
#requires :token, type: String
requires :notice_id,type:Integer,desc:'通知id'
optional :page,type:Integer,desc:'页码'
end
get ':notice_id/notice_comments' do
cs = CommentService.new
comments = cs.notice_comments params,current_user
present :data, comments, with: Mobile::Entities::Comment
present :status, 0
end
end
end
end
end

File diff suppressed because it is too large Load Diff

@ -0,0 +1,87 @@
# encoding=utf-8
module Mobile
module Apis
class Discusses < Grape::API
before {discusses_authenticate!}
content_type :json, 'application/json;charset=UTF-8'
# status -1 表示异常0 结果需要返回给用户
resources :discusses do
desc "所有评论"
get do
DiscussesService.new.index params, current_user
end
desc "添加评论"
params do
requires :shixun_id, type: Integer, desc: "shixun.id"
requires :challenge_id, type: Integer, desc: "challenge.id"
requires :content, type: String
requires :position, type: Integer, desc: "关卡数challenge.position"
end
post do
game = DiscussesService.new.create(params, current_user)
return {:status => 1, :message => "success"}
end
desc "回复评论"
params do
requires :id, type: Integer
requires :content, type: String
end
route_param :id do
post 'reply' do
game = DiscussesService.new.reply(params, current_user)
end
end
desc "隐藏评论"
params do
requires :id, type: Integer
requires :hidden, type: String
end
route_param :id do
post 'hidden' do
game = DiscussesService.new.hidden(params, current_user)
return {:status => 1, :message => "success"}
end
end
desc "奖励金币"
params do
requires :id, type: Integer, desc: "discuss.id"
requires :container_type, type: String, desc: "所加金币的对象类型,如:'Discusses'"
requires :score, type: Integer, desc: "金币数量"
requires :user_id, type: Integer, desc: "发帖者的ID"
end
route_param :id do
post 'reward_code' do
game = DiscussesService.new.reward_code(params, current_user)
end
end
desc "评论点赞/取消赞"
params do
requires :id, type: Integer, desc: "discuss.id"
requires :container_type, type: String, desc: "Discuss等"
requires :type, type: Integer, desc: "踩0赞1"
end
route_param :id do
post 'plus' do
game = DiscussesService.new.plus(params, current_user)
end
end
desc "删除评论"
params do
requires :id, type: String, desc: 'Discuss.id'
end
delete ':id' do
Discuss.find(params[:id]).destroy
return {:status => 1, :message => "success"}
end
end
end
end
end

@ -0,0 +1,279 @@
# encoding=utf-8
module Mobile
module Apis
class Games < Grape::API
before {authenticate!}
content_type :json, 'application/json;charset=UTF-8'
# status -1 表示异常0 结果需要返回给用户
resources :games do
desc "显示当前关卡"
params do
requires :identifier, type: String
end
route_param :identifier do
get do
game = GamesService.new.show_game(params, current_user)
end
end
desc "异步获取版本库内容"
params do
requires :identifier, type: String
requires :path, type: String
requires :shixun_gpid, type: Integer, desc: '实训(TPM)在gitlab中对应的项目id'
requires :status, type: Integer, desc: '0 表示默认路径获取内容1表示右侧树形结构中发送的请求'
end
route_param :identifier do
get 'rep_content' do
content = GamesService.new.rep_content(params, current_user)
end
end
desc "获取TPI全部任务"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'challenges' do
GamesService.new.challenges params
end
end
desc "评分打星星"
params do
requires :identifier, type: String
requires :star, type: String
requires :shixun_id, type: Integer
end
route_param :identifier do
post 'star' do
GamesService.new.star(params, current_user)
end
end
desc "获取版本库目录"
params do
requires :identifier, type: String
requires :path, type: String, desc: 'file path'
requires :rev, type: String, desc: 'branch'
requires :gpid, type: Integer, desc: 'myshixun.gpid'
end
route_param :identifier do
get 'entries' do
GamesService.new.entries params
end
end
desc "确定权限查看答案"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'answer' do
GamesService.new.answer(params, current_user)
end
end
desc "查看答案并扣取相应的积分"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'answer_grade' do
begin
GamesService.new.answer_grade(params, current_user)
rescue Exception => e
if e.message == "0" # 需要提醒给用户
present :message, "金币不够了!您可以通过签到、关卡评分等赚取更多金币"
present :status, 0
else
present :message,"#{e.message}"
present :status, -1
end
end
end
end
desc "查看隐藏测试集扣分"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'check_test_sets' do
GamesService.new.check_test_sets(params, current_user)
end
end
desc "编程题评测"
params do
requires :identifier, type: String
requires :resubmit, type: String, desc: "随机生成的秘钥唯一定义某次评测由file_update传入的"
requires :content_modified, type: Integer, desc: "content_modified:1表示文件有更新;0表示没有更新"
requires :first, type: String, desc: "first参数有前端JS轮询发送第一次为1"
end
route_param :identifier do
get 'game_build' do
GamesService.new.game_build params
end
end
desc "轮询获取状态"
params do
requires :identifier, type: String
requires :resubmit, type: String, desc: "随机生成的秘钥,唯一定义某次评测"
requires :port, type: String, desc: "PHP/Web类实训端口号 其他实训返回-1game build阶段中间层传入"
requires :time_out, type: String, desc: "判断评测是否超时,每个镜像有时间上限限制"
end
route_param :identifier do
get 'game_status' do
GamesService.new.game_status(params, current_user)
end
end
desc "编程题评测之代码提交"
params do
requires :identifier, type: String
requires :content, type: String
requires :path, type: String, desc: "文件路劲"
requires :evaluate, type: Integer, desc: "区分其它地方的更新和评测的更新,评测的更新会有一些相应的记录"
end
route_param :identifier do
post 'file_update' do
GamesService.new.file_update(params, current_user)
end
end
desc "恢复初始代码"
params do
requires :identifier, type: String
requires :path, type: String, desc: "文件路劲"
end
route_param :identifier do
get 'reset_original_code' do
GamesService.new.reset_original_code(params, current_user)
end
end
desc "恢复上次通关的代码"
params do
requires :identifier, type: String
requires :path, type: String, desc: "文件路径"
end
route_param :identifier do
get 'reset_passed_code' do
GamesService.new.reset_passed_code params, current_user
end
end
# Parameters: {"answer"=>["A", "AB", "C", "A", "B", "B", "D", "B", "C", "C"]}
desc "选择题评测"
params do
requires :identifier, type: String
requires :answer, type: Array[String]
end
route_param :identifier do
post 'choose_build' do
begin
GamesService.new.choose_build params
rescue Exception => e
if e.message == "0" # 需要提醒给用户
present :message, "金币不够了!您可以通过签到、关卡评分等赚取更多金币"
present :status, 0
else
present :message,"#{e.message}"
present :status, -1
end
end
end
end
desc "记录评测耗时"
params do
requires :identifier, type: String
requires :time, type: Integer, desc: "花费的时间,单位:秒"
end
route_param :identifier do
get 'cost_time' do
GamesService.new.cost_time params
end
end
desc "同步更新时间"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'sync_modify_time' do
GamesService.new.sync_modify_time params
end
end
desc "测试集更新弹框不再提醒"
params do
requires :identifier, type: String
requires :myshixun_id, type: Integer, desc: "myshixun.id"
end
route_param :identifier do
get 'system_update' do
GamesService.new.system_update params
end
end
desc "Tpi强制重置"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'reset_my_game' do
GamesService.new.reset_my_game params, current_user
end
end
desc "webssh删除操作"
params do
requires :identifier, type: String
end
route_param :identifier do
post 'close_webssh' do
GamesService.new.close_webssh params, current_user
end
end
desc "获取所有用户TPI通关答案"
params do
requires :identifier, type: String
end
route_param :identifier do
get 'get_passed_user' do
GamesService.new.get_passed_user params
end
end
desc "获取用户答案"
params do
requires :identifier, type: String, desc: "具体用户的game.identifier"
end
route_param :identifier do
get 'get_passed_code' do
GamesService.new.get_passed_code params
end
end
# desc "代码同步更新"
# params do
# requires :identifier, type: String
# requires :myshixun_id, type: Integer, desc: "myshixun.id"
# requires :myshixun_id, type: Integer, desc: "myshixun.id"
# end
# route_param :identifier do
# get 'sync_codes' do
# GamesService.new.sync_codes params
# end
# end
end
end
end
end

@ -0,0 +1,5 @@
module API
module Helpers
end
end

@ -0,0 +1,157 @@
#coding=utf-8
module Mobile
module Apis
class Homeworks < Grape::API
def self.get_service
HomeworkService.new
end
resources :homeworks do
desc "作业详情"
params do
requires :id, type: Integer, desc: "作业ID"
end
route_param :id do
get do
homework = Homeworks.get_service.show_homework params
present :data, homework, with: Mobile::Entities::Homework
present :status, 0
end
end
desc "我的作品列表"
params do
requires :token, type: String
end
get ':user_id/homework_attachs' do
ue = Homeworks.get_service.my_homework_list params,current_user.nil? ? User.find(2):current_user
present :data, ue,with: Mobile::Entities::Course
present :status, 0
end
# desc "启动匿评"
# params do
# requires :token, type: String
# end
# post ':id/start_anonymous_comment' do
# statue = Homeworks.get_service.start_anonymous_comment params,current_user.nil? ? User.find(2):current_user
# messages = ""
# case statue
# when 1
# messages = "启动成功"
# when 2
# messages = "启动失败作业总数大于等于2份时才能启动匿评"
# when 3
# messages = "已开启匿评,请务重复开启"
# end
# present :data,messages
# present :status, statue
# end
#
# desc "关闭匿评"
# params do
# requires :token, type: String
# end
# post ':id/stop_anonymous_comment' do
# Homeworks.get_service.stop_anonymous_comment params,current_user.nil? ? User.find(2):current_user
# present :status, 0
# end
desc "匿评作品详情"
params do
requires :token, type: String
end
get ':homework_id/anonymous_works_show' do
works,par = Homeworks.get_service.anonymous_works_show params.merge(:id => params[:homework_id]),current_user.nil? ? User.find(2):current_user
present :data, works ,with: Mobile::Entities::HomeworkAttach
present :otherdata,par,with: Mobile::Entities::AnonymousWorksParams
present :status, 0
end
desc "作品打分"
params do
requires :token, type: String
requires :is_teacher, type: String,desc: '是否为教师(匿评作品详情返回的结果中可获取此参数的值)'
requires :is_anonymous_comments, type: String, desc: '是否为匿评(匿评作品详情返回的结果中可获取此参数的值)'
optional :stars_value, type: Integer,desc: '用户给出的评分'
optional :cur_page,type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值'
optional :cur_type, type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值'
optional :user_message, type: String, desc: '用户评论'
end
post ':homework_id/scoring' do
cs_params = {
new_form: params.reject{|k,v| [:token,:is_teacher,:is_anonymous_comments,:stars_value,:cur_page,:cur_type,:homework_id].include?(k)},
token: params[:token],
is_teacher: params[:is_teacher],
is_anonymous_comments: params[:is_anonymous_comments],
stars_value: params[:stars_value],
cur_page: params[:cur_page],
cur_type: params[:cur_type],
homework_id: params[:homework_id]
}
Homeworks.get_service.add_score_and_jour cs_params,current_user
present :status, 0
end
desc '创建作业'
params do
requires :token,type:String
requires :work_name,type:String,desc:'作业名称'
requires :work_desc,type:String,desc:'作业描述'
requires :work_deadline,type:String,desc:'截止日期'
requires :is_blind_appr,type:Integer,desc:'是否匿评'
requires :blind_appr_num,type:Integer,desc:'匿评分配数'
requires :course_id,type:Integer,desc: '课程id'
end
post 'create_home_work' do
Homeworks.get_service.create_home_work params,current_user
present :status, 0
end
desc '开启匿评'
params do
requires :token,type:String
requires :course_id,type:Integer,desc:'课程id'
requires :homework_id,type:Integer,desc:'作业id'
end
post ':homework_id/start_anonymous_comment' do
hs = Homeworks.get_service
status = hs.start_anonymous_comment params,current_user
messages = ""
case status[:status]
when 1
messages = "启动成功"
when 2
messages = "启动失败作业总数大于等于2份时才能启动匿评"
when 3
messages = "已开启匿评,请务重复开启"
when 4
messages = "没有开启匿评的权限"
when 5
messages = "截止日期之前不可启动匿评"
end
present :data,messages
present :status,0
end
desc '关闭匿评'
params do
requires :token,type:String
requires :course_id,type:Integer,desc:'课程id'
requires :homework_id,type:Integer,desc:'作业id'
end
post ':homework_id/stop_anonymous_comment' do
hs = Homeworks.get_service
hs.stop_anonymous_comment params,current_user
message = "成功关闭"
present :data, message
present :status,0
end
end
end
end
end

@ -0,0 +1,44 @@
#coding=utf-8
module Mobile
module Apis
class Issues< Grape::API
resources :issues do
include IssuesHelper
desc "get special issuse"
post ':id' do
begin
# authenticate!
user = current_user
#is_public true 公开 false 私有 1公开 0私有
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
is_public = 1
if type == 0
issue = Issue.find params[:id]
issue.project.is_public
present :data, issue, with: Mobile::Entities::Issue,user: user,type: type,page: page
else
jour = Journal.find params[:id]
present :data, jour, with: Mobile::Entities::Issue,user: user,type: type,page: page
end
present :type, type
present :page, page
present :is_public,is_public
present :status, 0
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,41 @@
#coding=utf-8
module Mobile
module Apis
class JournalForMessages< Grape::API
resources :journal_for_messages do
desc "get special journal"
post ':id' do
begin
# authenticate!
user = current_user
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
jour = JournalsForMessage.find params[:id]
is_public = 1
if jour.jour_type == "Project"
is_public = jour.project.is_public
elsif jour.jour_type == "Course"
is_public = jour.course.is_public
end
present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page
present :type, type
present :page, page
present :is_public, is_public
present :status, 0
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,130 @@
# encoding=utf-8
module Mobile
module Apis
class Memos < Grape::API
before {memo_authenticate!}
content_type :json, 'application/json;charset=UTF-8'
resources :memos do
desc "新建帖子"
get 'new' do
MemosService.new.new current_user, session
end
desc "新建保存帖子"
params do
requires :memo, type: Hash do
requires :subject, type: String, desc: "帖子名"
requires :content, type: String, desc: "帖子内容"
requires :forum_id, type: Integer, desc: "帖子类型3操作指南 5技术分享"
end
end
post 'create' do
MemosService.new.create params, current_user
end
desc "帖子详情"
params do
requires :id, type: Integer, desc: "帖子ID"
end
get ':id' do
MemosService.new.show params, current_user
end
desc "讨论区列表"
get do
MemosService.new.get_memos_list params, current_user
end
desc "编辑帖子"
params do
requires :id, type: Integer, desc: "编辑帖子的ID"
end
route_param :id do
get 'edit' do
MemosService.new.edit params, current_user
end
end
desc "更新帖子"
params do
requires :id, type: Integer, desc: "帖子ID"
requires :content_changed, type: Boolean, desc: "判断帖子内容是否有更新"
end
route_param :id do
post 'update' do
MemosService.new.update params
end
end
# TODO 本来可以跟实训评论一起用,但说不要动实训,所以另起一个方法
desc "隐藏帖子"
params do
requires :id, type: Integer, desc: "帖子ID"
end
route_param :id do
get :hidden do
Memo.find(params[:id]).update_attribute(:hidden, true)
{status: 0, message: "隐藏成功"}
end
end
desc "回复帖子"
params do
requires :parent_id, type: Integer, desc: "给那个帖子的回复id"
requires :content, type: String, desc: "回复内容"
end
route_param :parent_id do
post :reply do
MemosService.new.reply params, current_user
end
end
desc "隐藏回复帖子"
params do
requires :id, type: Integer, desc: "要隐藏的对象id。如memo.id"
requires :hidden, type: String
end
route_param :id do
post 'hidden' do
MemosService.new.hidden(params, current_user)
return {:status => 1, :message => "success"}
end
end
desc "删除帖子/评论"
params do
requires :id, type: Integer, desc: "帖子ID"
end
delete ":id" do
Memo.find(params[:id]).destroy
{status: 0, message:"删除成功!"}
end
desc "设置贴子顶置/取消顶置"
params do
requires :id, type: Integer, desc: "帖子ID"
requires :sticky, type: Integer, desc:"顶置: 1 置顶,0 取消置顶 "
end
route_param :id do
get "set-top-or-down" do
MemosService.new.set_top_or_down params
end
end
desc "帖子回复更多"
params do
requires :id, type: Integer, desc: "帖子ID"
requires :page, type: String, desc: "初次第一页为0"
end
route_param :id do
get "more_reply" do
MemosService.new.more_reply params, current_user
end
end
end
end
end
end

@ -0,0 +1,44 @@
#coding=utf-8
module Mobile
module Apis
class Messages< Grape::API
resources :messages do
desc "get special topic"
post ':id' do
begin
# authenticate!
user = current_user
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
message = Message.find params[:id]
is_public = 1
type_name = ""
if message.project
is_public = message.project.is_public
type_name = "project"
elsif message.course
is_public = message.course.is_public
type_name = "course"
end
present :data, message, with: Mobile::Entities::Message,user: user,type: type,page: page
present :type, type
present :page, page
present :is_public, is_public
present :type_name, type_name
present :status, 0
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,321 @@
#coding=utf-8
module Mobile
module Apis
class NewComment< Grape::API
include ApplicationHelper
include ApiHelper
resources :new_comment do
desc "add a new comment"
params do
requires :type, type: String
requires :content, type: String
end
post ':id' do
unless current_user
#如果当前用户不存在 则需要根据微信昵称注册一个帐号
openid = session[:wechat_openid]
raise "无法获取到openid,请在微信中打开本页面" unless openid
us = UsersService.new
access_token = session[:access_token]
refresh_token = session[:refresh_token]
if access_token.present? && refresh_token.present?
begin
refreshinfo = Wechat.api.web_refresh_access_token(refresh_token)
rescue
info = Wechat.api.web_access_token(session[:wechat_code])
openid =info["openid"]
access_token =info["access_token"]
if access_token
session[:access_token] = access_token
end
refresh_token = info["refresh_token"]
if refresh_token
session[:refresh_token] = refresh_token
end
refreshinfo = Wechat.api.web_refresh_access_token(refresh_token)
end
access_token = refreshinfo["access_token"]
refresh_token = refreshinfo["refresh_token"]
session[:access_token] = access_token
session[:refresh_token] = refresh_token
#获取用户信息
userinfo = Wechat.api.web_userinfo(access_token,openid)
Rails.logger.info "userinfo!!!!!!!!!"
Rails.logger.info userinfo
name = userinfo["nickname"]
else
name = openid[0..3]+"***"+openid.last
end
user = us.register ({:login=>openid, :mail=>"#{openid}@163.com",
:password=>"12345678", :password_confirmation=>"12345678",
:should_confirmation_password => true})
raise user.errors.full_messages.first if user.new_record?
user.update_attributes(:lastname=>name)
#自动激活
if Setting.self_registration != '3'
user.activate
user.last_login_on = Time.now
if user.save
UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0)
end
end
UserWechat.create!(
openid: openid,
user: user,
bindtype: 1
)
end
authenticate!
## 能进来的就是已关注
## 因为取消订阅的记录被删除了
subscribe = 1 #默认未关注
status = 0
tip = 0 #0班级1项目
type = params[:type]
result = 1
#0回复 1回复的回复
reply_type = params[:reply_type]
update_id = 0
if params[:content]!="" && current_user
case type
when "HomeworkCommon"
#如果是私有的 并且不是成员则不能回复
# is_public = homework_common.course.is_public
# if is_public == 0 && !current_user.member_of_course?(homework_common.course)
# status = -1
# tip = 0
# else
# feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id])
# if (feedback.errors.empty?)
# homework_common.update_column(:updated_at, Time.now)
# result = 2
# end
# end
if reply_type == nil || reply_type == 0
homework_common = HomeworkCommon.find(params[:id])
feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id], params[:id])
if (feedback.errors.empty?)
homework_common.update_column(:updated_at, Time.now)
result = 2
update_id = homework_common.id
end
else
#二级回复
reply = JournalsForMessage.find params[:id].to_i
homework_common = HomeworkCommon.find reply.jour_id
options = {:notes => params[:content], :reply_id => reply.user_id,:user_id => current_user.id,:m_parent_id => params[:id].to_i,:m_reply_id => params[:id].to_i}
feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], reply.jour_id, reply.jour_id, options)
if (feedback.errors.empty?)
homework_common.update_column(:updated_at, Time.now)
result = 2
update_id = homework_common.id
end
end
when "News"
# if news.project
# if news.project.is_public == false && !current_user.member_of?(news.project)
# status = -1
# tip = 1
# end
# elsif news.course
# if news.course.is_public == 0 && !current_user.member_of_course?(news.course)
# status = -1
# tip = 0
# end
# end
if status == 0
if reply_type == nil || reply_type == 0
news = News.find(params[:id])
comment = Comment.new
comment.comments = params[:content]
comment.author = current_user
if news.comments << comment
result = 2
update_id = news.id
end
else
#二级回复
comment = Comment.find(params[:id])
news = News.find comment.commented_id
new_comment = news.comments.build(:author_id => current_user.id, :reply_id => params[:id], :comments => params[:content], :parent_id => comment.id)
if new_comment.save
result = 2
update_id = news.id
end
end
end
when "Message"
# if message.project
# if message.project.is_public == false && !current_user.member_of?(message.project)
# status = -1
# tip = 1
# end
# elsif message.course
# if message.course.is_public == 0 && !current_user.member_of_course?(message.course)
# status = -1
# tip = 0
# end
# end
if status == 0
# if reply_type == nil || reply_type == 0
message = Message.find(params[:id])
board = Board.find(message.board_id)
topic = message.root
reply = Message.new
reply.author = current_user
reply.board = board
reply.content = params[:content]
reply.parent_id = params[:id]
reply.subject = "RE: #{topic.subject}"
reply.root_id = topic.id
if message.children << reply
result = 2
update_id = topic.id
end
# else
#二级回复
# end
end
when "JournalsForMessage"
# if jour.jour_type == "Project"
# if jour.project.is_public == false && !current_user.member_of?(jour.project)
# status = -1
# tip = 1
# end
# elsif jour.jour_type == "Course"
# if jour.course.is_public == 0 && !current_user.member_of_course?(jour.course)
# status = -1
# tip = 0
# end
# end
if status == 0
# if reply_type == nil || reply_type == 0
jour = JournalsForMessage.find(params[:id]).root
parent_id = params[:id]
author_id = current_user.id
reply_user_id = jour.user_id
reply_id = params[:id]
content = params[:content]
options = {:user_id => author_id,
:status => true,
:m_parent_id => parent_id,
:m_reply_id => reply_id,
:reply_id => reply_user_id,
:root_id => jour.id,
:notes => content,
:is_readed => false}
jfm = jour.user.add_jour(nil, nil, nil, options)
if jfm.errors.empty?
(JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now)
result = 2
update_id = jour.id
end
# else
#二级回复
# end
end
when 'Issue'
# if issue.project.is_public == false && !current_user.member_of?(issue.project)
# status = -1
# tip = 1
# end
if status == 0
if reply_type == nil || reply_type == 0
issue = Issue.find params[:id]
is_jour = Journal.new
is_jour.user_id = current_user.id
is_jour.notes = params[:content]
is_jour.journalized = issue
if is_jour.save
result = 2
update_id = issue.id
end
else
#二级回复
jour = Journal.find(params[:id])
issue = jour.journalized
is_jour = Journal.new
is_jour.user_id = current_user.id
is_jour.notes = params[:content]
is_jour.reply_id = params[:id]
is_jour.parent_id = params[:id]
is_jour.journalized = issue
if is_jour.save
result = 2
update_id = issue.id
end
end
end
when 'BlogComment'
if reply_type == nil || reply_type == 0
blog = BlogComment.find(params[:id]).root
blogComment = BlogComment.new
blogComment.author = current_user
blogComment.blog = blog.blog
blogComment.content = params[:content]
blogComment.title = "RE: #{blog.title}"
blogComment.root_id = blog.id
if blog.children << blogComment
result = 2
update_id = blog.id
end
else
#二级回复
blog = BlogComment.find(params[:id]).root
blogComment = BlogComment.new
blogComment.author = current_user
blogComment.blog = blog.blog
blogComment.content = params[:content]
blogComment.title = "RE: #{blog.title}"
parent = BlogComment.find params[:id]
blogComment.parent_id = params[:id] #被回复的回复
blogComment.reply_id = parent.author.id #被回复者id
blogComment.root_id = blog.id
if parent.children << blogComment
result = 2
update_id = blog.id
end
end
end
if result == 2
update_course_activity_api(type,update_id)
update_user_activity_api(type,update_id)
update_org_activity_api(type,update_id)
update_forge_activity_api(type,update_id)
update_principal_activity_api(type,update_id)
end
else
result = 3
end
present :result, result
present :status, status
present :act_id, update_id
present :tip, tip
present :subscribe,subscribe
end
end
end
end
end

@ -0,0 +1,47 @@
#coding=utf-8
module Mobile
module Apis
class Newss< Grape::API
resources :newss do
desc "get special news"
post ':id' do
begin
# authenticate!
user = current_user
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
is_public = 1
if type == 0
news = News.find params[:id]
if news.project
is_public = news.project.is_public
elsif news.course
is_public = news.course.is_public
end
else
news = Comment.find params[:id]
end
present :data, news, with: Mobile::Entities::News,user: user,type: type,page: page
present :type, type
present :page, page
present :is_public, is_public
present :status, 0
rescue Exception=>e
Rails.logger.info "course_notice is not find!"
Rails.logger.info e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,77 @@
#coding=utf-8
module Mobile
module Apis
class Praise< Grape::API
include ApiHelper
content_type :json, 'application/json;charset=UTF-8'
resources :praise do
desc "点赞、踩、取消点赞"
params do
requires :container_id, type: Integer, desc: "点赞实例的id"
requires :container_type, type: String, desc: "类型"
requires :type, type: Integer, desc: "0表示踩 1表示赞"
end
post 'praise_or_tread' do
user = current_user
pt = PraiseTread.where(:praise_tread_object_id => params[:container_id],
:praise_tread_object_type => parmas[:container_type],
:user_id => user.id).first
case params[:container_type]
when 'GameCode'
game_codes = GameCode.where(:game_id => parmas[:container_id])
if (pt.blank? && params[:type] == 1) || (pt && params[:type] == 0)
game_codes.map{|code| code.increment!(:vote)}
else
game_codes.map{|code| code.decrement!(:vote)}
end
end
if pts.blank?
PraiseTread.create(:praise_tread_object_type => params[:container_type],
:praise_tread_object_id => params[:container_id],
:user_id => user.id,
:praise_or_tread => params[:type])
else
pt.destory
end
{status: 1}
end
desc "praise an activity"
params do
requires :type, type: String
requires :token, type: String
end
post ':id' do
authenticate!
obj_id = params[:id]
obj_type = params[:type]
user = current_user
pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?", obj_id, obj_type.to_s, user.id).first
if pts.blank?
praise_or_cancel(obj_type,obj_id,user,1)
obj = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
num = get_activity_praise_num(obj) if !obj.blank?
else
pts.destroy if !pts.blank?
#再更新praise_tread_cache表 使相应的记录减1 当为0时删除
ptc = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
ptc.praise_minus(1) if !ptc.blank?
if ptc.praise_num == 0
ptc.delete
end
obj = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
num = !obj.blank? ? get_activity_praise_num(obj) : 0
end
present :data, num
present :status, 0
end
end
end
end
end

@ -0,0 +1,350 @@
#coding=utf-8
module Mobile
module Apis
class Projects < Grape::API
resources :projects do
desc "获取项目列表"
params do
requires :token, type: String
end
get do
authenticate!
ps = ProjectsService.new
projects = ps.user_projects(current_user)
present :data, projects, with: Mobile::Entities::Project,user: current_user
present :status, 0
end
desc "返回单个项目"
params do
requires :id, type: Integer
requires :token,type:String
end
route_param :id do
get do
# course = Course.find(params[:id])
ps = ProjectsService.new
project = ps.show_project(params,current_user)
if project[:status] == 9
{status:-1, message: '该项目不存在或已被删除啦' }
else
present :data, project, with: Mobile::Entities::Project,user: current_user
present :status, 0
end
end
end
desc "获取项目动态"
params do
requires :id, type: Integer
requires :token, type: String
end
post 'activities' do
authenticate!
user = current_user
project_types = "('Message','Issue','Project')"
activities = UserActivity.where("(container_type = 'Project' and container_id = #{params[:id]} and act_type in #{project_types})").order('updated_at desc')
page = params[:page] ? params[:page] : 0
all_count = activities.count
activities = activities.limit(10).offset(page * 10)
count = activities.count
present :data, activities, with: Mobile::Entities::Activity,user: user
present :all_count, all_count
present :count, count
present :page, page
present :status, 0
end
desc "获取项目成员"
params do
requires :id, type: Integer
requires :token, type: String
end
post 'members' do
authenticate!
project = Project.find("#{params[:id]}")
members = project.member_principals
master_members = project.member_principals.includes(:roles, :principal).where("member_roles.role_id=3").all.sort
master_members.each do |m|
if m.user_id == project.user_id
master_members.delete(m)
master_members.insert(0,m)
break
end
end
develop_members = project.member_principals.includes(:roles, :principal).where("member_roles.role_id=4").all.sort
report_members = project.member_principals.includes(:roles, :principal).where("member_roles.role_id=5").all.sort
review_master_members = []
review_develop_members = []
my_member = project.member_principals.where("users.id=#{current_user.id}").first
if my_member && my_member.roles[0] && my_member.roles[0].id == 3
ps = ProjectsService.new
user = current_user
review_master_members = ps.get_project_review_members(project,3,user)
review_develop_members = ps.get_project_review_members(project,4,user)
end
present :master_members,master_members, with: Mobile::Entities::ProjectMember
present :develop_members,develop_members, with: Mobile::Entities::ProjectMember
present :report_members,report_members, with: Mobile::Entities::ProjectMember
present :review_master_members,review_master_members
present :review_develop_members,review_develop_members
present :status, 0
end
desc "获取项目某成员角色信息"
params do
requires :id, type: Integer
requires :token, type: String
requires :user_id, type: Integer
end
post 'get_member_info' do
authenticate!
project = Project.find("#{params[:id]}")
my_member = project.member_principals.where("users.id=#{params[:user_id]}").first
if my_member && my_member.roles[0]
present :project_id,params[:id]
present :user_id,params[:user_id]
present :member_info,my_member, with: Mobile::Entities::ProjectMember
present :status, 0
else
present :status, -1
present :message, "该用户已不在项目中"
end
end
desc "修改项目某成员角色信息"
params do
requires :id, type: Integer
requires :token, type: String
requires :user_id, type: Integer
requires :role_id, type: Integer
end
post 'edit_member_role' do
authenticate!
project = Project.find("#{params[:id]}")
my_member = project.member_principals.where("users.id=#{current_user.id}").first
#3管理 4开发 5报告
if !(my_member && my_member.roles[0] && my_member.roles[0].id == 3 ) || project.user_id == params[:user_id] || !(params[:role_id] == 3 || params[:role_id] == 4 || params[:role_id] == 5)
present :status, -1
else
ps = ProjectsService.new
status = ps.modify_user_project_role params
present :status, status
end
end
desc "新建项目"
params do
requires :token, type: String
requires :name, type: String, desc: '项目名称'
end
post 'create' do
authenticate!
ps = ProjectsService.new
status = ps.createNewProject params,current_user
present :status, 0
end
desc "加入项目"
params do
requires :token, type: String
requires :invite_code, type: String, desc: '邀请码'
requires :role_id, type: Integer, desc: '身份'
end
post "join_project" do
authenticate!
role_id = params[:role_id]
if role_id != 3 && role_id != 4 && role_id != 5
{status:-1,message:"请至少选择一个角色"}
else
ps = ProjectsService.new
status = ps.join_project({role: role_id, openid: params[:openid], invite_code: params[:invite_code]}, current_user)
{status:status, message:ProjectsService::JoinProjectError.message(status)}
end
# {status:-1, message: '该功能将在近日开放,敬请期待!' }
end
desc "项目单个未审核用户信息"
params do
requires :token, type: String
requires :project_id, type: Integer, desc: "项目id"
requires :user_id, type: Integer, desc: "用户id"
end
post 'review_member_info' do
authenticate!
ps = ProjectsService.new
result = ps.get_reviewer_info(params,current_user)
present :data, result[:reviewer], with: Mobile::Entities::User
present :status, result[:status]
end
desc "处理管理或开发的身份申请"
params do
requires :token, type: String
requires :project_id, type: Integer, desc: "项目id"
requires :user_id, type: Integer, desc: "用户id"
requires :type, type: Integer, desc: "同意或拒绝"
end
post 'deal_join_apply' do
authenticate!
ps = ProjectsService.new
result = ps.deal_join_apply(params,current_user)
present :status, result[:status]
present :message, result[:message]
end
desc "发布项目帖子"
params do
requires :id, type: Integer
requires :token, type: String
requires :text, type: String
end
post ':id/publishnote' do
authenticate!
project = Project.find("#{params[:id]}")
boards = project.boards.includes(:last_message => :author).all
if project.boards.empty?
board = project.boards.build
board.name = "项目讨论区"
board.description = project.name.to_s
board.course_id = -1
if board.save
boards = project.boards.includes(:last_message => :author).all
end
end
board = boards.first
message = Message.new
message.author = current_user
message.board = board
tmpparams = {}
tmpparams['subject'] = params[:title]
tmpparams['content'] = params[:text]
message.safe_attributes = tmpparams
message.save!
present :status, 0
end
desc "删除项目成员"
params do
requires :id, type: Integer
requires :token, type: String
requires :user_id, type: Integer
end
post ':id/deletemember' do
authenticate!
status = -1
if(current_user.id != params[:user_id].to_i)
#权限
project = Project.find("#{params[:id]}")
if project.user_id != params[:user_id].to_i
my_member = project.member_principals.where("users.id=#{current_user.id}").first
roles_ids = []
my_member.roles.each do |role|
roles_ids << role.id
end
if my_member && roles_ids.include?(3)
#删除该成员
ps = ProjectsService.new
status = ps.project_delete_member(project,params[:user_id].to_i,current_user)
else
status = -2
end
else
status = -3
end
else
status = -4
end
out = {status: status}
message = case status
when 0; "删除成功"
when 1; "该用户不在该项目中"
when -1; "您还未登录"
when -2; "您没有权限"
when -3; "不能删除项目创建者"
when -4; "不能删除自己"
else; "未知错误,请稍后再试"
end
out.merge(message: message)
end
desc "退出项目"
params do
requires :id, type: Integer
requires :token, type: String
end
post ':id/quit' do
authenticate!
project = Project.find("#{params[:id]}")
ps = ProjectsService.new
status = ps.exit_project(project,current_user)
out = {status: status}
message = case status
when 0; "退出项目成功"
when -3; "您不是该项目成员"
when -1; "您还未登录"
when -2; "项目创建者不能退出项目"
else; "未知错误,请稍后再试"
end
out.merge(message: message)
end
end
end
end
end

@ -0,0 +1,119 @@
#coding=utf-8
module Mobile
module Apis
class Resources < Grape::API
resource :competition_teams do
desc '获取所有课件'
params do
requires :token, type: String
requires :page, type: Integer
end
post do
authenticate!
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
# data = current_user.course_attachments
data = rs.all_course_attachments current_user
all_count = data.count
data = data.limit(10).offset(page * 10)
count = data.count
present :data, data, with: Mobile::Entities::Attachment,user: current_user
present :all_count, all_count
present :count, count
present :page, page
present :status, 0
end
desc '获取所有作业'
params do
requires :token, type: String
requires :page, type: Integer
end
post 'homeworks' do
authenticate!
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
homeworks = rs.all_homework_commons current_user
all_count = homeworks.count
homeworks = homeworks.limit(10).offset(page * 10)
count = homeworks.count
present :data, homeworks, with: Mobile::Entities::Homework,user: current_user
present :all_count, all_count
present :count, count
present :page, page
present :status, 0
end
desc '获取所有测验'
params do
requires :token, type: String
requires :page, type: Integer
end
post 'exercises' do
authenticate!
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
exercises = rs.all_exercises current_user
all_count = exercises.count
exercises = exercises.limit(10).offset(page * 10)
count = exercises.count
present :data, exercises, with: Mobile::Entities::Exercise,user: current_user
present :all_count, all_count
present :count, count
present :page, page
present :status, 0
end
desc '发送资源'
params do
requires :token, type: String
requires :course_ids, type: Array[Integer]
requires :send_id, type: Integer
requires :send_type, type: Integer
end
post 'send' do
authenticate!
rs = ResourcesService.new
ori = nil
flag = false
save_message = nil
case params[:send_type]
when 1
ori, flag, save_message = rs.send_resource_to_course(current_user,params)
when 2
ori, flag, save_message = rs.send_homework_to_course(current_user,params)
when 3
ori, flag, save_message = rs.send_exercise_to_course(current_user,params)
end
if flag
present :status, 0
else
{status: -1, message: save_message.first}
end
end
end
end
end
end

@ -0,0 +1,72 @@
# encoding=utf-8
module Mobile
module Apis
class Shixuns < Grape::API
before {authenticate!}
content_type :json, 'application/json;charset=UTF-8'
# status -1 表示异常0 结果需要返回给用户
resources :shixuns do
desc '显示TPI详情'
params do
requires :identifier, type: String, desc: 'shixun identifier.'
end
route_param :identifier do
get 'show_shixun' do
ShixunsService.new.show_shixun(params, current_user)
end
end
desc "显示TPM讨论区列表"
params do
requires :container_id, type: String, desc: "评论的载体,如: Shixun.id"
requires :container_type, type: String, desc: "评论的类型,如: Shixun"
requires :page, type: String, desc: "初次第一页为0"
end
route_param :container_id do
get 'shixun_discusses' do
game = ShixunsService.new.shixun_discuss(params, current_user)
end
end
desc "对于有分页的情况,新消息定位到当前位置"
params do
requires :container_id, type: String, desc: "评论的载体,如: Shixun.id"
requires :container_type, type: String, desc: "评论的类型,如: Shixun"
requires :discuss_id, type: String, desc: "评论的根节点root_idid"
requires :myshixun_id, type: String, desc: "获取是否有新的消息"
end
route_param :container_id do
get 'anchor' do
ShixunsService.new.anchor(params, current_user)
end
end
desc "轮询获取是否有新消息"
params do
requires :container_id, type: String, desc: "评论的载体,如: Shixun.id"
requires :container_type, type: String, desc: "评论的类型,如: Shixun"
requires :myshixun_id, type: String, desc: "学员载体myshixun"
end
route_param :container_id do
get 'new_message' do
ShixunsService.new.new_message(params, current_user)
end
end
desc "获取tpi版本库地址"
params do
requires :identifier, type: String, desc: 'shixun identifier.'
requires :page, type: String, desc: "初次第一页为0"
end
route_param :identifier do
get 'myshixun_git' do
ShixunsService.new.myshixun_git(params, current_user)
end
end
end
end
end
end

@ -0,0 +1,236 @@
# encoding=utf-8
module Mobile
module Apis
class Sources < Grape::API
before {private_auth}
content_type :json, 'application/json;charset=UTF-8'
resources :sources do
# shixuns TPM start ############################################
desc '所有实训列表'
params do
requires :time, type: Date, desc: '创建时间'
end
get "shixun_index" do
SourcesService.new.shixun_index(params)
end
desc '显示TPM详情'
params do
requires :identifier, type: String, desc: 'shixun identifier.'
end
route_param :identifier do
get 'shixun_detail' do
SourcesService.new.shixun_detail(params)
end
end
desc '显示实训的challenges'
params do
requires :identifier, type: String, desc: 'shixun identifier.'
end
route_param :identifier do
get 'shixun_challenges' do
SourcesService.new.shixun_challenges(params)
end
end
desc "获取TPM所有TPI的信息"
params do
requires :identifier, type: String, desc: "TPM的shixun.identifier"
end
route_param :identifier do
get 'get_tpi_info' do
SourcesService.new.get_tpi_info params
end
end
# games start ############################################
desc '所有games'
params do
requires :time, type: Date, desc: '创建时间'
end
get "myshixuns_index" do
SourcesService.new.myshixuns_index(params)
end
# 搜索学员实训
desc '搜索学员实训'
params do
requires :shixun_id, type: Integer
requires :user_id, type: Integer
end
get "search_myshixun" do
SourcesService.new.search_myshixun(params)
end
desc '所有games'
params do
requires :time, type: Date, desc: '创建时间'
end
get "games" do
SourcesService.new.games(params)
end
desc 'game详情'
params do
requires :identifier, type: String, desc: 'game identifier'
end
route_param :identifier do
get 'game_detail' do
SourcesService.new.game_detail(params)
end
end
# 搜索学员实训
desc '搜索学员实训'
params do
requires :user_id, type: Integer
requires :challenge_id, type: Integer
end
get "search_game" do
SourcesService.new.search_game(params)
end
# paths start #####################################
desc "实训路径信息"
params do
requires :id, type: Integer, desc: "实训路径id"
end
route_param :id do
get 'get_paths_info' do
SourcesService.new.get_paths_info params
end
end
# users start ############################################
desc '所有用户'
params do
requires :time, type: Date, desc: '创建时间'
end
get "users" do
SourcesService.new.users(params)
end
desc "登陆验证"
params do
requires :username, type: String, desc: "用户名"
requires :password, type: String, desc: "密码"
end
post "login" do
SourcesService.new.login params
end
desc "获取用户信息"
route_param :id do
get "get_user_info" do
SourcesService.new.get_user_info params
end
end
# courses start ############################################
desc "所有课程列表"
params do
requires :time, type: Date, desc: '创建时间'
end
get 'courses' do
SourcesService.new.courses params
end
desc '课堂详情'
route_param :id do
get 'course_detail' do
SourcesService.new.course_detail(params)
end
end
# Exercise start ###########################
desc "所有试卷"
params do
requires :time, type: Date, desc: '创建时间'
end
get 'exercises' do
SourcesService.new.exercises params
end
desc '试卷详情'
route_param :id do
get 'exercise_detail' do
SourcesService.new.exercise_detail(params)
end
end
desc "题目详情"
params do
requires :id, type: Integer, desc: "试卷id"
end
route_param :id do
get 'get_exercise_questions' do
SourcesService.new.get_exercise_questions params
end
end
desc '课堂实训作业'
params do
requires :course_id, type: Integer, desc: 'course id'
end
route_param :identifier do
get 'exercise_detail' do
SourcesService.new.shixun_homework_commons(params)
end
end
desc "获取竞赛用户所在组"
params do
requires :identifier, type: String, desc: 'competition.identifier竞赛唯一标识'
requires :user_id, type: Integer, desc: "用户id"
end
route_param :identifier do
get "get_competition_members" do
SourcesService.new.get_competition_members(params)
end
end
desc "获取竞赛战队ID"
params do
requires :identifier, type: String, desc: 'competition.identifier竞赛唯一标识'
end
route_param :identifier do
get 'get_team_ids' do
SourcesService.new.get_team_ids params
end
end
desc "获取竞赛组内成员"
params do
requires :team_id, type: Integer, desc: "竞赛分组id"
requires :identifier
end
route_param :identifier do
get 'get_team_user_ids' do
SourcesService.new.get_team_user_ids params
end
end
desc "获取用户在比赛中对应的组id"
params do
requires :educoder_userid
requires :identifier
end
get 'get_user_competition_id' do
SourcesService.new.get_user_competition_id params
end
desc "获取竞赛小组信息"
params do
requires :identifier, type: String, desc: "竞赛标识"
end
get 'get_team_info' do
SourcesService.new.get_team_info params
end
end
end
end
end

@ -0,0 +1,103 @@
#coding=utf-8
module Mobile
module Apis
class Syllabuses < Grape::API
resources :syllabuses do
desc "获取大纲列表"
params do
requires :token, type: String
end
get do
authenticate!
cs = SyllabusesService.new
courses = cs.user_syllabus(current_user)
present :data, courses, with: Mobile::Entities::Syllabus,user: current_user
present :status, 0
end
desc "获取某个大纲"
params do
requires :token, type: String
end
get ':id' do
authenticate!
ss = SyllabusesService.new
sy = ::Syllabus.find(params[:id])
result = {:title=>sy.title, :id=>sy.id , :user_id=>sy.user_id,:courses=>sy.courses.not_deleted}
present :data, result, with: Mobile::Entities::Syllabus,user: current_user
present :status, 0
end
desc "获取新建大纲的权限"
params do
requires :token, type: String
end
post 'auth' do
authenticate!
auth = 0
if (current_user.user_extensions && current_user.user_extensions.identity == 0 && current_user.allowed_to?(:add_course, nil, :global => true))
auth = 1
end
present :auth, auth
end
desc "新建大纲"
params do
requires :token, type: String
requires :title, type: String, desc: '大纲标题'
requires :courses, type: Array[String], desc: '课程名'
end
post do
authenticate!
ss = SyllabusesService.new
sy = ss.create(current_user, params[:title],
params[:courses].map{|c| {name: c} })
if sy.new_record?
{status:-1, message: '创建大纲失败' }
else
present :data, sy, with: Mobile::Entities::Syllabus,user: current_user
present :status, 0
end
end
desc '编辑大纲'
params do
requires :token, type: String
requires :title, type: String, desc: '大纲标题'
# requires :add_courses, type: Array[String], desc: '课程名'
# requires :modify_courses, type: Array[Integer,String], desc: '课程名'
end
post ':id/edit' do
authenticate!
ss = SyllabusesService.new
#修改课程大纲
status = ss.edit(current_user, params)
if status == -1
{status:status, message: '修改课程信息失败' }
else
present :status, status
end
end
end
end
end
end

@ -0,0 +1,29 @@
#coding=utf-8
module Mobile
module Apis
class Upgrade < Grape::API
include ApplicationHelper
resource :upgrade do
desc "get update info"
params do
requires :platform, type: String, desc: '平台名android, ios'
end
get do
@current_version = ::PhoneAppVersion.reorder('created_at desc').first
attachment = @current_version.attachments.first
if attachment.nil?
raise '未发现客户端!'
end
url = "http://" + Setting.host_name + "/attachments/download/" + attachment.id.to_s + "/" + attachment.filename
{
version: @current_version.version,
url: url,
desc: @current_version.description
}
end
end
end
end
end

@ -0,0 +1,383 @@
#coding=utf-8
module Mobile
module Apis
class Users < Grape::API
resource :users do
content_type :json, 'application/json;charset=UTF-8'
desc "扣除金币"
params do
requires :container_id, type: Integer, desc: '扣分类型的ID'
requires :container_type, type: String, desc: '扣分类型'
requires :score, type: Integer, desc: "扣分分值"
end
get 'consume_score' do
UsersService.new.consume_score params, current_user
end
desc "用户登录非Phone(仅仅平台登录)"
params do
requires :username, type: String
requires :password, type: String
requires :autologin, type: String
end
post "login" do
UsersService.new.login(params, current_user)
end
desc "用户注册mini app"
params do
requires :phone, type: String, desc: 'username'
requires :password, type: String, desc: 'password'
end
post do
us = UsersService.new
us.register params.merge(:password_confirmation => params[:password],
:should_confirmation_password => true)
end
desc "获取用户信息"
params do
requires :token, type: String
optional :login, type: String, desc: '不传则查自己的信息'
end
get 'get_userinfo' do
authenticate!
ue = current_user
if params[:login].present?
ue = UsersService.new.show_user params
end
present :data, ue, with: Mobile::Entities::User
present :status, 0
end
desc '获取用户课程'
params do
requires :token, type: String
optional :page, type: Integer, desc: '页码默认1', default: 1
optional :limit, type: Integer, desc: '一页多少条', default: 5
end
post 'courses' do
authenticate!
us = UsersService.new
data = us.current_courses params, current_user
present :data, data, with: Mobile::Entities::Users::Course
present :status, 0
end
desc "获取用户消息(新)"
params do
requires :token, type: String
optional :page, type: Integer, desc: '页码默认1'
end
post "tidings" do
authenticate!
tidings = UsersService.new.get_tidings(params,current_user)
present :data, tidings, with: Mobile::Entities::Users::Tiding
present :status, 0
end
desc "上传头像"
params do
requires :token, type: String
requires :imgdata, type: String
end
post 'upload_head' do
authenticate!
#直接生成头像
dirPath = "public/images/avatars/User"
fileDir = dirPath + "/" + current_user.id.to_s
imgdata = Base64.decode64(params[:imgdata].split(",")[1])
if imgdata.length > 1024 * 1024
present :message, "上传图片不能超过1M"
present :status, -1
else
begin
File.open(fileDir, 'wb') {|f| f.write(imgdata)}
present :status, 0
rescue
present :message, "上传失败"
present :status, -1
end
end
end
desc "解除绑定"
params do
requires :token, type: String
end
post 'user_unbind' do
authenticate!
user = current_user
uw = user.user_wechat
us = UsersService.new
us.wechat_unbind user.user_wechat
present :status, 0
end
desc "查询是否已绑定"
params do
requires :openid, type: String, desc: 'wechat openid'
end
post 'isbind' do
openid = params[:openid]
uw = UserWechat.where(openid: openid).first
raise "还未绑定trustie帐户" unless uw
user = uw.user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
present status: 0, token: key.access_token
end
desc "绑定微信用户"
params do
requires :login, type: String, desc: 'username'
requires :password, type: String, desc: 'password'
end
post 'wxbind' do
openid = session[:wechat_openid]
logger.debug "openid ============== #{openid}"
raise "无法获取到openid,请在微信中打开本页面" unless openid
uw = UserWechat.where(openid: openid).first
raise "此微信号已绑定用户(#{uw.user.login}), 不能重复绑定" if uw && uw.real?
user, last_login_on = User.try_to_login(params[:login], params[:password])
raise "用户名或密码错误,请重新输入" unless user
#补全用户信息
raise "此用户已经绑定过公众号, 请换一个帐户试试" if user.user_wechat
if uw && !uw.real?
uw.migrate_user(user)
else
UserWechat.create!(
openid: openid,
user: user
)
end
ws = WechatService.new
ws.binding_succ_notice(user.id, "您已成功绑定Trustie平台。", user.show_name + "(" + user.login + ")", Time.now.strftime("%Y-%m-%d"))
present status: 0, message: '您已成功绑定Trustie平台'
end
# desc "注册用户"
# params do
# requires :login, type: String, desc: 'username'
# requires :mail, type: String, desc: 'mail'
# requires :password, type: String, desc: 'password'
# end
# post do
# openid = session[:wechat_openid]
# logger.debug "openid ============== #{openid}"
# raise "无法获取到openid,请在微信中打开本页面" unless openid
#
# uw = UserWechat.where(openid: openid).first
# raise "此微信号已绑定用户(#{uw.user.login}), 不能重复绑定" if uw && uw.real?
# us = UsersService.new
# user = us.register params.merge(:password_confirmation => params[:password],
# :should_confirmation_password => true)
# raise user.errors.full_messages.first if user.new_record?
# #自动激活
# if Setting.self_registration != '3'
# # user = automatically_register(user)
# user.activate
# user.last_login_on = Time.now
# if user.save
# UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0)
# end
# end
#
# if uw && !uw.real?
# user.update_attributes(:lastname=>uw.user[:lastname])
# uw.migrate_user(user)
# else
# UserWechat.create!(
# openid: openid,
# user: user
# )
# end
#
# ws = WechatService.new
# ws.binding_succ_notice(user.id, "您已成功绑定Trustie平台。", user.show_name+"("+user.login+")", Time.now.strftime("%Y-%m-%d"))
# present :data, user, with: Mobile::Entities::User
# present :status, 0
# end
desc "修改用户"
params do
requires :token, type: String
#optional :file, type: File, desc: 'avatar'
optional :occupation, type: String
optional :brief_introduction, type: String
optional :province, type: String
optional :city, type: String
optional :gender, type: Integer
end
put ':id' do
authenticate!
us = UsersService.new
ue = us.edit_user params.merge(id: current_user.id)
present :data, ue, with: Mobile::Entities::User
present :status, 0
end
desc "关注"
params do
requires :object_id, type: Integer
requires :object_type, type: String, desc: '关注的类型'
end
get ':id/watch' do
us = UsersService.new
us.watch(params, current_user)
end
desc "取消关注"
params do
requires :object_id, type: Integer
requires :object_type, type: String, desc: '关注的类型'
end
get ':id/unwatch' do
us = UsersService.new
us.unwatch(params, current_user)
end
desc '修改密码'
params do
requires :token, type: String
requires :password, type: String, desc: '原密码'
requires :new_password, type: String, desc: '新密码'
end
post 'password' do
authenticate!
us = UsersService.new
user = us.change_password params.merge(current_user_id: current_user.id,
new_password_confirmation: params[:new_password])
present :data, user, with: Mobile::Entities::User
present :status, 0
end
desc "用户搜索"
params do
requires :name, type: String, desc: '用户名关键字'
requires :search_by, type: String, desc: '搜索依据0 登录名1 用户名2 邮箱,3 登录名和姓名'
optional :is_search_assitant, type: Integer, desc: '是否搜索注册用户来作为助教'
optional :course_id, type: Integer, desc: '课程id搜索注册用户不为该课程教师的其他用户'
optional :user_id, type: Integer, desc: '用户id'
end
get 'search/search_user' do
us = UsersService.new
user = us.search_user params
present :data, user, with: Mobile::Entities::User
present :status, 0
end
desc "用户留言"
params do
requires :token, type: String
requires :user_id, type: Integer, desc: '被留言的用户id'
requires :page, type: Integer, desc: '请求数据的页码'
end
get ':user_id/messages' do
us = UsersService.new
jours = us.get_all_messages params
present :data, jours, with: Mobile::Entities::Jours
present :status, 0
end
desc "回复用户留言"
params do
requires :token, type: String
requires :user_id, type: Integer, desc: '被留言的用户id'
requires :content, type: String, desc: '留言内容'
requires :ref_user_id, type: Integer, desc: '被回复的用户id'
requires :parent_id, type: Integer, desc: '留言父id'
requires :ref_message_id, type: Integer, desc: '引用消息id'
optional :type, type: Integer, desc: '回复类型'
optional :course_id, type: Integer, desc: '课程id'
end
post ':user_id/reply_message' do
us = UsersService.new
jours = us.reply_user_messages params, current_user
present :status, 0
end
desc "给用户留言"
params do
requires :token, type: String
requires :user_id, type: Integer, desc: '被留言的用户id'
requires :content, type: String, desc: '留言内容'
end
post ':user_id/leave_message' do
us = UsersService.new
us.leave_message params, current_user
present :data, 0
end
desc "与我相关"
params do
requires :token, type: String
requires :page, type: Integer, desc: '页码'
end
get ':user_id/all_my_dynamic' do
us = UsersService.new
my_jours = us.reply_my_messages params, current_user
present :data, my_jours, with: Mobile::Entities::Jours
present :status, 0
end
desc "修改我的资料"
params do
requires :token, type: String
requires :lastname, type: String
requires :sex, type: Integer
end
post 'edit_userinfo' do
authenticate!
user = current_user
status = 0
message = ""
#昵称 不能超过30个字符
if params[:lastname].length > 30 then
message = "姓名不能超过30个字符!"
status = -1
end
if status == 0
user.lastname = params[:lastname]
user.update_attribute(:lastname, params[:lastname])
se = user.extensions
se.update_attribute(:gender, params[:sex])
end
if status == 0
present :data, current_user, with: Mobile::Entities::User
present :status, 0
else
present :status, -1
present :message, message
end
end
end
end
end
end

@ -0,0 +1,49 @@
#coding=utf-8
module Mobile
module Apis
class Watches < Grape::API
resource :watches do
desc "获取所有关注"
params do
requires :token, type: String
end
get do
authenticate!
us = UsersService.new
ws = us.user_watcher(id: current_user.id)
present :data, ws, with: Mobile::Entities::User
present :status, 0
end
desc "关注某人"
params do
requires :token, type: String
requires :object_id, type: Integer, desc: '关注的用户的id'
end
post do
authenticate!
ws = WatchesService.new
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
present :data, o, with: Mobile::Entities::User
present :status, 0
end
desc "取消关注"
params do
requires :token, type: String
requires :object_id, type: Integer, desc: '取消关注的用户的id'
end
delete do
authenticate!
ws = WatchesService.new
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
{status: 0}
end
end
end
end
end

@ -0,0 +1,40 @@
#coding=utf-8
module Mobile
module Apis
class Whomeworks< Grape::API
resources :whomeworks do
desc "get one homework"
post ':id' do
begin
# authenticate!
user = current_user
#0一级回复的更多 1 二级回复的更多
type = params[:type] || 0
page = params[:page] || 0
is_public = 1
if type == 0
homework = HomeworkCommon.find params[:id]
is_public = homework.course.is_public
present :data, homework, with: Mobile::Entities::Whomework,user: user,type: type,page: page,comment_type: "homework"
else
jour = JournalsForMessage.find params[:id]
present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page,comment_type: "homework"
end
present :type, type
present :page, page
present :is_public, is_public
present :status, 0
rescue Exception=>e
present :status, -1
present :message, e.message
end
end
end
end
end
end

@ -0,0 +1,195 @@
# encoding: utf-8
module Mobile
module Entities
class Activity <Grape::Entity
include ApplicationHelper
include ApiHelper
def self.act_expose(f)
expose f do |ac,opt|
if ac.is_a?(Hash) && ac.key?(f)
ac[f]
elsif ac.is_a?(::UserActivity)
if ac.respond_to?(f)
ac.send(f)
else
case f
when :user_act
if ac.act_type == "Project"
ac unless ac.nil?
else
ac.act unless ac.nil? || ac.act.nil?
end
when :reply_count
if ac.act_type == "HomeworkCommon"
ac.nil? || ac.act.nil? ? 0 : ac.act.journals_for_messages.count
elsif ac.act_type == "News"
ac.nil? || ac.act.nil? ? 0 : ac.act.comments.count
elsif ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage"
className = ac.act_type.classify.constantize
# all_comments = []
# ac.nil? || ac.act.nil? ? 0 : get_all_children(all_comments, ac.act).count
ac.nil? || ac.act.nil? ? 0 : className.where("root_id = #{ac.act.id}").count
elsif ac.act_type == "Issue"
ac.nil? || ac.act.nil? ? 0 : ac.act.journals.where("notes is not null and notes != ''").count
end
when :subject
if ac.act_type == "HomeworkCommon"
ac.act.name unless ac.nil? || ac.act.nil?
elsif ac.act_type == "News" || ac.act_type == "BlogComment"
ac.act.title unless ac.nil? || ac.act.nil?
elsif ac.act_type == "Message" || ac.act_type == "Issue"
ac.act.subject unless ac.nil? || ac.act.nil?
elsif ac.act_type == "JournalsForMessage"
ac.act.private == 0 ? "留言" : "私信" unless ac.nil? || ac.act.nil?
elsif ac.act_type == "Poll"
ac.act.polls_name unless ac.nil? || ac.act.nil?
end
when :description
if ac.act_type == "HomeworkCommon" || ac.act_type == "Issue" || ac.act_type == "News"
strip_html(ac.act.description) unless ac.nil? || ac.act.nil?
elsif ac.act_type == "Message" || ac.act_type == "BlogComment"
strip_html(ac.act.content) unless ac.nil? || ac.act.nil?
elsif ac.act_type == "JournalsForMessage"
strip_html(ac.act.notes) unless ac.nil? || ac.act.nil?
elsif ac.act_type == "Poll"
ac.act.polls_description unless ac.nil? || ac.act.nil?
end
when :latest_update
time_from_now ac.updated_at unless ac.nil?
when :praise_count
if ac.act_type == "HomeworkCommon" || ac.act_type == "News" || ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage" || ac.act_type == "Issue"
ac.nil? || ac.act.nil? ? 0 : get_activity_praise_num(ac.act)
end
#when :homework_common_detail_manual
# if ac.act_type == "HomeworkCommon"
# ac.act.homework_detail_manual unless ac.nil? || ac.act.nil? || ac.act.homework_detail_manual.nil?
# end
when :course_project_name
if ac.container_type == "Course"
name = (get_course(ac.container_id)).name
name
elsif ac.container_type == "Project"
name = (get_project(ac.container_id)).name
name
elsif ac.container_type == "Blog"
"发表博客"
end
when :syllabus_title
if ac.container_type == "Course"
course = get_course(ac.container_id)
name = course.syllabus.nil? ? "":course.syllabus.title
name
end
when :course_project_id
if ac.container_type == "Course"
ac.container_id
elsif ac.container_type == "Project"
ac.container_id
end
when :activity_type_name
if ac.container_type == "Course"
case ac.act_type
when "HomeworkCommon"
"班级作业"
when "News"
"班级通知"
when "Message"
"班级讨论区"
when "Poll"
"班级问卷"
when "Course"
"班级"
end
elsif ac.container_type == "Project"
case ac.act_type
when "Issue"
"项目问题"
when "Message"
"项目讨论区"
when "Project"
"项目"
end
end
when :homework_commit_count
if ac.act_type == "HomeworkCommon"
ac.act.student_works.has_committed.count
end
when :last_commit_info
if ac.act_type == "HomeworkCommon"
if ac.act.student_works.has_committed.count > 0
lc = ac.act.student_works.has_committed.reorder("commit_time desc").first
{:lasttime=>time_from_now(lc.commit_time), :lastname=>lc.user.show_name}
end
end
when :last_score_info
if ac.act_type == "HomeworkCommon"
if ac.act.student_works.has_committed.count > 0
sw_id = "("+ac.act.student_works.map{|sw| sw.id}.join(",")+")"
student_work_scores = StudentWorksScore.where("student_work_id in #{sw_id} and score is not null").reorder("created_at desc")
unless student_work_scores.empty?
last_score = student_work_scores.first
{:lasttime=>time_from_now(last_score.created_at), :lastname=>last_score.user.show_name}
end
end
end
end
end
end
end
end
expose :act_type #缺陷/作业/讨论区/留言等类型
expose :act_id
expose :id
expose :container_type #课程/项目/博客/个人
expose :author, using: Mobile::Entities::User do |a, opt| #用户信息
if a.is_a? ::UserActivity
if a.act_type == "Project"
get_user(get_project(a.act_id).user_id)
elsif !a.act.nil?
if a.act_type == 'Issue' || a.act_type == 'News' || a.act_type == 'Message' || a.act_type == 'BlogComment'
a.act.author
elsif a.act_type == 'HomeworkCommon' || a.act_type == 'Poll' || a.act_type == 'JournalsForMessage'
a.act.user
elsif a.act_type == 'Course'
a.act.teacher
end
end
end
end
expose :homework_common_detail , using: Mobile::Entities::Whomework do |a, opt| #作业相关信息
if a.act_type == "HomeworkCommon"
a.act
end
end
expose :issue_detail, using: Mobile::Entities::Issue do |a, opt| #缺陷信息
if a.act_type == "Issue"
a.act
end
end
act_expose :reply_count #回复数
act_expose :praise_count #点赞数
#act_expose :user_act #某个动态
act_expose :subject #标题
act_expose :description #描述
act_expose :latest_update #最新更新时间
act_expose :course_project_id #课程/项目ID
act_expose :course_project_name #课程/项目名字
act_expose :syllabus_title #课程名字
act_expose :activity_type_name #课程问答区/项目缺陷等
act_expose :homework_commit_count #作品提交数
act_expose :last_commit_info #最近提交信息
act_expose :last_score_info #最近评阅信息
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
if instance.act_type == "HomeworkCommon" || instance.act_type == "News" || instance.act_type == "Message" || instance.act_type == "BlogComment" || instance.act_type == "JournalsForMessage" || instance.act_type == "Issue"
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.act_id,instance.act_type.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
end
end
end
end

@ -0,0 +1,36 @@
module Mobile
module Entities
#匿评作品页面相关参数
class AnonymousWorksParams < Grape::Entity
def self.anonymous_works_params_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(Hash) && !f.key?(field)
end
end
end
anonymous_works_params_expose :is_teacher
anonymous_works_params_expose :m_score
anonymous_works_params_expose :is_anonymous_comments
anonymous_works_params_expose :cur_type
anonymous_works_params_expose :cur_page
expose :jours ,using: Mobile::Entities::Jours do |f, opt|
if f.is_a?(Hash) && f.key?(:jours)
f[:jours]
end
end
expose :teacher_stars,using: Mobile::Entities::HomeworkJours do |f, opt|
if f.is_a?(Hash) && f.key?(:teacher_stars)
f[:teacher_stars]
end
end
expose :student_stars , using: Mobile::Entities::HomeworkJours do |f, opt|
if f.is_a?(Hash) && f.key?(:student_stars)
f[:student_stars]
end
end
end
end
end

@ -0,0 +1,58 @@
module Mobile
module Entities
class Attachment < Grape::Entity
include Redmine::I18n
include ActionView::Helpers::NumberHelper
include ApplicationHelper
def self.attachment_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::Attachment)
if f.respond_to?(field)
if field == :created_on
format_time(f.send(field))
else
f.send(field)
end
else
case field
when :download_url
"attachments/download/#{f.try(:id)}"
when :file_dir
"attachments/download/" << f.send(:id).to_s << '/'
when :attafile_size
(number_to_human_size(f.filesize)).gsub("ytes", "").to_s
when :coursename
f.try(:course).try(:name) || ''
when :syllabus_title
f.try(:course).try(:syllabus).try(:title) || ''
when :course_id
f.try(:course).try(:id) || 0
end
end
end
end
end
attachment_expose :id
attachment_expose :filename
attachment_expose :description
attachment_expose :downloads
attachment_expose :quotes
attachment_expose :created_on
attachment_expose :file_dir
attachment_expose :attafile_size
attachment_expose :coursename #所属班级名
attachment_expose :syllabus_title #所属班级名
attachment_expose :course_id #所属班级名
expose :current_user_is_teacher, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
current_user_is_teacher = false
current_user_is_teacher = is_course_teacher(current_user,instance.course)
current_user_is_teacher
end
attachment_expose :download_url
end
end
end

@ -0,0 +1,13 @@
module Mobile
module Entities
class Auth < Grape::Entity
expose :token
expose :user, using: Mobile::Entities::User do |f, u|
f[:user]
end
end
end
end

@ -0,0 +1,139 @@
module Mobile
module Entities
class BlogComment < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.blog_comment_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::BlogComment)
if u.respond_to?(f)
if f == :created_at
format_time( u.send(f))
else
u.send(f)
end
else
case f
when :praise_count
get_activity_praise_num(u)
when :lasted_comment
time_from_now(u.created_at)
when :act_type
'BlogComment'
when :act_id
u.id
when :comment_count
# u.children.count
all_comments = []
get_all_children(all_comments, u).count
end
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::BlogComment)
c.author
end
end
blog_comment_expose :act_type
blog_comment_expose :act_id
blog_comment_expose :blog_id
blog_comment_expose :title
blog_comment_expose :content
blog_comment_expose :comment_count
blog_comment_expose :created_at
blog_comment_expose :lasted_comment
blog_comment_expose :id
blog_comment_expose :locked
blog_comment_expose :praise_count
expose :all_children, using:Mobile::Entities::BlogComment do |c,opt|
if c.is_a? (::BlogComment)
if !opt[:children]
if c.parent.nil? && opt[:type] == 0
opt[:children] = true
all_comments = []
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = get_all_children(all_comments, c)[tStart..tEnd]
all_comments
end
end
end
end
expose :has_praise, if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, instance)
parents_reply.count
end
expose :parents_reply_bottom, using:Mobile::Entities::BlogComment do |c,opt|
if c.is_a? (::BlogComment)
#取二级回复的底楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
if parents_reply.count > 0 && !opt[:bottom]
if opt[:type] == 1
# opt[:bottom] = true
# parents_reply[opt[:page]..opt[:page]]
else
opt[:bottom] = true
parents_reply[0..0]
end
else
[]
end
end
end
expose :parents_reply_top, using:Mobile::Entities::BlogComment do |c,opt|
if c.is_a? (::BlogComment)
#取二级回复的顶楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
if parents_reply.count > 2 && !opt[:top]
if opt[:type] == 1
opt[:top] = true
tStart = (opt[:page]-1)*5+2
tEnd = (opt[:page])*5+2 - 1
if tEnd >= parents_reply.count - 1
tEnd = parents_reply.count - 2
end
if tStart <= parents_reply.count - 2
parents_reply = parents_reply.reverse[tStart..tEnd]
parents_reply.reverse
else
[]
end
else
opt[:top] = true
parents_reply = parents_reply.reverse[0..1]
parents_reply.reverse
end
elsif parents_reply.count == 2 && !opt[:top]
opt[:top] = true
parents_reply = parents_reply.reverse[0..0]
parents_reply.reverse
else
[]
end
end
end
end
end
end

@ -0,0 +1,30 @@
module Mobile
module Entities
class Comment < Grape::Entity
include Redmine::I18n
def self.comment_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::Comment)
if f.respond_to?(field)
if field == :created_on
time_from_now(f.send(field))
else
f.send(field)
end
end
end
end
end
comment_expose :id
expose :author, using: Mobile::Entities::User do |c, opt|
if c.is_a? ::Comment
c.author
end
end
comment_expose :comments
comment_expose :created_on
end
end
end

@ -0,0 +1,123 @@
module Mobile
module Entities
class Course < Grape::Entity
include Redmine::I18n
include ApplicationHelper
include ApiHelper
def self.course_expose(field)
expose field do |f,opt|
c = nil
if f.is_a? ::Course
c = f
else
c = f[:course]
end
if f.is_a?(Hash) && f.key?(field)
f[field] if f.is_a?(Hash) && f.key?(field)
#f.img_url if f.respond_to?(:img_url)
elsif field == :created_at || field == :updated_at
(format_time(c[field]) if (c.is_a?(Hash) && c.key?(field))) || (format_time(c.send(field)) if c.respond_to?(field))
elsif field == :member_count
::Course===c ? c.members.count : 0
else
(c[field] if (c.is_a?(Hash) && c.key?(field))) || (c.send(field) if c.respond_to?(field))
end
end
end
course_expose :img_url
course_expose :attachmenttype
course_expose :class_period
course_expose :code
course_expose :created_at
course_expose :description
course_expose :endup_time
course_expose :extra
course_expose :id
course_expose :inherit_members
course_expose :is_public
course_expose :lft
course_expose :location
course_expose :name
course_expose :open_student
# course_expose :password
course_expose :rgt
course_expose :school_id
course_expose :setup_time
course_expose :state
course_expose :status
course_expose :string
course_expose :tea_id
course_expose :term
course_expose :time
course_expose :invite_code
course_expose :qrcode
course_expose :updated_at
course_expose :course_student_num
course_expose :member_count
course_expose :groupnum
expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
can_setting = false
if instance[:course]
course = instance[:course]
else
course = instance
end
member = course.members.where("user_id=#{current_user.id} and course_id=#{course.id}")[0]
role_ids = member.role_ids if member
if role_ids && (role_ids.include?(7) || role_ids.include?(9) )
can_setting = true
end
if course.tea_id == current_user.id
can_setting = true
end
can_setting
end
expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
if instance[:course]
course = instance[:course]
else
course = instance
end
current_user.member_of_course?(course)
end
expose :teacher, using: Mobile::Entities::User do |c, opt|
if c.is_a? ::Course
c.teacher
else
c[:course].teacher
end
end
expose :my_homework,using: Mobile::Entities::Homework do |f, opt|
f[:my_homework] if f.is_a?(Hash) && f.key?(:my_homework)
end
expose :is_creator, if: lambda { |instance, options| options[:user] } do |instance, options|
if instance[:course]
course = instance[:course]
else
course = instance
end
current_user = options[:user]
course.tea_id == current_user.id
end
course_expose :current_user_is_member
course_expose :current_user_is_teacher
course_expose :work_unit
course_expose :syllabus_title
expose :boards, using: Mobile::Entities::Courses::Board do |f,opt|
if f.is_a? ::Course
f.boards
else
f[:course].boards
end
end
end
end
end

@ -0,0 +1,90 @@
module Mobile
module Entities
class CourseDynamic < Grape::Entity
include Redmine::I18n
def self.course_dynamic_expose(field)
expose field do |c,opt|
# if field == :news_count
# obj = nil
# c[:dynamics].each do |d|
# if d[:type] == 1
# obj = d[:count]
# end
# end
# obj
# elsif field == :document_count
# obj = nil
# c[:dynamics].each do |d|
# if d[:type] == 3
# obj = d[:count]
# end
# end
# obj
# elsif field == :topic_count
# obj = nil
# c[:dynamics].each do |d|
# if d[:type] == 2
# obj = d[:count]
# end
# end
# obj
# elsif field == :homework_count
# obj = nil
# c[:dynamics].each do |d|
# if d[:type] == 4
# obj = d[:count]
# end
# end
# obj
# else
c[field] if (c.is_a?(Hash) && c.key?(field))
# end
end
end
course_dynamic_expose :course_name
course_dynamic_expose :course_term
course_dynamic_expose :course_time
course_dynamic_expose :course_id
course_dynamic_expose :course_img_url
course_dynamic_expose :message
course_dynamic_expose :news_count
course_dynamic_expose :document_count
course_dynamic_expose :topic_count
course_dynamic_expose :homework_count
course_dynamic_expose :course_student_num
course_dynamic_expose :time_from_now
course_dynamic_expose :current_user_is_member
course_dynamic_expose :current_user_is_teacher
# expose :documents,using:Mobile::Entities::Attachment do |f,opt|
# obj = nil
# f[:dynamics].each do |d|
# if d[:type] == 3
# obj = d[:documents]
# end
# end
# obj
# end
expose :topics,using:Mobile::Entities::Message do |f,opt|
f[:topics]
end
expose :homeworks,using:Mobile::Entities::Homework do |f,opt|
f[:homeworks]
end
expose :news,using:Mobile::Entities::News do |f,opt|
f[:news]
end
expose :better_students,using:Mobile::Entities::User do |f,opt|
f[:better_students]
end
expose :active_students,using:Mobile::Entities::User do |f,opt|
f[:active_students]
end
end
end
end

@ -0,0 +1,19 @@
module Mobile
module Entities
class CourseGroup < Grape::Entity
include Redmine::I18n
include ApplicationHelper
include ApiHelper
def self.course_group_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
end
end
end
expose :id
expose :name
expose :users, using: Mobile::Entities::User
end
end
end

@ -0,0 +1,12 @@
# encoding: utf-8
module Mobile
module Entities
module Courses
class Board < Grape::Entity
expose :id
expose :name
expose :description
end
end
end
end

@ -0,0 +1,21 @@
module Mobile
module Entities
module Courses
class Comment < Grape::Entity
expose :created_on
expose :id
expose :author, using: Mobile::Entities::ShortUser do |f,u|
f.author
end
expose :to_user, using: Mobile::Entities::ShortUser do |f, u|
f.reply.try(:author)
end
expose :content
end
end
end
end

@ -0,0 +1,32 @@
module Mobile
module Entities
module Courses
class Message < Grape::Entity
def self.message_expose field
expose field do |f, opt|
f[:topic].try(field)
end
end
message_expose :id
message_expose :subject
message_expose :content
message_expose :created_on
expose :author, using: Mobile::Entities::User do |f, opt|
f[:topic].author
end
expose :comments, using: Mobile::Entities::Courses::Comment do |f, opt|
f[:comments]
end
expose :attachments, using: Mobile::Entities::Attachment do |f,opt|
f[:topic].attachments
end
end
end
end
end

@ -0,0 +1,43 @@
module Mobile
module Entities
class Exercise < Grape::Entity
include Redmine::I18n
include ApplicationHelper
include ApiHelper
def self.exercise_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
if field == :created_on
format_time(f[field])
else
f[field]
end
elsif f.is_a?(::Exercise)
if f.respond_to?(field)
f.send(field)
else
case field
when :coursename
f.course.nil? ? "" : f.course.name
when :syllabus_title
f.course.nil? ? "" : f.course.syllabus.nil? ? "" : f.course.syllabus.title
end
end
end
end
end
expose :exercise_name
expose :exercise_description
exercise_expose :coursename #所属班级名
exercise_expose :syllabus_title
expose :current_user_is_teacher, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
current_user_is_teacher = false
current_user_is_teacher = is_course_teacher(current_user,instance.course)
current_user_is_teacher
end
end
end
end

@ -0,0 +1,121 @@
# 这个模块由于作业模块的改变,里边的注释以及属性不可信
module Mobile
module Entities
class Homework < Grape::Entity
include Redmine::I18n
include ApplicationHelper
include ApiHelper
def self.homework_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
if field == :created_on
format_time(f[field])
else
f[field]
end
elsif f.is_a?(::HomeworkCommon)
if f.respond_to?(field)
f.send(field)
else
case field
when :homework_name
f.send(:name)
when :homework_notsubmit_num
f.course.members.count - f.student_works.count
when :homework_submit_num
f.student_works.count
when :homework_status_student
get_homework_status f
when :homework_times
f.course.homework_commons.index(f) + 1
when :homework_status_teacher
homework_status_desc f
when :student_evaluation_part
get_evaluation_part f ,3
when :ta_evaluation_part
get_evaluation_part f , 2
when :homework_anony_type
val = f.homework_type == 1 && !f.homework_detail_manual.nil?
val
when :coursename
f.course.nil? ? "" : f.course.name
when :syllabus_title
f.course.nil? ? "" : f.course.syllabus.nil? ? "" : f.course.syllabus.title
end
end
end
end
end
#作业id
homework_expose :id
#课程名称
homework_expose :course_name
homework_expose :syllabus_title
homework_expose :course_id
#作业发布者
expose :author,using: Mobile::Entities::User do |f, opt|
f[:author]
end
#作业发布者真名
homework_expose :author_real_name
#作业次数
homework_expose :homework_times
#作业名称
homework_expose :homework_name
#已提交的作业数量
homework_expose :homework_count
#学生提问数量
homework_expose :student_questions_count
#作业描述
homework_expose :description
#作业是否启用匿评功能 0不启用1启用
homework_expose :open_anonymous_evaluation
#作业状态 0:新建1已开启匿评2已关闭匿评
#只有作业启用了匿评功能且当前用户是课程老师且已提交的作品数量大于或等于2才能开启匿评
homework_expose :homework_state
homework_expose :created_on
homework_expose :deadline
expose :jours,using: Mobile::Entities::Jours do |f, opt|
f[:jours] if f.is_a?(Hash) && f.key?(:jours)
end
expose :homework_for_anonymous_comments,using: Mobile::Entities::HomeworkAttach do |f, opt|
f[:homework_for_anonymous_comments] if f.is_a?(Hash) && f.key?(:homework_for_anonymous_comments)
end
homework_expose :homework_submit_num
homework_expose :homework_notsubmit_num
homework_expose :homework_status_student #学生看到的作业的状态
homework_expose :homework_status_teacher #老师看到的状态
homework_expose :student_evaluation_part #学生匿评比率
homework_expose :ta_evaluation_part #教辅评价比率
homework_expose :homework_anony_type #是否是匿评作业
homework_expose :coursename #所属班级名
expose :current_user_is_teacher, if: lambda { |instance, options| options[:user] } do |instance, options|
if instance[:current_user_is_teacher].nil?
current_user = options[:user]
current_user_is_teacher = false
current_user_is_teacher = is_course_teacher(current_user,instance.course)
current_user_is_teacher
end
end
##学生提交作业的状态 0 未提交 1 已提交
homework_expose :status
homework_expose :homework_type
end
end
end

@ -0,0 +1,42 @@
module Mobile
module Entities
class HomeworkAttach < Grape::Entity
include Redmine::I18n
def self.homework_attach_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::HomeworkAttach)
if f.respond_to?(field)
if field == :created_at
format_time(f.send(:created_at))
else
f.send(field)
end
else
case field
when :homework_times
f.bid.courses.first.homeworks.index(f.bid) + 1 unless (f.bid.nil? || f.bid.courses.nil? || f.bid.courses.first.nil?)
when :comment_status
f.bid.comment_status
end
end
end
end
end
homework_attach_expose :id
homework_attach_expose :name
homework_attach_expose :homework_times
homework_attach_expose :description
homework_attach_expose :created_at
#comment_status 0:所属作业尚未开启匿评1匿评中 2匿评结束
homework_attach_expose :comment_status
expose :attachments,using: Mobile::Entities::Attachment do |f, opt|
if f.respond_to?(:attachments)
f.send(:attachments)
end
end
end
end
end

@ -0,0 +1,29 @@
module Mobile
module Entities
#带评分的留言(教师评论、学生匿名评分都属于此类)
class HomeworkJours < Grape::Entity
include Redmine::I18n
def self.homework_jours_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
if field == :created_at
format_time(f[field])
else
f[field]
end
elsif f.is_a?(::SeemsRateableRates)
end
end
end
homework_jours_expose :rater_id
homework_jours_expose :rater_name
homework_jours_expose :created_at
homework_jours_expose :stars
expose :comment,using: Mobile::Entities::Jours do |f,opt|
f[:comment]
end
end
end
end

@ -0,0 +1,18 @@
module Mobile
module Entities
class Homeworkscore < Grape::Entity
include Redmine::I18n
include ApplicationHelper
def self.homeworkscore_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
end
end
end
homeworkscore_expose :name
homeworkscore_expose :score
end
end
end

@ -0,0 +1,187 @@
#coding=utf-8
module Mobile
module Entities
class Issue <Grape::Entity
include ApiHelper
include Redmine::I18n
def self.issue_expose(f)
expose f do |issue, opt|
if issue.is_a?(Hash) && issue.key?(f)
issue[f]
elsif issue.is_a?(::Issue)
if issue.respond_to?(f)
if f == :created_on
format_time(issue.send(f))
else
issue.send(f)
end
else
case f
when :issue_priority
get_issue_priority_api issue.priority_id
when :issue_assigned_to
issue.assigned_to_id.nil? ? "未指派" : (get_user(issue.assigned_to_id)).login
when :issue_status
IssueStatus.find(issue.status_id).name
when :comment_count
issue.journals.where("notes is not null and notes != ''").count
# all_comments = []
# get_all_children(all_comments, f).count
when :project_name
issue.project.name
when :praise_count
get_activity_praise_num(issue)
when :act_type
'Issue'
when :act_id
issue.id
when :title
issue.subject
when :subject
issue.subject
when :description
issue.description
when :done_ratio
issue.done_ratio
end
end
elsif issue.is_a?(::Journal)
case f
when :content
issue[:notes]
when :lasted_comment
time_from_now issue.created_on
when :act_id
issue.id
when :act_type
'Journal'
when :praise_count
get_activity_praise_num(issue)
end
end
end
end
issue_expose :subject
issue_expose :description
expose :author, using: Mobile::Entities::User do |f, opt|
if f.is_a?(::Issue)
f.send(:author)
end
end
expose :user,using: Mobile::Entities::User do |f, opt|
if f.is_a?(::Journal)
f.send(:user)
end
end
issue_expose :content
issue_expose :lasted_comment
issue_expose :done_ratio
issue_expose :title
issue_expose :act_type
issue_expose :act_id
issue_expose :created_on
issue_expose :issue_priority
issue_expose :issue_assigned_to
issue_expose :issue_status
issue_expose :comment_count
issue_expose :project_name
issue_expose :praise_count
expose :id
# expose :issue_journals, using: Mobile::Entities::Journal do |f, opt|
# if f.is_a?(::Issue)
# f.journals.where("notes is not null and notes != ''").reverse
# end
# end
expose :all_children, using: Mobile::Entities::Issue do |f, opt|
#f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
if f.is_a?(::Issue)
# f.journals_for_messages.reverse
if !opt[:children] && opt[:type] == 0
opt[:children] = true
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = f.journals.where("notes is not null and notes != ''").reorder("created_on desc")
all_comments[tStart..tEnd]
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
if instance.is_a?(::Journal)
parents_reply = []
parents_reply = get_reply_parents(parents_reply, instance)
parents_reply.count
end
end
expose :parents_reply_bottom, using:Mobile::Entities::Issue do |f,opt|
if f.is_a? (::Journal)
#取二级回复的底楼层
parents_reply = []
parents_reply = get_reply_parents(parents_reply, f)
if parents_reply.count > 0 && !opt[:bottom]
if opt[:type] == 1
# opt[:bottom] = true
# parents_reply[opt[:page]..opt[:page]]
else
opt[:bottom] = true
parents_reply[0..0]
end
else
[]
end
end
end
expose :parents_reply_top, using:Mobile::Entities::Issue do |f,opt|
if f.is_a? (::Journal)
#取二级回复的顶楼层
parents_reply = []
parents_reply = get_reply_parents(parents_reply, f)
if parents_reply.count > 2 && !opt[:top]
if opt[:type] == 1
opt[:top] = true
tStart = (opt[:page]-1)*5+2
tEnd = (opt[:page])*5+2 - 1
if tEnd >= parents_reply.count - 1
tEnd = parents_reply.count - 2
end
if tStart <= parents_reply.count - 2
parents_reply = parents_reply.reverse[tStart..tEnd]
parents_reply.reverse
else
[]
end
else
opt[:top] = true
parents_reply = parents_reply.reverse[0..1]
parents_reply.reverse
end
elsif parents_reply.count == 2 && !opt[:top]
opt[:top] = true
parents_reply = parents_reply.reverse[0..0]
parents_reply.reverse
else
[]
end
end
end
expose :attachments, using: Mobile::Entities::Attachment
end
end
end

@ -0,0 +1,44 @@
module Mobile
module Entities
class Journal <Grape::Entity
include Redmine::I18n
=begin
include Redmine::I18n
include ApplicationHelper
include ApiHelper
include IssuesHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
=end
def self.journal_expose(f)
expose f do |journal, opt|
if journal.is_a?(Hash) && journal.key?(f)
journal[f]
elsif journal.is_a?(::Journal)
if journal.respond_to?(f)
if f == :created_on
time_from_now(journal.send(f))
else
journal.send(f)
end
else
case f
when :detail_journal
if journal.details.any?
details_to_strings(journal.details)
end
end
end
end
end
end
expose :notes
journal_expose :created_on
expose :user,using: Mobile::Entities::User do |f, opt|
f.user
end
#journal_expose :detail_journal
end
end
end

@ -0,0 +1,161 @@
module Mobile
module Entities
#普通留言
class Jours < Grape::Entity
include Redmine::I18n
def self.jours_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::JournalsForMessage) && f.respond_to?(field)
if field == :created_on
format_time(f.send(field))
else
f.send(field)
end
else
case field
when :lasted_comment
time_from_now f.created_on
when :comment_count
# f.children.count
all_comments = []
get_all_children(all_comments, f).count
when :praise_count
get_activity_praise_num(f)
when :act_type
'JournalsForMessage'
when :act_id
f.id
when :content
f.notes
end
end
end
end
jours_expose :act_type
jours_expose :act_id
jours_expose :id
jours_expose :jour_type
jours_expose :jour_id
expose :user,using: Mobile::Entities::User do |f, opt|
f.user
end
jours_expose :created_on
jours_expose :lasted_comment
jours_expose :content
jours_expose :m_reply_id
jours_expose :m_parent_id
jours_expose :comment_count
jours_expose :praise_count
expose :course,using:Mobile::Entities::Course do |f,opt|
if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course"
f.course
end
end
expose :reply_user,using: Mobile::Entities::User do |f, opt|
f.at_user
end
expose :all_children,using: Mobile::Entities::Jours do |f, opt|
if f.is_a?(::JournalsForMessage)
# f.children.reverse
if !opt[:children] && opt[:comment_type].nil?
if f.parent.nil? && opt[:type] == 0
opt[:children] = true
all_comments = []
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = get_all_children(all_comments, f)[tStart..tEnd]
all_comments
end
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
parents_reply = []
if options[:comment_type].nil?
parents_reply = get_reply_parents_no_root(parents_reply, instance)
elsif options[:comment_type] == "homework"
parents_reply = get_reply_parents(parents_reply, instance)
end
parents_reply.count
end
expose :parents_reply_bottom, using:Mobile::Entities::Jours do |f,opt|
if f.is_a? (::JournalsForMessage)
#取二级回复的底楼层
parents_reply = []
if opt[:comment_type].nil?
parents_reply = get_reply_parents_no_root(parents_reply, f)
elsif opt[:comment_type] == "homework"
parents_reply = get_reply_parents(parents_reply, f)
end
if parents_reply.count > 0 && !opt[:bottom]
if opt[:type] == 1
# opt[:bottom] = true
# parents_reply[opt[:page]..opt[:page]]
else
opt[:bottom] = true
parents_reply[0..0]
end
else
[]
end
end
end
expose :parents_reply_top, using:Mobile::Entities::Jours do |f,opt|
if f.is_a? (::JournalsForMessage)
#取二级回复的顶楼层
parents_reply = []
if opt[:comment_type].nil?
parents_reply = get_reply_parents_no_root(parents_reply, f)
elsif opt[:comment_type] == "homework"
parents_reply = get_reply_parents(parents_reply, f)
end
if parents_reply.count > 2 && !opt[:top]
if opt[:type] == 1
opt[:top] = true
tStart = (opt[:page]-1)*5+2
tEnd = (opt[:page])*5+2 - 1
if tEnd >= parents_reply.count - 1
tEnd = parents_reply.count - 2
end
if tStart <= parents_reply.count - 2
parents_reply = parents_reply.reverse[tStart..tEnd]
parents_reply.reverse
else
[]
end
else
opt[:top] = true
parents_reply = parents_reply.reverse[0..1]
parents_reply.reverse
end
elsif parents_reply.count == 2 && !opt[:top]
opt[:top] = true
parents_reply = parents_reply.reverse[0..0]
parents_reply.reverse
else
[]
end
end
end
end
end
end

@ -0,0 +1,34 @@
module Mobile
module Entities
class Member < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.member_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::Member)
if u.respond_to?(f)
u.send(f)
else
case f
when :student_id
u.user.user_extensions.student_id
end
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::Member)
c.user
end
end
member_expose :student_id
member_expose :score
member_expose :id
end
end
end

@ -0,0 +1,154 @@
module Mobile
module Entities
class Message < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.message_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::Message)
if u.respond_to?(f)
if f == :created_on
format_time( u.send(f))
else
u.send(f)
end
else
case f
when :course_project_name
if u.board.project_id == -1
u.course.name
else
u.project.name
end
when :syllabus_title
if u.board.project_id == -1
u.course.syllabus.nil? ? "" : u.course.syllabus.title
end
when :lasted_comment
time_from_now u.created_on
when :praise_count
get_activity_praise_num(u)
when :act_type
'Message'
when :act_id
u.id
when :comment_count
all_comments = []
get_all_children(all_comments, u).count
when :title
u.subject
end
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::Message)
c.author
end
end
message_expose :act_type
message_expose :act_id
message_expose :course_project_name
message_expose :syllabus_title
message_expose :board_id
message_expose :subject
message_expose :title
message_expose :content
message_expose :comment_count
message_expose :praise_count
message_expose :created_on
message_expose :locked
message_expose :id
message_expose :lasted_comment
expose :all_children,using:Mobile::Entities::Message do |c,opt|
if c.is_a? (::Message)
# c.children.reverse
if !opt[:children]
if c.parent.nil? && opt[:type] == 0
opt[:children] = true
all_comments = []
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = get_all_children(all_comments, c)[tStart..tEnd]
all_comments
end
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, instance)
parents_reply.count
end
expose :parents_reply_bottom, using:Mobile::Entities::Message do |c,opt|
if c.is_a? (::Message)
#取二级回复的底楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
if parents_reply.count > 0 && !opt[:bottom]
if opt[:type] == 1
# opt[:bottom] = true
# parents_reply[opt[:page]..opt[:page]]
else
opt[:bottom] = true
parents_reply[0..0]
end
else
[]
end
end
end
expose :parents_reply_top, using:Mobile::Entities::Message do |c,opt|
if c.is_a? (::Message)
#取二级回复的顶楼层
parents_reply = []
parents_reply = get_reply_parents_no_root(parents_reply, c)
if parents_reply.count > 2 && !opt[:top]
if opt[:type] == 1
opt[:top] = true
tStart = (opt[:page]-1)*5+2
tEnd = (opt[:page])*5+2 - 1
if tEnd >= parents_reply.count - 1
tEnd = parents_reply.count - 2
end
if tStart <= parents_reply.count - 2
parents_reply = parents_reply.reverse[tStart..tEnd]
parents_reply.reverse
else
[]
end
else
opt[:top] = true
parents_reply = parents_reply.reverse[0..1]
parents_reply.reverse
end
elsif parents_reply.count == 2 && !opt[:top]
opt[:top] = true
parents_reply = parents_reply.reverse[0..0]
parents_reply.reverse
else
[]
end
end
end
end
end
end

@ -0,0 +1,198 @@
module Mobile
module Entities
class News < Grape::Entity
include Redmine::I18n
include ApiHelper
def self.news_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::News)
if f.respond_to?(field)
if field == :created_on
format_time(f.send(field))
else
f.send(field)
end
else
case field
when :course_name
get_course(f.course_id).name unless f.course_id == nil
when :praise_count
get_activity_praise_num(f)
when :act_type
'News'
when :act_id
f.id
when :comment_count
f.comments.count
when :syllabus_title
unless f.course_id == nil
course = get_course(f.course_id)
course.syllabus.nil? ? "" : course.syllabus.title
end
end
end
elsif f.is_a?(::Comment)
case field
when :content
f[:comments]
when :lasted_comment
time_from_now f.created_on
when :act_id
f.id
when :praise_count
get_activity_praise_num(f)
when :act_type
'Comment'
end
elsif f.is_a?(Hash) && !f.key?(field)
n = f[:news]
comments = f[:comments]
if n.is_a?(::News)
if field == :created_on
format_time(n.send(field)) if n.respond_to?(field)
else
n.send(field) if n.respond_to?(field)
end
end
end
end
end
expose :id
#新闻标题
news_expose :title
expose :user,using: Mobile::Entities::User do |f, opt|
obj = nil
if f.is_a?(::News) && f.respond_to?(:author)
obj = f.send(:author)
elsif f.is_a?(::Comment) && f.respond_to?(:author)
obj = f.send(:author)
elsif f.is_a?(Hash) && f.key?(:author)
obj = f[:author]
end
obj
end
news_expose :act_type
news_expose :act_id
#作者id
news_expose :author_id
#作者名
news_expose :author_name
#作者头像url
news_expose :author_img_url
#新闻内容
news_expose :description
#发布时间
news_expose :created_on
#评论数量
news_expose :comment_count
news_expose :praise_count
#课程名字
news_expose :course_name
news_expose :syllabus_title
news_expose :lasted_comment
#评论
# expose :comments, using: Mobile::Entities::Comment do |f, opt|
# if f.is_a?(Hash) && f.key?(:comments)
# f[:comments]
# elsif f.is_a?(::News) && f.respond_to?(:comments)
# f.comments.reverse
# end
# end
news_expose :content
expose :all_children, using: Mobile::Entities::News do |f, opt|
#f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
if f.is_a?(::News)
# f.journals_for_messages.reverse
if !opt[:children] && opt[:type] == 0
opt[:children] = true
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = f.comments.reorder("created_on desc")
all_comments[tStart..tEnd]
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
expose :parents_count, if: lambda { |instance, options| options[:user] } do |instance, options|
if instance.is_a?(::Comment)
parents_reply = []
parents_reply = get_reply_parents(parents_reply, instance)
parents_reply.count
end
end
expose :parents_reply_bottom, using:Mobile::Entities::News do |f,opt|
if f.is_a? (::Comment)
#取二级回复的底楼层
parents_reply = []
parents_reply = get_reply_parents(parents_reply, f)
if parents_reply.count > 0 && !opt[:bottom]
if opt[:type] == 1
# opt[:bottom] = true
# parents_reply[opt[:page]..opt[:page]]
else
opt[:bottom] = true
parents_reply[0..0]
end
else
[]
end
end
end
expose :parents_reply_top, using:Mobile::Entities::News do |f,opt|
if f.is_a? (::Comment)
#取二级回复的顶楼层
parents_reply = []
parents_reply = get_reply_parents(parents_reply, f)
if parents_reply.count > 2 && !opt[:top]
if opt[:type] == 1
opt[:top] = true
tStart = (opt[:page]-1)*5+2
tEnd = (opt[:page])*5+2 - 1
if tEnd >= parents_reply.count - 1
tEnd = parents_reply.count - 2
end
if tStart <= parents_reply.count - 2
parents_reply = parents_reply.reverse[tStart..tEnd]
parents_reply.reverse
else
[]
end
else
opt[:top] = true
parents_reply = parents_reply.reverse[0..1]
parents_reply.reverse
end
elsif parents_reply.count == 2 && !opt[:top]
opt[:top] = true
parents_reply = parents_reply.reverse[0..0]
parents_reply.reverse
else
[]
end
end
end
end
end
end

@ -0,0 +1,43 @@
module Mobile
module Entities
class Project < Grape::Entity
expose :name
expose :id
expose :is_public
expose :user_id
expose :invite_code
expose :qrcode
expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
my_member = instance.member_principals.where("users.id=#{current_user.id}").first
can_setting = false
if my_member && my_member.roles[0] && my_member.roles[0].id == 3
can_setting = true
end
can_setting
end
expose :is_creator, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
current_user.id == instance.user_id
end
expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
if instance[:project]
project = instance[:project]
else
project = instance
end
current_user.member_of?(project)
end
expose :member_count, if: lambda { |instance, options| options[:user] } do |instance, options|
instance.members.count
end
end
end
end

@ -0,0 +1,35 @@
module Mobile
module Entities
class ProjectMember < Grape::Entity
include Redmine::I18n
include ApplicationHelper
include ApiHelper
def self.member_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::Member)
if u.respond_to?(f)
u.send(f)
else
case f
when :roles_id
# u.roles[0].id
u.roles.map {|r| r.id}
end
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::Member)
c.user
end
end
member_expose :roles_id
end
end
end

@ -0,0 +1,9 @@
module Mobile
module Entities
class Response < Grape::Entity
expose :status
expose :message
expose :data
end
end
end

@ -0,0 +1,18 @@
module Mobile
module Entities
class ShortUser < Grape::Entity
include ApplicationHelper
expose :id
expose :login
expose :name do |f, u|
f.try(:show_real_name)
end
expose :img_url do |f, u|
"/images/#{url_to_avatar(f)}"
end
end
end
end

@ -0,0 +1,45 @@
module Mobile
module Entities
class StudentWork < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.student_work_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::StudentWork)
if u.respond_to?(f)
if f == :created_at
format_time(u.send(:created_at))
else
u.send(f)
end
else
case f
when :student_id
u.user.user_extensions.student_id
end
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::StudentWork)
c.user
end
end
student_work_expose :student_id
student_work_expose :id
student_work_expose :name
student_work_expose :description
student_work_expose :final_score
student_work_expose :teacher_score
student_work_expose :student_score
student_work_expose :teacher_asistant_score
student_work_expose :created_at
end
end
end

@ -0,0 +1,15 @@
module Mobile
module Entities
class Syllabus < Grape::Entity
expose :title
expose :id
expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
can_setting = instance[:user_id] == current_user.id ? true : false
can_setting = false if instance[:id].nil?
can_setting
end
expose :courses, using: Mobile::Entities::Course
end
end
end

@ -0,0 +1,95 @@
module Mobile
module Entities
class User < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.user_expose(f)
expose f do |u, opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::User)
if u.respond_to?(f)
u.send(f) unless u.user_extensions.nil?
else
case f
when :img_url
"/images/#{url_to_avatar(u)}"
when :gender
u.nil? || u.user_extensions.nil? || u.user_extensions.gender.nil? ? 0 : u.user_extensions.gender
when :work_unit
get_user_work_unit u unless u.user_extensions.nil?
when :location
get_user_location u unless u.user_extensions.nil?
when :brief_introduction
u.nil? || u.user_extensions.nil? ? "" : u.user_extensions.brief_introduction
when :student_num
u.nil? || u.user_extensions.nil? ? "" : u.user_extensions.student_id
when :real_name
u.nil? ? "" : u.show_real_name;
when :name
u.nil? ? "" : u.show_name
when :roles_id
u[:roles_id].nil? ? nil : u.roles_id
when :login
u.login
when :mail
u.mail
when :is_me
defined? u.is_me ? u.is_me : 0
when :is_teacher
u.is_teacher
when :fans_count
u.watcher_users.count
when :watched_count
::User.watched_by(u.id).count
end
end
end
end
end
expose :id
#头像
user_expose :login
#邮箱地址
user_expose :mail
#真名
user_expose :img_url
#昵称
user_expose :real_name
#性别
user_expose :gender
#我的二维码
#工作单位
user_expose :work_unit
#地区
user_expose :location
#签名
user_expose :brief_introduction
#总成绩
user_expose :score
#学号
user_expose :student_num
# 活跃值
user_expose :active_count
user_expose :is_me
user_expose :is_teacher
# 金币
user_expose :grade
# 经验
user_expose :experience
# 粉丝
user_expose :fans_count
# 关注
user_expose :watched_count
end
end
end

@ -0,0 +1,33 @@
# encoding: utf-8
module Mobile
module Entities
module Users
class Course < Grape::Entity
def self.course_expose(field)
expose field do |f, u|
if field == :teacher_name
::User.find(f[:course].try(:tea_id)).show_name
elsif field==:student_num
f[:course].try(:student).try(:count)
else
f[:course].try(field)
end
end
end
course_expose :id
course_expose :name
course_expose :teacher_name
expose :is_teacher
course_expose :invite_code
course_expose :student_num
expose :boards, using: Mobile::Entities::Courses::Board do |f, opt|
f[:course].boards
end
end
end
end
end

@ -0,0 +1,24 @@
# encoding: utf-8
module Mobile
module Entities
module Users
class Tiding < Grape::Entity
expose :user, using: ShortUser
expose :title
expose :content do |f,u|
f.key_word
end
expose :created_at
expose :container_id
expose :container_type
expose :parent_container_id
expose :parent_container_type
end
end
end
end

@ -0,0 +1,119 @@
# encoding: utf-8
module Mobile
module Entities
class Whomework <Grape::Entity
include ApiHelper
include ApplicationHelper
include Redmine::I18n
def self.whomework_expose(f)
expose f do |wh, opt|
if wh.is_a?(Hash) && wh.key?(f)
wh[f]
elsif wh.is_a?(::HomeworkCommon)
if wh.respond_to?(f)
if f == :created_at
format_time(wh.send(f))
else
wh.send(f)
end
else
case f
when :absence_penalty
wh.nil? || wh.homework_detail_manual.nil? ? 0 : wh.homework_detail_manual.absence_penalty
when :evaluation_start
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_start, 0)
when :evaluation_end
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_end, 1)
when :praise_count
get_activity_praise_num(wh)
when :comment_count
wh.journals_for_messages.count
when :course_name
wh.course.name
when :syllabus_title
wh.course.syllabus.nil? ? "" : wh.course.syllabus.title
when :act_type
'HomeworkCommon'
when :act_id
wh.id
when :title
wh.name
when :homework_commit_count
wh.student_works.has_committed.count
when :last_commit_info
if wh.student_works.has_committed.count > 0
lc = wh.student_works.has_committed.reorder("commit_time desc").first
{:lasttime=>time_from_now(lc.commit_time), :lastname=>lc.user.show_name}
end
when :last_score_info
if wh.student_works.has_committed.count > 0
sw_id = "("+wh.student_works.map{|sw| sw.id}.join(",")+")"
student_work_scores = StudentWorksScore.where("student_work_id in #{sw_id} and score is not null").reorder("created_at desc")
unless student_work_scores.empty?
last_score = student_work_scores.first
{:lasttime=>time_from_now(last_score.created_at), :lastname=>last_score.user.show_name}
end
end
end
end
end
end
end
expose :author, using: Mobile::Entities::User do |w, opt|
if w.is_a?(::HomeworkCommon)
w.user
end
end
expose :current_user, using: Mobile::Entities::User do |w, opt|
current_user
end
expose :name
expose :description
expose :publish_time
expose :end_time
expose :homework_type
expose :late_penalty
expose :course_id
expose :anonymous_comment
expose :quotes
expose :is_open
expose :id
whomework_expose :title
whomework_expose :act_type
whomework_expose :act_id
whomework_expose :course_name
whomework_expose :syllabus_title
whomework_expose :created_at
whomework_expose :absence_penalty
whomework_expose :evaluation_start
whomework_expose :evaluation_end
whomework_expose :praise_count
whomework_expose :comment_count
whomework_expose :homework_commit_count #作品提交数
whomework_expose :last_commit_info #最近提交信息
whomework_expose :last_score_info #最近评阅信息
expose :all_children, using: Mobile::Entities::Jours do |f, opt|
#f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
if f.is_a?(::HomeworkCommon)
# f.journals_for_messages.reverse
if !opt[:children] && opt[:type] == 0
opt[:children] = true
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = f.journals_for_messages.reorder("created_on desc")
all_comments[tStart..tEnd]
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
end
end
end

@ -0,0 +1,13 @@
#coding=utf-8
#
module Mobile
module Exceptions
class AuthException < StandardError
attr_reader :err_code, :msg
def initialize(code, msg)
@err_code = code
@msg = msg
end
end
end
end

@ -0,0 +1,26 @@
#coding=utf-8
module Mobile
module Middleware
class ErrorHandler < Grape::Middleware::Base
def call!(env)
@env = env
begin
@app.call(@env)
rescue =>e
code = -1
message = {status: code, message: e.message }.to_json
Rails.logger.error e.inspect
Rails.logger.error e.backtrace.join("\n")
status = 200
headers = { 'Content-Type' => content_type }
Rack::Response.new([message], status, headers).finish
# throw :error, :message => e.message || options[:default_message], :status => 500
end
end
end
end
end

@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save