From 824f9d91741e3b043bcf29cbd42ab7a0b8504d1c Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Tue, 12 Jul 2016 17:00:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=9B=E5=BB=BA=E7=8F=AD?= =?UTF-8?q?=E7=BA=A7=E7=9A=84=E6=A8=A1=E7=89=88=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 3 ++ app/services/syllabuses_service.rb | 12 +++++++ app/services/wechat_service.rb | 45 +++++++++++++++++++++++++++ config/wechat.yml | 1 + 4 files changed, 61 insertions(+) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index a9d84c2d9..deb1d9bc2 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -633,6 +633,9 @@ class CoursesController < ApplicationController =end end if @course + #发送微信消息 + ss = SyllabusesService.new + ss.send_wechat_create_class_notice User.current,@course respond_to do |format| flash[:notice] = l(:notice_successful_create) format.html {redirect_to course_url(@course)} diff --git a/app/services/syllabuses_service.rb b/app/services/syllabuses_service.rb index b3190182c..55e0768d7 100644 --- a/app/services/syllabuses_service.rb +++ b/app/services/syllabuses_service.rb @@ -72,6 +72,15 @@ class SyllabusesService course.course_infos << course_info end + def send_wechat_create_class_notice user,course + count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count + if count == 0 + ws = WechatService.new + title = "恭喜您创建班级成功" + ws.create_class_notice user.id, "create_course_notice", course.id,title, course.name, user.show_name, 0, "点击查看班级详情" + end + end + #创建大纲 # params {title: '大纲名称', [{course}, {course}]} def create(user, title, courses = []) @@ -83,6 +92,7 @@ class SyllabusesService if ::Course === course course.syllabus_id = sy.id course.save! + send_wechat_create_class_notice user,course elsif Hash === course c = ::Course.new(course) c.tea_id = user.id @@ -91,6 +101,7 @@ class SyllabusesService c.is_public = 0 c.save! after_create_course(c, user) + send_wechat_create_class_notice user,c end end @@ -134,6 +145,7 @@ class SyllabusesService course.is_public = 0 course.save! after_create_course(course, user) + send_wechat_create_class_notice user,course end status = 0 end diff --git a/app/services/wechat_service.rb b/app/services/wechat_service.rb index 3169763c4..eefc127d4 100644 --- a/app/services/wechat_service.rb +++ b/app/services/wechat_service.rb @@ -139,6 +139,38 @@ class WechatService data end + def three_keys_template(openid, template_id, type, id, first, key1, key2, key3, remark="") + data = { + touser:openid, + template_id:template_id, + url:"#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/#{type}/#{id}", + topcolor:"#FF0000", + data:{ + first: { + value:first, + color:"#707070" + }, + keyword1:{ + value:key1, + color:"#707070" + }, + keyword2:{ + value:key2, + color:"#707070" + }, + keyword3:{ + value:key3, + 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, @@ -250,4 +282,17 @@ class WechatService end end + def create_class_notice(user_id, type, id, first, key1, key2, key3, remark="") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = three_keys_template uw.openid,Wechat.config.create_class_notice, type, id, 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 "[wechat_create_class_notice] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end + end \ No newline at end of file diff --git a/config/wechat.yml b/config/wechat.yml index d243ca44c..152135358 100644 --- a/config/wechat.yml +++ b/config/wechat.yml @@ -26,6 +26,7 @@ default: &default journal_notice: "uC1zAw4F2q6HTA3Pcj8VUO6wKKKiYFwnPJB4iXxpdoM" homework_message_notice: "tCf7teCVqc2vl2LZ_hppIdWmpg8yLcrI8XifxYePjps" class_notice: "MQ_mFupbXP-9jWbeHT3C5xqNBvPo8EIlNv4ULakSpJA" + create_class_notice: "2GtJJGzzNlNy2i0UrsjEDlvfSVIUXQfSo47stpcQAVw" production: <<: *default