Commit 227edd8a by lra

短信模板化

parent f0be74f1
...@@ -57,7 +57,7 @@ options.merge!(attributes) ...@@ -57,7 +57,7 @@ options.merge!(attributes)
begin begin
puts options.inspect puts options.inspect
puts SmsSender.new(options).send("#{options[:method]}Send", options[:phone], options[:content]) puts SmsSender.new(options).send("#{options[:method]}Send", options[:phone])
rescue => e rescue => e
puts 'Uncaught exception' puts 'Uncaught exception'
puts e.message puts e.message
......
...@@ -30,7 +30,7 @@ class SmsSender ...@@ -30,7 +30,7 @@ class SmsSender
SOURCES = %w(zucp taobao) SOURCES = %w(zucp taobao)
attr_accessor :source attr_accessor :source, :template_code
def initialize(options = {}) def initialize(options = {})
options[:source] = redis.get('sms:source') || SOURCES.first if options[:source].blank? options[:source] = redis.get('sms:source') || SOURCES.first if options[:source].blank?
...@@ -41,32 +41,46 @@ class SmsSender ...@@ -41,32 +41,46 @@ class SmsSender
end end
options = HashWithIndifferentAccess.new(options) options = HashWithIndifferentAccess.new(options)
@source = "Sms/#{options[:source]}".camelize.constantize.new(options) if options[:source].eql? 'zucp'
end
if options[:template_code].present?
templates = backend(I18n.locale => {'sms_zucp_templates' => load_config[:zucp][:templates]})
I18n.backend = I18n::Backend::Chain.new(templates)
options.merge!(content: I18n.t("sms_zucp_templates.#{options[:template_code].to_s.gsub('.', '_')}", options[:content_params]))
end
elsif options[:source].eql? 'taobao'
if options[:template_code].present?
options.merge!(sms_template_code: load_config[:taobao][:templates][options[:template_code].to_s.gsub('.', '_')])
options.merge!(sms_param: options[:content_params])
end
def content_must_exits?
case source
when 'zucp'
true
else
false
end end
@source = "Sms/#{options[:source]}".camelize.constantize.new(options)
end end
def singleSend(number, content = nil) def singleSend(number)
return unless @source return unless @source
return unless number.present? return unless number.present?
call = content_must_exits? ? content.present? : true @source.singleSend(number)
@source.singleSend(number, content) if call
end end
def batchSend(numbers, content = nil) def batchSend(numbers)
return unless @source return unless @source
_numbers = numbers.is_a?(String) ? numbers.split(',') : (numbers.is_a?(Array) ? numbers : []) _numbers = numbers.is_a?(String) ? numbers.split(',') : (numbers.is_a?(Array) ? numbers : [])
_numbers = _numbers.compact.uniq _numbers = _numbers.compact.uniq
return unless _numbers.present? @source.batchSend(_numbers)
call = content_must_exits? ? content.present? : true end
@source.batchSend(_numbers, content) if call
protected
def backend(translations)
backend = I18n::Backend::Simple.new
translations.each { |locale, data| backend.store_translations(locale, data) }
backend
end end
end end
......
...@@ -12,18 +12,18 @@ module Sms ...@@ -12,18 +12,18 @@ module Sms
self.app_key = options[:app_key] self.app_key = options[:app_key]
self.sms_free_sign_name = options[:sms_free_sign_name] self.sms_free_sign_name = options[:sms_free_sign_name]
self.sms_template_code = options[:sms_template_code] self.sms_template_code = options[:sms_template_code]
self.sms_param = options[:sms_param] self.sms_param = options[:sms_param] || {}
end end
def singleSend(number, content = nil) def singleSend(number)
send(number.split, content) send(number.split)
end end
def batchSend(numbers, content = nil) def batchSend(numbers)
send(numbers, content) send(numbers)
end end
def defaultParams(numbers, content = nil) def defaultParams(numbers)
{ {
method: 'alibaba.aliqin.fc.sms.num.send', method: 'alibaba.aliqin.fc.sms.num.send',
app_key: self.app_key, app_key: self.app_key,
...@@ -34,7 +34,7 @@ module Sms ...@@ -34,7 +34,7 @@ module Sms
sms_type: 'normal', sms_type: 'normal',
sms_free_sign_name: self.sms_free_sign_name, sms_free_sign_name: self.sms_free_sign_name,
rec_num: numbers.join(','), rec_num: numbers.join(','),
sms_param: (self.sms_param || {content: content}).to_json, sms_param: self.sms_param.to_json,
sms_template_code: self.sms_template_code sms_template_code: self.sms_template_code
} }
end end
...@@ -44,8 +44,8 @@ module Sms ...@@ -44,8 +44,8 @@ module Sms
Digest::MD5.hexdigest("#{app_secret.to_s}#{str}#{app_secret.to_s}").upcase Digest::MD5.hexdigest("#{app_secret.to_s}#{str}#{app_secret.to_s}").upcase
end end
def send(numbers, content) def send(numbers)
params = defaultParams(numbers, content) params = defaultParams(numbers)
params = params.merge(sign: generate_sign(params)) params = params.merge(sign: generate_sign(params))
data = request_api(params) data = request_api(params)
data = JSON.parse(data.body) data = JSON.parse(data.body)
......
...@@ -6,12 +6,13 @@ module Sms ...@@ -6,12 +6,13 @@ module Sms
class Zucp class Zucp
include REXML include REXML
attr_accessor :messages, :sn, :pwd, :suffix attr_accessor :messages, :sn, :pwd, :suffix, :content
def initialize(options = {}) def initialize(options = {})
self.sn = options[:sn] self.sn = options[:sn]
self.pwd = options[:pwd] self.pwd = options[:pwd]
self.suffix = options[:suffix] self.suffix = options[:suffix]
self.content = options[:content]
self.messages = { self.messages = {
1 => "没有需要取得的数据 取用户回复就出现1的返回值,表示没有回复数据", 1 => "没有需要取得的数据 取用户回复就出现1的返回值,表示没有回复数据",
-1 =>"重复注册 多次点击“注册”按钮或注册方法(Register)的“调用”按钮", -1 =>"重复注册 多次点击“注册”按钮或注册方法(Register)的“调用”按钮",
...@@ -38,8 +39,8 @@ module Sms ...@@ -38,8 +39,8 @@ module Sms
end end
# 批量发送 numbers是个数组 # 批量发送 numbers是个数组
def batchSend(numbers, content) def batchSend(numbers)
params = defaultParams( numbers, content) params = defaultParams(numbers)
data = send(params) data = send(params)
puts "data ************* #{data}" puts "data ************* #{data}"
# VcoolineLog::SmsApi.add("sms api batchSend data: #{data}") # VcoolineLog::SmsApi.add("sms api batchSend data: #{data}")
...@@ -54,8 +55,8 @@ module Sms ...@@ -54,8 +55,8 @@ module Sms
end end
# 单次发送(用户名,密码,接收方号码,内容) # 单次发送(用户名,密码,接收方号码,内容)
def singleSend(number, content) def singleSend(number)
params = defaultParams(number.split, content) params = defaultParams(number.split)
data = send(params) data = send(params)
# VcoolineLog::SmsApi.add("sms api singleSend data: #{data}") # VcoolineLog::SmsApi.add("sms api singleSend data: #{data}")
body = parseResBody data.body body = parseResBody data.body
...@@ -88,9 +89,9 @@ module Sms ...@@ -88,9 +89,9 @@ module Sms
root.text root.text
end end
def defaultParams(numbers, content) def defaultParams(numbers)
params = {} params = {}
content = content + suffix.to_s content = self.content + suffix.to_s
params["sn"] = sn params["sn"] = sn
params["pwd"] = md5password(sn, pwd) params["pwd"] = md5password(sn, pwd)
params["mobile"] = numbers.join(',') params["mobile"] = numbers.join(',')
......
...@@ -3,12 +3,19 @@ defaults: &defaults ...@@ -3,12 +3,19 @@ defaults: &defaults
sn: xxxx sn: xxxx
pwd: xxxx pwd: xxxx
suffix: xxxx suffix: xxxx
templates:
devise_supplier_applies_otp_code_confirm: "验证码:%{code},有效期5分钟~ 如非本人操作,请忽略本短信"
devise_passwords_otp_code_confirm: "验证码:%{code},有效期5分钟~ 如非本人操作,请忽略本短信"
devise_confirmations_otp_code_confirm: "验证码:%{code},有效期5分钟,请尽快完成注册哦~ 如非本人操作,请忽略本短信"
taobao: taobao:
app_secret: xxxx app_secret: xxxx
app_key: xxxx app_key: xxxx
sms_free_sign_name: xxxx sms_free_sign_name: xxxx
sms_template_code: xxxx templates:
devise_supplier_applies_otp_code_confirm: xxxx
devise_passwords_otp_code_confirm: xxxx
devise_confirmations_otp_code_confirm: xxxx
development: development:
<<: *defaults <<: *defaults
......
...@@ -23,6 +23,7 @@ Gem::Specification.new do |spec| ...@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
# end # end
spec.add_dependency 'redis', '~> 3.2', '>= 3.2.1' spec.add_dependency 'redis', '~> 3.2', '>= 3.2.1'
spec.add_dependency 'i18n'
spec.add_development_dependency "bundler", "~> 1.9" spec.add_development_dependency "bundler", "~> 1.9"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
......
...@@ -3,7 +3,7 @@ class TaobaoTest < Minitest::Test ...@@ -3,7 +3,7 @@ class TaobaoTest < Minitest::Test
describe 'Sms::Taobao test' do describe 'Sms::Taobao test' do
before do before do
@options = {source: "taobao", app_secret: '5aff003ac3c7243da65adef1e7c9c363', app_key: '23272925', sms_free_sign_name: '爱客验证码', sms_template_code: 'SMS_2575137', sms_param: {code: 'xxx', product: 'ddd'}, phone: '13262902619'} @options = {source: "taobao", app_secret: '5aff003ac3c7243da65adef1e7c9c363', app_key: '23272925', sms_free_sign_name: '爱客', sms_template_code: 'SMS_2575137', sms_param: {code: 'xxx', product: 'ddd'}, phone: '13262902619'}
end end
it 'SmsSender singleSend' do it 'SmsSender singleSend' do
......
...@@ -3,14 +3,14 @@ class ZucpTest < Minitest::Test ...@@ -3,14 +3,14 @@ class ZucpTest < Minitest::Test
describe 'Sms::Zucp test' do describe 'Sms::Zucp test' do
before do before do
@options = {source: "zucp", phone: "13262902619", content: Time.now.to_i.to_s, sn: "SDK-WSS-010-059256", pwd: "1234567", suffix: '[test]'} @options = {source: "zucp", phone: "13262902619", content: Time.now.to_i.to_s, sn: "SDK-WSS-010-05925", pwd: "123456", suffix: '[test]'}
end end
it 'SmsSender singleSend' do it 'SmsSender singleSend' do
obj = SmsSender.new(@options) obj = SmsSender.new(@options)
assert_instance_of Sms::Zucp, obj.source assert_instance_of Sms::Zucp, obj.source
result = obj.singleSend(@options[:phone], @options[:content]) result = obj.singleSend(@options[:phone])
assert result.to_i > 0 assert result.to_i > 0
end end
...@@ -18,7 +18,7 @@ class ZucpTest < Minitest::Test ...@@ -18,7 +18,7 @@ class ZucpTest < Minitest::Test
obj = SmsSender.new(@options) obj = SmsSender.new(@options)
assert_instance_of Sms::Zucp, obj.source assert_instance_of Sms::Zucp, obj.source
result = obj.batchSend(@options[:phone], @options[:content]) result = obj.batchSend(@options[:phone])
assert result.to_i > 0 assert result.to_i > 0
end end
...@@ -27,7 +27,7 @@ class ZucpTest < Minitest::Test ...@@ -27,7 +27,7 @@ class ZucpTest < Minitest::Test
assert obj.sn.present? assert obj.sn.present?
assert obj.pwd.present? assert obj.pwd.present?
result = obj.singleSend(@options[:phone], @options[:content]) result = obj.singleSend(@options[:phone])
assert result.to_i > 0 assert result.to_i > 0
end end
...@@ -36,7 +36,7 @@ class ZucpTest < Minitest::Test ...@@ -36,7 +36,7 @@ class ZucpTest < Minitest::Test
assert obj.sn.present? assert obj.sn.present?
assert obj.pwd.present? assert obj.pwd.present?
result = obj.batchSend(@options[:phone].split, @options[:content]) result = obj.batchSend(@options[:phone].split)
assert result.to_i > 0 assert result.to_i > 0
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment