修改数据格式转换方法
This commit is contained in:
parent
0a33a716de
commit
b949f70c64
|
@ -169,7 +169,7 @@ module Mobile
|
|||
route_param :id do
|
||||
get do
|
||||
cs = CoursesService.new
|
||||
course = cs.show_course params,current_user
|
||||
course = cs.show_course(params,(current_user.nil? ? User.find(2):current_user))
|
||||
#course = Course.find(params[:id])
|
||||
{status: 0, data: course}
|
||||
end
|
||||
|
@ -196,6 +196,18 @@ module Mobile
|
|||
present :status, 0
|
||||
end
|
||||
|
||||
desc "显示课程通知"
|
||||
params do
|
||||
|
||||
end
|
||||
get "news/:id" do
|
||||
cs = CoursesService.new
|
||||
cs.show_course_news_authorize(current_user.nil? ? User.find(2):current_user)
|
||||
news = cs.show_course_news params,current_user.nil? ? User.find(2):current_user
|
||||
present :data, news, with: Mobile::Entities::News
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,22 +5,37 @@ module Mobile
|
|||
expose field do |f,opt|
|
||||
if f.is_a?(Hash) && f.key?(field)
|
||||
f[field]
|
||||
elsif f.is_a?(Hash) && !f.key?(field)
|
||||
n = f[:news]
|
||||
comments = f[:comments]
|
||||
if n.is_a?(::News)
|
||||
n.send(field) if n.respond_to?(field)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#新闻标题
|
||||
news_expose :title
|
||||
|
||||
expose :author,using: Mobile::Entities::User do |f, opt|
|
||||
n = f[:news]
|
||||
n.author if n.respond_to?(:author)
|
||||
end
|
||||
#作者id
|
||||
news_expose :author_id
|
||||
#作者名
|
||||
news_expose :author_name
|
||||
#新闻内容
|
||||
news_expose :content
|
||||
news_expose :description
|
||||
#发布时间
|
||||
news_expose :time
|
||||
news_expose :created_on
|
||||
#评论数量
|
||||
news_expose :comments_count
|
||||
#评论
|
||||
news_expose :comments
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,29 @@
|
|||
module Mobile
|
||||
module Entities
|
||||
class User < Grape::Entity
|
||||
include ApplicationHelper
|
||||
include ApiHelper
|
||||
def self.user_expose(f)
|
||||
expose f do |u,opt|
|
||||
if u.is_a?(Hash) && u.key?(f)
|
||||
u[f]
|
||||
#else
|
||||
# u.send(f) if u.respond_to?(f)
|
||||
elsif u.is_a?(::User)
|
||||
if u.respond_to?(f)
|
||||
u.send(f)
|
||||
else
|
||||
case f
|
||||
when :img_url
|
||||
url_to_avatar(u)
|
||||
when :gender
|
||||
u.user_extensions.gender.nil? ? 0 : u.user_extensions.gender
|
||||
when :work_unit
|
||||
get_user_work_unit u
|
||||
when :location
|
||||
get_user_location u
|
||||
when :brief_introduction
|
||||
u.user_extensions.brief_introduction
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -92,7 +92,9 @@ class NewsController < ApplicationController
|
|||
|
||||
def show
|
||||
cs = CoursesService.new
|
||||
@news,@comments = cs.show_course_news params,User.current
|
||||
result = cs.show_course_news params,User.current
|
||||
@news = result[:news]
|
||||
@comments = result[:comments]
|
||||
#@comments = @news.comments
|
||||
#@comments.reverse! if User.current.wants_comments_in_reverse_order?
|
||||
#modify by nwb
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
module ApiHelper
|
||||
#获取用户的工作单位
|
||||
def get_user_work_unit user
|
||||
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
|
||||
work_unit
|
||||
end
|
||||
|
||||
#获取用户地区
|
||||
def get_user_location user
|
||||
location = ""
|
||||
location << (user.user_extensions.location || '')
|
||||
location << (user.user_extensions.location_city || '')
|
||||
location
|
||||
end
|
||||
end
|
|
@ -112,14 +112,14 @@ class CoursesService
|
|||
scope = @course ? @course.news.course_visible : News.course_visible
|
||||
news = []
|
||||
scope.each do |n|
|
||||
news << {:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :content => n.description,:time => format_time(n.created_on),:comments_count => n.comments_count}
|
||||
news << {:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :description => n.description,:created_on => format_time(n.created_on),:comments_count => n.comments_count}
|
||||
end
|
||||
news
|
||||
end
|
||||
|
||||
#查看新闻权限验证
|
||||
def show_course_news_authorize(current_user,course)
|
||||
unless current_user.allowed_to?({:controller => 'news', :action => 'show'}, course)
|
||||
def show_course_news_authorize(current_user)
|
||||
unless current_user.allowed_to?({:controller => 'news', :action => 'show'}, false)
|
||||
raise '403'
|
||||
end
|
||||
end
|
||||
|
@ -129,7 +129,13 @@ class CoursesService
|
|||
@news = News.find(params[:id])
|
||||
@comments = @news.comments
|
||||
@comments.reverse! if current_user.wants_comments_in_reverse_order?
|
||||
[@news,@comments]
|
||||
{:news => @news,:comments => @comments}
|
||||
#comments = []
|
||||
#@comments.each do |comment|
|
||||
# comments << {:author_id => comment.author_id,:author_name => comment.author.name,:commont_content => comment.comments,:time => format_time(comment.created_on)}
|
||||
#end
|
||||
#{:title => @news.title,:author_name => @news.author.name,:author_id => @news.author.id, :description => @news.description,:created_on => format_time(@news.created_on),
|
||||
# :comments_count => @news.comments_count,:comments => comments}
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ class UsersService
|
|||
include AccountHelper
|
||||
include AvatarHelper
|
||||
include CoursesHelper
|
||||
include ApiHelper
|
||||
#将用户注册的功能函数写这里
|
||||
#参数约定
|
||||
#成功返回注册后的User实例,失败直接抛异常
|
||||
|
@ -36,11 +37,12 @@ class UsersService
|
|||
ue.user_id = @user.id
|
||||
ue.save
|
||||
end
|
||||
img_url = url_to_avatar(@user)
|
||||
gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
||||
work_unit = get_user_work_unit @user
|
||||
location = get_user_location @user
|
||||
{: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}
|
||||
@user
|
||||
#img_url = url_to_avatar(@user)
|
||||
#gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
||||
#work_unit = get_user_work_unit @user
|
||||
#location = get_user_location @user
|
||||
#{: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
|
||||
|
||||
#显示用户
|
||||
|
@ -99,33 +101,17 @@ class UsersService
|
|||
logger.error "[Error] avatar : users_service#edit_user ===> #{e}"
|
||||
end
|
||||
end
|
||||
img_url = url_to_avatar(@user)
|
||||
gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
||||
work_unit = get_user_work_unit @user
|
||||
location = get_user_location @user
|
||||
{: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}
|
||||
#img_url = url_to_avatar(@user)
|
||||
#gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
||||
#work_unit = get_user_work_unit @user
|
||||
#location = get_user_location @user
|
||||
#{: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}
|
||||
@user
|
||||
end
|
||||
|
||||
#获取用户的工作单位
|
||||
def get_user_work_unit user
|
||||
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
|
||||
work_unit
|
||||
end
|
||||
|
||||
#获取用户地区
|
||||
def get_user_location user
|
||||
location = ""
|
||||
location << (user.user_extensions.location || '')
|
||||
location << (user.user_extensions.location_city || '')
|
||||
location
|
||||
end
|
||||
|
||||
|
||||
|
||||
#关注列表
|
||||
def user_watcher params
|
||||
|
|
Loading…
Reference in New Issue