Files
Yi.Admin/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs

224 lines
8.7 KiB
C#
Raw Normal View History

2021-10-26 00:59:06 +08:00
using Microsoft.AspNetCore.Authorization;
2021-11-02 00:25:23 +08:00
using Microsoft.AspNetCore.Http;
2021-10-26 00:59:06 +08:00
using Microsoft.AspNetCore.Mvc;
2021-10-13 16:44:15 +08:00
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-10-15 14:45:16 +08:00
using Yi.Framework.Common;
2021-11-04 15:15:00 +08:00
using Yi.Framework.Common.Const;
2021-10-14 20:29:07 +08:00
using Yi.Framework.Common.Helper;
2021-10-13 16:44:15 +08:00
using Yi.Framework.Common.Models;
2021-11-04 15:15:00 +08:00
using Yi.Framework.Common.QueueModel;
2021-10-14 20:29:07 +08:00
using Yi.Framework.Core;
2021-10-18 21:54:40 +08:00
using Yi.Framework.DTOModel;
2021-10-13 16:44:15 +08:00
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
2021-11-02 23:22:37 +08:00
using Yi.Framework.WebCore;
2021-12-25 16:55:11 +08:00
using Yi.Framework.WebCore.Mapper;
2021-10-13 16:44:15 +08:00
namespace Yi.Framework.ApiMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class AccountController : Controller
{
private readonly ILogger<UserController> _logger;
private IUserService _userService;
2021-10-24 17:13:28 +08:00
private IMenuService _menuService;
2021-11-04 15:15:00 +08:00
private RabbitMQInvoker _rabbitMQInvoker;
private CacheClientDB _cacheClientDB;
private IRoleService _roleService;
2021-11-06 17:53:54 +08:00
private IHttpContextAccessor _httpContext;
public AccountController(ILogger<UserController> logger, IUserService userService, IMenuService menuService,RabbitMQInvoker rabbitMQInvoker,CacheClientDB cacheClientDB, IRoleService roleService, IHttpContextAccessor httpContext)
2021-10-13 16:44:15 +08:00
{
_logger = logger;
_userService = userService;
2021-10-24 17:13:28 +08:00
_menuService = menuService;
2021-11-04 15:15:00 +08:00
_rabbitMQInvoker = rabbitMQInvoker;
_cacheClientDB = cacheClientDB;
_roleService = roleService;
2021-11-06 17:53:54 +08:00
_httpContext = httpContext;
2021-10-13 16:44:15 +08:00
}
/// <summary>
2021-10-17 18:37:07 +08:00
/// 登录方法要返回data:{user,token} token
2021-10-13 16:44:15 +08:00
/// </summary>
2021-12-25 16:55:11 +08:00
/// <param name="login"></param>
2021-10-13 16:44:15 +08:00
/// <returns></returns>
[HttpPost]
2021-12-25 16:55:11 +08:00
public async Task<Result> Login(loginDto login)
2021-10-13 16:44:15 +08:00
{
2021-12-25 16:55:11 +08:00
var _user= MapperHelper.Map<user, loginDto>(login);
2021-10-16 17:50:55 +08:00
var user_data = await _userService.Login(_user);
2021-11-02 00:25:23 +08:00
if (user_data == null)
{
return Result.Error("该用户不存在");
}
var menuList = await _menuService.GetTopMenuByUserId(user_data.id);
2021-10-24 17:13:28 +08:00
if ( user_data!=null)
{
var token = MakeJwt.app(new jwtUser() {user=user_data,menuIds= menuList});
2021-11-03 21:14:58 +08:00
JobModel.visitNum += 1;
2021-11-03 20:28:45 +08:00
return Result.Success().SetData(new { user = new { user_data.id, user_data.username, user_data.introduction, user_data.icon, user_data.nick }, token });
2021-10-13 23:08:42 +08:00
}
return Result.Error();
2021-10-13 16:44:15 +08:00
}
/// <summary>
/// 不用写,单纯制作日志
/// </summary>
/// <returns></returns>
[HttpPost]
2021-10-14 20:29:07 +08:00
public Result Logout()
2021-10-13 16:44:15 +08:00
{
return Result.Success();
}
/// <summary>
/// code为验证码,从redis中判断一下code是否正确
2021-10-13 16:44:15 +08:00
/// </summary>
/// <param name="_user"></param>
/// <param name="code"></param>
/// <returns></returns>
[HttpPost]
public async Task<Result> Register(user _user, string code)
2021-10-13 23:08:42 +08:00
{
_user.username=_user.username.Trim();
if(string.IsNullOrEmpty(_user.username))
code = code.Trim();
string trueCode= _cacheClientDB.Get<string>(RedisConst.keyCode + _user.phone);
if (code == trueCode)
2021-10-14 20:29:07 +08:00
{
//设置默认头像
var setting = JsonHelper.StrToObj<SettingDto>(_cacheClientDB.Get<string>(RedisConst.key));
_user.icon = setting.InitIcon;
2021-11-06 17:53:54 +08:00
_user.ip = _httpContext.HttpContext.Request.Headers["X-Real-IP"].FirstOrDefault();//通过上下文获取ip
//设置默认角色
if (string.IsNullOrEmpty(setting.InitRole))
{
return Result.Error("无默认角色,请初始化数据库");
}
_user.roles = new List<role>();
_user.roles.Add(await _roleService.GetEntity(u => u.role_name == setting.InitRole));
2021-10-13 23:08:42 +08:00
await _userService.Register(_user);
return Result.Success("恭喜,你已加入我们!");
2021-10-13 23:08:42 +08:00
}
return Result.Error("验证码有误,请重新输入!");
2021-10-13 16:44:15 +08:00
}
2021-11-04 15:15:00 +08:00
/// <summary>
/// 发送短信需要将生成的sms+code存入redis
/// </summary>
/// <param name="SMSAddress"></param>
/// <returns></returns>
2021-11-04 15:38:38 +08:00
[HttpPost]
public async Task<Result> SendSMS(string SMSAddress)
2021-11-04 15:15:00 +08:00
{
2021-11-06 17:53:54 +08:00
if (string.IsNullOrEmpty(SMSAddress))
{
return Result.Error("请输入电话号码");
}
SMSAddress = SMSAddress.Trim();
if (!await _userService.PhoneIsExsit(SMSAddress))
{
SMSQueueModel sMSQueueModel = new SMSQueueModel();
sMSQueueModel.phone = SMSAddress;
sMSQueueModel.code =RandomHelper.GenerateCheckCodeNum(6);
//10分钟过期
_cacheClientDB.Set(RedisConst.keyCode+sMSQueueModel.phone, sMSQueueModel.code, TimeSpan.FromMinutes(10));
_rabbitMQInvoker.Send(new Common.IOCOptions.RabbitMQConsumerModel() { ExchangeName = RabbitConst.SMS_Exchange, QueueName = RabbitConst.SMS_Queue_Send }, JsonHelper.ObjToStr(sMSQueueModel));
return Result.Success("发送短信成功10分钟后过期请留意短信接收");
}
return Result.Error("该号码已被注册");
2021-11-04 15:15:00 +08:00
}
2021-10-13 16:44:15 +08:00
/// <summary>
/// 发送邮箱需要先到数据库判断该邮箱是否被人注册过到userservice写mail_exist方法还有接口别忘了。
2021-10-13 16:44:15 +08:00
/// </summary>
/// <param name="emailAddress"></param>
/// <returns></returns>
[HttpPost]//邮箱验证
public async Task<Result> Email(string emailAddress)
{
emailAddress = emailAddress.Trim().ToLower();
//先判断邮箱是否被注册使用过,如果被使用过,便不让操作
2021-10-13 23:08:42 +08:00
if (!await _userService.EmailIsExsit(emailAddress))
2021-10-13 16:44:15 +08:00
{
string code = RandomHelper.GenerateRandomLetter(6);
code = code.ToUpper();//全部转为大写
EmailHelper.sendMail(code, emailAddress);
//我要把邮箱和对应的code加进到数据库还有申请时间
//设置10分钟过期
//set不存在便添加如果存在便替换
//CacheHelper.SetCache<string>(emailAddress, code, TimeSpan.FromSeconds(10));
return Result.Success("发送邮件成功,请查看邮箱(可能在垃圾箱)");
}
else
{
return Result.Error("该邮箱已被注册");
}
// 邮箱和验证码都要被记住,然后注册时候比对邮箱和验证码是不是都和现在生成的一样
}
2021-10-20 16:23:57 +08:00
/// <summary>
/// 修改密码
/// </summary>
/// <param name="pwdDto"></param>
/// <returns></returns>
2021-10-18 21:54:40 +08:00
[HttpPut]
2021-10-26 00:59:06 +08:00
[Authorize]
2021-10-18 21:54:40 +08:00
public async Task<Result> ChangePassword(ChangePwdDto pwdDto)
2021-10-29 23:23:32 +08:00
{
var user_data = await _userService.GetUserById(pwdDto.user.id);
2021-10-22 16:48:03 +08:00
string msg = "修改成功";
2021-10-22 19:59:12 +08:00
if (! string.IsNullOrEmpty( pwdDto.newPassword))
2021-10-20 18:09:59 +08:00
{
if (user_data.password == pwdDto.user.password)
2021-10-20 16:23:57 +08:00
{
2021-10-22 16:48:03 +08:00
2021-10-20 16:23:57 +08:00
user_data.password = pwdDto.newPassword;
2021-10-20 18:09:59 +08:00
user_data.phone = pwdDto.user.phone;
user_data.introduction = pwdDto.user.introduction;
user_data.email = pwdDto.user.email;
user_data.age = pwdDto.user.age;
user_data.address = pwdDto.user.address;
user_data.nick = pwdDto.user.nick;
2021-10-22 19:59:12 +08:00
2021-10-20 16:23:57 +08:00
await _userService.UpdateAsync(user_data);
user_data.password = null;
2021-10-22 19:59:12 +08:00
return Result.Success(msg);
2021-10-22 16:48:03 +08:00
}
else
{
msg = "密码错误";
return Result.Error(msg);
2021-10-20 16:23:57 +08:00
}
2021-10-18 21:54:40 +08:00
}
2021-10-22 16:48:03 +08:00
2021-10-20 18:09:59 +08:00
user_data.phone = pwdDto.user.phone;
user_data.introduction = pwdDto.user.introduction;
user_data.email = pwdDto.user.email;
user_data.age = pwdDto.user.age;
user_data.address = pwdDto.user.address;
user_data.nick = pwdDto.user.nick;
2021-10-22 19:59:12 +08:00
2021-10-20 18:09:59 +08:00
await _userService.UpdateAsync(user_data);
2021-10-22 19:59:12 +08:00
return Result.Success(msg);
2021-10-18 21:54:40 +08:00
}
2021-11-02 23:22:37 +08:00
2021-10-13 16:44:15 +08:00
}
}