username@email.com
2021-07-21 fc1fe356e1cb43c7654c653e8921ee9e72150f96
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using zhengcaioa.IService;
using zhengcaioa.Models;
using Microsoft.AspNetCore.Http;
using CommonToolsCore;
using Newtonsoft.Json;
using DTO;
using static CommonToolsCore.SendMailHelper;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Net.Http.Headers;
 
namespace zhengcaioa.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IUserService _userService;
        private readonly IConfiguration _configuration;
        private readonly IHttpClientFactory _clientFactory;
 
 
        public HomeController(ILogger<HomeController> logger, IUserService userService
              , IConfiguration configuration
            , IHttpClientFactory clientFactory)
        {
            _logger = logger;
            _userService = userService;
            _configuration = configuration;
            _clientFactory = clientFactory;
        }
       
        public IActionResult Index()
        {
            //SMSHelper.PushWToUserBySMS("15800678244", "2021-02-24");
            //MailModel model = new MailModel();
            //model.ReceiverAddress = "272629192@qq.com";
            //model.ReceiverName = "尧";
            //model.Title = "你好";
            //model.Content = "你好呀";
            //model.Host = "smtp.163.com";
            //model.Port = 25;
            //model.SenderName = "尧";
            //model.SenderAddress = "yzy2002yzy@163.com";
            //model.SenderPassword = "";
 
            //SendMailHelper.SendMail(model);
            //String ssssss = _userService.GetName();
            //string sss = "1111";
 
            //ViewBag.aaaa = ssssss;
            var session = HttpContext.Session.GetString(("User"));
            if (session == null || session == "")
            {
                return View("Login");
            }
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;            
            return View();
        }
 
 
        public async Task<IActionResult> HomeIndexAsync()
        {
            var session = HttpContext.Session.GetString(("User"));
            if (session == null || session == "")
            {
                return View("Login");
            }
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ViewData["curentuser"] = curentuser;
            if (string.IsNullOrEmpty(curentuser.Yaoqingma))
            {
                try
                {
                    var pltUserDTO = _userService.GetPltUserEntity(curentuser.Id);
                    string huiyuanurl = _configuration.GetSection("huiyuanurl").Value;
                    string GetYGPromoteCode = _configuration.GetSection("GetYGPromoteCode").Value;
                    Uri postUrl2 = new Uri(huiyuanurl + GetYGPromoteCode);
 
                    JObject questions2 = new JObject();
                    questions2.Add("UserID", pltUserDTO.Id);
                    questions2.Add("UserName", pltUserDTO.UserName);
 
                    string requestJson2 = questions2.ToString();
                    string result2 = string.Empty;
 
                    using (HttpContent httpContent = new StringContent(requestJson2))
                    {
                        httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
                        var httpClient2 = _clientFactory.CreateClient();
                        httpClient2.Timeout = new TimeSpan(0, 0, 10);
                        var Result2 = await httpClient2.PostAsync(postUrl2, httpContent);
                        result2 = Result2.Content.ReadAsStringAsync().Result;
                    }
 
                    _logger.LogInformation("result2:" + result2);
                    JObject jobjectresult = (JObject)JsonConvert.DeserializeObject(result2);
 
                    if (jobjectresult["code"] != null && jobjectresult["code"].ToString() == "1")
                    {
                        curentuser.Yaoqingma = jobjectresult["data"].ToString();
                        pltUserDTO.Yaoqingma = curentuser.Yaoqingma;
                        ResultEntity resultEntity = _userService.savePltUser(pltUserDTO);
                    }
                }
                catch (Exception ex)
                {
 
                }
 
            }
            string yaoqingma = _configuration.GetSection("yaoqingma").Value;
            ViewBag.yaoqingma = yaoqingma + curentuser.Yaoqingma;
            return View();
        }
 
        public IActionResult Privacy()
        {
            return View();
        }
 
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
 
        public IActionResult Login()
        {
            var session = HttpContext.Session.GetString(("User"));
            if (session != null&& session!="")
            {
              var curentuser =  JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User")); 
                ViewData["curentuser"] = curentuser;
                return View("Index");
            }
            return View();
        }
        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult VerifyImage()
        {
            var validateCodeType = new ValidateCode_Style6();
            string code = string.Empty;
 
            byte[] bytes = validateCodeType.CreateImage(out code);
            HttpContext.Session.SetString("verifyCode", code);
 
            return File(bytes, @"image/jpeg");
        }
 
        [HttpPost]
        public IActionResult Login(string txtID, string txtPwd, string verifyCode, string hidIp)
        {
            try
            {
 
            if (string.IsNullOrEmpty(verifyCode))
            {
                ModelState.AddModelError("error", "请录入验证码");
                return View();
            }
            if (HttpContext.Session.GetString("verifyCode") == null || verifyCode.Trim().ToLower() != HttpContext.Session.GetString("verifyCode").ToLower())
            {
                ModelState.AddModelError("error", "验证码输入错误");
                return View();
            }
 
            HttpContext.Session.SetString("verifyCode", "");
 
            _logger.LogInformation("login:验证码通过");
 
                if (string.IsNullOrEmpty(txtID))
                {
                    ModelState.AddModelError("error", "请录入用户名");
                    return View();
                }
                if (string.IsNullOrEmpty(txtPwd))
                {
                    ModelState.AddModelError("error", "请录入密码");
                    return View();
                }
 
                var user = _userService.login(txtID, txtPwd);
                if (user == null)
                {
                    ModelState.AddModelError("error", "用户名密码错误");
                    return View();
                }
                string curentuser = JsonConvert.SerializeObject(user);
                HttpContext.Session.SetString("User", curentuser);
                ViewData["curentuser"] = curentuser;
 
                _logger.LogInformation(user.UserSn+user.UserName+"login:登录成功" );
                ModelState.AddModelError("error", "登录成功:");
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
 
                ModelState.AddModelError("error", "登录失败12:" + ex.ToString());
                return View();
            }
        }
 
        [HttpPost]
        public string LoginLock(string txtID, string txtPwd)
        {
             ResultEntity result = new ResultEntity();
            if (string.IsNullOrEmpty(txtID.Trim()))
            {
                result.Remark = "gotoLoginPage";
                result.Message = "用户编号不存在";
                return JsonConvert.SerializeObject(result);
            }
            if (string.IsNullOrEmpty(txtPwd.Trim()))
            {
                result.Message = "用户密码为空";
                return JsonConvert.SerializeObject(result);
            }
 
 
 
            var user = _userService.login(txtID, txtPwd);
 
            if (user != null)
            {
                string curentuser = JsonConvert.SerializeObject(user);
                HttpContext.Session.SetString("User", curentuser);
                ViewData["curentuser"] = curentuser;
 
                result.Result = true;
                result.Message = "登录成功";
                return   JsonConvert.SerializeObject(result);
            }
            result.Remark = "gotoLoginPage";
            result.Message = "登录失败";
            return  JsonConvert.SerializeObject(result);
        }
        [HttpPost]
        public void LoginLockInit(string txtID)
        {
            HttpContext.Session.Remove("User");
        }
        public IActionResult logout()
        {
 
            HttpContext.Session.Remove("User");
            
            return RedirectToAction("LoginOutClosePage", "Home");
        }
 
 
        public ActionResult LoginOutClosePage()
        {
            return View();
        }
 
        #region 密码管理
 
        [CheckLogin]
        public IActionResult ChangePassword()
        {
            return View();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        [HttpPost]
        [CheckLogin]
        public string ChangePassword(string oldPwd, string newPwd)
        {
            var curentuser = JsonConvert.DeserializeObject<PltUser>(HttpContext.Session.GetString("User"));
            ResultEntity<PltUser> resultEntity = _userService.changePassword(oldPwd, newPwd, curentuser.Id);
            if (resultEntity.Result)
            {
                resultEntity.Message = "保存成功";
            }
            else
            {
                if (!resultEntity.Remark.Equals("PWD_ERROR"))
                {
                    resultEntity.Message = "保存失败";
                }
            }
            return JsonConvert.SerializeObject(resultEntity);  
        }
        #endregion
    }
}