签名请看: https://blog.csdn.net/tang05709/article/details/88366228
module Aliyun
class AliyunSmsAchieve < AliyunSign
def 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'.freeze
end
def 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_json
else
code_h = { code: code}.to_json
end
param[:TemplateParam] = code_h
end
key = @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 true
else
return false
end
end
'''
phone_numbers, 逗号隔开
'''
def set_phones(phone_numbers)
if phone_numbers.is_a?(Array)
phones = phone_numbers.join(',')
else
phones = phone_numbers
end
phones
end
'''
信息模板
'''
def service_types(type)
types = {
'register' => 'SMS_159575658',
'login' => 'SMS_159575660',
'repassword' => 'SMS_159575657',
'orderend' => 'SMS_159621878'
}
types.fetch(type)
end
end
end
短信发送倒计时
total_time = 60
send_cannot_click = () ->
obj = $('.account-form-body .send_code')
if total_time == 0
obj.attr('disabled', false)
obj.css('background', '#7ad4cb')
obj.html('发送验证码')
total_time = 60
else
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 false
phone = $('.account-form-body #user_phone').val()
# 需要先填写图片验证码
if $('.account-form-body #captcha').val() == ''
$('.account-form-body #captcha').parent().parent().addClass('active')
return false
code = $('.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()