2014-12-03 17:28:19 +08:00
|
|
|
|
class UsersService
|
2014-12-09 16:40:52 +08:00
|
|
|
|
include ApplicationHelper
|
2014-12-04 15:53:47 +08:00
|
|
|
|
include AccountHelper
|
2014-12-10 11:21:23 +08:00
|
|
|
|
include AvatarHelper
|
2014-12-03 17:28:19 +08:00
|
|
|
|
#将用户注册的功能函数写这里
|
|
|
|
|
#参数约定
|
|
|
|
|
#成功返回注册后的User实例,失败直接抛异常
|
2014-12-04 15:53:47 +08:00
|
|
|
|
|
2014-12-03 17:28:19 +08:00
|
|
|
|
def register(params)
|
|
|
|
|
@user = User.new
|
|
|
|
|
@user.admin = false
|
|
|
|
|
@user.register
|
|
|
|
|
@user.login = params[:login]
|
2014-12-04 15:53:47 +08:00
|
|
|
|
@user.mail = params[:mail]
|
|
|
|
|
password = params[:password]
|
|
|
|
|
password_confirmation = params[:password_confirmation]
|
|
|
|
|
should_confirmation_password = params[:should_confirmation_password]
|
2014-12-06 11:27:52 +08:00
|
|
|
|
if !password.blank? && !password_confirmation.blank? && should_confirmation_password
|
2014-12-04 15:53:47 +08:00
|
|
|
|
@user.password,@user.password_confirmation = password,password_confirmation
|
|
|
|
|
elsif !password.blank? && !should_confirmation_password
|
|
|
|
|
@user.password = password
|
|
|
|
|
else
|
|
|
|
|
@user.password = ""
|
2014-12-03 17:28:19 +08:00
|
|
|
|
end
|
|
|
|
|
case Setting.self_registration
|
|
|
|
|
when '1'
|
2014-12-04 15:53:47 +08:00
|
|
|
|
@user = email_activation_register(@user)
|
2014-12-03 17:28:19 +08:00
|
|
|
|
when '3'
|
2014-12-04 15:53:47 +08:00
|
|
|
|
@user = automatically_register(@user)
|
2014-12-03 17:28:19 +08:00
|
|
|
|
else
|
2014-12-04 15:53:47 +08:00
|
|
|
|
@user = administrator_manually__register(@user)
|
2014-12-03 17:28:19 +08:00
|
|
|
|
end
|
|
|
|
|
if @user.id != nil
|
|
|
|
|
ue = @user.user_extensions ||= UserExtensions.new
|
|
|
|
|
ue.user_id = @user.id
|
|
|
|
|
ue.save
|
|
|
|
|
end
|
|
|
|
|
@user
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-09 16:40:52 +08:00
|
|
|
|
#显示用户
|
|
|
|
|
#id用户id
|
2014-12-09 16:57:08 +08:00
|
|
|
|
def show_user(params)
|
2014-12-09 16:40:52 +08:00
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
img_url = url_to_avatar(@user)
|
2014-12-09 16:57:08 +08:00
|
|
|
|
gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
2014-12-09 16:40:52 +08:00
|
|
|
|
work_unit = ""
|
|
|
|
|
if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1
|
|
|
|
|
work_unit = @user.user_extensions.school.name unless @user.user_extensions.school.nil?
|
|
|
|
|
elsif @user.user_extensions.identity == 3
|
|
|
|
|
work_unit = @user.user_extensions.occupation
|
|
|
|
|
elsif @user.user_extensions.identity == 2
|
|
|
|
|
work_unit = @user.firstname
|
|
|
|
|
end
|
|
|
|
|
location = ""
|
2014-12-09 16:57:08 +08:00
|
|
|
|
location << (@user.user_extensions.location || '')
|
|
|
|
|
location << (@user.user_extensions.location_city || '')
|
2014-12-09 16:40:52 +08:00
|
|
|
|
{: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
|
|
|
|
|
|
|
|
|
|
#编辑用户
|
|
|
|
|
def edit_user params
|
|
|
|
|
@user = User.find(params[:id])
|
2014-12-10 11:21:23 +08:00
|
|
|
|
fileio = params[:file]
|
2014-12-09 16:40:52 +08:00
|
|
|
|
|
2014-12-10 11:21:23 +08:00
|
|
|
|
@se = @user.extensions
|
|
|
|
|
if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1
|
|
|
|
|
@se.school_id = params[:occupation]
|
|
|
|
|
elsif @user.user_extensions.identity == 3
|
|
|
|
|
@se.occupation = params[:occupation]
|
|
|
|
|
elsif @user.user_extensions.identity == 2
|
|
|
|
|
@user.firstname = params[:occupation]
|
|
|
|
|
end
|
|
|
|
|
@se.brief_introduction = params[:brief_introduction]
|
|
|
|
|
@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))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
end
|
|
|
|
|
return true
|
|
|
|
|
else
|
|
|
|
|
@se
|
|
|
|
|
end
|
2014-12-09 16:40:52 +08:00
|
|
|
|
end
|
2014-12-03 17:28:19 +08:00
|
|
|
|
end
|