c# 淘寶短信驗證

發送短信驗證碼api

 1         /// <summary>
 2         /// 發送短信驗證碼
 3         /// </summary>
 4         /// <param name="Mobile">手機號</param>
 5         [WebMethod]
 6         public string SendValidCode(string Mobile)
 7         {
 8             ResponseData ret = new ResponseData(1, string.Empty);
 9 
10             try
11             {
12                 if (string.IsNullOrEmpty(Mobile))
13                 {
14                     ret.status = -1;
15                     ret.info = "手機號不能爲空!";
16                 }
17                 else
18                 {
19                     SP_Valid_Code model = new SP_Valid_Code();
20                     model.Mobile = Mobile;
21                     model.ValidCode = ServiceHelper.GenerateRandomNumber(6);
22                     model.CreateTime = DateTime.Now;
23 
24                     var validcodeid = SportsAppDB.SportsAppDB.Trackable.InsertValidCode(model);
25                     if (validcodeid > 0)
26                     {
27                         //正式環境:http://gw.api.taobao.com/router/rest
28                         //沙箱環境:http://gw.api.tbsandbox.com/router/rest
29                         string url = ServiceHelper.SmsUrl;// http://gw.api.taobao.com/router/rest
30                         string appkey = ServiceHelper.SmsAppKey;// 23331487
31                         string appsecret = ServiceHelper.SmsAppSecret;// 2c777d8cbece6577ecadfadb991595eb
32 
33                         ITopClient client = new DefaultTopClient(url, appkey, appsecret);
34                         AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
35                         req.Extend = Mobile;
36                         req.SmsType = "normal";
37                         req.SmsFreeSignName = ServiceHelper.SmsFreeSignName;// 正式:註冊驗證
38                         req.SmsParam = "{\"code\":\"" + model.ValidCode + "\",\"product\":\"" + ServiceHelper.SmsProduct + "\"}";
39                         req.RecNum = Mobile;
40                         req.SmsTemplateCode = ServiceHelper.SmsTemplateCode;// 短信模板【用戶註冊驗證碼】 "驗證碼${code},您正在註冊成爲${product}用戶,感謝您的支持!"
41                         AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
42                         if (rsp.IsError)
43                         {
44                             ret.status = -1;
45                             ret.info = rsp.ErrCode + ":" + rsp.ErrMsg;
46                         }
47                     }
48                     else
49                     {
50                         ret.status = -1;
51                         ret.info = "新增驗證碼記錄失敗!";
52                     }
53                 }
54             }
55             catch (Exception ex)
56             {
57                 ret.status = -1;
58                 ret.info = ex.Message;
59             }
60             return ServiceHelper.ToJSON(ret);
61         }    
View Code
相關文章
相關標籤/搜索