短信验证接口

一. 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";
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值