Merge branch 'szzh' of http://xianbo_trustie2@repository.trustie.net/xianbo/trustie2.git into szzh
commit
36dfd7c77c
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,41 @@
|
||||
module Mobile
|
||||
|
||||
class API < Grape::API
|
||||
version 'v1', using: :path
|
||||
format :json
|
||||
content_type :json, "application/json;charset=UTF-8"
|
||||
use Mobile::Middleware::ErrorHandler
|
||||
|
||||
helpers do
|
||||
def logger
|
||||
API.logger
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
raise('Unauthorized. Invalid or expired token.') unless current_user
|
||||
end
|
||||
|
||||
def current_user
|
||||
token = ApiKey.where(access_token: params[:token]).first
|
||||
if token && !token.expired?
|
||||
@current_user = User.find(token.user_id)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
mount Apis::Auth
|
||||
mount Apis::Users
|
||||
mount Apis::Courses
|
||||
mount Apis::Watches
|
||||
mount Apis::Upgrade
|
||||
mount Apis::Homeworks
|
||||
|
||||
#add_swagger_documentation ({api_version: 'v1', base_path: 'http://u06.shellinfo.cn/trustie/api'})
|
||||
add_swagger_documentation ({api_version: 'v1', base_path: '/api'})
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,48 @@
|
||||
#coding=utf-8
|
||||
|
||||
module Mobile
|
||||
|
||||
module Entities
|
||||
class Auth < Grape::Entity
|
||||
expose :token
|
||||
expose :user, using: User
|
||||
end
|
||||
end
|
||||
|
||||
module Apis
|
||||
class Auth < Grape::API
|
||||
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}, using: Entities::Auth
|
||||
present :status, 0
|
||||
else
|
||||
raise 'Unauthorized.'
|
||||
end
|
||||
end
|
||||
|
||||
desc "用户登出"
|
||||
params do
|
||||
requires :token, type: String
|
||||
end
|
||||
delete do
|
||||
authenticate!
|
||||
::ApiKey.delete_all(user_id: current_user.id)
|
||||
{status: 0}
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -0,0 +1,96 @@
|
||||
#coding=utf-8
|
||||
module Mobile
|
||||
module Apis
|
||||
class Users < Grape::API
|
||||
resource :users do
|
||||
|
||||
desc "注册用户"
|
||||
params do
|
||||
requires :login, type: String, desc: 'username'
|
||||
requires :mail, type: String, desc: 'mail'
|
||||
requires :password, type: String, desc: 'password'
|
||||
end
|
||||
post do
|
||||
us = UsersService.new
|
||||
user = us.register params.merge(:password_confirmation => params[:password],
|
||||
:should_confirmation_password => true)
|
||||
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
||||
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "显示用户"
|
||||
params do
|
||||
|
||||
end
|
||||
get ':id' do
|
||||
us = UsersService.new
|
||||
ue = us.show_user params
|
||||
present :data, ue,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
|
||||
optional :token, type: String
|
||||
end
|
||||
|
||||
get ':id/courses' do
|
||||
us = UsersService.new
|
||||
ue = us.user_courses_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
|
||||
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: '用户名关键字'
|
||||
end
|
||||
get 'search' do
|
||||
us = UsersService.new
|
||||
user = us.search_user params
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
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,35 @@
|
||||
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
|
||||
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,23 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class Attachment < Grape::Entity
|
||||
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)
|
||||
f.send(field)
|
||||
else
|
||||
#case field
|
||||
# when ""
|
||||
#end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
attachment_expose :filename
|
||||
attachment_expose :description
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,58 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class Course < Grape::Entity
|
||||
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 field == :img_url
|
||||
f[field] if f.is_a?(Hash) && f.key?(field)
|
||||
#f.img_url if f.respond_to?(:img_url)
|
||||
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 :updated_at
|
||||
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::HomeworkAttach do |f, opt|
|
||||
f[:my_homework] if f.is_a?(Hash) && f.key?(:my_homework)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,39 @@
|
||||
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?)
|
||||
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
|
||||
expose :attachments,using: Mobile::Entities::Attachment do |f, opt|
|
||||
if f.respond_to?(:attachments)
|
||||
f.send(:attachments)
|
||||
end
|
||||
end
|
||||
#homework_attach_expose :user
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,25 @@
|
||||
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)
|
||||
f[field]
|
||||
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,37 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
#普通留言
|
||||
class Jours < Grape::Entity
|
||||
include Redmine::I18n
|
||||
include WordsHelper
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
jours_expose :id
|
||||
expose :user,using: Mobile::Entities::User do |f, opt|
|
||||
f.user
|
||||
end
|
||||
jours_expose :created_on
|
||||
jours_expose :notes
|
||||
jours_expose :m_reply_id
|
||||
expose :reply_user,using: Mobile::Entities::User do |f, opt|
|
||||
f.at_user
|
||||
end
|
||||
expose :child_reply,using: Mobile::Entities::Jours do |f, opt|
|
||||
if f.is_a?(::JournalsForMessage)
|
||||
fetch_user_leaveWord_reply(f)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,42 @@
|
||||
module Mobile
|
||||
module Entities
|
||||
class News < Grape::Entity
|
||||
def self.news_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)
|
||||
n = f[:news]
|
||||
comments = f[:comments]
|
||||
if n.is_a?(::News)
|
||||
n.send(field) if n.respond_to?(field)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#新闻标题
|
||||
news_expose :title
|
||||
|
||||
expose :author,using: Mobile::Entities::User do |f, opt|
|
||||
n = f[:news]
|
||||
n.author if n.respond_to?(:author)
|
||||
end
|
||||
#作者id
|
||||
news_expose :author_id
|
||||
#作者名
|
||||
news_expose :author_name
|
||||
#新闻内容
|
||||
news_expose :description
|
||||
#发布时间
|
||||
news_expose :created_on
|
||||
#评论数量
|
||||
news_expose :comments_count
|
||||
#评论
|
||||
news_expose :comments
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,51 @@
|
||||
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)
|
||||
else
|
||||
case f
|
||||
when :img_url
|
||||
url_to_avatar(u)
|
||||
when :gender
|
||||
u.user_extensions.gender.nil? ? 0 : u.user_extensions.gender
|
||||
when :work_unit
|
||||
get_user_work_unit u
|
||||
when :location
|
||||
get_user_location u
|
||||
when :brief_introduction
|
||||
u.user_extensions.brief_introduction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
expose :id
|
||||
#头像
|
||||
user_expose :img_url
|
||||
#昵称
|
||||
expose :nickname
|
||||
#性别
|
||||
user_expose :gender
|
||||
#我的二维码
|
||||
#工作单位
|
||||
user_expose :work_unit
|
||||
#邮箱地址
|
||||
user_expose :mail
|
||||
#地区
|
||||
user_expose :location
|
||||
#签名
|
||||
user_expose :brief_introduction
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,19 @@
|
||||
module Mobile
|
||||
module Middleware
|
||||
class ErrorHandler < Grape::Middleware::Base
|
||||
def call!(env)
|
||||
@env = env
|
||||
begin
|
||||
@app.call(@env)
|
||||
rescue =>e
|
||||
message = {status: 1, message: e.message }.to_json
|
||||
puts(e.backtrace.join("\n")) if Rails.env.development?
|
||||
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
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue