Merge branch 'api' of http://repository.trustie.net/xianbo/trustie2 into api
This commit is contained in:
commit
29069c58ae
|
@ -4,6 +4,7 @@ module Mobile
|
||||||
version 'v1', using: :path
|
version 'v1', using: :path
|
||||||
format :json
|
format :json
|
||||||
content_type :json, "application/json;charset=UTF-8"
|
content_type :json, "application/json;charset=UTF-8"
|
||||||
|
use Mobile::Middleware::ErrorHandler
|
||||||
|
|
||||||
helpers do
|
helpers do
|
||||||
def logger
|
def logger
|
||||||
|
|
|
@ -26,7 +26,7 @@ module Mobile
|
||||||
present :data, {token: key.access_token, user: api_user}, using: Entities::Auth
|
present :data, {token: key.access_token, user: api_user}, using: Entities::Auth
|
||||||
present :status, 0
|
present :status, 0
|
||||||
else
|
else
|
||||||
{status: 1, message: 'Unauthorized.'}
|
raise 'Unauthorized.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,15 +12,11 @@ module Mobile
|
||||||
end
|
end
|
||||||
post do
|
post do
|
||||||
us = UsersService.new
|
us = UsersService.new
|
||||||
begin
|
|
||||||
user = us.register params.merge(:password_confirmation => params[:password],
|
user = us.register params.merge(:password_confirmation => params[:password],
|
||||||
:should_confirmation_password => true)
|
:should_confirmation_password => true)
|
||||||
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
||||||
present :data, user, with: Mobile::Entities::User
|
present :data, user, with: Mobile::Entities::User
|
||||||
present :status, 0
|
present :status, 0
|
||||||
rescue => e
|
|
||||||
{status: 1, message: e.message}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,12 +33,24 @@ module Mobile
|
||||||
put ':id' do
|
put ':id' do
|
||||||
authenticate!
|
authenticate!
|
||||||
us = UsersService.new
|
us = UsersService.new
|
||||||
begin
|
|
||||||
ue = us.edit_user params.merge(id: current_user.id)
|
ue = us.edit_user params.merge(id: current_user.id)
|
||||||
{status: 0, data: ue}
|
present :data, user, with: Mobile::Entities::User
|
||||||
rescue => e
|
present :status, 0
|
||||||
{status: 1, message: e.message}
|
|
||||||
end
|
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
|
||||||
|
ue = us.change_password params.merge(new_password_confirmation: params[:new_password])
|
||||||
|
present :data, user, with: Mobile::Entities::User
|
||||||
|
present :status, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,9 @@ module Mobile
|
||||||
end
|
end
|
||||||
get do
|
get do
|
||||||
authenticate!
|
authenticate!
|
||||||
ws = UsersService.new
|
us = UsersService.new
|
||||||
ws.user_watcher(id: current_user.id)
|
ws = us.user_watcher(id: current_user.id)
|
||||||
|
{status: 0, data: ws }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,12 +24,8 @@ module Mobile
|
||||||
post do
|
post do
|
||||||
authenticate!
|
authenticate!
|
||||||
ws = WatchesService.new
|
ws = WatchesService.new
|
||||||
begin
|
|
||||||
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||||
{status:0, data: o}
|
{status:0, data: o}
|
||||||
rescue =>e
|
|
||||||
{status:1, message: e.message}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,12 +37,8 @@ module Mobile
|
||||||
delete do
|
delete do
|
||||||
authenticate!
|
authenticate!
|
||||||
ws = WatchesService.new
|
ws = WatchesService.new
|
||||||
begin
|
|
||||||
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||||
{status: 0}
|
{status: 0}
|
||||||
rescue =>e
|
|
||||||
{status:1, message: e.message}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
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
|
||||||
|
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
|
Loading…
Reference in New Issue