增加ucloud短信发送通道

This commit is contained in:
“xxq250” 2022-11-15 10:03:01 +08:00
parent e0588b7863
commit 518f373b88
2 changed files with 123 additions and 2 deletions

View File

@ -103,8 +103,10 @@ class ApplicationController < ActionController::Base
when 1, 2, 4, 9
# 手机类型的发送
sigle_para = {phone: value}
status = Gitlink::Sms.send(mobile: value, code: code)
tip_exception(-2, code_msg(status)) if status != 0
# status = Gitlink::Sms.send(mobile: value, code: code)
# tip_exception(-2, code_msg(status)) if status != 0
status = Sms::UcloudService.call(value, code)
tip_exception(-2, ucloud_code_msg(status)) if status != 0
when 8, 3, 5
# 邮箱类型的发送
sigle_para = {email: value}
@ -149,6 +151,27 @@ class ApplicationController < ActionController::Base
end
end
def ucloud_code_msg status
case status
when 0
"验证码已经发送到您的手机,请注意查收"
when 171
"API签名错误"
when 18014
"无效手机号码"
when 18017
"无效模板"
when 18018
"短信模板参数与短信模板不匹配"
when 18023
"短信内容中含有运营商拦截的关键词"
when 18033
"变量内容不符合规范"
else
"错误码#{status}"
end
end
def validate_type(object_type)
normal_status(2, "参数") if params.has_key?(:sort_type) && !SORT_TYPE.include?(params[:sort_type].strip)
end

View File

@ -0,0 +1,98 @@
class Sms::UcloudService < ApplicationService
attr_reader :phone, :code
def initialize(phone, code)
@phone = phone
@code = code
end
def call
public_key = EduSetting.get("ucloud_public_key") || "4Z7QYDY0SumplMtmNmd9PERgPPFiMpR1R"
private_key = EduSetting.get("ucloud_private_key") || "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtm2d"
project_id = "org-3ozbh2"
sign_params = {
"Action" => "SendUSMSMessage",
"ProjectId" => project_id,
"TemplateId" => "UTA221114S2MGTY",
"PublicKey" => public_key,
"PhoneNumbers.0" => @phone,
"TemplateParams.0" => "#{@code}",
"SigContent" => "GitLink确实开源"
}
sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
# Rails.logger.info("create_signature=========#{sequence}#{private_key}")
req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
uri = URI("https://api.ucloud.cn")
uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
# Rails.logger.info("uri.query=========#{uri.query}")
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
req = Net::HTTP::Get.new uri.request_uri
response = http.request(req)
# Rails.logger.info("ucloud sms response.body=========#{response.body}")
result = ActiveSupport::JSON.decode(response.body)
result['RetCode']
end
end
def send_by_params(opt={})
public_key = "4Z7QYDY0SumplMtmNmd9PERgPPFiMpRR"
private_key = "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtmd"
project_id = "org-3ozbh2"
sign_params = {
"Action" => "SendUSMSMessage",
"ProjectId" => project_id,
"TemplateId" => "#{opt[:TemplateId]}",
"PublicKey" => public_key,
"PhoneNumbers.0" => "#{opt[:PhoneNumbers]}",
"TemplateParams.0" => "#{opt[:TemplateParams]}",
"SigContent" => "GitLink确实开源"
}
sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
# Rails.logger.info("create_signature=========#{sequence}#{private_key}")
req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
uri = URI("https://api.ucloud.cn")
uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
# Rails.logger.info("uri.query=========#{uri.query}")
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
req = Net::HTTP::Get.new uri.request_uri
response = http.request(req)
# Rails.logger.info("ucloud sms response.body=========#{response.body}")
ActiveSupport::JSON.decode(response.body)
end
end
def send_sms(template_id)
end
def GetProjectList
public_key = "4Z7QYDY0SumplMtmNmd9PERgPPFiMpRR"
private_key = "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtmd"
sign_params = {
"Action" => "GetProjectList",
"PublicKey" => public_key
}
sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
Rails.logger.info("create_signature=========#{sequence}#{private_key}")
req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
uri = URI("https://api.ucloud.cn")
uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
Rails.logger.info("uri.query=========#{uri.query}")
Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
req = Net::HTTP::Get.new uri.request_uri
response = http.request(req)
Rails.logger.info("ucloud sms response.body=========#{response.body}")
response.body
end
end
end