namespace WebAPI

{

   /// <summary>

   /// 短信发送接口

   /// author:

   /// </summary>

   [WebService(Namespace = "http://tempuri.org/")]

   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   [System.ComponentModel.ToolboxItem(false)]

   // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。

   // [System.Web.Script.Services.ScriptService]

   public class SMS : System.Web.Services.WebService

   {

       /// <summary>

       /// 验证密钥

       /// </summary>

       private string _key = "dhccpass";


       /// <summary>

       /// 发送验证码短信

       /// </summary>

       /// <param name="key">验证密钥</param>

       /// <param name="phone">电话号码</param>

       /// <param name="type">password取回密码,register邀请注册,sms短信</param>

       /// <returns>0-发送失败 1-发送成功</returns>

       [WebMethod(Description = "发送验证码短信")]

       public string SendSMS(string key, string phone, string type)

       {

           Random r = new Random();

           if (key==_key)

           {

               try

               {

                   Entities db = new Entities();

                   user_code uc = new user_code();

                   DateTime add_time = DateTime.Now;//当前时间

                   var rannum = r.Next(100000, 1000000).ToString();//随机数

                   var ntime = db.user_code.SingleOrDefault(t =>t.phone==phone && t.eff_time > add_time);

                   if (ntime == null)

                   {

                       uc.id = Guid.NewGuid().ToString("N");//id值

                       //uc.user_id = "";

                       uc.user_name = "";

                       uc.phone = phone;//电话

                       uc.type = type;//类型

                       uc.str_code = rannum;

                       uc.count = 1;//使用次数

                       uc.status = 0;//是否使用

                       uc.add_time = add_time;

                       uc.eff_time = add_time.AddMinutes(5);//过期时间

                       db.user_code.Add(uc);

                       db.SaveChanges();

                   }

                   else

                   {

                       rannum = ntime.str_code;

                       ntime.count++;

                       db.SaveChanges();

                   }



                   string s = string.Empty;

                   if (type == "password")

                   {

                       s = string.Format("[{0}]{1}", rannum, "为密码找回的验证码");

                   }

                   //此处为密码找回输入内容

                   else if (type == "register")

                   {

                       s = string.Format("[{0}]{1}", rannum, "为邀请注册的验证码");

                   }

                   //此处为邀请注册输入内容

                   else if (type == "sms")

                   {

                       s = string.Format("[{0}]{1}", rannum, "为手机登陆的验证码");

                   }

                   //此处为短信输入内容

                   return "1;"+ s;

               }

               //}

               catch (Exception ex)

               {

                   return "0";

                   //throw;

               }

           }

           else

           {

               return "0";

           }


       }


   }

}