shop_template_api/ARW.Common/AliyunMsgHelper.cs

66 lines
2.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Aliyun.OSS.Common;
using Aliyun.OSS;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core;
using ClientException = Aliyun.Acs.Core.Exceptions.ClientException;
using Infrastructure;
using System.Collections.Generic;
namespace ARW.Common
{
public static class AliyunMsgHelper
{
/// <summary>
/// 发送手机验证码
/// </summary>
/// <param name="phone"></param>
public static void SendPhoneMsgCode(string phone)
{
var accessKeyId = AppSettings.GetConfig("AARWYUN_MSG:accessKeyId");
var accessSecret = AppSettings.GetConfig("AARWYUN_MSG:accessSecret");
var signName = AppSettings.GetConfig("AARWYUN_MSG:signName");
var templateCode = AppSettings.GetConfig("AARWYUN_MSG:templateCode");
/*这里的*"cn-hangzhou":不需要更改, "<accessKeyId>"就是上面要求记录到本地的accessKeyId, "<accessSecret>:就是上面要求记录到本地的accessSecret"*/
IClientProfile profile = DefaultProfile.GetProfile("cn-hongkong", accessKeyId, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.aliyuncs.com";
request.Version = "2017-05-25";
request.Action = "SendSms";
// request.Protocol = ProtocolType.HTTP;1
request.AddQueryParameters("PhoneNumbers", phone);
request.AddQueryParameters("SignName", signName);
request.AddQueryParameters("TemplateCode", templateCode);
Dictionary<string, object> pairs = new Dictionary<string, object>();
pairs.Add("code", new Random().Next(100000, 1000000).ToString());
string json = Newtonsoft.Json.JsonConvert.SerializeObject(pairs);
request.AddQueryParameters("TemplateParam", json);
try
{
CommonResponse response = client.GetCommonResponse(request);
var status = response.HttpResponse;
}
catch (ServerException e)
{
Console.WriteLine(e);
}
}
}
}