From 70bc6acbd2301e99b70b527756d2410659ba0d17 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Wed, 10 Dec 2014 11:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile.lock | 11 +++++++ app/api/mobile/api.rb | 2 +- app/api/mobile/apis/users.rb | 21 +++++++++++++ app/services/users_service.rb | 59 +++++++++++++++++------------------ 4 files changed, 61 insertions(+), 32 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a7239fc80..377ded53e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -130,6 +130,7 @@ GEM htmlentities (4.3.2) i18n (0.6.1) ice_nine (0.11.0) + iconv (1.0.4) journey (1.0.4) jquery-rails (2.0.3) railties (>= 3.1.0, < 5.0) @@ -185,6 +186,8 @@ GEM pry-remote (0.1.8) pry (~> 0.9) slop (~> 3.0) + puma (2.10.1) + rack (>= 1.1, < 2.0) rack (1.4.5) rack-accept (0.4.5) rack (>= 0.4) @@ -233,6 +236,7 @@ GEM sass-rails rmagick (2.13.2) ruby-openid (2.1.8) + ruby-prof (0.15.2) rubyzip (1.1.6) sass (3.3.10) sass-rails (3.2.6) @@ -283,6 +287,8 @@ GEM win32console (1.3.2-x86-mingw32) xpath (2.0.0) nokogiri (~> 1.3) + zip-zip (0.3) + rubyzip (>= 1.0.0) PLATFORMS ruby @@ -306,6 +312,7 @@ DEPENDENCIES guard-test (~> 1.0.0) htmlentities i18n (~> 0.6.0) + iconv jquery-rails (~> 2.0.2) kaminari mocha (~> 1.1.0) @@ -316,12 +323,15 @@ DEPENDENCIES pry-byebug pry-rails pry-remote + puma rack-mini-profiler! rack-openid rails (= 3.2.13) rich (= 1.4.6) rmagick (>= 2.0.0) ruby-openid (~> 2.1.4) + ruby-prof (~> 0.15.1) + rubyzip sass-rails (~> 3.2.3) seems_rateable! selenium-webdriver (~> 2.42.0) @@ -329,3 +339,4 @@ DEPENDENCIES spork-testunit (~> 0.0.8) therubyracer uglifier (>= 1.0.3) + zip-zip diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 4add19248..b540c73d3 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -10,7 +10,7 @@ module Mobile API.logger end - def authticate! + def authenticate! error!('Unauthorized. Invalid or expired token.', 401) unless current_user end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index df17782f2..9b5bf8c73 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -26,6 +26,27 @@ module Mobile end end + + desc "modify user" + params do + #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 + begin + ue = us.edit_user params + {status: 0, data: ue} + rescue => e + {status: 1, message: e.message} + end + end + end end end diff --git a/app/services/users_service.rb b/app/services/users_service.rb index cd955e18d..b47bf6a28 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -16,7 +16,7 @@ class UsersService password_confirmation = params[:password_confirmation] should_confirmation_password = params[:should_confirmation_password] if !password.blank? && !password_confirmation.blank? && should_confirmation_password - @user.password,@user.password_confirmation = password,password_confirmation + @user.password, @user.password_confirmation = password, password_confirmation elsif !password.blank? && !should_confirmation_password @user.password = password else @@ -42,7 +42,7 @@ class UsersService #id用户id def show_user(params) @user = User.find(params[:id]) - img_url = url_to_avatar(@user) + img_url = url_to_avatar(@user) gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender work_unit = "" if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1 @@ -55,7 +55,7 @@ class UsersService location = "" location << (@user.user_extensions.location || '') location << (@user.user_extensions.location_city || '') - {:id => @user.id,:img_url =>img_url,:nickname => @user.login,:gender => gender,:work_unit => work_unit,:mail => @user.mail,:location => location,:brief_introduction => @user.user_extensions.brief_introduction} + {:id => @user.id, :img_url => img_url, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction} end #编辑用户 @@ -75,36 +75,33 @@ class UsersService @se.gender = params[:gender] @se.location = params[:province] if params[:province] @se.location_city = params[:city] if params[:city] - if @se.save - unless fileio.nil? - file = fileio[:tempfile] - diskfile=disk_filename(@user.class.to_s,@user.id) - @image_file = fileio[:name] - @urlfile='/' << File.join("images","avatars",avatar_directory(@user.class.to_s),avatar_filename(@user.id,@image_file)) + raise @se.errors.full_message unless @se.save + unless fileio.nil? + file = fileio[:tempfile] + diskfile=disk_filename(@user.class.to_s, @user.id) + @image_file = fileio[:name] + @urlfile='/' << File.join("images", "avatars", avatar_directory(@user.class.to_s), avatar_filename(@user.id, @image_file)) - path = File.dirname(diskfile) - unless File.directory?(path) - FileUtils.mkdir_p(path) - end - File.rename(file.path, @urlfile) - begin - f = Magick::ImageList.new(diskfile) - # gif格式不再做大小处理 - if f.format != 'GIF' - width = 300.0 - proportion = (width/f[0].columns) - height = (f[0].rows*proportion) - f.resize_to_fill!(width,height) - f.write(diskfile) - end - - rescue Exception => e - logger.error "[Error] avatar : users_service#edit_user ===> #{e}" - end + path = File.dirname(diskfile) + unless File.directory?(path) + FileUtils.mkdir_p(path) + end + File.rename(file.path, @urlfile) + begin + f = Magick::ImageList.new(diskfile) + # gif格式不再做大小处理 + if f.format != 'GIF' + width = 300.0 + proportion = (width/f[0].columns) + height = (f[0].rows*proportion) + f.resize_to_fill!(width, height) + f.write(diskfile) + end + + rescue Exception => e + logger.error "[Error] avatar : users_service#edit_user ===> #{e}" end - return true - else - @se end + @se end end