username@email.com
2023-08-16 9c7ff006100d327b1b60615304de348f5ad36c63
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using DocumentServiceAPI.Application.UserAndLogin.Services;
using DocumentServiceAPI.Application.UserAndLogin.ViewMode;
using DocumentServiceAPI.Utility;
using Furion.DynamicApiController;
using Furion.JsonSerialization;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DocumentServiceAPI.Application.UserAndLogin
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [ApiDescriptionSettings("LogInAndUser")]
    [DynamicApiController]
 
    public class LogInController
    {
     private   TokenService _tokenService;
        private UserService _userService;
        public LogInController(TokenService tokenService,UserService  userService) {
            _tokenService = tokenService;
            _userService = userService;
         }
        /// <summary>
        /// 登录 1 输入员工账户或者租户账户,2 选择租户账户,3 选择公司
        /// </summary>
        /// <returns>登录信息</returns>
        [HttpPost("LogoIn")]
        public async Task<RetLoginVM> LogoIn (LoginPsWordIN Parma)
        {
              bool needtoken=false;
            if (Parma.TenantId==null)
            {
                //如果还没确定租户ID 需要先确定租户ID
                List<TenderVM>Tender = await _userService.GetUserTenderList(Parma.ITCode);
                
                switch (Tender.Count)
                {
                    case 0:
                        throw Oops.Oh($"账户或者密码错误");
                    case 1:
                         needtoken = await _userService.CheckPsw(Tender[0].TenderId, Tender[0].EmployeeID,Parma.PassWord);
                       
                        if(!needtoken)
                            throw Oops.Oh($"账户或者密码错误");
 
                        Parma.TenantId = Tender[0].TenderId;
                        Parma.employeeID = Tender[0].EmployeeID;
                        if (Parma.UnitID == null)
                        {
                        
                            List<UnitVM> Units = await _userService.GetUserUnitList(Parma.TenantId ?? 0);
                            if (Units.Count == 0)
                            {
                                //没有公司
                                needtoken = true;
                            }
                            else if (Units.Count == 1)
                            {
                                Parma.UnitID = Units[0].UnitID;
                                needtoken = true;
                            }
                            else
                            {
                                //其他需要用户选择租户
                                return new RetLoginVM
                                {
                                    NeedNext = true,
                                    Units = Units,
 
                                };
                            }
                        }
                    
                        break;
                      
                    default:
                        //其他需要用户选择租户
                        return new RetLoginVM
                        {
                            NeedNext = true,
                            Tenants = Tender,
 
                        };
 
                      
                }
 
 
 
 
              
            }
            else if(Parma.UnitID==null)
            {
 
                var Units = await _userService.GetUserUnitList(Parma.TenantId??0);
                if(Units.Count==0)
                {
                    //没有公司
                    needtoken = true;
                }
                else if (Units.Count==1) {
                    Parma.UnitID= Units[0].UnitID;
                    needtoken = true;
                }
                else
                {
                    //其他需要用户选择租户
                    return new RetLoginVM
                    {
                        NeedNext = true,
                       Units = Units,
 
                    };
                }
 
            }
 
            if(needtoken|| Parma.UnitID!=null)
            {
                
                TenderVM Tender = (await _userService.GetUserTenderList(Parma.ITCode)).Where(x=>x.TenderId==Parma.TenantId&&x.EmployeeID==Parma.employeeID).SingleOrDefault();
                if(Tender==null)
                {
                    throw Oops.Oh($"账户密码或者租户选择错误");
                }
                needtoken = await _userService.CheckPsw(Tender. TenderId, Tender.EmployeeID, Parma.PassWord);
 
                if (!needtoken)
                    throw Oops.Oh($"账户或者密码错误");
              var token=  _tokenService.CreateToken(new Model.JwtInfo
                {
                    EID = Tender.EmployeeID,
                    LogInSource = Parma.Form,
                    TEID = Tender.TenderId,
                    UID = Parma.UnitID,
                });
                return new RetLoginVM
                {
                    NeedNext = false,
                   Token = token,
 
                };
            }
            throw Oops.Oh($"参数错误");
 
 
        }
 
 
        
 
        /// <summary>
        /// 获取公司列表
        /// </summary>
        /// <returns>登录信息</returns>      
        public bool GetUnitList()
        {
 
            return true;
        }
        /// <summary>
        /// 获取公司列表
        /// </summary>
        /// <returns>登录信息</returns>
        public bool GetTenantInfoList()
        {
 
            return true;
        }
        public string Get()
        {
            return $"Hello {nameof(Furion)}";
        }
    }
}