socialforge/lib/trustie/sms/sms.rb

41 lines
1.2 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#coding=utf-8
require 'net/http'
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
Rails.logger.error "发送短信出错: #{o['msg']}"
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)登录后用户中心首页看到
apikey = '2affbf2ff83f9810512622ec83bccd4f'
#指定模板发送接口HTTP地址
send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/tpl_single_send.json')
params = {}
params['apikey'] = apikey
params['mobile'] = mobile
#指定模板发送
#设置模板ID如使用1号模板:【#company#】您的验证码是#code#
#设置对应的模板变量值
params['tpl_id'] = 1733594
params['tpl_value'] = URI::escape('#code#')+'='+URI::escape(code)
response = Net::HTTP.post_form(send_tpl_sms_uri,params)
ActiveSupport::JSON.decode(response.body)
end
end
end