配合前后端
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common;
|
||||
using Yi.Framework.Common.Models;
|
||||
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>
|
||||
/// 登录方法,要返回data:{user,token} token先写123456789,不要有导航属性
|
||||
/// </summary>
|
||||
/// <param name="_user"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<Result> Login(user _user)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不用写,单纯制作日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public Result Logout()
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <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();
|
||||
//先判断邮箱是否被注册使用过,如果被使用过,便不让操作
|
||||
if (!await _userService.mail_exist(emailAddress))
|
||||
{
|
||||
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("该邮箱已被注册");
|
||||
}
|
||||
// 邮箱和验证码都要被记住,然后注册时候比对邮箱和验证码是不是都和现在生成的一样
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Model.Models;
|
||||
using Yi.Framework.WebCore;
|
||||
|
||||
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
@@ -69,5 +70,15 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
await _userService.AddAsync(_user);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过上下文对象获取user(注意,_user下只有userId),返回值为该用户下所有的menu,(注意子类递归)并且需要关联mould
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<Result> GetMenuMould()
|
||||
{
|
||||
var _user= this.HttpContext.GetCurrentUserInfo();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
125
Yi.Framework/Yi.Framework.Common/Helper/EmailHelper.cs
Normal file
125
Yi.Framework/Yi.Framework.Common/Helper/EmailHelper.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Mail;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Common
|
||||
{
|
||||
public class EmailHelper
|
||||
{
|
||||
public static string fromMail { get; set; }
|
||||
public static string pwdMail { get; set; }
|
||||
public static string senderMail { get; set; }
|
||||
public static string subjectMail { get; set; }
|
||||
public static void Init(string fromMail,string pwdMail,string senderMail, string subjectMail)
|
||||
{
|
||||
EmailHelper.fromMail = fromMail;
|
||||
EmailHelper.pwdMail = pwdMail;
|
||||
EmailHelper.senderMail = senderMail;
|
||||
EmailHelper.subjectMail = subjectMail;
|
||||
}
|
||||
|
||||
public static bool sendMail(string body, string toMail)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
//string fromMail = "454313500@qq.com";
|
||||
//string pwdMail = "yvjioburildgbhdf";
|
||||
MailMessage message = new MailMessage();
|
||||
//设置发件人,发件人需要与设置的邮件发送服务器的邮箱一致
|
||||
System.Net.Mail.MailAddress fromAddr = new System.Net.Mail.MailAddress(fromMail, EmailHelper.senderMail);
|
||||
message.From = fromAddr;
|
||||
|
||||
//设置收件人,可添加多个,添加方法与下面的一样
|
||||
message.To.Add(toMail);
|
||||
|
||||
|
||||
//设置邮件标题
|
||||
message.Subject = EmailHelper.subjectMail;
|
||||
|
||||
//设置邮件内容
|
||||
message.Body = body;
|
||||
|
||||
//设置邮件发送服务器,服务器根据你使用的邮箱而不同,可以到相应的 邮箱管理后台查看,下面是QQ的
|
||||
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com", 25)
|
||||
;
|
||||
//设置发送人的邮箱账号和密码,POP3/SMTP服务要开启, 密码要是POP3/SMTP等服务的授权码
|
||||
client.Credentials = new System.Net.NetworkCredential(fromMail, pwdMail);//vtirsfsthwuadjfe fhszmpegwoqnecja
|
||||
|
||||
//启用ssl,也就是安全发送
|
||||
client.EnableSsl = true;
|
||||
|
||||
//发送邮件
|
||||
client.Send(message);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void 接受邮件()
|
||||
{
|
||||
using (TcpClient client = new TcpClient("pop.qq.com", 110))
|
||||
using (NetworkStream n = client.GetStream())
|
||||
{
|
||||
GetEmail.ReadLine(n); // Read the welcome message.
|
||||
GetEmail.SendCommand(n, "USER 1040079213@qq.com");
|
||||
GetEmail.SendCommand(n, "PASS odfaizoqdiupbfgi");
|
||||
GetEmail.SendCommand(n, "LIST"); // Retrieve message IDs
|
||||
List<int> messageIDs = new List<int>();
|
||||
while (true)
|
||||
{
|
||||
string line = GetEmail.ReadLine(n); // e.g., "11876"
|
||||
if (line == ".") break;
|
||||
messageIDs.Add(int.Parse(line.Split(' ')[0])); // Message ID
|
||||
}
|
||||
foreach (int id in messageIDs) // Retrieve each message.
|
||||
{
|
||||
GetEmail.SendCommand(n, "RETR " + id);
|
||||
string randomFile = Guid.NewGuid().ToString() + ".eml";
|
||||
using (StreamWriter writer = File.CreateText(randomFile))
|
||||
while (true)
|
||||
{
|
||||
string line = GetEmail.ReadLine(n); // Read next line of message.
|
||||
if (line == ".") break; // Single dot = end of message.
|
||||
if (line == "..") line = "."; // "Escape out" double dot.
|
||||
writer.WriteLine(line); // Write to output file.
|
||||
}
|
||||
GetEmail.SendCommand(n, "DELE " + id); // Delete message off server.
|
||||
}
|
||||
GetEmail.SendCommand(n, "QUIT");
|
||||
}
|
||||
}
|
||||
}
|
||||
//接受邮件pop
|
||||
public class GetEmail
|
||||
{
|
||||
public static void SendCommand(Stream stream, string line)
|
||||
{
|
||||
byte[] data = Encoding.UTF8.GetBytes(line + "\r\n");
|
||||
stream.Write(data, 0, data.Length);
|
||||
string response = ReadLine(stream);
|
||||
if (!response.StartsWith("+OK"))
|
||||
throw new Exception("POP Error: " + response);
|
||||
}
|
||||
public static string ReadLine(Stream s)
|
||||
{
|
||||
List<byte> lineBuffer = new List<byte>();
|
||||
while (true)
|
||||
{
|
||||
int b = s.ReadByte();
|
||||
if (b == 10 || b < 0) break;
|
||||
if (b != 13) lineBuffer.Add((byte)b);
|
||||
}
|
||||
return Encoding.UTF8.GetString(lineBuffer.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
99
Yi.Framework/Yi.Framework.Common/Helper/RandomHelper.cs
Normal file
99
Yi.Framework/Yi.Framework.Common/Helper/RandomHelper.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Yi.Framework.Common
|
||||
{
|
||||
public class RandomHelper
|
||||
{
|
||||
public static string replaceBianLiang(string content)
|
||||
{
|
||||
content = content.Replace("{当前时间}", DateTime.Now.TimeOfDay.ToString());
|
||||
string[] bianliang = new string[] { "{随机字母}", "{随机数字}", "{随机汉字}" };
|
||||
Regex r;
|
||||
int count;
|
||||
string readstr = "";
|
||||
foreach (string str in bianliang)
|
||||
{
|
||||
count = (content.Length - content.Replace(str, "").Length) / str.Length;
|
||||
if (str == "{随机汉字}") readstr = RandChina(count);
|
||||
if (str == "{随机数字}") readstr = GenerateCheckCodeNum(count);
|
||||
if (str == "{随机字母}") readstr = GenerateRandomLetter(count);
|
||||
if (count > readstr.Length) count = readstr.Length;
|
||||
r = new Regex(str.Replace("{", "\\{").Replace("}", "\\}"));
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
content = r.Replace(content, readstr.Substring(i, 1), 1);
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 随机生成字母
|
||||
/// </summary>
|
||||
/// <param name="Length"></param>
|
||||
/// <returns></returns>
|
||||
public static string GenerateRandomLetter(int Length)
|
||||
{
|
||||
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
|
||||
string result = "";
|
||||
int n = Pattern.Length;
|
||||
Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
|
||||
for (int i = 0; i < Length; i++)
|
||||
{
|
||||
int rnd = random.Next(0, n);
|
||||
result += Pattern[rnd];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 随机生成数字
|
||||
/// </summary>
|
||||
/// <param name="codeCount"></param>
|
||||
/// <returns></returns>
|
||||
public static string GenerateCheckCodeNum(int codeCount)
|
||||
{
|
||||
int rep = 0;
|
||||
string str = string.Empty;
|
||||
long num2 = DateTime.Now.Ticks + rep;
|
||||
rep++;
|
||||
Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
|
||||
for (int i = 0; i < codeCount; i++)
|
||||
{
|
||||
int num = random.Next();
|
||||
str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 此函数为生成指定数目的汉字
|
||||
/// </summary>
|
||||
/// <param name="charLen">汉字数目</param>
|
||||
/// <returns>所有汉字</returns>
|
||||
public static string RandChina(int charLen)
|
||||
{
|
||||
int area, code;//汉字由区位和码位组成(都为0-94,其中区位16-55为一级汉字区,56-87为二级汉字区,1-9为特殊字符区)
|
||||
StringBuilder strtem = new StringBuilder();
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < charLen; i++)
|
||||
{
|
||||
area = rand.Next(16, 88);
|
||||
if (area == 55)//第55区只有89个字符
|
||||
{
|
||||
code = rand.Next(1, 90);
|
||||
}
|
||||
else
|
||||
{
|
||||
code = rand.Next(1, 94);
|
||||
}
|
||||
strtem.Append(Encoding.GetEncoding("GB2312").GetString(new byte[] { Convert.ToByte(area + 160), Convert.ToByte(code + 160) }));
|
||||
}
|
||||
return strtem.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user