diff --git a/.gitignore b/.gitignore index 38f29a661..0727a920a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,4 @@ vendor/cache /public/files /tags /config/initializers/gitlab_config.rb -/config/wechat.yml -/config/menu.yml 1234567 diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 4bf95fdbd..679b6382f 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -193,6 +193,8 @@ class WechatsController < ActionController::Base openid: openid, user: user ) + ws = WechatService.new + ws.binding_succ_notice(user.id, "您已成功绑定Trustie平台", user.login, format_time(Time.now)) render :json => {status:0, msg: "绑定成功"} rescue Exception=>e render :json => {status: -1, msg: e.message} diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb index af21b4f69..255410a89 100644 --- a/app/models/blog_comment.rb +++ b/app/models/blog_comment.rb @@ -77,16 +77,16 @@ class BlogComment < ActiveRecord::Base end #博客回复微信模板消息 - def blog_wechat_message - ws = WechatService.new - if self.parent_id.nil? - self.author.watcher_users.each do |watcher| - content = strip_html self.author.try(:realname) + " 发表了博客:" + self.title.html_safe, 200 - ws.message_update_template watcher.id, "blog_comment", self.id, "#{l(:label_new_blog_template)}", content, format_time(self.created_at) - end - else - content = strip_html self.content.html_safe, 200 - ws.comment_template self.parent.author_id, "blog_comment", self.parent_id, "#{l(:label_blog_comment_template)}", self.author.try(:realname), format_time(self.created_at), content - end - end + # def blog_wechat_message + # ws = WechatService.new + # if self.parent_id.nil? + # self.author.watcher_users.each do |watcher| + # content = strip_html self.author.try(:realname) + " 发表了博客:" + self.title.html_safe, 200 + # ws.message_update_template watcher.id, "blog_comment", self.id, "#{l(:label_new_blog_template)}", content, format_time(self.created_at) + # end + # else + # content = strip_html self.content.html_safe, 200 + # ws.comment_template self.parent.author_id, "blog_comment", self.parent_id, "#{l(:label_blog_comment_template)}", self.author.try(:realname), format_time(self.created_at), content + # end + # end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 8632c243a..edb5e472f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -41,15 +41,15 @@ class Comment < ActiveRecord::Base after_destroy :down_course_score def act_as_system_message - ws = WechatService.new + #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) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.commented.author_id} and shield_type='Course' and shield_id=#{self.commented.course.id}").count - if count == 0 - content = strip_html self.comments.html_safe, 200 - ws.comment_template self.commented.author_id, "course_notice", self.id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), content - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.commented.author_id} and shield_type='Course' and shield_id=#{self.commented.course.id}").count + # if count == 0 + # content = strip_html self.comments.html_safe, 200 + # ws.comment_template self.commented.author_id, "course_notice", self.id, "#{l(:label_notice_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + # end end else # 项目相关 if self.author_id != self.commented.author_id diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index e4426e78f..325b599e3 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -63,7 +63,7 @@ class HomeworkCommon < ActiveRecord::Base count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count if count == 0 ws = WechatService.new - ws.homework_template(m.user_id, "homework", self.id, "#{l(:label_new_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + ws.homework_message_notice(m.user_id, "homework", self.id, "#{m.user.show_name},#{l(:label_new_homework_template)}:", self.course.name, "新作业:"+ self.name.html_safe, "作业内容请点击“详情”查看。") end # end end @@ -108,15 +108,15 @@ class HomeworkCommon < ActiveRecord::Base end #修改作业后发送微信模板消息 - def wechat_message - self.course.members.each do |member| - count = ShieldWechatMessage.where("container_type='User' and container_id=#{member.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count - if count == 0 - ws = WechatService.new - ws.homework_template(member.user_id, "homework", self.id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") - end - end - end + # def wechat_message + # self.course.members.each do |member| + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{member.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count + # if count == 0 + # ws = WechatService.new + # ws.homework_template(member.user_id, "homework", self.id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59") + # end + # end + # end delegate :language_name, :language, :to => :homework_detail_programing diff --git a/app/models/issue.rb b/app/models/issue.rb index b5e87feea..4cfea1f43 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -160,27 +160,27 @@ 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) end - self.project.members.each do |m| - count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project_id}").count - if m.user_id != self.author_id && count == 0 - ws = WechatService.new - str = " 有新缺陷了: " - case self.tracker_id - when 1 - str = " 有新缺陷了: " - when 2 - str = " 有新功能了: " - when 3 - str = " 有新支持了: " - when 4 - str = " 有新任务了: " - when 5 - str = " 有新周报了: " - end - content = strip_html self.project.name + str + self.subject.html_safe, 200 - ws.message_update_template m.user_id, "issues", self.id, "#{l(:label_new_issue_template)}", content, format_time(self.created_on) - end - end + # self.project.members.each do |m| + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project_id}").count + # if m.user_id != self.author_id && count == 0 + # ws = WechatService.new + # str = " 有新缺陷了: " + # case self.tracker_id + # when 1 + # str = " 有新缺陷了: " + # when 2 + # str = " 有新功能了: " + # when 3 + # str = " 有新支持了: " + # when 4 + # str = " 有新任务了: " + # when 5 + # str = " 有新周报了: " + # end + # content = strip_html self.project.name + str + self.subject.html_safe, 200 + # ws.message_update_template m.user_id, "issues", self.id, "#{l(:label_new_issue_template)}", content, format_time(self.created_on) + # end + # end if self.tracker_id == 5 self.project.members.each do |m| if m.roles.first.to_s.include?("Manager") && m.user_id != self.author_id && m.user_id != self.assigned_to_id diff --git a/app/models/journal.rb b/app/models/journal.rb index eeaa17dd6..6d59c7414 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -238,11 +238,11 @@ class Journal < ActiveRecord::Base end #缺陷回复微信模板消息 - def issue_wechat_message - if !self.notes.nil? && self.notes.gsub(' ','') != '' - ws = WechatService.new - content = strip_html self.notes.html_safe, 200 - ws.comment_template self.issue.author_id, "issues", self.journalized_id, "#{l(:label_issue_comment_template)}", self.user.try(:realname), format_time(self.created_on), content - end - end + # def issue_wechat_message + # if !self.notes.nil? && self.notes.gsub(' ','') != '' + # ws = WechatService.new + # content = strip_html self.notes.html_safe, 200 + # ws.comment_template self.issue.author_id, "issues", self.journalized_id, "#{l(:label_issue_comment_template)}", self.user.try(:realname), format_time(self.created_on), content + # end + # end end diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index d0113744c..d8819515c 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -259,24 +259,24 @@ class JournalsForMessage < ActiveRecord::Base if self.m_parent_id.nil? if self.user_id != self.jour.user_id self.course_messages << CourseMessage.new(:user_id => self.jour.user_id,:course_id => self.jour.course.id, :viewed => false) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.jour.user_id} and shield_type='Course' and shield_id=#{self.jour.course_id}").count - if count == 0 - ws = WechatService.new - #content = truncate(strip_tags(self.notes.to_s), length: 200) - content = strip_html self.notes.html_safe, 200 - ws.comment_template self.jour.user_id, "homework", self.jour_id, "#{l(:label_homework_comment_template)}", self.user.try(:realname), format_time(self.created_on), content - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.jour.user_id} and shield_type='Course' and shield_id=#{self.jour.course_id}").count + # if count == 0 + # ws = WechatService.new + # #content = truncate(strip_tags(self.notes.to_s), length: 200) + # content = strip_html self.notes.html_safe, 200 + # ws.comment_template self.jour.user_id, "homework", self.jour_id, "#{l(:label_homework_comment_template)}", self.user.try(:realname), format_time(self.created_on), content + # end end else if self.user_id != self.parent.user_id self.course_messages << CourseMessage.new(:user_id => self.parent.user_id,:course_id => self.jour.course.id, :viewed => false) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.parent.user_id} and shield_type='Course' and shield_id=#{self.jour.course_id}").count - if count == 0 - ws = WechatService.new - #content = truncate(strip_tags(self.notes.to_s), length: 200) - content = strip_html self.notes.html_safe, 200 - ws.comment_template self.parent.user_id, "homework", self.jour_id, "#{l(:label_new_second_comment_template)}", self.user.try(:realname), format_time(self.created_on), content - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{self.parent.user_id} and shield_type='Course' and shield_id=#{self.jour.course_id}").count + # if count == 0 + # ws = WechatService.new + # #content = truncate(strip_tags(self.notes.to_s), length: 200) + # content = strip_html self.notes.html_safe, 200 + # ws.comment_template self.parent.user_id, "homework", self.jour_id, "#{l(:label_new_second_comment_template)}", self.user.try(:realname), format_time(self.created_on), content + # end end end end @@ -293,7 +293,7 @@ class JournalsForMessage < ActiveRecord::Base if self.user_id != self.jour_id # 过滤自己给自己的留言消息 receivers << self.jour content = strip_html self.notes, 200 - ws.message_update_template self.jour_id, "journal_for_message", self.id, "#{l(:label_new_journals_template)}", content, format_time(self.created_on) + ws.journal_notice self.jour_id, "journal_for_message", self.id, "#{l(:label_new_journals_template)}", self.user.show_name, format_time(self.created_on),content end else # 留言回复 reply_to = User.find(self.reply_id) @@ -303,8 +303,8 @@ class JournalsForMessage < ActiveRecord::Base if self.user_id != self.parent.jour_id && self.reply_id != self.parent.jour_id # 给东家发信息,如果回复的对象是东家则不发 receivers << self.parent.jour end - content = strip_html self.notes, 200 - ws.comment_template self.reply_id, "journal_for_message", self.parent.id, "#{l(:label_journals_comment_template)}", self.user.try(:realname), format_time(self.created_on), content + #content = strip_html self.notes, 200 + #ws.comment_template self.reply_id, "journal_for_message", self.parent.id, "#{l(:label_journals_comment_template)}", self.user.try(:realname), format_time(self.created_on), content 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 cf9adab4e..393fd3a60 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -258,28 +258,28 @@ class Message < ActiveRecord::Base # 主贴项目成员都能收到 # 回帖:帖子的发布人收到 def act_as_system_message - ws = WechatService.new + #ws = WechatService.new if self.course if self.parent_id.nil? # 主贴 self.course.members.includes(:user).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) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course.id}").count - if count == 0 - content = strip_html self.subject, 200 - ws.topic_publish_template m.user_id, "course_discussion", self.id, "#{l(:label_course_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course.id}").count + # if count == 0 + # content = strip_html self.subject, 200 + # ws.topic_publish_template m.user_id, "course_discussion", self.id, "#{l(:label_course_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + # end end end else # 回帖 parent_author_id = Message.find(self.parent_id).author_id if parent_author_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.course_messages << CourseMessage.new(:user_id => parent_author_id, :course_id => self.board.course_id, :viewed => false) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Course' and shield_id=#{self.board.course_id}").count - if count == 0 - content = strip_html self.content.html_safe, 200 - ws.comment_template parent_author_id, "course_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Course' and shield_id=#{self.board.course_id}").count + # if count == 0 + # content = strip_html self.content.html_safe, 200 + # ws.comment_template parent_author_id, "course_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + # end end end elsif self.project # 项目相关 @@ -287,22 +287,22 @@ class Message < ActiveRecord::Base self.project.members.includes(:user).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) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project.id}").count - if count == 0 - content = strip_html self.subject, 200 - ws.topic_publish_template m.user_id, "project_discussion", self.id, "#{l(:label_project_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Project' and shield_id=#{self.project.id}").count + # if count == 0 + # content = strip_html self.subject, 200 + # ws.topic_publish_template m.user_id, "project_discussion", self.id, "#{l(:label_project_topic_template)}", content, self.author.try(:realname), format_time(self.created_on) + # end end end else # 回帖 parent_author_id = Message.find(self.parent_id).author_id if parent_author_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.forge_messages << ForgeMessage.new(:user_id => parent_author_id, :project_id => self.board.project_id, :viewed => false) - count = ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Project' and shield_id=#{self.board.project_id}").count - if count == 0 - content = strip_html self.content.html_safe, 200 - ws.comment_template parent_author_id, "project_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content - end + # count = ShieldWechatMessage.where("container_type='User' and container_id=#{parent_author_id} and shield_type='Project' and shield_id=#{self.board.project_id}").count + # if count == 0 + # content = strip_html self.content.html_safe, 200 + # ws.comment_template parent_author_id, "project_discussion", self.parent_id, "#{l(:label_topic_comment_template)}", self.author.try(:realname), format_time(self.created_on), content + # end end end end diff --git a/app/models/news.rb b/app/models/news.rb index 0b03dcc48..54f6fce08 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -174,7 +174,7 @@ class News < ActiveRecord::Base if count == 0 ws = WechatService.new content = strip_html self.author.try(:realname) + " 发布了通知:" + self.title.html_safe, 200 - ws.message_update_template m.user_id, "course_notice", self.id, "#{l(:label_new_notice_template)}", content, format_time(self.created_on) + ws.class_notice m.user_id, "course_notice", self.id, "#{l(:label_new_notice_template)}", self.course.name, self.author.show_name, format_time(self.created_on), content, "点击查看通知详情" end end end diff --git a/app/services/wechat_service.rb b/app/services/wechat_service.rb index 6d8c0f234..e0c7811fd 100644 --- a/app/services/wechat_service.rb +++ b/app/services/wechat_service.rb @@ -110,4 +110,144 @@ class WechatService Rails.logger.info "send over. #{req}" end end + + def two_keys_template(openid, template_id, type, id, first, key1, key2, remark="") + data = { + touser:openid, + template_id:template_id, + url:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/#{type}/#{id}?response_type=code&scope=snsapi_base&state=123#wechat_redirect", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#707070" + }, + keyword1:{ + value:key1, + color:"#707070" + }, + keyword2:{ + value:key2, + color:"#707070" + }, + remark:{ + value:remark, + color:"#707070" + } + } + } + data + end + + def four_keys_template(openid, template_id, type, id, first, key1, key2, key3, key4, remark="") + data = { + touser:openid, + template_id:template_id, + url:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/#{type}/#{id}?response_type=code&scope=snsapi_base&state=123#wechat_redirect", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#707070" + }, + keyword1:{ + value:key1, + color:"#707070" + }, + keyword2:{ + value:key2, + color:"#707070" + }, + keyword3:{ + value:key3, + color:"#707070" + }, + keyword4:{ + value:key4, + color:"#707070" + }, + remark:{ + value:remark, + color:"#707070" + } + } + } + data + end + + def binding_succ_notice(user_id, first, key1, key2) + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = { + touser:uw.openid, + template_id:Wechat.config.binding_succ_notice, + url:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/activities?response_type=code&scope=snsapi_base&state=123#wechat_redirect", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#707070" + }, + keyword1:{ + value:key1, + color:"#707070" + }, + keyword2:{ + value:key2, + color:"#707070" + }, + remark:{ + value:"绑定成功后可使用微信查看Trustie平台最新动态", + color:"#707070" + } + } + } + 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 journal_notice(user_id, type, id, first, key1, key2, remark="") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = two_keys_template uw.openid,Wechat.config.journal_notice, type, id, first, key1, key2, 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 homework_message_notice(user_id, type, id, first, key1, key2, remark="") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = two_keys_template uw.openid,Wechat.config.homework_message_notice, type, id, first, key1, key2, 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 class_notice(user_id, type, id, first, key1, key2, key3, key4, remark="") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = four_keys_template uw.openid,Wechat.config.class_notice, type, id, first, key1, key2, key3, key4, 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 + end \ No newline at end of file diff --git a/config/locales/zh.yml b/config/locales/zh.yml index a347359bb..94be8af6d 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -2128,7 +2128,7 @@ zh: label_resource_belongs_project: 所属项目 #微信模板消息 - label_new_homework_template: 您的课程有新作业了 + label_new_homework_template: 您有新作业了 label_update_homework_template: 您的作业已被修改 label_course_topic_template: 课程问答区有新帖子发布了 label_topic_comment_template: 您的帖子有新回复了 diff --git a/config/wechat.yml b/config/wechat.yml index 5b528487b..3d679c4ce 100644 --- a/config/wechat.yml +++ b/config/wechat.yml @@ -11,6 +11,12 @@ default: &default encoding_aes_key: "QyocNOkRmrT5HzBpCG54EVPUQjk86nJapXNVDQm6Yy6" jsapi_ticket: "C:/Users/[user_name]/wechat_jsapi_ticket" + #template + binding_succ_notice: "n4KLwcWNrIMYkKxWL2hUwzunm5RTT54EbWem2MIUapU" + journal_notice: "XpHHYkqSGkwuF9vHthRdmPQLvCFRQ4_NbRBP12T7ciE" + homework_message_notice: "Kom0TsYYKsNKCS6luweYVRo9z-mH0wRPr24b1clGCPQ" + class_notice: "8LVu33l6bP-56SDomVgHn-yJc57YpCwwJ81rAJgRONk" + production: <<: *default