点赞的api

This commit is contained in:
cxt 2016-04-06 11:40:05 +08:00
parent be30ac889b
commit 6c527a9a54
6 changed files with 122 additions and 95 deletions

View File

@ -24,11 +24,7 @@ module Mobile
feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id])
if (feedback.errors.empty?) if (feedback.errors.empty?)
homework_common.update_attributes(:updated_at => Time.now) homework_common.update_attributes(:updated_at => Time.now)
data = homework_common.journals_for_messages.last
present :data, data, with: Mobile::Entities::Jours
result = 2 result = 2
else
result = 3
end end
when "News" when "News"
news = News.find(params[:id]) news = News.find(params[:id])
@ -36,11 +32,7 @@ module Mobile
comment.comments = params[:content] comment.comments = params[:content]
comment.author = current_user comment.author = current_user
if news.comments << comment if news.comments << comment
data = comment
present :data, data, with: Mobile::Entities::Comment
result = 2 result = 2
else
result = 3
end end
when "Message" when "Message"
message = Message.find(params[:id]) message = Message.find(params[:id])
@ -53,11 +45,7 @@ module Mobile
reply.parent_id = params[:id] reply.parent_id = params[:id]
reply.subject = "RE: #{topic.subject}" reply.subject = "RE: #{topic.subject}"
if topic.children << reply if topic.children << reply
data = reply
present :data, data, with: Mobile::Entities::Message
result = 2 result = 2
else
result = 3
end end
when "JournalsForMessage" when "JournalsForMessage"
jour = JournalsForMessage.find params[:id] jour = JournalsForMessage.find params[:id]
@ -77,8 +65,6 @@ module Mobile
if jfm.errors.empty? if jfm.errors.empty?
(JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now) (JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now)
result = 2 result = 2
else
result = 3
end end
when 'Issue' when 'Issue'
issue = Issue.find params[:id] issue = Issue.find params[:id]
@ -86,13 +72,8 @@ module Mobile
is_jour.user_id = current_user.id is_jour.user_id = current_user.id
is_jour.notes = params[:content] is_jour.notes = params[:content]
is_jour.journalized = issue is_jour.journalized = issue
#is_jour.journalized_type = "Issue"
if is_jour.save if is_jour.save
data = is_jour
present :data, data, with: Mobile::Entities::Journal
result = 2 result = 2
else
result = 3
end end
when 'BlogComment' when 'BlogComment'
blog = BlogComment.find(params[:id]).root blog = BlogComment.find(params[:id]).root
@ -101,11 +82,8 @@ module Mobile
blogComment.blog = blog.blog blogComment.blog = blog.blog
blogComment.content = params[:content] blogComment.content = params[:content]
blogComment.title = "RE: #{blog.title}" blogComment.title = "RE: #{blog.title}"
blog.children << blogComment if blog.children << blogComment
if blog.save
result = 2 result = 2
else
result = 3
end end
end end
if result == 2 if result == 2
@ -116,7 +94,7 @@ module Mobile
update_principal_activity_api(type,params[:id]) update_principal_activity_api(type,params[:id])
end end
else else
result = 4 result = 3
end end
present :result, result present :result, result
present :status, 0 present :status, 0

View File

@ -0,0 +1,40 @@
#coding=utf-8
module Mobile
module Apis
class Praise< Grape::API
include ApiHelper
resources :praise do
desc "praise an activity"
params do
requires :type, type: String
requires :openid, type: String
end
post ':id' do
obj_id = params[:id]
obj_type = params[:type]
user = UserWechat.find_by_openid(params[:openid]).user
obj = find_object_by_type_and_id(obj_id,obj_type)
pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",obj_id,obj_type.to_s,user.id)
if pts.empty?
praise_or_cancel(obj_type,obj_id,user,1)
num = get_activity_praise_num(obj)
else
pts.delete if !pts.nil?
#再更新praise_tread_cache表 使相应的记录减1 当为0时删除
ptc = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
ptc.praise_minus(1) if !ptc.nil?
if ptc.praise_num == 0
ptc.delete
end
num = get_activity_praise_num(obj)
end
present :data, num
present :status, 0
end
end
end
end
end

View File

@ -19,6 +19,8 @@ module Mobile
time_from_now f.created_on time_from_now f.created_on
when :reply_count when :reply_count
f.children.count f.children.count
when :message_praise_count
get_activity_praise_num(f)
end end
end end
end end
@ -35,6 +37,7 @@ module Mobile
jours_expose :m_reply_id jours_expose :m_reply_id
jours_expose :m_parent_id jours_expose :m_parent_id
jours_expose :reply_count jours_expose :reply_count
jours_expose :message_praise_count
expose :course,using:Mobile::Entities::Course do |f,opt| expose :course,using:Mobile::Entities::Course do |f,opt|
if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course" if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course"
f.course f.course

View File

@ -450,4 +450,39 @@ module ApiHelper
principal_activity.save principal_activity.save
end end
end end
#赞/取消赞
def praise_or_cancel(type,id,user,flag)
unless id.nil? and type.nil?
#首先创建或更新praise_tread 表
pt = PraiseTread.new
pt.user_id = user.id
pt.praise_tread_object_id = id.to_i
pt.praise_tread_object_type = type
pt.praise_or_tread = flag
pt.save
# end
#再创建或更新praise_tread_cache表
#@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type)
ptc = PraiseTreadCache.where("object_id = ? and object_type = ?",id.to_i,type).first
ptc = ptc.nil? ? PraiseTreadCache.new : ptc
ptc.object_id = id.to_i
ptc.object_type = type
ptc.save
ptc.praise_plus(flag,1)
end
end
def praise_plus(flag,num)
case flag
when 1
self.update_attribute(:praise_num, self.praise_num.to_i + num)
end
end
def praise_minus(num)
self.update_attribute(:praise_num, self.praise_num.to_i - num)
end
end end

