41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
#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 |