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
|
}
|
}
|