diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index 8b27803ad..f7d723090 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -24,11 +24,7 @@ module Mobile feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) if (feedback.errors.empty?) homework_common.update_attributes(:updated_at => Time.now) - data = homework_common.journals_for_messages.last - present :data, data, with: Mobile::Entities::Jours result = 2 - else - result = 3 end when "News" news = News.find(params[:id]) @@ -36,11 +32,7 @@ module Mobile comment.comments = params[:content] comment.author = current_user if news.comments << comment - data = comment - present :data, data, with: Mobile::Entities::Comment result = 2 - else - result = 3 end when "Message" message = Message.find(params[:id]) @@ -53,11 +45,7 @@ module Mobile reply.parent_id = params[:id] reply.subject = "RE: #{topic.subject}" if topic.children << reply - data = reply - present :data, data, with: Mobile::Entities::Message result = 2 - else - result = 3 end when "JournalsForMessage" jour = JournalsForMessage.find params[:id] @@ -77,8 +65,6 @@ module Mobile if jfm.errors.empty? (JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now) result = 2 - else - result = 3 end when 'Issue' issue = Issue.find params[:id] @@ -86,13 +72,8 @@ module Mobile is_jour.user_id = current_user.id is_jour.notes = params[:content] is_jour.journalized = issue - #is_jour.journalized_type = "Issue" if is_jour.save - data = is_jour - present :data, data, with: Mobile::Entities::Journal result = 2 - else - result = 3 end when 'BlogComment' blog = BlogComment.find(params[:id]).root @@ -101,11 +82,8 @@ module Mobile blogComment.blog = blog.blog blogComment.content = params[:content] blogComment.title = "RE: #{blog.title}" - blog.children << blogComment - if blog.save + if blog.children << blogComment result = 2 - else - result = 3 end end if result == 2 @@ -116,7 +94,7 @@ module Mobile update_principal_activity_api(type,params[:id]) end else - result = 4 + result = 3 end present :result, result present :status, 0 diff --git a/app/api/mobile/apis/praise.rb b/app/api/mobile/apis/praise.rb new file mode 100644 index 000000000..3ba6e5eb7 --- /dev/null +++ b/app/api/mobile/apis/praise.rb @@ -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 \ No newline at end of file diff --git a/app/api/mobile/entities/jours.rb b/app/api/mobile/entities/jours.rb index e3ce04cf6..9d4d85f7a 100644 --- a/app/api/mobile/entities/jours.rb +++ b/app/api/mobile/entities/jours.rb @@ -19,6 +19,8 @@ module Mobile time_from_now f.created_on when :reply_count f.children.count + when :message_praise_count + get_activity_praise_num(f) end end end @@ -35,6 +37,7 @@ module Mobile jours_expose :m_reply_id jours_expose :m_parent_id jours_expose :reply_count + jours_expose :message_praise_count expose :course,using:Mobile::Entities::Course do |f,opt| if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course" f.course diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index 1955b78c0..28a8f74e4 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -450,4 +450,39 @@ module ApiHelper principal_activity.save 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 \ No newline at end of file diff --git a/public/assets/wechat/jour_message_detail.html b/public/assets/wechat/jour_message_detail.html new file mode 100644 index 000000000..e0ef85ecd --- /dev/null +++ b/public/assets/wechat/jour_message_detail.html @@ -0,0 +1,42 @@ + +