LoginParse.cs 2.81 KB
/*
 * 登录解析
 * 处理登录逻辑,验证客户段提交的账号密码,保存登录信息
 */
using Hh.Mes.Common;
using Hh.Mes.Common.Redis;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Linq;

namespace Hh.Mes.Service.SystemAuth
{
    public class LoginParse : RepositorySqlSugar<sys_user>
    {

        public LoginResult Do(PassportLoginRequest model)
        {
            var result = new LoginResult();
            try
            {
                model.Trim();
                //获取应用信息
                var appInfo = Context.Queryable<sys_info>().First(t => t.appKey == model.AppKey);
                if (appInfo == null) throw new Exception("应用不存在");

                if (Encryption.Decrypt(appInfo.appSecret) != "hhweb2.0") throw new Exception("应用密钥不正确!");
                //获取用户信息
                var userInfo = Context.Queryable<sys_user>().First(t => t.account == model.Account);
                if (userInfo == null || userInfo.account != model.Account) throw new Exception("登录失败,请检查用户名和密码!");
                if (Encryption.Decrypt(userInfo.password) != model.Password) throw new Exception("登录失败,请检查用户名和密码!");
                //获取用户的所有组织
                var orgsList = Context.Queryable<sys_relevance, sys_dept>((x, y) => new JoinQueryInfos(
                                                 JoinType.Inner, x.secondId == y.id && x.relKey == Define.USERORG && x.firstId == userInfo.id))
                                      .Select((x, y) => y).ToList();

                var token = Guid.NewGuid().ToString("N");
                var currentSession = new UserAuthSession
                {
                    Id = userInfo.id,
                    Account = userInfo.account,
                    Name = userInfo.name,
                    Sex= userInfo.sex,
                    Idcard = userInfo.idcard,

                    Token= token,
                    Organizations = string.Join(",", orgsList.Select(u => u.Name).ToList()),
                    CreateTime = DateTime.Now,
                };

                //创建Session
                //_cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10));
                var cl = new RedisBase();
                cl.SetT(token, currentSession, cl.dayTime);
                result.Code = 200;
                result.ReturnUrl = appInfo.returnUrl;
                result.currentSession = currentSession;
                result.Token = token;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }

    }
}