Mishop项目流程图


1:创建一个项目用来调用第三方的类,右键Nuget添加第三方的引用类库 qcloudsms_csharp


using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiShop.Remote
{
    /// <summary>
    /// 
    /// </summary>
    public class TenXunYunSMS
    {
        //appId
        public int appId;
        //appKey
        public string appKey = "";
        //短信模板ID
        private int tmplateId = 379257;
        //签名内容
        private string smsSign = "7hhhcn";
        /// <summary>
        /// 验证码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public void SetSMS(string phone)
        {
            Random random = new Random();
            int code = random.Next(100000, 999999);
            try
             {
                SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
                var result = ssender.sendWithParam("86", phone,
                    tmplateId, new[] { code.ToString() }, smsSign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
            }
            catch (JSONException ex)
            {
                throw;
            }
            catch (HTTPException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw;
            }
            Code = code;
        }
    }
}

2.数据访问层写插入短信信息表方法

using Mishop.Core;
using Mishop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mishop.DAL
{
   public  class SMSInfoReopsitory
    {
        public int CrodUserinfo(SMSInfo sMSInfo)
        {
            string sql = @"insert into SMSInfo(code,TelPhone,CreateTime,ExTime) Values
                         (@code,@TelPhone,@CreateTime,@ExTime);";
            SqlParameter[] parameters =
            {
                 new SqlParameter()
                 {
                     DbType=DbType.Int32,
                     ParameterName="@code",
                     Value=sMSInfo.Code

                 },
                  new SqlParameter()
                 {
                     DbType=DbType.Int64,
                     ParameterName="@TelPhone",
                     Value=sMSInfo.TelPhone

                 },
                   new SqlParameter()
                 {
                     DbType=DbType.DateTime,
                     ParameterName="@CreateTime",
                     Value=DateTime.Now

                 },
                     new SqlParameter()
                 {
                     DbType=DbType.DateTime,
                     ParameterName="@ExTime",
                     Value=DateTime.Now.AddMinutes(5)

                 }
            };
            sqlHelper hel = new sqlHelper();
            return hel.ExcuteNoQuery(sql, parameters);
        }
    }
}

3.业务层:先引用第三方项目,先调用第三方类,发送验证码,然后将验证码存储到短信信息对象,最后调用数据访问层的插入短信的方法。

using MiShop.Remote;

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mishop.DAL;
using Mishop.Model;

namespace Mishop.BLL
{
   public  class SMSInfoService
    {
        public bool SendCode(string telPhone)
        {
            TenXunYunSMS tss = new TenXunYunSMS();
            try
            {
                //new 第三方类
               
                tss.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
                tss.appKey = ConfigurationManager.AppSettings["appKey"];
                tss.SetSMS(telPhone);
            }
            catch (Exception)
            {

                return false;
            }
            SMSInfoReopsitory sr = new SMSInfoReopsitory();
            SMSInfo si = new SMSInfo();
            si.Code = tss.Code;
            si.TelPhone = Convert.ToInt64(telPhone);
            sr.CrodUserinfo(si);
            int result = -1;
            result= sr.CrodUserinfo(si);
            return result > 0;
        }
    }
}

4:控制器写一个JsonResult的发送验证码方法需要接收手机号 

using Mishop.BLL;
using Mishop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MiShop.Controllers
{
    public class CommonsController : Controller
    {
        // GET: Common
        public JsonResult SendCord(string tel)
        {
            OperateResult or = new OperateResult();
            SMSInfoService srs = new SMSInfoService();
            or.Success = srs.SendCode(tel);
            return Json(or);
        }
    }
}

5:页面点击获取验证码按钮:先禁用按钮,然后ajax post提交到控制器对应的发送验证码方法,传入手机号,然后success处理返回的结果。

 $("#submit1ByRegs").click(function () {
           
            validateCode();
        })
        function IntervalCord() {
             var time = 60;
          
               $("#submit1ByRegs").attr("disabled", "disabled");
             $("#submit1ByRegs").css("color", "black");
               $("#submit1ByRegs").val(time + "s后在发送");
              var timer = setInterval(function () {
                if (time > 0) {
                    time--;
                    $("#submit1ByRegs").val(time + "s后在发送");
                } else {
                    $("#submit1ByRegs").removeAttr("disabled").css("color", "white");
                    clearInterval(timer);
                }
            },1000)
        }
        function validateCode() {
         var telPhone = $("#tel").val();
            //var data = {};
            //data.code = $("#code").val();
            //data.tel = $("#tel").val();
            $.ajax({
                type:"post",
                url: "/Commons/SendCord?tel=" + telPhone,
                success: function (or) {
                    if (or.Success) {
                        alert("发送成功!");
                        IntervalCord()
                    } else {
                        alert("发送失败!")
                    }
                }
            })
        }
             });

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值