This commit is contained in:
z9hang 2014-12-10 18:26:54 +08:00
commit 29069c58ae
5 changed files with 49 additions and 29 deletions

View File

@ -4,6 +4,7 @@ module Mobile
version 'v1', using: :path
format :json
content_type :json, "application/json;charset=UTF-8"
use Mobile::Middleware::ErrorHandler
helpers do
def logger

View File

@ -26,7 +26,7 @@ module Mobile
present :data, {token: key.access_token, user: api_user}, using: Entities::Auth
present :status, 0
else
{status: 1, message: 'Unauthorized.'}
raise 'Unauthorized.'
end
end

View File

@ -12,15 +12,11 @@ module Mobile
end
post do
us = UsersService.new
begin
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
rescue => e
{status: 1, message: e.message}
end
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
@ -37,12 +33,24 @@ module Mobile
put ':id' do
authenticate!
us = UsersService.new
begin
ue = us.edit_user params.merge(id: current_user.id)
{status: 0, data: ue}
rescue => e
{status: 1, message: e.message}
end
ue = us.edit_user params.merge(id: current_user.id)
present :data, user, with: Mobile::Entities::User
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
ue = us.change_password params.merge(new_password_confirmation: params[:new_password])
present :data, user, with: Mobile::Entities::User
present :status, 0
end
end

View File

@ -10,8 +10,9 @@ module Mobile
end
get do
authenticate!
ws = UsersService.new
ws.user_watcher(id: current_user.id)
us = UsersService.new
ws = us.user_watcher(id: current_user.id)
{status: 0, data: ws }
end
@ -23,12 +24,8 @@ module Mobile
post do
authenticate!
ws = WatchesService.new
begin
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
{status:0 , data: o}
rescue =>e
{status:1, message: e.message}
end
{status:0, data: o}
end
@ -40,12 +37,8 @@ module Mobile
delete do
authenticate!
ws = WatchesService.new
begin
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
{status:0}
rescue =>e
{status:1, message: e.message}
end
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
{status: 0}
end
end

View File

@ -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