2016-06-12 17:25:47 +08:00
|
|
|
#coding=utf-8
|
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
module Mobile
|
2016-07-20 10:09:27 +08:00
|
|
|
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'
|
2016-07-25 15:20:01 +08:00
|
|
|
require_relative 'apis/projects'
|
2016-03-21 18:17:39 +08:00
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
class API < Grape::API
|
|
|
|
version 'v1', using: :path
|
|
|
|
format :json
|
|
|
|
content_type :json, "application/json;charset=UTF-8"
|
2016-06-22 12:11:34 +08:00
|
|
|
use ActionDispatch::Session::CookieStore
|
2016-07-18 16:34:50 +08:00
|
|
|
use Middleware::ErrorHandler
|
2015-04-07 13:47:39 +08:00
|
|
|
|
|
|
|
helpers do
|
|
|
|
def logger
|
|
|
|
API.logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate!
|
2016-06-12 17:18:05 +08:00
|
|
|
raise('Unauthorized. 用户认证失败.') unless current_user
|
2015-04-07 13:47:39 +08:00
|
|
|
end
|
|
|
|
|
2016-06-22 12:11:34 +08:00
|
|
|
def session
|
|
|
|
env['rack.session']
|
|
|
|
end
|
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
def current_user
|
2016-06-12 17:18:05 +08:00
|
|
|
openid = params[:openid]
|
|
|
|
if openid
|
|
|
|
uw = UserWechat.find_by_openid(params[:openid])
|
|
|
|
return uw.user if uw
|
|
|
|
end
|
|
|
|
|
2015-04-07 13:47:39 +08:00
|
|
|
token = ApiKey.where(access_token: params[:token]).first
|
|
|
|
if token && !token.expired?
|
2016-06-12 17:18:05 +08:00
|
|
|
return User.find(token.user_id)
|
2015-04-07 13:47:39 +08:00
|
|
|
end
|
2016-06-12 17:18:05 +08:00
|
|
|
|
|
|
|
nil
|
2015-04-07 13:47:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mount Apis::Auth
|
|
|
|
mount Apis::Users
|
|
|
|
mount Apis::Courses
|
|
|
|
mount Apis::Watches
|
|
|
|
mount Apis::Upgrade
|
|
|
|
mount Apis::Homeworks
|
|
|
|
mount Apis::Comments
|
2016-03-21 18:17:39 +08:00
|
|
|
mount Apis::Issues
|
2016-03-30 10:32:45 +08:00
|
|
|
mount Apis::Activities
|
|
|
|
mount Apis::Whomeworks
|
2016-03-30 15:51:27 +08:00
|
|
|
mount Apis::Newss
|
|
|
|
mount Apis::JournalForMessages
|
|
|
|
mount Apis::Messages
|
|
|
|
mount Apis::BlogComments
|
2016-04-01 18:23:12 +08:00
|
|
|
mount Apis::NewComment
|
2016-04-07 15:42:08 +08:00
|
|
|
mount Apis::Praise
|
2016-06-27 17:24:06 +08:00
|
|
|
mount Apis::Resources
|
2016-07-04 16:19:05 +08:00
|
|
|
mount Apis::Syllabuses
|
2016-07-25 15:20:01 +08:00
|
|
|
mount Apis::Projects
|
2015-04-07 13:47:39 +08:00
|
|
|
|
2016-06-15 13:44:28 +08:00
|
|
|
add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development?
|
2015-04-07 13:47:39 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|