#coding=utf-8 require 'net/https' 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['code']}--#{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 = Redmine::Configuration['sms_apikey'] #指定模板发送接口HTTP地址 send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json') params = {} params['apikey'] = apikey params['mobile'] = mobile params['text'] = "【实训吧】" + code + "(手机验证码)。如非本人操作,请忽略。" 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 end end end