View File

@ -0,0 +1,42 @@
<!-- 模板1开始可以使用scripttype设置为text/html来存放模板片段并且用id标示 -->
<div class="post-container">
<div class="post-wrapper">
<div class="post-main">
<div class="post-avatar fl"><img ng-src="{{message.user.img_url}}" width="45" height="45" class="border-radius" /></div>
<div class="post-title mb5 hidden"><a herf="javascript:void(0);" class="mr10">{{message.user.nickname}}</a><span style="vertical-align:top;">给您留言了</span><br /></div>
<div class="post-title hidden">{{message.created_on}}</div>
<div class="cl"></div>
<div class="post-content c-grey2 mt10" style="height:auto;">
<div class="post-all-content">{{message.notes}}</div>
</div>
<div class="cl"></div>
</div>
<div class="post-interactive border-bottom">
<div class="post-interactive-reply c-grey2">回复 <span class="reply-num">({{message.reply_count}})</span></div>
<div class="post-interactive-praise c-grey2"><span class="paise-text"></span> <span class="praise-num">({{message.message_praise_count}})</span></div>
</div>
<div id="all_message_reply">
<div class="post-reply-wrap border-bottom" ng-repeat="journal in message.child_reply">
<div class="post-reply-row">
<div class="post-reply-avatar fl"><img ng-src="{{journal.user.img_url}}" width="45" height="45" class="border-radius" /></div>
<div class="ml55">
<div class="post-reply-user hidden">{{journal.user.realname}}</div>
<div class="post-reply-content c-grey2 mb10" ng-bind-html="journal.content|safeHtml"></div>
<div class="post-reply-date fl">{{journal.lasted_comment}}</div>
<div class="post-reply-trigger fr undis">回复</div>
</div>
<div class="cl"></div>
</div>
</div>
</div>
<div class="post-input-wrap">
<div class="post-reply-row">
<!--<div class="post-reply-avatar fl"><img src="images/post-avatar.jpg" width="30" height="30" /></div>-->
<input type="text" class="post-reply-input" id="postInput" ng-model="formData.comment" />
<button ng-click="addJournalReply(formData)" class="post-reply-submit fr mt10">回复</button>
<div class="cl"></div>
</div>
</div>
</div>
</div>

View File

@ -1,71 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>留言</title>
<meta charset='utf-8' />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="apple-mobile-web-app-capable" content="no">
<meta content='True' name='HandheldFriendly' />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<link type="text/css" rel="stylesheet" href="/stylesheets/weui/weixin.css" />
</head>
<body>
<div id="message-container"></div>
<!-- 模板1开始可以使用scripttype设置为text/html来存放模板片段并且用id标示 -->
<script id="t:message-detail" type="text/html">
<div id="check"></div>
<div class="post-container">
<div class="post-wrapper">
<div class="post-main">
<div class="post-avatar fl"><img src="<!=message.user.img_url!>" width="45" height="45" class="border-radius" /></div>
<div class="post-title mb5 hidden"><a herf="javascript:void(0);" class="mr10"><!=message.user.nickname!></a><span style="vertical-align:top;">给您留言了</span><br /></div>
<div class="post-title hidden"><!=message.created_on!></div>
<div class="cl"></div>
<div class="post-content c-grey2 mt10" style="height:auto;">
<div class="post-all-content"><!:=message.notes!></div>
</div>
<div class="cl"></div>
</div>
<div class="post-interactive border-bottom">
<div class="post-interactive-reply c-grey2">回复 <span class="reply-num">(<!=message.reply_count!>)</span></div>
<div class="post-interactive-praise c-grey2"><span class="paise-text"></span><span class="praise-num">()</span></div>
</div>
<! for(var j =0; j < message.child_reply.length; ++j){ !>
<div class="post-reply-wrap border-bottom">
<div class="post-reply-row">
<div class="post-reply-avatar fl"><img src="<!=message.child_reply[j].user.img_url!>" width="45" height="45" class="border-radius" /></div>
<div class="ml55">
<div class="post-reply-user hidden"><!=message.child_reply[j].user.realname!></div>
<div class="post-reply-content c-grey2 mb10"><!:=message.child_reply[j].content!></div>
<div class="post-reply-date fl"><!=message.child_reply[j].lasted_comment!></div>
<div class="post-reply-trigger fr undis">回复</div>
</div>
<div class="cl"></div>
</div>
</div>
<! } !>
<div class="post-input-wrap">
<div class="post-reply-row">
<!--<div class="post-reply-avatar fl" id = "100"><img src="images/post-avatar.jpg" width="30" height="30" /></div>-->
<input type="text" class="post-reply-input" id="postInput" />
<input type="submit" value="回复" class="post-reply-submit fr mt10" onclick="replyInsert()" />
<div class="cl"></div>
</div>
</div>
</div>
</div>
</script>
<script src="/javascripts/jquery-1.3.2.js"></script>
<script src="/javascripts/baiduTemplate.js"></script>
<script src="/javascripts/wechat/auth.js"></script>
<script src="/javascripts/wechat/message_detail.js"></script>
</body>
</html>