100 lines
3.0 KiB
Ruby
100 lines
3.0 KiB
Ruby
#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
|
||
requires :id, type: Integer
|
||
end
|
||
route_param :id do
|
||
get do
|
||
us = UsersService.new
|
||
ue = us.show_user params
|
||
present :data, ue,with: Mobile::Entities::User
|
||
present :status, 0
|
||
end
|
||
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: '用户名关键字'
|
||
requires :search_by, type: String,desc: '搜索依据:0 昵称,1 用户名,2 邮箱'
|
||
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
|
||
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
|