From 76cfa836668f5a785d4ae0a953364d022e453da5 Mon Sep 17 00:00:00 2001 From: cxt Date: Thu, 14 Apr 2016 10:47:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=A8=A1=E6=9D=BF=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1234567 | 2 +- app/models/blog_comment.rb | 7 +- app/models/comment.rb | 5 +- app/models/homework_common.rb | 6 +- app/models/issue.rb | 3 +- app/models/journal.rb | 3 +- app/models/journals_for_message.rb | 8 +- app/models/message.rb | 9 ++- app/models/news.rb | 3 +- app/services/wechat_service.rb | 113 +++++++++++++++++++++++++++++ 10 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 app/services/wechat_service.rb diff --git a/1234567 b/1234567 index 62c5e6df2..459204fe5 100644 --- a/1234567 +++ b/1234567 @@ -1 +1 @@ -{"access_token":"ONE7uqb5MhyuYiOtMkzUMPJs8krCiQeBjWek_Q9glYtfCH-kkQKzv84dgxUv5VLBnb5zdKajWDyHqocvu71UwlVuVFhsXs9MwbVdCu1i4l2L5vKCkhNE5GuyO6WisXfvKOUfAEAYCI","expires_in":7200,"got_token_at":1460441290} +{"access_token":"q51KZUeA6_-CCCH-Buy1mxFmRjcrCViHgk2mHHHqEDbjuA_pgCM1IyW1DASYvpzyB06xHiarujo3rz1Ucq3GRoXdgQ7hAoFCzkL_q3Z5vczLjwAjowAVwmulYE-cAij8ATUfADAWPQ","expires_in":7200,"got_token_at":1460601163} \ No newline at end of file diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb index 359522f9a..62451591f 100644 --- a/app/models/blog_comment.rb +++ b/app/models/blog_comment.rb @@ -77,13 +77,14 @@ class BlogComment < ActiveRecord::Base end #博客回复微信模板消息 - def blog_wechat_message + def blog_wechat_message + ws = WechatService.new if self.parent_id.nil? self.author.watcher_users.each do |watcher| - Wechat.api.message_update_template watcher.id, "#{l(:label_new_blog_template)}", self.author.try(:realname) + " 发表了博客:" + self.title.html_safe, format_time(self.created_at) + ws.message_update_template watcher.id, "#{l(:label_new_blog_template)}", self.author.try(:realname) + " 发表了博客:" + self.title.html_safe, format_time(self.created_at) end else - Wechat.api.comment_template self.parent.author_id, "#{l(:label_blog_comment_template)}", self.author.try(:realname), format_time(self.created_at), self.content.html_safe + ws.comment_template self.parent.author_id, "#{l(:label_blog_comment_template)}", self.author.try(:realname), format_time(self.created_at), self.content.html_safe end end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 59835cfc9..9a752129b 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -41,15 +41,16 @@ class Comment < ActiveRecord::Base after_destroy :down_course_score def act_as_system_message + ws = WechatService.new if self.commented.course if self.author_id != self.commented.author_id self.course_messages << CourseMessage.new(:user_id => self.commented.author_id, :course_id => self.commented.course.id, :viewed => false) - Wechat.api.comment_template self.commented.author_id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.comments.html_safe + ws.comment_template self.commented.author_id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.comments.html_safe end else # 项目相关 if self.author_id != self.commented.author_id self.forge_messages << ForgeMessage.new(:user_id => self.commented.author_id, :project_id => self.commented.project.id, :viewed => false) - Wechat.api.comment_template self.commented.author_id, "#{l(:label_news_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.comments.html_safe + ws.comment_template self.commented.author_id, "#{l(:label_news_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.comments.html_safe end end end diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index 2774295a1..663bd6065 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -59,7 +59,8 @@ class HomeworkCommon < ActiveRecord::Base self.course.members.each do |m| # if m.user_id != self.user_id self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false) - Wechat.api.homework_template(m.user_id, "#{l(:label_new_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + ws = WechatService.new + ws.homework_template(m.user_id, "#{l(:label_new_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") # end end end @@ -105,7 +106,8 @@ class HomeworkCommon < ActiveRecord::Base #修改作业后发送微信模板消息 def wechat_message self.course.members.each do |member| - Wechat.api.homework_template(member.user_id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + ws = WechatService.new + ws.homework_template(member.user_id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 45347b5ad..5124dd54b 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -159,7 +159,8 @@ class Issue < ActiveRecord::Base # 指派给自己的缺陷不提示消息 unless self.author_id == self.assigned_to_id self.forge_messages << ForgeMessage.new(:user_id => self.assigned_to_id, :project_id => self.project_id, :viewed => false) - Wechat.api.message_update_template self.assigned_to_id, "#{l(:label_new_issue_template)}", self.author.try(:realname) + " 给您指派了缺陷:" + self.subject.html_safe, format_time(self.created_on) + ws = WechatService.new + ws.message_update_template self.assigned_to_id, "#{l(:label_new_issue_template)}", self.author.try(:realname) + " 给您指派了缺陷:" + self.subject.html_safe, format_time(self.created_on) end if self.tracker_id == 5 self.project.members.each do |m| diff --git a/app/models/journal.rb b/app/models/journal.rb index 95aab7f96..80d585b06 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -239,6 +239,7 @@ class Journal < ActiveRecord::Base #缺陷回复微信模板消息 def issue_wechat_message - Wechat.api.comment_template self.issue.author_id, "#{l(:label_issue_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe + ws = WechatService.new + ws.comment_template self.issue.author_id, "#{l(:label_issue_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe end end diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index ce18e1bdb..799af8f74 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -256,7 +256,8 @@ class JournalsForMessage < ActiveRecord::Base end end if self.jour_type == 'HomeworkCommon' - Wechat.api.comment_template self.jour.user_id, "#{l(:label_homework_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe + ws = WechatService.new + ws.comment_template self.jour.user_id, "#{l(:label_homework_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe end end @@ -264,12 +265,13 @@ class JournalsForMessage < ActiveRecord::Base # 用户留言消息通知 def act_as_user_feedback_message # 主留言 + ws = WechatService.new if self.jour_type == 'Principal' receivers = [] if self.reply_id == 0 if self.user_id != self.jour_id # 过滤自己给自己的留言消息 receivers << self.jour - Wechat.api.message_update_template self.jour_id, "#{l(:label_new_journals_template)}", self.notes.html_safe, format_time(self.created_on) + ws.message_update_template self.jour_id, "#{l(:label_new_journals_template)}", self.notes.html_safe, format_time(self.created_on) end else # 留言回复 reply_to = User.find(self.reply_id) @@ -279,7 +281,7 @@ class JournalsForMessage < ActiveRecord::Base if self.user_id != self.parent.jour_id && self.reply_id != self.parent.jour_id # 给东家发信息,如果回复的对象是东家则不发 receivers << self.parent.jour end - Wechat.api.comment_template self.reply_id, "#{l(:label_journals_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe + ws.comment_template self.reply_id, "#{l(:label_journals_comment_template)}", self.user.try(:realname), format_time(self.created_on), self.notes.html_safe end receivers.each do |r| self.user_feedback_messages << UserFeedbackMessage.new(:user_id => r.id, :journals_for_message_id => self.id, :journals_for_message_type => "Principal", :viewed => false) diff --git a/app/models/message.rb b/app/models/message.rb index 2ff900858..2f4637042 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -260,19 +260,20 @@ class Message < ActiveRecord::Base # 主贴项目成员都能收到 # 回帖:帖子的发布人收到 def act_as_system_message + ws = WechatService.new if self.course if self.parent_id.nil? # 主贴 self.course.members.each do |m| if self.author.allowed_to?(:as_teacher, self.course) && m.user_id != self.author_id # 老师 自己的帖子不给自己发送消息 self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) - Wechat.api.topic_publish_template m.user_id, "#{l(:label_course_topic_template)}", self.subject, self.author.try(:realname), format_time(self.created_on) + ws.topic_publish_template m.user_id, "#{l(:label_course_topic_template)}", self.subject, self.author.try(:realname), format_time(self.created_on) end end else # 回帖 self.course.members.each do |m| if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) - Wechat.api.comment_template m.user_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.content.html_safe + ws.comment_template m.user_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.content.html_safe end end end @@ -281,14 +282,14 @@ class Message < ActiveRecord::Base self.project.members.each do |m| if m.user_id != self.author_id self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) - Wechat.api.topic_publish_template m.user_id, "#{l(:label_project_topic_template)}", self.subject, self.author.try(:realname), format_time(self.created_on) + ws.topic_publish_template m.user_id, "#{l(:label_project_topic_template)}", self.subject, self.author.try(:realname), format_time(self.created_on) end end else # 回帖 self.project.members.each do |m| if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) - Wechat.api.comment_template m.user_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.content.html_safe + ws.comment_template m.user_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), self.content.html_safe end end end diff --git a/app/models/news.rb b/app/models/news.rb index 967516e65..25a70095f 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -170,7 +170,8 @@ class News < ActiveRecord::Base self.course.members.each do |m| if m.user_id != self.author_id self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false) - Wechat.api.message_update_template m.user_id, "#{l(:label_new_notice_template)}", self.author.try(:realname) + " 发布了通知:" + self.title.html_safe, format_time(self.created_on) + ws = WechatService.new + ws.message_update_template m.user_id, "#{l(:label_new_notice_template)}", self.author.try(:realname) + " 发布了通知:" + self.title.html_safe, format_time(self.created_on) end end else diff --git a/app/services/wechat_service.rb b/app/services/wechat_service.rb new file mode 100644 index 000000000..e0a5feca8 --- /dev/null +++ b/app/services/wechat_service.rb @@ -0,0 +1,113 @@ +class WechatService + + def template_data(openid, template_id, first, key1, key2, key3, remark="具体内容请点击详情查看网站") + data = { + touser:openid, + template_id:template_id, + url:"https://www.trustie.net/", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#173177" + }, + keyword1:{ + value:key1, + color:"#173177" + }, + keyword2:{ + value:key2, + color:"#173177" + }, + keyword3:{ + value:key3, + color:"#173177" + }, + remark:{ + value:remark, + color:"#173177" + } + } + } + data + end + + def homework_template(user_id, first, key1, key2, key3, remark="具体内容请点击详情查看网站") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = template_data uw.openid,"3e5Dj2GIx8MOcMyRKpTUEQnM7Tg0ASSCNc01NS9HCGI",first, key1, key2, key3, remark + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[homework] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end + + def topic_publish_template(user_id, first, key1, key2, key3, remark="具体内容请点击详情查看网站") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = template_data uw.openid,"oKzFCdk7bsIHnGbscA__N8LPQrBkUShvpjV3-kuwWDQ",first, key1, key2, key3, remark + Rails.logger.info "start send template message: #{data}" + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[topic_publish] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end + + def comment_template(user_id, first, key1, key2, key3, remark="具体内容请点击详情查看网站") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = template_data uw.openid,"A_3f5v90-zK73V9Kijm-paDkl9S-NuM8Cf-1UJi92_c",first, key1, key2, key3, remark + Rails.logger.info "start send template message: #{data}" + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[comment] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end + + def message_update_template(user_id, first, key1, key2, remark="具体内容请点击详情查看网站") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = { + touser:uw.openid, + template_id:"YTyNPZnQD8uZFBFq-Q6cCOWaq5LA9vL6RFlF2JuD5Cg", + url:"https://www.trustie.net/", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#173177" + }, + keyword1:{ + value:key1, + color:"#173177" + }, + keyword2:{ + value:key2, + color:"#173177" + }, + remark:{ + value:remark, + color:"#173177" + } + } + } + + Rails.logger.info "start send template message: #{data}" + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[message_update] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end +end \ No newline at end of file