wanshenmean
4 天以前 64a2aa2301946f777659239247233e47ad1e3076
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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using StackExchange.Profiling;
using WIDESEA_Core;
using WIDESEA_Core.Authorization;
using WIDESEA_Core.BaseController;
using WIDESEA_Core.Caches;
using WIDESEA_Core.Const;
using WIDESEA_Core.Extensions;
using WIDESEA_Core.Helper;
using WIDESEA_Core.HttpContextUser;
using WIDESEA_Core.Utilities;
using WIDESEA_ISystemService;
using WIDESEA_Model;
using WIDESEA_Model.Models;
 
namespace WIDESEA_WMSServer.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/User")]
    [ApiController]
    public class Sys_UserController : ApiBaseController<ISys_UserService, Sys_User>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICacheService _cacheService;
 
        public Sys_UserController(ISys_UserService userService, IHttpContextAccessor httpContextAccessor, ICacheService cacheService) : base(userService)
        {
            _httpContextAccessor = httpContextAccessor;
            _cacheService = cacheService;
        }
 
        [HttpPost, Route("login"), AllowAnonymous]
        public IActionResult Login([FromBody] LoginInfo loginInfo)
        {
            return Json(Service.Login(loginInfo));
        }
 
        /// <summary>
        /// Swagger 登录接口
        /// </summary>
        /// <param name="loginRequest">登录请求</param>
        /// <returns>登录结果</returns>
        [HttpPost, Route("swgLogin"), AllowAnonymous]
        public dynamic SwgLogin([FromBody] SwaggerLoginRequest loginRequest)
        {
            if (loginRequest is null)
            {
                return new { result = false };
            }
 
            try
            {
                LoginInfo loginInfo = new LoginInfo
                {
                    Password = loginRequest.pwd,
                    UserName = loginRequest.name
                };
                var result = Service.Login(loginInfo);
                if (result.Status)
                {
                    HttpContext.SuccessSwagger();
                    Dictionary<string, object>? dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(result.Data.Serialize());
                    if (dict != null)
                    {
                        HttpContext.SuccessSwaggerJwt((dict.ContainsKey("token") ? dict["token"].ToString() : "") ?? "");
                    }
                    string str = HttpContext.GetSuccessSwaggerJwt();
                    return new { result = true };
                }
            }
            catch (Exception ex)
            {
                // 记录异常日志
                return new { result = false, message = ex.Message };
            }
 
            return new { result = false };
        }
 
        [HttpPost, Route("getCurrentUserInfo")]
        public WebResponseContent GetCurrentUser()
        {
            return Service.GetCurrentUserInfo();
        }
 
        [HttpPost, Route("modifyPwd")]
        public IActionResult ModifyPwd(string oldPwd, string newPwd)
        {
            return Json(Service.ModifyPwd(oldPwd, newPwd));
        }
 
        /// <summary>
        /// 获取验证码
        /// 注意:当前版本使用固定验证码 "1234" 用于测试
        /// </summary>
        /// <returns>验证码图片和唯一标识</returns>
        [HttpGet, Route("getVierificationCode"), AllowAnonymous]
        public IActionResult GetVierificationCode()
        {
            // TODO: 生产环境应启用真实验证码生成
            // var html = MiniProfiler.Current.RenderIncludes(_httpContextAccessor.HttpContext);
            string code = "1234"; // 测试用固定验证码,生产环境应使用 VierificationCode.RandomText()
            var data = new
            {
                img = VierificationCode.CreateBase64Imgage(code),
                uuid = Guid.NewGuid()
            };
 
            return Json(data);
        }
        [HttpGet, Route("SerializeJwt"), AllowAnonymous]
        public WebResponseContent SerializeJwt(string code)
        {
            return WebResponseContent.Instance.OK(data: JwtHelper.SerializeJwt(code));
        }
        /// <summary>
        /// 替换Token(刷新令牌)
        /// </summary>
        /// <returns>新的Token</returns>
        [HttpPost, Route("replaceToken")]
        public WebResponseContent ReplaceToken()
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                string token = App.User.GetToken();
                if (string.IsNullOrEmpty(token))
                {
                    return responseContent.Error("Token无效,请重新登录!");
                }
                TokenModelJwt tokenModelJwt = new TokenModelJwt()
                {
                    RoleId = App.User.RoleId,
                    TenantId = App.User.TenantId,
                    UserId = App.User.UserId,
                    UserName = App.User.UserName
                };
                string newToken = JwtHelper.IssueJwt(tokenModelJwt);
 
                App.User.UpdateToke(newToken, App.User.UserId);
                return responseContent.OK(data: newToken);
            }
            catch (Exception ex)
            {
                return responseContent.Error(ex.Message);
            }
 
        }
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        [HttpPost,Route("ModifyUserPwd")]
        public WebResponseContent ModifyUserPwd(string password, string userName)
        {
            return Service.ModifyUserPwd(password,userName);
        }
    }
 
    public class SwaggerLoginRequest
    {
        public string name { get; set; }
        public string pwd { get; set; }
    }
}