LoginParse.cs 3.84 KB
/*
 * 登录解析
 * 处理登录逻辑,验证客户段提交的账号密码,保存登录信息
 */
using Hh.Mes.Common;
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.Collections.Generic;
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) ?? 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();

                //用户角色ID
                var userRoleIds = Context.Queryable<sys_relevance>()
                                         .Where(u => u.firstId == userInfo.id && u.relKey == Define.USERROLE)
                                         .Select(u => u.secondId)
                                         .ToList();

                //根据角色找到角色名称
                var userNameList = Context.Queryable<sys_role>()
                                        .Where(t =>userRoleIds.Contains(t.id))
                                        .Select(t =>t.name ).ToList();

                var currentSession = new UserAuthSession
                {
                    Id = userInfo.id,
                    Account = userInfo.account,
                    Name = userInfo.name,
                    Sex = userInfo.sex,
                    Idcard = userInfo.idcard,
                    Organizations = string.Join(",", orgsList.Select(u => u.Name).ToList()),
                    CreateTime = DateTime.Now,
                    RoleName= string.Join(",", userNameList.ToList())
                };
                var option = new Dictionary<string, object>
                {
                    {nameof(currentSession.Id), currentSession.Id},
                    {nameof(currentSession.Account),currentSession.Account},
                    {nameof(currentSession.Name), currentSession.Name},
                    {nameof(currentSession.Sex), currentSession.Sex},
                    {nameof(currentSession.Idcard), currentSession.Idcard},
                    {nameof(currentSession.Organizations), currentSession.Organizations},
                    {nameof(currentSession.CreateTime), currentSession.CreateTime},
                };
                var token = JwtEncryption.Encode(option);
                currentSession.Token = token;
                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;
        }

    }
}