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

149 lines
5.4 KiB
C#
Raw Normal View History

2021-10-13 16:44:15 +08:00
using Microsoft.AspNetCore.Mvc;
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-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-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;
namespace Yi.Framework.ApiMicroservice.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class AccountController : Controller
{
private readonly ILogger<UserController> _logger;
private IUserService _userService;
public AccountController(ILogger<UserController> logger, IUserService userService)
{
_logger = logger;
_userService = userService;
}
/// <summary>
2021-10-17 18:37:07 +08:00
/// 登录方法要返回data:{user,token} token
2021-10-13 16:44:15 +08:00
/// </summary>
/// <param name="_user"></param>
/// <returns></returns>
[HttpPost]
public async Task<Result> Login(user _user)
{
2021-10-16 17:50:55 +08:00
var user_data = await _userService.Login(_user);
if( user_data!=null)
2021-10-13 23:08:42 +08:00
{
2021-10-16 17:50:55 +08:00
2021-10-16 21:28:28 +08:00
var token = MakeJwt.app(user_data);
return Result.Success().SetData(new { user = new { _user.id, _user.username, _user.introduction, _user.icon, _user.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为验证码,判断一下,假装验证码都是对的
/// </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
{
2021-10-14 20:29:07 +08:00
if (code != null)
{
2021-10-13 23:08:42 +08:00
await _userService.Register(_user);
}
2021-10-14 20:29:07 +08:00
return Result.Error();
2021-10-13 16:44:15 +08:00
}
/// <summary>
/// 传入邮箱需要先到数据库判断该邮箱是否被人注册过到userservice写mail_exist方法还有接口别忘了。这个接口不需要洞只需要完成userservice写mail_exist与接口即可
/// </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]
public async Task<Result> ChangePassword(ChangePwdDto pwdDto)
{
2021-10-22 16:48:03 +08:00
var uid= pwdDto.user.id;
var user_data = await _userService.GetUserById(uid);
string msg = "修改成功";
2021-10-20 16:23:57 +08:00
if (pwdDto.newPassword != null)
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-20 16:23:57 +08:00
await _userService.UpdateAsync(user_data);
user_data.password = null;
2021-10-22 16:48:03 +08:00
return Result.Success(msg).SetData(user_data);
}
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;
await _userService.UpdateAsync(user_data);
user_data.password = null;
2021-10-22 16:48:03 +08:00
return Result.Success(msg).SetData(user_data);
2021-10-18 21:54:40 +08:00
}
2021-10-13 16:44:15 +08:00
}
}