From 5eba06ddf9f056ef973059461b47cd040731febb Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 10 Dec 2014 15:11:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5api=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Gemfile | 1 + Gemfile.lock | 4 ++++ app/api/mobile/api.rb | 1 + app/api/mobile/apis/auth.rb | 14 ++++++++++++-- app/api/mobile/apis/users.rb | 2 +- 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 061314adc..f1eed7367 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ /config/configuration.yml .rbenv-gemsets .DS_Store +public/api_doc/ diff --git a/Gemfile b/Gemfile index 34a1a7956..1fe5b6c0f 100644 --- a/Gemfile +++ b/Gemfile @@ -20,6 +20,7 @@ gem "builder", "3.0.0" gem 'acts-as-taggable-on', '2.4.1' group :development do + gem 'grape-swagger' gem 'puma' gem 'better_errors', path: 'lib/better_errors' gem 'rack-mini-profiler', path: 'lib/rack-mini-profiler' diff --git a/Gemfile.lock b/Gemfile.lock index 377ded53e..95ea5b6e2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -110,6 +110,9 @@ GEM grape-entity (0.4.4) activesupport multi_json (>= 1.3.2) + grape-swagger (0.8.0) + grape + grape-entity guard (2.6.1) formatador (>= 0.2.4) listen (~> 2.7) @@ -307,6 +310,7 @@ DEPENDENCIES fastercsv (~> 1.5.0) grape (~> 0.9.0) grape-entity + grape-swagger guard-rails (~> 0.5.3) guard-spork (~> 1.5.1) guard-test (~> 1.0.0) diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index b540c73d3..548793f3e 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -28,6 +28,7 @@ module Mobile mount Apis::Users mount Apis::Courses + add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) end end diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index fa6fec6e2..136e5b171 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -10,12 +10,12 @@ module Mobile module Apis class Auth < Grape::API resource :auth do - desc "Creates and returns access_token if valid login" + desc "用户登录" params do requires :login, type: String, desc: 'Username or email' requires :password, type: String, desc: 'Password' end - post :login do + post do user,last_logon = ::User.try_to_login(params[:login], params[:password]) if user ::ApiKey.delete_all(user_id: user.id) @@ -27,6 +27,16 @@ module Mobile {status: 1, message: 'Unauthorized.'} end end + + desc "用户登出" + params do + end + delete do + authenticate! + ::ApiKey.delete_all(user_id: current_user.id) + {status: 0} + end + end end end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 9b5bf8c73..5dc25d401 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -40,7 +40,7 @@ module Mobile authenticate! us = UsersService.new begin - ue = us.edit_user params + ue = us.edit_user params.merge(id: current_user.id) {status: 0, data: ue} rescue => e {status: 1, message: e.message}