2014-12-10 18:26:38 +08:00
|
|
|
#coding=utf-8
|
2014-11-27 19:43:04 +08:00
|
|
|
module Mobile
|
2014-12-09 15:16:47 +08:00
|
|
|
module Apis
|
2014-12-09 16:36:43 +08:00
|
|
|
class Users < Grape::API
|
|
|
|
resource :users do
|
2014-12-04 10:11:55 +08:00
|
|
|
|
2014-12-10 16:23:56 +08:00
|
|
|
desc "注册用户"
|
2014-12-09 16:36:43 +08:00
|
|
|
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
|
2014-12-10 17:29:39 +08:00
|
|
|
user = us.register params.merge(:password_confirmation => params[:password],
|
|
|
|
:should_confirmation_password => true)
|
|
|
|
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
2014-12-15 14:06:00 +08:00
|
|
|
|
2014-12-10 17:29:39 +08:00
|
|
|
present :data, user, with: Mobile::Entities::User
|
|
|
|
present :status, 0
|
2014-11-27 19:43:04 +08:00
|
|
|
end
|
2014-12-09 15:16:47 +08:00
|
|
|
|
2015-01-06 16:59:22 +08:00
|
|
|
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
|
2014-12-10 11:56:33 +08:00
|
|
|
|
2014-12-10 16:23:56 +08:00
|
|
|
desc "修改用户"
|
2014-12-10 11:56:33 +08:00
|
|
|
params do
|
2014-12-10 16:23:56 +08:00
|
|
|
requires :token, type: String
|
2014-12-10 11:56:33 +08:00
|
|
|
#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
|
2014-12-10 17:29:39 +08:00
|
|
|
ue = us.edit_user params.merge(id: current_user.id)
|
2015-01-06 16:51:44 +08:00
|
|
|
present :data, ue,with: Mobile::Entities::User
|
2014-12-10 17:29:39 +08:00
|
|
|
present :status, 0
|
2014-12-10 16:51:19 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 17:46:16 +08:00
|
|
|
desc '获取用户课程'
|
|
|
|
params do
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-12-10 16:51:19 +08:00
|
|
|
|
|
|
|
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
|
2014-12-23 14:46:23 +08:00
|
|
|
user = us.change_password params.merge(current_user_id: current_user.id,
|
2014-12-23 10:16:23 +08:00
|
|
|
new_password_confirmation: params[:new_password])
|
2014-12-10 17:29:39 +08:00
|
|
|
present :data, user, with: Mobile::Entities::User
|
|
|
|
present :status, 0
|
2014-12-10 11:56:33 +08:00
|
|
|
end
|
|
|
|
|
2014-12-13 21:52:26 +08:00
|
|
|
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
|
|
|
|
|
2014-12-09 15:16:47 +08:00
|
|
|
end
|
2014-11-27 19:43:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|