一. server层
public Result Verification(JObject jObject)
{
var telephone = Convert.ToString(jObject["Telephone"]);
if (string.IsNullOrWhiteSpace(telephone))
{
return Result.GetError("请输入手机号");
}
var randomCode = RandomHelper.GetValue(RandomType.Num, 6);
CacheHelper.Set(telephone, randomCode, DateTime.Now.AddMinutes(1));
var ret = SMSHelper.SendSmsVerifyCode(telephone, randomCode);
if (ret != "success")
{
return Result.GetError(ret);
}
else
{
return Result.GetSuccess();
}
}
二. 调用帮助类
public static string GetValue(RandomType type, int length)
{
int minValue = 0;
int maxValue = 0;
RandomType randomType = RandomType.Num | RandomType.AtoZ;
RandomType randomType2 = RandomType.AtoZ | RandomType.atoz;
RandomType randomType3 = RandomType.Num | RandomType.atoz;
if ((type & (RandomType.Num | RandomType.AtoZ | RandomType.atoz)) == (RandomType.Num | RandomType.AtoZ | RandomType.atoz))
{
maxValue = 62;
}
else if ((type & (RandomType.Num | RandomType.AtoZ)) == (RandomType.Num | RandomType.AtoZ))
{
maxValue = 26;
}
else if ((type & (RandomType.Num | RandomType.atoz)) == (RandomType.Num | RandomType.atoz))
{
minValue = 36;
maxValue = 72;
}
else if ((type & (RandomType.AtoZ | RandomType.atoz)) == (RandomType.AtoZ | RandomType.atoz))
{
minValue = 10;
maxValue = 62;
}
else if ((type & RandomType.Num) == RandomType.Num)
{
maxValue = 10;
}
else if ((type & RandomType.atoz) == RandomType.atoz)
{
minValue = 36;
maxValue = 62;
}
else if ((type & RandomType.AtoZ) == RandomType.AtoZ)
{
minValue = 10;
maxValue = 36;
}
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random(GetRandomSeed());
for (int i = 0; i < length; i++)
{
stringBuilder.Append(m_Pool[random.Next(minValue, maxValue)]);
}
return stringBuilder.ToString();
}
public static void Set(string name, object value, DateTime absoluteExpiration)
{
Set(name, value, absoluteExpiration, TimeSpan.Zero);
}
public static class SMSHelper
{
private const string accountSid = "容联云";
private const string accountToken = "容联云";
private const string appId = "容联云";
/// <summary>
/// 发送短信验证码
/// </summary>
/// <param name="telephone"></param>
/// <param name="code"></param>
/// <returns></returns>
public static string SendSmsVerifyCode(string telephone, string code)
{
return Send(telephone, new string[] { code }, "580209");
}
/// <summary>
/// 发送库存不足提醒
/// </summary>
/// <param name="telephone"></param>
/// <param name="goodName"></param>
/// <returns></returns>
public static string SendStockVerify(string telephone, string goodName)
{
return Send(telephone, new string[] { goodName }, "580948");
}
private static string Send(string telephone, string[] arry,string templateNo)
{
var smsFactory = new SMSFactory();
var client = (RongLianClient)smsFactory.CreateClient(SMSEnum.容联云通讯);
client.Init(accountSid, accountToken, appId);
client.SetTemplate(templateNo);
if (!client.SendMessage(telephone, arry))
{
LogHelper.Info("短信发送失败;" + client.Message);
return client.Message;
}
else
{
return "success";
}
}
}