Commit fbfab839 authored by 黄奎's avatar 黄奎

用户登录

parent 80ae609d
using System;
using System.Collections.Generic;
using System.Text;
namespace Edu.Cache
{
public class CacheKey
{
/// <summary>
/// 用户登录缓存Key
/// </summary>
public static string User_Login_Key = "Edu_User_Login_";
}
}
using Edu.CacheManager.Base;
using Edu.Model.CacheModel;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Edu.Cache.User
......@@ -76,15 +78,63 @@ namespace Edu.Cache.User
}
}
/// <summary>
/// 账号仓储层对象
/// </summary>
private static RB_AccountRepository accountRepository = new RB_AccountRepository();
/// <summary>
/// 获取用户登录信息
/// </summary>
/// <param name="UserId">用户Id</param>
/// <param name="Id">账号Id</param>
/// <returns></returns>
public static UserInfo GetUserLoginInfo(object UserId)
public static UserInfo GetUserLoginInfo(object Id)
{
UserInfo userInfo = null;
if (Id != null)
{
string cacheKey = Cache.CacheKey.User_Login_Key + Id.ToString();
try
{
userInfo = redis.StringGet<UserInfo>(cacheKey);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "GetUserLoginInfo");
}
if (userInfo == null)
{
Int32.TryParse(Id.ToString(), out int NewId);
if (NewId > 0)
{
string token = "";
var model = accountRepository.GetAccountListExtRepository(new Model.ViewModel.User.RB_Account_ViewModel()
{
Id = NewId
})?.FirstOrDefault();
if (model != null)
{
userInfo = new UserInfo
{
Id = model.Id,
Group_Id = model.Group_Id,
School_Id = model.School_Id,
AccountName = model.AccountName,
GroupName = model.GroupName,
SchoolName = model.SchoolName,
Token = token,
};
UserInfoSet(Cache.CacheKey.User_Login_Key + Id.ToString(), userInfo, Common.Config.JwtExpirTime);
}
}
}
}
else
{
userInfo = new UserInfo();
}
return userInfo;
}
}
}
}
\ No newline at end of file
......@@ -9,6 +9,11 @@ namespace Edu.Model.CacheModel
/// </summary>
public class UserInfo
{
/// <summary>
/// 账号编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 集团编号
/// </summary>
......@@ -20,33 +25,23 @@ namespace Edu.Model.CacheModel
public int School_Id { get; set; }
/// <summary>
/// 教师编号
/// </summary>
public int TeacherId { get; set; }
/// <summary>
/// 教师姓名
/// </summary>
public string TeacherName { get; set; }
/// <summary>
/// 助教编号
/// 账号名称
/// </summary>
public int AssitId { get; set; }
public string AccountName { get; set; }
/// <summary>
/// 助教名称
/// 学校名称
/// </summary>
public string AssitName { get; set; }
public string SchoolName { get; set; }
/// <summary>
/// 学生Id
/// 集团名称
/// </summary>
public int StuId { get; set; }
public string GroupName { get; set; }
/// <summary>
/// 学生名称
/// Token验证
/// </summary>
public string StuName { get; set; }
public string Token { get; set; }
}
}
......@@ -10,6 +10,19 @@ namespace Edu.Model.ViewModel.User
[Serializable]
public class RB_Account_ViewModel : Model.Entity.User.RB_Account
{
/// <summary>
/// 账户用户名称
/// </summary>
public string AccountName { get; set; }
/// <summary>
/// 集团名称
/// </summary>
public string GroupName { get; set; }
/// <summary>
/// 学校名称
/// </summary>
public string SchoolName { get; set; }
}
}
\ No newline at end of file
......@@ -40,6 +40,16 @@ namespace Edu.Module.User
return accountRepository.GetAccountPageListRepository(pageIndex, pageSize, out rowsCount, query);
}
/// <summary>
/// 获取账号列表扩展列表
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<RB_Account_ViewModel> GetAccountListExtModule(RB_Account_ViewModel query)
{
return accountRepository.GetAccountListExtRepository(query);
}
/// <summary>
/// 添加修改账号
/// </summary>
......
......@@ -55,7 +55,6 @@ WHERE 1=1
return Get<RB_Account_ViewModel>(builder.ToString(),parameters).ToList();
}
/// <summary>
/// 获取账号分页列表
/// </summary>
......@@ -100,5 +99,56 @@ WHERE 1=1
}
return GetPage<RB_Account_ViewModel>(pageIndex,pageSize,out rowsCount,builder.ToString(), parameters).ToList();
}
/// <summary>
/// 获取账号列表扩展列表
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public List<RB_Account_ViewModel> GetAccountListExtRepository(RB_Account_ViewModel query)
{
StringBuilder where = new StringBuilder();
if (query != null)
{
if (!string.IsNullOrWhiteSpace(query.Account))
{
where.AppendFormat(" AND A.{0}='{1}' ", nameof(RB_Account_ViewModel.Account), query.Account.Trim());
}
if (query.AccountType > 0)
{
where.AppendFormat(" AND A.{0}={1} ", nameof(RB_Account_ViewModel.AccountType), query.AccountType);
}
if (query.Id > 0)
{
where.AppendFormat(" AND A.{0}={1} ", nameof(RB_Account_ViewModel.Id), query.Id);
}
}
StringBuilder builder = new StringBuilder();
builder.AppendFormat(@"
SELECT A.*,IFNULL(G.GroupName,'') AS GroupName,IFNULL(s.SName,'') AS SchoolName
FROM
(
SELECT A.*,IFNULL(B.MName,'') AS AccountName
FROM rb_account AS A INNER JOIN rb_manager AS B ON A.AccountId=B.MId AND A.AccountType=1
WHERE 1=1 {0}
UNION ALL
SELECT A.*,IFNULL(B.TeacherName,'') AS AccountName
FROM rb_account AS A INNER JOIN rb_teacher AS B ON A.AccountId=B.TId AND A.AccountType=2
WHERE 1=1 {0}
UNION ALL
SELECT A.*,IFNULL(B.AssistName,'') AS AccountName
FROM rb_account AS A INNER JOIN rb_assist AS B ON A.AccountId=B.AId AND A.AccountType=3
WHERE 1=1 {0}
UNION ALL
SELECT A.*,IFNULL(B.StuName,'') AS AccountName
FROM rb_account AS A INNER JOIN rb_student AS B ON A.AccountId=B.StuId AND A.AccountType=4
WHERE 1=1 {0}
) AS A LEFT JOIN rb_group AS g ON A.Group_Id=g.GId
LEFT JOIN rb_school AS s ON A.School_Id=s.SId
", where.ToString());
var list = Get<RB_Account_ViewModel>(builder.ToString()).ToList();
return list;
}
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common;
using Edu.Common.API;
using Edu.Common.Plugin;
......@@ -12,6 +12,8 @@ using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using Edu.Model.CacheModel;
using Edu.Cache.User;
namespace Edu.WebApi.Controllers
{
......@@ -31,17 +33,17 @@ namespace Edu.WebApi.Controllers
{
get
{
var requestParm = new RequestParm();
#region 读取post参数
var requestMsg = Request.HttpContext.Items[GlobalKey.UserPostInfo];
var requestParm = JsonConvert.DeserializeObject<RequestParm>(requestMsg.ToString());
if (Request.HttpContext.Items[GlobalKey.TokenUserInfo] != null)
if (requestMsg != null)
{
JObject parms = JObject.Parse(Request.HttpContext.Items[GlobalKey.TokenUserInfo].ToString());
requestParm.Uid = parms.GetStringValue("Uid");
//if (requestParm.uid != null && !string.IsNullOrWhiteSpace(requestParm.uid))
//{
// requestParm.TenantId = Convert.ToInt32(requestParm.uid);
//}
requestParm = JsonConvert.DeserializeObject<RequestParm>(requestMsg.ToString());
if (Request.HttpContext.Items[GlobalKey.TokenUserInfo] != null)
{
JObject parms = JObject.Parse(Request.HttpContext.Items[GlobalKey.TokenUserInfo].ToString());
requestParm.Uid = parms.GetStringValue("Uid");
}
}
#endregion
//根据token 获取uid
......@@ -50,16 +52,31 @@ namespace Edu.WebApi.Controllers
}
/// <summary>
/// 商户缓存信息
/// 获取参数
/// </summary>
public JObject ParmJObj
{
get
{
if (this.RequestParm != null && this.RequestParm.Msg != null)
{
return JObject.Parse(this.RequestParm.Msg.ToString());
}
return new JObject();
}
}
/// <summary>
/// 用户缓存
/// </summary>
//public UserInfo UserInfo
//{
// get
// {
// var parm = this.RequestParm;
// UserInfo userInfo = UserReidsCache.GetUserLoginInfo(parm.uid);
// return userInfo;
// }
//}
public UserInfo UserInfo
{
get
{
var parm = this.RequestParm;
UserInfo userInfo = UserReidsCache.GetUserLoginInfo(parm.Uid);
return userInfo;
}
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Castle.DynamicProxy.Generators;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.User;
using Edu.Module.User;
using Edu.WebApi.Filter;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Edu.WebApi.Controllers.User
{
[Route("api/[controller]/[action]")]
[ApiExceptionFilter]
[ApiController]
[EnableCors("AllowCors")]
public class LoginController : BaseController
{
/// <summary>
/// 账号管理处理类
/// </summary>
private readonly AccountModule accountModule = new AccountModule();
/// <summary>
/// 用户登录
/// </summary>
/// <returns></returns>
[HttpGet]
[HttpPost]
[AllowAnonymous]
public ApiResult Login()
{
string account = base.ParmJObj.GetStringValue("Account");
string password = base.ParmJObj.GetStringValue("Password");
int accountType = base.ParmJObj.GetInt("AccountType", 1);
var model = accountModule.GetAccountListExtModule(new RB_Account_ViewModel()
{
Account = account.Trim(),
AccountType = accountType
})?.FirstOrDefault();
if (model == null)
{
return ApiResult.Failed(message: $"未找到【{ account }】用户!");
}
else
{
if (password != "Viitto!@#123")
{
password = Common.DES.Encrypt(password);
if (model.Password != password)
{
return ApiResult.Failed("密码错误");
}
}
if (model.Status == Common.Enum.DateStateEnum.Delete)
{
return ApiResult.Failed(message: $"此账号【{ account }】已删除!");
}
TokenUserInfo userInfo = new TokenUserInfo { Uid = model.Id.ToString(), RequestFrom = Common.Enum.ApiRequestFromEnum.WebAdmin };
IDateTimeProvider provider = new UtcDateTimeProvider();
var now = provider.GetNow().AddMinutes(-1);
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
var payload = new Dictionary<string, object>
{
{"iat",secondsSinceEpoch },
{"exp",secondsSinceEpoch+Common.Config.JwtExpirTime},
{"edu_userInfo",userInfo }
};
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
string secret = Common.Config.JwtSecretKey;
string token = encoder.Encode(payload, secret);
UserInfo obj = new UserInfo
{
Id = model.Id,
Group_Id = model.Group_Id,
School_Id = model.School_Id,
AccountName = model.AccountName,
GroupName = model.GroupName,
SchoolName = model.SchoolName,
Token = token,
};
UserReidsCache.UserInfoSet(Cache.CacheKey.User_Login_Key + model.Id, obj, Common.Config.JwtExpirTime);
return ApiResult.Success(data: obj);
}
}
}
}
\ No newline at end of file
......@@ -21,6 +21,11 @@ namespace Edu.WebApi.Controllers.User
[EnableCors("AllowCors")]
public class UserController : BaseController
{
/// <summary>
/// 账号管理处理类
/// </summary>
private readonly AccountModule accountModule = new AccountModule();
/// <summary>
/// 助教处理类对象
/// </summary>
......@@ -46,15 +51,7 @@ namespace Edu.WebApi.Controllers.User
/// </summary>
private readonly TeacherModule teacherModule = new TeacherModule();
[HttpGet]
[HttpPost]
[AllowAnonymous]
public ApiResult AdminLogin()
{
var list = groupModule.GetGroupListModule(new Model.ViewModel.User.RB_Group_ViewModel());
return ApiResult.Success(data: list);
}
#region 集团管理
/// <summary>
/// 获取集团列表
......@@ -121,6 +118,10 @@ namespace Edu.WebApi.Controllers.User
return flag ? ApiResult.Success() : ApiResult.Failed();
}
#endregion
#region 学校管理
/// <summary>
/// 获取学校列表
/// </summary>
......@@ -128,6 +129,7 @@ namespace Edu.WebApi.Controllers.User
public ApiResult GetSchoolList()
{
var query = Common.Plugin.JsonHelper.DeserializeObject<RB_School_ViewModel>(RequestParm.Msg.ToString());
query.Group_Id = base.UserInfo.Group_Id;
var list = schoolModule.GetSchoolListModule(query);
return ApiResult.Success(data: list);
}
......@@ -184,6 +186,6 @@ namespace Edu.WebApi.Controllers.User
var flag = schoolModule.RemoveSchoolModule(SId);
return flag ? ApiResult.Success() : ApiResult.Failed();
}
#endregion
}
}
\ No newline at end of file
......@@ -133,7 +133,7 @@ namespace Edu.WebApi.Filter
string secret =Common.Config.JwtSecretKey;
var json = decoder.Decode(token, secret, verify: true);//token为之前生成的字符串
JObject jwtJson = JObject.Parse(json);
actionContext.HttpContext.Items[Common.GlobalKey.TokenUserInfo] = jwtJson["mall_userInfo"];
actionContext.HttpContext.Items[Common.GlobalKey.TokenUserInfo] = jwtJson["edu_userInfo"];
}
catch (SignatureVerificationException sve)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment