From 9427db3e5a9c1856e8519506fada75c7dc68248b Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 31 May 2022 16:34:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/notices_controller.rb | 14 ++++++- app/jobs/send_template_message_job.rb | 11 +++++ app/models/message_template/custom_tip.rb | 51 +++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 app/models/message_template/custom_tip.rb diff --git a/app/controllers/notices_controller.rb b/app/controllers/notices_controller.rb index bf7eb21b1..7b67412e7 100644 --- a/app/controllers/notices_controller.rb +++ b/app/controllers/notices_controller.rb @@ -1,7 +1,7 @@ class NoticesController < ApplicationController def create - tip_exception("参数有误") if params["source"].blank? + return tip_exception("参数有误") if params["source"].blank? user_id = params[:user_id] if params["source"] == "CompetitionBegin" @@ -13,9 +13,21 @@ class NoticesController < ApplicationController elsif params["source"] == "CompetitionReview" competition_id = params[:competition_id] SendTemplateMessageJob.perform_later('CompetitionReview', user_id, competition_id) + elsif params["source"] == "CustomTip" + users_id = params[:users_id] + props = params[:props].to_unsafe_hash + return tip_exception("参数有误") unless props.is_a?(Hash) && users_id.is_a?(Array) + template_id = params[:template_id] + SendTemplateMessageJob.perform_later('CustomTip', users_id, template_id, props) else tip_exception("#{params["source"]}未配置") end render_ok end + + + private + def params_props + params.require(:notice).permit(:props) + end end diff --git a/app/jobs/send_template_message_job.rb b/app/jobs/send_template_message_job.rb index 9cc819a11..bec3f7b08 100644 --- a/app/jobs/send_template_message_job.rb +++ b/app/jobs/send_template_message_job.rb @@ -4,6 +4,17 @@ class SendTemplateMessageJob < ApplicationJob def perform(source, *args) Rails.logger.info "SendTemplateMessageJob [args] #{args}" case source + when 'CustomTip' + receivers_id, template_id, props = args[0], args[1], args[2] + template = MessageTemplate.find_by_id(template_id) + return unless template.present? + receivers = User.where(id: receivers_id) + receivers_string, content, notification_url = MessageTemplate::CustomTip.get_message_content(receivers, template, props) + Notice::Write::CreateService.call(receivers_string, content, notification_url, source, {receivers_id: receivers_id, template_id: template_id, props: props}) + receivers.find_each do |receiver| + receivers_email_string, email_title, email_content = MessageTemplate::CustomTip.get_email_message_content(receiver, template, props) + Notice::Write::EmailCreateService.call(receivers_email_string, email_title, email_content) + end when 'FollowTip' watcher_id = args[0] watcher = Watcher.find_by_id(watcher_id) diff --git a/app/models/message_template/custom_tip.rb b/app/models/message_template/custom_tip.rb new file mode 100644 index 000000000..6c5a3345d --- /dev/null +++ b/app/models/message_template/custom_tip.rb @@ -0,0 +1,51 @@ +# == Schema Information +# +# Table name: message_templates +# +# id :integer not null, primary key +# type :string(255) +# sys_notice :text(65535) +# email :text(65535) +# created_at :datetime not null +# updated_at :datetime not null +# notification_url :string(255) +# email_title :string(255) +# + +# 统一模板( + +class MessageTemplate::CustomTip < MessageTemplate + + # MessageTemplate::CustomTip.get_message_content(User.where(login: 'yystopf'), "hahah") + def self.get_message_content(receivers, template, props={}) + return '', '', '' if receivers.blank? || template.blank? + content = template.sys_notice + notification_url = template.notification_url + props.each do |k, v| + content.gsub!("{#{k}}", v) + notification_url.gsub!("{#{k}}", v) + end + return receivers_string(receivers), content, notification_url + rescue => e + Rails.logger.info("MessageTemplate::CustomTip.get_message_content [ERROR] #{e}") + return '', '', '' + end + + def self.get_email_message_content(receiver, template, props={}) + return '', '', '' if receiver.blank? || template.blank? + title = template.email_title + content = template.email + props.each do |k, v| + title.gsub!("{#{k}}", v) + content.gsub!("{#{k}}", v) + end + content.gsub!('{receiver}', receiver&.real_name) + title.gsub!('{platform}', PLATFORM) + content.gsub!('{platform}', PLATFORM) + + return receiver&.mail, title, content + rescue => e + Rails.logger.info("MessageTemplate::CustomTip.get_email_message_content [ERROR] #{e}") + return '', '', '' + end +end \ No newline at end of file