123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using CB.Cache;
- using Common;
- using Common.ZST;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Web.Http;
- using UC.Api.Models.Entity;
- using UC.Api.Models.Query;
- using UC.BLL.IBLL;
- using UC.Models.Entity;
- namespace UC.Api.Controllers
- {
- public class SMSController : BaseApiController
- {
- #region 构造
- private IUsers_LoginBLL bll_user { get; set; }
- private ICache cache;
- public SMSController(ICommonBLL commonBLL, ICache cache) : base(commonBLL)
- {
- this.cache = cache;
- }
- #endregion
- /// <summary>
- /// 获取手机验证码
- /// </summary>
- /// <param name="query">条件</param>
- /// <returns></returns>
- [NoYz]
- [HttpPost]
- public async Task<bool> GetPhoneCode(PhoneCodeQuery query)
- {
- if (!Regex.IsMatch(query.Phone, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
- {
- Accepted("手机号码错误!");
- }
- var key = query.Type == PhoneCodeTypeEnum.登陆验证 ? $"{CacheConstant.LoginPhoneCode}_{query.Phone}" :
- query.Type == PhoneCodeTypeEnum.修改验证 ? $"{CacheConstant.EditPhoneCode}_{query.Phone}" :
- query.Type==PhoneCodeTypeEnum.忘记密码?$"{CacheConstant.ForgotPwdPhoneCode}_{query.Phone}":
- $"{CacheConstant.RegisterPhoneCode}_{query.Phone}";
- var viKey = query.Type == PhoneCodeTypeEnum.登陆验证 ? $"{CacheConstant.LoginPhoneCode}_{query.Phone}_Cf" :
- query.Type == PhoneCodeTypeEnum.修改验证 ? $"{CacheConstant.EditPhoneCode}_{query.Phone}_Cf" :
- query.Type == PhoneCodeTypeEnum.忘记密码 ? $"{CacheConstant.ForgotPwdPhoneCode}_{query.Phone}_Cf" :
- $"{CacheConstant.RegisterPhoneCode}_{query.Phone}_Cf";
- if (!cache.GetCache<string>(viKey).IsEmpty())
- {
- Accepted("验证码已发送,若未收到,请1分钟后再次尝试!");
- }
- var code =OtherHelper.RandomInt(6).Select(p => p.ToString()).Join("");
- if (query.Type == PhoneCodeTypeEnum.注册验证 && bll_com.FindEntity<Users_Login>(p => p.UserName == query.Phone) != null)
- Accepted("该手机号码已注册账户信息!");
- await Task.Run(() =>
- {
- var isS = ZSTSendSMSHelper.SendSMS(query.Phone, ConfigHelper.GetValue("PhoneSMS").Replace("$code", code));
- if (isS)
- {
- cache.WriteCache<string>(key, code, ConfigHelper.GetValue("PhoneSMSOutTime").TryToInt32());
- cache.WriteCache<string>(viKey, code, 60);
- }
- else
- {
- Accepted("一个用户一天只能发送三次验证码,请尝试账号密码登陆!");
- }
- });
- return true;
- }
- [HttpPost]
- public bool ValidationPhoneCode(ValidationPhoneCodeQuery query)
- {
- if (!Regex.IsMatch(query.Phone, "^[1]+[3,5,6,7,8,9]+\\d{9}"))
- {
- BaseApiController.Accepted("手机号码错误!");
- }
- string text = "";
- switch (query.Type)
- {
- case PhoneCodeTypeEnum.登陆验证:
- text = CacheConstant.LoginPhoneCode + "_" + query.Phone;
- break;
- case PhoneCodeTypeEnum.修改验证:
- text = CacheConstant.EditPhoneCode + "_" + query.Phone;
- break;
- case PhoneCodeTypeEnum.注册验证:
- text = CacheConstant.RegisterPhoneCode + "_" + query.Phone;
- break;
- case PhoneCodeTypeEnum.忘记密码:
- text = CacheConstant.ForgotPwdPhoneCode + "_" + query.Phone;
- break;
- }
- var i= this.cache.GetCache<string>(text);
- return this.cache.GetCache<string>(text) == query.Code;
- }
- }
- }
|