heyuntao
2023-05-05 cc09b6fe6ffac34a4eeeb26d313b187713cae0de
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
using CommonUtil;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Operater.DAL;
using Operater.DbModel;
using Operater.DTO.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
 
namespace Api.Operater.Controllers
{
    [Route(TopConstants.API_ROUTE)]
    public class LoginController : BaseController
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult LoginCheck([FromBody] LoginCheckRequest request)
        {
           // string pwdEn = SecurityUtil.MD5Encrypt(request.Pwd, false);
            var userInfo = new UserInfoDAL().ListGet(t => t.UserName == request.UserName && t.Pwd == request.Pwd);
            if (userInfo.IsNull() || userInfo.Count == 0)
            {
                return Error("账号或密码错误!");
            }
            LoginCheckResponse loginCheckResponse = new LoginCheckResponse()
            {
                UserId = userInfo[0].Id
            };
            //生成token
            string enKey = "ophwuv2E1qyy6R2r";
            TokenUserInfo tokenUserInfo = new TokenUserInfo()
            {
                Id = loginCheckResponse.UserId,
                ExpirationTime = DateTime.Now.AddHours(5)
            };
            string EncryStr = SecurityUtil.AesEncrypt(JSONUtil.ObjectToJson(tokenUserInfo), enKey, "0102030405060708");
            loginCheckResponse.LoginToken = EncryStr;
            return Success(loginCheckResponse);
        }
 
 
 
        public IActionResult AddUser([FromBody] AddUserRequest request)
        {
            string userId = string.Empty;
            while (true)
            {
                userId = GetRandomString(6);
                if (userId.StartsWith("0"))
                {
                    continue;
                }
                var hasExit = new UserInfoDAL().GetById(userId);
                if (!hasExit.IsNull() && !hasExit.Id.IsNullOrEmpty())
                {
                    continue;
                }
                break;
            }
            string enPwd = SecurityUtil.MD5Encrypt(request.Pwd, false);
            UserInfo userInfo = new UserInfo()
            {
                Id = userId,
                UserName = request.UserName,
                Pwd = enPwd,
                Mobile = request.Mobile
            };
            var res = new UserInfoDAL().Insert(userInfo);
            return Success(res);
        }
 
        public string GetRandomString(int len)
        {
            string s = "1234567890";
            string reValue = string.Empty;
            Random rd = new Random();
            while (reValue.Length < len)
            {
                string s1 = s[rd.Next(0, s.Length)].ToString();
                if (reValue.IndexOf(s1) == -1)
                    reValue += s1;
            }
            return reValue;
        }
    }
}