验证码
This commit is contained in:
31
Api/Ewide.Core/Service/User/Dto/Userphone.cs
Normal file
31
Api/Ewide.Core/Service/User/Dto/Userphone.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Service
|
||||
{
|
||||
public class Usermailphone
|
||||
{
|
||||
/// <summary>
|
||||
/// 绑定的值
|
||||
/// </summary>
|
||||
public string Target { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 发送验证码类型,1为发送给原邮箱,2为发送给原手机号
|
||||
/// </summary>
|
||||
public int? Type { get; set; }
|
||||
|
||||
///<summary>
|
||||
/// 第一次验证码
|
||||
/// </summary>
|
||||
public int? Orgcode { get; set; }
|
||||
|
||||
///<summary>
|
||||
/// 第二次验证码
|
||||
/// </summary>
|
||||
public int? Code { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -26,5 +26,8 @@ namespace Ewide.Core.Service
|
||||
Task UpdateUser(UpdateUserInput input);
|
||||
Task UpdateUserInfo(UserInput input);
|
||||
Task UpdateUserPwd(ChangePasswordUserInput input);
|
||||
|
||||
Task<dynamic> SendCode(Usermailphone input);
|
||||
Task<dynamic> CheckBindcode(Usermailphone input);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Ewide.Core.Service.Role;
|
||||
using Ewide.Core.Util;
|
||||
using Furion.DatabaseAccessor;
|
||||
using Furion.DatabaseAccessor.Extensions;
|
||||
using Furion.DataEncryption;
|
||||
@@ -8,9 +9,11 @@ using Furion.FriendlyException;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ewide.Core.Service
|
||||
@@ -23,15 +26,16 @@ namespace Ewide.Core.Service
|
||||
{
|
||||
private readonly IRepository<SysUser> _sysUserRep; // 用户表仓储
|
||||
private readonly IUserManager _userManager;
|
||||
|
||||
private readonly ISysCacheService _sysCacheService;
|
||||
private readonly ISysEmpService _sysEmpService;
|
||||
private readonly IMemoryCache _iMemoryCache;
|
||||
private readonly ISysUserDataScopeService _sysUserDataScopeService;
|
||||
private readonly ISysUserRoleService _sysUserRoleService;
|
||||
private readonly ISysUserAreaService _sysUserAreaService;
|
||||
|
||||
public SysUserService(IRepository<SysUser> sysUserRep,
|
||||
IUserManager userManager,
|
||||
IMemoryCache memoryCache,
|
||||
ISysCacheService sysCacheService,
|
||||
ISysEmpService sysEmpService,
|
||||
ISysUserDataScopeService sysUserDataScopeService,
|
||||
@@ -40,13 +44,14 @@ namespace Ewide.Core.Service
|
||||
{
|
||||
_sysUserRep = sysUserRep;
|
||||
_userManager = userManager;
|
||||
_iMemoryCache = memoryCache;
|
||||
_sysCacheService = sysCacheService;
|
||||
_sysEmpService = sysEmpService;
|
||||
_sysUserDataScopeService = sysUserDataScopeService;
|
||||
_sysUserRoleService = sysUserRoleService;
|
||||
_sysUserAreaService = sysUserAreaService;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询用户
|
||||
/// </summary>
|
||||
@@ -448,5 +453,204 @@ namespace Ewide.Core.Service
|
||||
throw Oops.Oh(ErrorCode.D1013);
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///发送验证码
|
||||
/// </summary>
|
||||
[HttpPost("/sysUser/SendCode")]
|
||||
public async Task<dynamic> SendCode(Usermailphone input)
|
||||
{
|
||||
var Orgcode_Key = "ewide_Orgcode";
|
||||
var Smscode_Key = "ewide_smscode";
|
||||
var Mailcode_Key = "ewide_mailcode";
|
||||
var Regex_phone = @"^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[0,1,3,6-8])|(18[0-9])|(19[8,9])|(166))[0-9]{8}$";
|
||||
var Regex_Email = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
|
||||
CodeHelper ch = new CodeHelper(_iMemoryCache);
|
||||
//Type为1时,给原手机号发送验证码
|
||||
if(input.Type == 1)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ch.SendSmscode(_userManager.User.Phone, Orgcode_Key);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw Oops.Oh(ErrorCode.D1018);
|
||||
}
|
||||
|
||||
}
|
||||
//Type为2时,给原邮箱发送验证码
|
||||
else if(input.Type == 2)
|
||||
{
|
||||
if(new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").IsMatch(_userManager.User.Email))
|
||||
{
|
||||
try
|
||||
{
|
||||
return ch.SendMail(_userManager.User.Email, Orgcode_Key);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw Oops.Oh(ErrorCode.D1018);
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("原邮箱错误");
|
||||
}
|
||||
//Type为null时,则发验证码
|
||||
else
|
||||
{
|
||||
//通过正则判断绑定类型
|
||||
if(new Regex(Regex_phone).IsMatch(input.Target))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
ch.SendSmscode(input.Target, Smscode_Key);
|
||||
return true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw Oops.Oh(ErrorCode.D1018);
|
||||
}
|
||||
}
|
||||
if(new Regex(Regex_Email).IsMatch(input.Target))
|
||||
{
|
||||
try
|
||||
{
|
||||
ch.SendMail(input.Target, Mailcode_Key);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw Oops.Oh(ErrorCode.D1018);
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("格式错误");
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///检验验证码并且绑定
|
||||
/// </summary>
|
||||
[HttpPost("/sysUser/CheckBindcode")]
|
||||
public async Task<dynamic> CheckBindcode(Usermailphone input)
|
||||
{
|
||||
var Orgcode_Key = "ewide_Orgcode";
|
||||
var Smscode_Key = "ewide_smscode";
|
||||
var Mailcode_Key = "ewide_mailcode";
|
||||
var Regex_phone = @"^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[0,1,3,6-8])|(18[0-9])|(19[8,9])|(166))[0-9]{8}$";
|
||||
var Regex_Email = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
|
||||
var user = await _sysUserRep.FirstOrDefaultAsync(u => u.Id == _userManager.UserId);
|
||||
CodeHelper ch = new CodeHelper(_iMemoryCache);
|
||||
if(input.Type == 1)
|
||||
{
|
||||
if (ch.Checkcode(_userManager.User.Phone, input.Orgcode, Orgcode_Key))
|
||||
{
|
||||
return true;
|
||||
};
|
||||
throw Oops.Oh("验证错误");
|
||||
}
|
||||
else if(input.Type == 2)
|
||||
{
|
||||
if(ch.Checkcode(_userManager.User.Email, input.Orgcode, Orgcode_Key))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
throw Oops.Oh("验证错误");
|
||||
}
|
||||
else
|
||||
{
|
||||
//为第一次绑定
|
||||
if(string.IsNullOrEmpty(_userManager.User.Phone) && string.IsNullOrEmpty(_userManager.User.Email))
|
||||
{
|
||||
if(new Regex(Regex_phone).IsMatch(input.Target))
|
||||
{
|
||||
if (ch.Checkcode(input.Target,input.Code, Smscode_Key))
|
||||
{
|
||||
try
|
||||
{
|
||||
user.Phone = input.Target;
|
||||
await user.UpdateIncludeNowAsync(new string[] {
|
||||
nameof(SysUser.Phone)
|
||||
}, true);
|
||||
return "手机绑定成功";
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw Oops.Oh("手机绑定错误");
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("验证码失效");
|
||||
}
|
||||
if(new Regex(Regex_Email).IsMatch(input.Target))
|
||||
{
|
||||
if (ch.Checkcode(input.Target, input.Code, Mailcode_Key))
|
||||
{
|
||||
try
|
||||
{
|
||||
user.Email = input.Target;
|
||||
await user.UpdateIncludeNowAsync(new string[] {
|
||||
nameof(SysUser.Email)
|
||||
}, true);
|
||||
return "邮箱绑定成功";
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw Oops.Oh("邮箱绑定错误");
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("验证码失效");
|
||||
}
|
||||
throw Oops.Oh("号码格式不对");
|
||||
}
|
||||
else
|
||||
{
|
||||
bool CheckOrgPhone = ch.Checkcode(_userManager.User.Phone, input.Orgcode, Orgcode_Key);
|
||||
bool CheckOrgEmail = ch.Checkcode(_userManager.User.Email, input.Orgcode, Orgcode_Key);
|
||||
if (CheckOrgPhone || CheckOrgEmail)
|
||||
{
|
||||
if (new Regex(Regex_phone).IsMatch(input.Target))
|
||||
{
|
||||
if (ch.Checkcode(input.Target, input.Code, Smscode_Key))
|
||||
{
|
||||
try
|
||||
{
|
||||
user.Phone = input.Target;
|
||||
await user.UpdateIncludeNowAsync(new string[] {
|
||||
nameof(SysUser.Phone)
|
||||
}, true);
|
||||
return "手机改绑成功";
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw Oops.Oh("手机绑定错误");
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("验证码失效");
|
||||
}
|
||||
if (new Regex(Regex_Email).IsMatch(input.Target))
|
||||
{
|
||||
if (ch.Checkcode(input.Target, input.Code, Mailcode_Key))
|
||||
{
|
||||
try
|
||||
{
|
||||
user.Email = input.Target;
|
||||
await user.UpdateIncludeNowAsync(new string[] {
|
||||
nameof(SysUser.Email)
|
||||
}, true);
|
||||
return "邮箱改绑成功";
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw Oops.Oh("邮箱绑定错误");
|
||||
}
|
||||
}
|
||||
throw Oops.Oh("验证码失效");
|
||||
}
|
||||
throw Oops.Oh("号码格式不对");
|
||||
}
|
||||
throw Oops.Oh("验证码失效");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user