本文主要是介绍ruby on rails aliyun 发送短信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
签名请看: https://blog.csdn.net/tang05709/article/details/88366228
module Aliyunclass AliyunSmsAchieve < AliyunSigndef initialize@access_key_id = Rails.configuration.application['ALIYUN_OSS_ACCESS_KEY_ID']@access_key_secret = Rails.configuration.application['ALIYUN_OSS_ACCESS_KEY_SECRET']@send_url = 'http://dysmsapi.aliyuncs.com'.freezeenddef send_sms(phone_numbers, service_type, code = nil)# PhoneNumbers 持对多个手机号码发送短信,手机号码之间以英文逗号(,)分隔。上限为1000个# SignName 短信签名名称# TemplateCode 短信模板ID# Action 系统规定参数。取值:SendSms# 返回 RequestId:请求ID, Message:状态码的描述,Code:请求状态码,BizId:发送回执ID,可根据该ID查询具体的发送状态param = {PhoneNumbers: set_phones(phone_numbers),SignName: '签名',TemplateCode: service_types(service_type),Action: 'SendSms',AccessKeyId: @access_key_id}if code.present? if service_type == 'orderend'code_h = { product: code}.to_jsonelsecode_h = { code: code}.to_jsonendparam[:TemplateParam] = code_hendkey = @access_key_secret + '&'options = create_sign_url("POST", param, key, 'sms')result = Faraday.post(@send_url, options)res = JSON.parse(result.body)if res["Code"] == 'OK'return trueelse return falseend end'''phone_numbers, 逗号隔开'''def set_phones(phone_numbers)if phone_numbers.is_a?(Array)phones = phone_numbers.join(',')else phones = phone_numbersendphonesend'''信息模板'''def service_types(type)types = {'register' => 'SMS_159575658','login' => 'SMS_159575660','repassword' => 'SMS_159575657','orderend' => 'SMS_159621878'}types.fetch(type)endend
end
短信发送倒计时
total_time = 60send_cannot_click = () ->obj = $('.account-form-body .send_code')if total_time == 0 obj.attr('disabled', false)obj.css('background', '#7ad4cb')obj.html('发送验证码')total_time = 60else total_time--obj.html("重新发送(#{total_time}s)")setTimeout send_cannot_click, 1000# 发送验证码$('.account-form-body .send_code').click -># 电话必须if $('.account-form-body #user_phone').val() == ''$('.account-form-body #user_phone').parent().parent().addClass('active')return falsephone = $('.account-form-body #user_phone').val()# 需要先填写图片验证码if $('.account-form-body #captcha').val() == ''$('.account-form-body #captcha').parent().parent().addClass('active')return falsecode = $('.account-form-body #captcha').val()$.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}})$.post('api/xxx',{phone: phone, _rucaptcha: code},(data) ->if data.status != 'success'$('.account-form-body #captcha').next('small').html(data.status))$(this).attr('disabled', true)$(this).css('background', '#2b7069')send_cannot_click()
这篇关于ruby on rails aliyun 发送短信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!