Commit 227edd8a by lra

短信模板化

parent f0be74f1
......@@ -57,7 +57,7 @@ options.merge!(attributes)
begin
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
puts 'Uncaught exception'
puts e.message
......
......@@ -30,7 +30,7 @@ class SmsSender
SOURCES = %w(zucp taobao)
attr_accessor :source
attr_accessor :source, :template_code
def initialize(options = {})
options[:source] = redis.get('sms:source') || SOURCES.first if options[:source].blank?
......@@ -41,32 +41,46 @@ class SmsSender
end
options = HashWithIndifferentAccess.new(options)
@source = "Sms/#{options[:source]}".camelize.constantize.new(options)
end
if options[:source].eql? 'zucp'
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
@source = "Sms/#{options[:source]}".camelize.constantize.new(options)
end
def singleSend(number, content = nil)
def singleSend(number)
return unless @source
return unless number.present?
call = content_must_exits? ? content.present? : true
@source.singleSend(number, content) if call
@source.singleSend(number)
end
def batchSend(numbers, content = nil)
def batchSend(numbers)
return unless @source
_numbers = numbers.is_a?(String) ? numbers.split(',') : (numbers.is_a?(Array) ? numbers : [])
_numbers = _numbers.compact.uniq
return unless _numbers.present?
call = content_must_exits? ? content.present? : true
@source.batchSend(_numbers, content) if call
@source.batchSend(_numbers)
end
protected
def backend(translations)
backend = I18n::Backend::Simple.new
translations.each { |locale, data| backend.store_translations(locale, data) }
backend
end
end
......
......@@ -12,18 +12,18 @@ module Sms
self.app_key = options[:app_key]
self.sms_free_sign_name = options[:sms_free_sign_name]
self.sms_template_code = options[:sms_template_code]
self.sms_param = options[:sms_param]
self.sms_param = options[:sms_param] || {}
end
def singleSend(number, content = nil)
send(number.split, content)
def singleSend(number)
send(number.split)
end
def batchSend(numbers, content = nil)
send(numbers, content)
def batchSend(numbers)
send(numbers)
end
def defaultParams(numbers, content = nil)
def defaultParams(numbers)
{
method: 'alibaba.aliqin.fc.sms.num.send',
app_key: self.app_key,
......@@ -34,7 +34,7 @@ module Sms
sms_type: 'normal',
sms_free_sign_name: self.sms_free_sign_name,
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
}
end
......@@ -44,8 +44,8 @@ module Sms
Digest::MD5.hexdigest("#{app_secret.to_s}#{str}#{app_secret.to_s}").upcase
end
def send(numbers, content)
params = defaultParams(numbers, content)
def send(numbers)
params = defaultParams(numbers)
params = params.merge(sign: generate_sign(params))
data = request_api(params)
data = JSON.parse(data.body)
......
......@@ -6,12 +6,13 @@ module Sms
class Zucp
include REXML
attr_accessor :messages, :sn, :pwd, :suffix
attr_accessor :messages, :sn, :pwd, :suffix, :content
def initialize(options = {})
self.sn = options[:sn]
self.pwd = options[:pwd]
self.suffix = options[:suffix]
self.content = options[:content]
self.messages = {
1 => "没有需要取得的数据 取用户回复就出现1的返回值,表示没有回复数据",
-1 =>"重复注册 多次点击“注册”按钮或注册方法(Register)的“调用”按钮",
......@@ -38,8 +39,8 @@ module Sms
end
# 批量发送 numbers是个数组
def batchSend(numbers, content)
params = defaultParams( numbers, content)
def batchSend(numbers)
params = defaultParams(numbers)
data = send(params)
puts "data ************* #{data}"
# VcoolineLog::SmsApi.add("sms api batchSend data: #{data}")
......@@ -54,8 +55,8 @@ module Sms
end
# 单次发送(用户名,密码,接收方号码,内容)
def singleSend(number, content)
params = defaultParams(number.split, content)
def singleSend(number)
params = defaultParams(number.split)
data = send(params)
# VcoolineLog::SmsApi.add("sms api singleSend data: #{data}")
body = parseResBody data.body
......@@ -88,9 +89,9 @@ module Sms
root.text
end
def defaultParams(numbers, content)
def defaultParams(numbers)
params = {}
content = content + suffix.to_s
content = self.content + suffix.to_s
params["sn"] = sn
params["pwd"] = md5password(sn, pwd)
params["mobile"] = numbers.join(',')
......
......@@ -3,12 +3,19 @@ defaults: &defaults
sn: xxxx
pwd: 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:
app_secret: xxxx
app_key: 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:
<<: *defaults
......
......@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
# end
spec.add_dependency 'redis', '~> 3.2', '>= 3.2.1'
spec.add_dependency 'i18n'
spec.add_development_dependency "bundler", "~> 1.9"
spec.add_development_dependency "rake", "~> 10.0"
......
......@@ -3,7 +3,7 @@ class TaobaoTest < Minitest::Test
describe 'Sms::Taobao test' 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
it 'SmsSender singleSend' do
......
......@@ -3,14 +3,14 @@ class ZucpTest < Minitest::Test
describe 'Sms::Zucp test' 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
it 'SmsSender singleSend' do
obj = SmsSender.new(@options)
assert_instance_of Sms::Zucp, obj.source
result = obj.singleSend(@options[:phone], @options[:content])
result = obj.singleSend(@options[:phone])
assert result.to_i > 0
end
......@@ -18,7 +18,7 @@ class ZucpTest < Minitest::Test
obj = SmsSender.new(@options)
assert_instance_of Sms::Zucp, obj.source
result = obj.batchSend(@options[:phone], @options[:content])
result = obj.batchSend(@options[:phone])
assert result.to_i > 0
end
......@@ -27,7 +27,7 @@ class ZucpTest < Minitest::Test
assert obj.sn.present?
assert obj.pwd.present?
result = obj.singleSend(@options[:phone], @options[:content])
result = obj.singleSend(@options[:phone])
assert result.to_i > 0
end
......@@ -36,7 +36,7 @@ class ZucpTest < Minitest::Test
assert obj.sn.present?
assert obj.pwd.present?
result = obj.batchSend(@options[:phone].split, @options[:content])
result = obj.batchSend(@options[:phone].split)
assert result.to_i > 0
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