* Utils.cs:
* SMSTest.cs: * LCCaptchaClient.cs: * CaptchaTest.cs: * Storage-Unity.csproj: * LCSMSClient.cs: chore: 支持验证码和短信
parent
0e6f4abb98
commit
94f1c9feca
|
@ -117,6 +117,12 @@
|
|||
<Compile Include="..\Storage\Internal\Query\LCCompositionalCondition.cs">
|
||||
<Link>Internal\Query\LCCompositionalCondition.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Storage\LCCaptchaClient.cs">
|
||||
<Link>LCCaptchaClient.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Storage\LCSMSClient.cs">
|
||||
<Link>LCSMSClient.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
using NUnit.Framework;
|
||||
using System.Threading.Tasks;
|
||||
using LeanCloud;
|
||||
using LeanCloud.Storage;
|
||||
|
||||
using static NUnit.Framework.TestContext;
|
||||
|
||||
namespace Storage.Test {
|
||||
public class CaptchaTest {
|
||||
[SetUp]
|
||||
public void SetUp() {
|
||||
LCLogger.LogDelegate += Utils.Print;
|
||||
LCApplication.Initialize(Utils.AppId, Utils.AppKey, Utils.AppServer);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown() {
|
||||
LCLogger.LogDelegate -= Utils.Print;
|
||||
}
|
||||
|
||||
//[Test]
|
||||
public async Task Request() {
|
||||
LCCapture captcha = await LCCaptchaClient.RequestCaptcha();
|
||||
WriteLine($"url: {captcha.Url}");
|
||||
WriteLine($"token: {captcha.Token}");
|
||||
Assert.NotNull(captcha);
|
||||
Assert.NotNull(captcha.Url);
|
||||
Assert.NotNull(captcha.Token);
|
||||
}
|
||||
|
||||
//[Test]
|
||||
public async Task Verify() {
|
||||
await LCCaptchaClient.VerifyCaptcha("on2r", "1TUDkEMu");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using LeanCloud;
|
||||
using LeanCloud.Storage;
|
||||
|
||||
namespace Storage.Test {
|
||||
public class SMSTest {
|
||||
[SetUp]
|
||||
public void SetUp() {
|
||||
LCLogger.LogDelegate += Utils.Print;
|
||||
LCApplication.Initialize(Utils.AppId, Utils.AppKey, Utils.AppServer);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown() {
|
||||
LCLogger.LogDelegate -= Utils.Print;
|
||||
}
|
||||
|
||||
//[Test]
|
||||
public async Task RequestSMS() {
|
||||
await LCSMSClient.RequestSMSCode("15101006007",
|
||||
template: "test_template",
|
||||
signature: "flutter-test",
|
||||
variables: new Dictionary<string, object> {
|
||||
{ "k1", "v1" }
|
||||
});
|
||||
}
|
||||
|
||||
//[Test]
|
||||
public async Task RequestVoice() {
|
||||
await LCSMSClient.RequestVoiceCode("+8615101006007");
|
||||
}
|
||||
|
||||
//[Test]
|
||||
public async Task Verify() {
|
||||
await LCSMSClient.VerifyMobilePhone("15101006007", "");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,10 @@ using LeanCloud;
|
|||
|
||||
namespace Storage.Test {
|
||||
public static class Utils {
|
||||
internal const string AppId = "ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz";
|
||||
internal const string AppKey = "NUKmuRbdAhg1vrb2wexYo1jo";
|
||||
internal const string AppServer = "https://ikggdre2.lc-cn-n1-shared.com";
|
||||
|
||||
internal static void Print(LCLogLevel level, string info) {
|
||||
switch (level) {
|
||||
case LCLogLevel.Debug:
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeanCloud.Storage {
|
||||
/// <summary>
|
||||
/// 验证码
|
||||
/// </summary>
|
||||
public class LCCapture {
|
||||
public string Url {
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string Token {
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证码工具类
|
||||
/// </summary>
|
||||
public static class LCCaptchaClient {
|
||||
/// <summary>
|
||||
/// 请求验证码
|
||||
/// </summary>
|
||||
/// <param name="width">验证码图片宽度</param>
|
||||
/// <param name="height">验证码图片高度</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<LCCapture> RequestCaptcha(int width = 82,
|
||||
int height = 39) {
|
||||
string path = "requestCaptcha";
|
||||
Dictionary<string, object> queryParams = new Dictionary<string, object> {
|
||||
{ "width", width },
|
||||
{ "height", height }
|
||||
};
|
||||
Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path, queryParams: queryParams);
|
||||
return new LCCapture {
|
||||
Url = response["captcha_url"] as string,
|
||||
Token = response["captcha_token"] as string
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task VerifyCaptcha(string code,
|
||||
string token) {
|
||||
if (string.IsNullOrEmpty(code)) {
|
||||
throw new ArgumentNullException(nameof(code));
|
||||
}
|
||||
if (string.IsNullOrEmpty(token)) {
|
||||
throw new ArgumentNullException(nameof(token));
|
||||
}
|
||||
|
||||
string path = "verifyCaptcha";
|
||||
Dictionary<string, object> data = new Dictionary<string, object> {
|
||||
{ "captcha_code", code },
|
||||
{ "captcha_token", token }
|
||||
};
|
||||
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeanCloud.Storage {
|
||||
/// <summary>
|
||||
/// 短信工具类
|
||||
/// </summary>
|
||||
public static class LCSMSClient {
|
||||
/// <summary>
|
||||
/// 请求短信验证码
|
||||
/// </summary>
|
||||
/// <param name="mobile"></param>
|
||||
/// <param name="template"></param>
|
||||
/// <param name="signature"></param>
|
||||
/// <param name="captchaToken"></param>
|
||||
/// <param name="variables"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RequestSMSCode(string mobile,
|
||||
string template = null,
|
||||
string signature = null,
|
||||
string captchaToken = null,
|
||||
Dictionary<string, object> variables = null) {
|
||||
if (string.IsNullOrEmpty(mobile)) {
|
||||
throw new ArgumentNullException(nameof(mobile));
|
||||
}
|
||||
|
||||
string path = "requestSmsCode";
|
||||
Dictionary<string, object> data = new Dictionary<string, object> {
|
||||
{ "mobilePhoneNumber", mobile }
|
||||
};
|
||||
if (!string.IsNullOrEmpty(template)) {
|
||||
data["template"] = template;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(signature)) {
|
||||
data["sign"] = signature;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(captchaToken)) {
|
||||
data["validate_token"] = captchaToken;
|
||||
}
|
||||
if (variables != null) {
|
||||
foreach (KeyValuePair<string, object> kv in variables) {
|
||||
data[kv.Key] = kv.Value;
|
||||
}
|
||||
}
|
||||
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 请求语音验证码
|
||||
/// </summary>
|
||||
/// <param name="mobile"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RequestVoiceCode(string mobile) {
|
||||
string path = "requestSmsCode";
|
||||
Dictionary<string, object> data = new Dictionary<string, object> {
|
||||
{ "mobilePhoneNumber", mobile },
|
||||
{ "smsType", "voice" }
|
||||
};
|
||||
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证手机号
|
||||
/// </summary>
|
||||
/// <param name="mobile"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task VerifyMobilePhone(string mobile, string code) {
|
||||
string path = $"verifySmsCode/{code}";
|
||||
Dictionary<string, object> data = new Dictionary<string, object> {
|
||||
{ "mobilePhoneNumber", mobile }
|
||||
};
|
||||
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue