diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 64016458e..d3fe414a4 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -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 diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index 5a6c0f34f..320497e50 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -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 diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 52932b883..6e2952290 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -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 diff --git a/app/api/mobile/apis/watches.rb b/app/api/mobile/apis/watches.rb index ce846debd..4e8510d89 100644 --- a/app/api/mobile/apis/watches.rb +++ b/app/api/mobile/apis/watches.rb @@ -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 diff --git a/app/api/mobile/middleware/error_handler.rb b/app/api/mobile/middleware/error_handler.rb new file mode 100644 index 000000000..3cbc74bd6 --- /dev/null +++ b/app/api/mobile/middleware/error_handler.rb @@ -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 \ No newline at end of file