liaoxujun@qq.com
2023-08-21 5b6a33b320199d6d7c08d7e905f5488d59e2d589
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using DocumentServiceAPI.Application.UserAndLogin.Services;
using DocumentServiceAPI.Application.UserAndLogin.ViewMode;
using DocumentServiceAPI.Utility;
using Furion.DynamicApiController;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Tokens;
using NetTaste;
using Newtonsoft.Json.Linq;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
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 选择公司
        /// 当密码为空时,带入用户中心token即可实现登录
        /// </summary>
        /// <returns>登录信息</returns>
        [HttpPost("LogoIn")]
        public async Task<RetLoginVM> LogoIn (  LoginPsWordIN Parma)
        {
            //zctok校验成功是否标志
            bool IsTokenPass=false;
     
               if(string.IsNullOrEmpty(Parma.PassWord))
            {
 
                var Request = App.HttpContext.Request;
                string stoken = Request.Headers["Authorization"];
                if (stoken == null)
                    throw Oops.Oh("没有输入密码");
                stoken = stoken.Replace("Bearer ", "");
 
                var isvale = _tokenService.CheckJwt(stoken, out SecurityToken securityToken);
                JwtSecurityToken jwtToken = securityToken as JwtSecurityToken;
                string itcode = jwtToken.Claims.FirstOrDefault(claim => claim.Type == "sub")?.Value;
                if (isvale == false)
                    throw Oops.Oh("登录信息出错");
                if(Parma.ITCode!= itcode)
                    throw Oops.Oh("用户名错误");
                IsTokenPass=true;
 
            }
 
                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:
                        if (!needtoken)
                        {
                            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($"账户密码或者租户选择错误");
                    }
                if (!IsTokenPass)
                {
                    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)}";
        }
    }
}