sunpengfei
2025-08-01 937dbb7cc77729b1a7d35b87fd421bc44046c108
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.User.Application
{
    /// <summary>
    /// 密码登录
    /// </summary>
    public class AuthPasswordLoginCommandHandler : IRequestHandler<AuthPasswordLoginCommand, AuthPasswordLoginCallback>
    {
        private readonly IRepository<UserAuth> userAuthRep;
        private readonly IRepository<UserInfo> userInfoRep;
 
        public AuthPasswordLoginCommandHandler(
            IRepository<UserAuth> userAuthRep,
            IRepository<UserInfo> userInfoRep)
        {
            this.userAuthRep = userAuthRep;
            this.userInfoRep = userInfoRep;
        }
 
        public async Task<AuthPasswordLoginCallback> Handle(AuthPasswordLoginCommand request, CancellationToken cancellationToken)
        {
            var userInfo = await userInfoRep.AsQueryable().AsNoTracking()
                .Include(it => it.UserAuth)
                .Where(it => it.UserAuth.UserName == request.UserName && it.Type == request.Type)
                .Select(it => new
                {
                    it.Id,
                    it.UserAuth.AvatarId,
                    it.UserAuth.Name,
                    it.UserAuth.UserName,
                    it.UserAuth.PhoneNumber,
                    it.UserAuth.Password
                })
                .FirstOrDefaultAsync(cancellationToken);
            if (userInfo == null) throw Oops.Oh(EnumUserErrorCodeType.u1000);
            if (!PBKDF2Encryption.Compare(request.Password, userInfo.Password)) throw Oops.Oh(EnumUserErrorCodeType.u1000);
            var logier = new CurrentLogier
            {
                UserInfoId = userInfo.Id,
                AvatarId = userInfo.AvatarId,
                Name = userInfo.Name,
                UserName = userInfo.UserName,
                PhoneNumber = userInfo.PhoneNumber,
            };
            JwtUtils.GenerateToken(logier);
            return new AuthPasswordLoginCallback();
        }
    }
}