SMSController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using CB.Cache;
  2. using Common;
  3. using Common.ZST;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Web.Http;
  12. using UC.Api.Models.Entity;
  13. using UC.Api.Models.Query;
  14. using UC.BLL.IBLL;
  15. using UC.Models.Entity;
  16. namespace UC.Api.Controllers
  17. {
  18. public class SMSController : BaseApiController
  19. {
  20. #region 构造
  21. private IUsers_LoginBLL bll_user { get; set; }
  22. private ICache cache;
  23. public SMSController(ICommonBLL commonBLL, ICache cache) : base(commonBLL)
  24. {
  25. this.cache = cache;
  26. }
  27. #endregion
  28. /// <summary>
  29. /// 获取手机验证码
  30. /// </summary>
  31. /// <param name="query">条件</param>
  32. /// <returns></returns>
  33. [NoYz]
  34. [HttpPost]
  35. public async Task<bool> GetPhoneCode(PhoneCodeQuery query)
  36. {
  37. if (!Regex.IsMatch(query.Phone, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
  38. {
  39. Accepted("手机号码错误!");
  40. }
  41. var key = query.Type == PhoneCodeTypeEnum.登陆验证 ? $"{CacheConstant.LoginPhoneCode}_{query.Phone}" :
  42. query.Type == PhoneCodeTypeEnum.修改验证 ? $"{CacheConstant.EditPhoneCode}_{query.Phone}" :
  43. query.Type==PhoneCodeTypeEnum.忘记密码?$"{CacheConstant.ForgotPwdPhoneCode}_{query.Phone}":
  44. $"{CacheConstant.RegisterPhoneCode}_{query.Phone}";
  45. var viKey = query.Type == PhoneCodeTypeEnum.登陆验证 ? $"{CacheConstant.LoginPhoneCode}_{query.Phone}_Cf" :
  46. query.Type == PhoneCodeTypeEnum.修改验证 ? $"{CacheConstant.EditPhoneCode}_{query.Phone}_Cf" :
  47. query.Type == PhoneCodeTypeEnum.忘记密码 ? $"{CacheConstant.ForgotPwdPhoneCode}_{query.Phone}_Cf" :
  48. $"{CacheConstant.RegisterPhoneCode}_{query.Phone}_Cf";
  49. if (!cache.GetCache<string>(viKey).IsEmpty())
  50. {
  51. Accepted("验证码已发送,若未收到,请1分钟后再次尝试!");
  52. }
  53. var code =OtherHelper.RandomInt(6).Select(p => p.ToString()).Join("");
  54. if (query.Type == PhoneCodeTypeEnum.注册验证 && bll_com.FindEntity<Users_Login>(p => p.UserName == query.Phone) != null)
  55. Accepted("该手机号码已注册账户信息!");
  56. await Task.Run(() =>
  57. {
  58. var isS = ZSTSendSMSHelper.SendSMS(query.Phone, ConfigHelper.GetValue("PhoneSMS").Replace("$code", code));
  59. if (isS)
  60. {
  61. cache.WriteCache<string>(key, code, ConfigHelper.GetValue("PhoneSMSOutTime").TryToInt32());
  62. cache.WriteCache<string>(viKey, code, 60);
  63. }
  64. else
  65. {
  66. Accepted("一个用户一天只能发送三次验证码,请尝试账号密码登陆!");
  67. }
  68. });
  69. return true;
  70. }
  71. [HttpPost]
  72. public bool ValidationPhoneCode(ValidationPhoneCodeQuery query)
  73. {
  74. if (!Regex.IsMatch(query.Phone, "^[1]+[3,5,6,7,8,9]+\\d{9}"))
  75. {
  76. BaseApiController.Accepted("手机号码错误!");
  77. }
  78. string text = "";
  79. switch (query.Type)
  80. {
  81. case PhoneCodeTypeEnum.登陆验证:
  82. text = CacheConstant.LoginPhoneCode + "_" + query.Phone;
  83. break;
  84. case PhoneCodeTypeEnum.修改验证:
  85. text = CacheConstant.EditPhoneCode + "_" + query.Phone;
  86. break;
  87. case PhoneCodeTypeEnum.注册验证:
  88. text = CacheConstant.RegisterPhoneCode + "_" + query.Phone;
  89. break;
  90. case PhoneCodeTypeEnum.忘记密码:
  91. text = CacheConstant.ForgotPwdPhoneCode + "_" + query.Phone;
  92. break;
  93. }
  94. var i= this.cache.GetCache<string>(text);
  95. return this.cache.GetCache<string>(text) == query.Code;
  96. }
  97. }
  98. }