socialforge/lib/trustie/sms/sms.rb

47 lines
1.5 KiB
Ruby
Raw Normal View History

2017-03-12 16:53:47 +08:00
#coding=utf-8
2017-03-24 16:05:52 +08:00
require 'net/https'
2017-03-12 16:53:47 +08:00
require 'uri'
module Trustie
module Sms
def self.send(opt={})
Rails.logger.info "#{opt[:mobile]} - #{opt[:code]}"
begin
o = sendYunpian(opt[:mobile], opt[:code])
if o["code"] != 0
2017-03-24 16:05:52 +08:00
Rails.logger.error "发送短信出错: #{o['code']}--#{o['msg']}"
2017-03-12 16:53:47 +08:00
end
return o["code"] == 0
rescue => e
Rails.logger.error "发送短信出错: #{e}"
return false
end
end
def self.sendYunpian(mobile, code)
#修改为您的apikey.可在官网http://www.yunpian.com)登录后用户中心首页看到
2017-03-24 16:05:52 +08:00
apikey = Redmine::Configuration['sms_apikey']
2017-03-12 16:53:47 +08:00
#指定模板发送接口HTTP地址
2017-03-24 16:05:52 +08:00
send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json')
2017-03-12 16:53:47 +08:00
params = {}
params['apikey'] = apikey
params['mobile'] = mobile
2017-03-24 16:05:52 +08:00
params['text'] = "【实训吧】" + code + "(手机验证码)。如非本人操作,请忽略。"
2017-03-12 16:53:47 +08:00
2017-03-24 16:05:52 +08:00
http = Net::HTTP.new(send_tpl_sms_uri.host, send_tpl_sms_uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
begin
request = Net::HTTP::Post.new(send_tpl_sms_uri.request_uri)
request.set_form_data(params)
request['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
response = http.start { |http| http.request(request) }
ActiveSupport::JSON.decode(response.body)
rescue =>err
return nil
end
2017-03-12 16:53:47 +08:00
end
end
end