/** * 命名空间: CZFW.Authorization.Logic * * 功 能: N/A * 类 名: UserLogic * * Ver 变更日期 负责人 变更内容 * ─────────────────────────────────── * V0.01 2016/12/17 1:27:33 曹湘 初稿 * * Copyright (c) 2016 CHUANGZHIKEJI Corporation. All rights reserved. *┌──────────────────────────────────┐ *│ 此技术信息为本公司机密信息,未经本公司书面同意禁止向第三方披露. │ *│ 版权所有:创执科技(北京)有限公司                │ *└──────────────────────────────────┘ */ using System; using System.Collections.Generic; using System.Linq; using CZFW.Core.Security; using CZFW.Framework.Interface; using CZFW.Framework.Model; using CZFW.Framework.Model.ViewModel; using CZFW.Framework.Model.Entity; namespace CZFW.Framework.Logic { public class SysUserLogic : LogicBase, ISysUser { public ResultModel AddUser(SysUserEntity entity) { ResultModel model = AddEntity(entity); var userLogon = new SysUserLogonEntity { CreatedBy = entity.Id, LastModifyBy = entity.Id, CreatedTime = DateTime.Now, UserId = entity.Id, UserPassword = "000000", UserSecretKey = DesEncrypt.GetMd5Hash(Guid.NewGuid().ToString()).ToLower() }; userLogon.UserPassword = DesEncrypt.GetMd5Hash(DesEncrypt.Encrypt(DesEncrypt.GetMd5Hash(userLogon.UserPassword).ToLower(), userLogon.UserSecretKey).ToLower()).ToLower(); ISysUserLogon logonLogic = new SysUserLogonLogic(); model = logonLogic.AddEntity(userLogon); return model; } public OperatorModel CheckLogin(string username, string password, out string msg) { SysUserEntity userEntity = GetEntity(t => t.Account == username); msg = null; var model = new OperatorModel(); if (userEntity != null) { if (userEntity.Enabled == true) { ISysUserLogon logonLogic = new SysUserLogonLogic(); SysUserLogonEntity userLogOnEntity = logonLogic.GetEntity(x => x.UserId == userEntity.Id); string dbPassword = DesEncrypt.GetMd5Hash(DesEncrypt.Encrypt(DesEncrypt.GetMd5Hash(password).ToLower(), userLogOnEntity.UserSecretKey).ToLower()).ToLower(); if (dbPassword == userLogOnEntity.UserPassword) { DateTime lastVisitTime = DateTime.Now; int? logOnCount = (userLogOnEntity.LogonCount) + 1; if (userLogOnEntity.LastVisitTime != null) { userLogOnEntity.PreviousVisitTime = userLogOnEntity.LastVisitTime; } userLogOnEntity.LastVisitTime = lastVisitTime; userLogOnEntity.LogonCount = logOnCount; logonLogic.EditEntity(userLogOnEntity); if (userEntity.IsAdministrator != null) model.IsSystem = (bool)userEntity.IsAdministrator; model.Avatar = userEntity.HeadIcon; model.DisplayName = userEntity.NickName; model.UserId = userEntity.Id; return model; } msg = "登录失败"; return null; } msg = "登录失败"; return null; } msg = "登录失败"; return null; } public ResultModel DeleteAccount(int id) { var entity = GetEntity(id); entity.Deleted = true; return EditEntity(entity); } public ResultModel DisableAccount(int id) { var entity = GetEntity(id); entity.Enabled = false; return EditEntity(entity); } public ResultModel EnableAccount(int id) { var entity = GetEntity(id); entity.Enabled = true; return EditEntity(entity); } public ResultModel GetNickName(int id) { var user = GetEntity(id)??new SysUserEntity(); return new ResultModel { Success = true, Code = user.NickName }; } public UserItem GetUserItem(int id) { return GetQueryable().Where(x => x.Id == id).Select(x => new UserItem() { Id = x.Id, Account = x.Account, Enabled = x.Enabled, HeadIcon = x.HeadIcon, MobilePhone = x.MobilePhone, NickName = x.NickName }).FirstOrDefault(); } public IList GetUserItems(int pageSize, int pageNumber, out int rows) { var queryable = Queryable.Where(x => !(bool)x.Deleted); rows = queryable.Count(); return queryable.Skip(pageSize * (pageNumber - 1)).Take(pageSize).Select(x => new UserItem() { Id = x.Id, Account = x.Account, Enabled = x.Enabled, MobilePhone = x.MobilePhone, NickName = x.NickName, HeadIcon = x.HeadIcon }).ToList(); } public IList GetUserItemsByIdList(IList userIds) { userIds = userIds.Distinct().ToList(); var queryable = Queryable.Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.Account, x.Enabled, x.HeadIcon, x.MobilePhone, x.NickName, x.SortOrder }).OrderByDescending(x => x.SortOrder).ToList(); return queryable.Select(x => new UserItem() { Account = x.Account, Enabled = x.Enabled, HeadIcon = x.HeadIcon, Id = x.Id, MobilePhone = x.MobilePhone, NickName = x.NickName }).ToList(); } public ResultModel Logout(int id) { CZDbContext context = new CZDbContext(); var entity = context.Set().FirstOrDefault(x =>x.Id ==id); entity.UserOnline = false; var count = context.SaveChanges(); return new ResultModel { Success = count > 0 }; } public ResultModel UpdateUser(SysUserEntity entity) { throw new NotImplementedException(); } } }