Files
Backend-Api/ServiceHost/Pages/login/Index.cshtml.cs

396 lines
11 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using _0_Framework.Application;
using _0_Framework.Application.Sms;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Domain.AccountAgg;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using System.Security.Claims;
using AccountManagement.Application.Contracts.CameraAccount;
using AccountMangement.Infrastructure.EFCore.Repository;
using Company.Domain.RollCallAgg.DomainService;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Authentication.Cookies;
using CompanyManagment.App.Contracts.AndroidApkVersion;
using CompanyManagment.EFCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace ServiceHost.Pages.login;
public class IndexModel : PageModel
{
private readonly IAccountApplication _accountApplication;
private readonly IGoogleRecaptcha _googleRecaptcha;
private readonly IAuthHelper _authHelper;
private readonly IAndroidApkVersionApplication _androidApkVersionApplication;
private readonly CompanyContext _context;
private readonly IRollCallDomainService _rollCallDomainService;
private readonly AppSettingConfiguration _appSettingConfiguration;
public string Mess { get; set; }
[BindProperty] public string Username { get; set; }
[BindProperty] public string Password { get; set; }
[BindProperty] public string CaptchaResponse { get; set; }
public bool HasApkToDownload { get; set; }
public bool HasFaceDetectionApkToDownload { get; set; }
private static Timer aTimer;
public Login login;
public AccountViewModel Search;
public IndexModel(IAccountApplication accountApplication, IGoogleRecaptcha googleRecaptcha,
IAuthHelper authHelper, IAndroidApkVersionApplication androidApkVersionApplication, CompanyContext context,
IRollCallDomainService rollCallDomainService, IOptions<AppSettingConfiguration> optionsAppSetting)
{
_accountApplication = accountApplication;
_googleRecaptcha = googleRecaptcha;
_authHelper = authHelper;
_androidApkVersionApplication = androidApkVersionApplication;
_context = context;
_rollCallDomainService = rollCallDomainService;
_appSettingConfiguration = optionsAppSetting.Value;
}
public IActionResult OnGet()
{
//var customizeWorkshopSettings = _context.CustomizeWorkshopSettings.AsSplitQuery();
//var rollCalls =
// _context.RollCalls.Where(x => customizeWorkshopSettings.Any(a => a.WorkshopId == x.WorkshopId))
// .ToList();
//foreach (var rollCall in rollCalls)
//{
// rollCall.SetShiftDate(_rollCallDomainService);
//}
//_context.SaveChanges();
HasApkToDownload = _androidApkVersionApplication.HasAndroidApkToDownload(ApkType.WebView);
HasFaceDetectionApkToDownload = _androidApkVersionApplication.HasAndroidApkToDownload(ApkType.FaceDetection);
if (User.Identity is { IsAuthenticated: true })
{
if (User.FindFirstValue("IsCamera") == "true")
{
return Redirect("/Camera");
}
else if ((User.FindFirstValue("ClientAriaPermission") == "true") &&
(User.FindFirstValue("AdminAreaPermission") == "false"))
{
return Redirect("/Client");
}
else
{
return Redirect($"https://{_appSettingConfiguration.AdminDomain}");
}
}
_authHelper.SignOut();
return Page();
}
#region Vafa
//public IActionResult OnGetGenerateAntiForgeryToken()
//{
// var tokens = _antiforgery.GetAndStoreTokens(HttpContext);
// return new JsonResult(new { token = tokens.RequestToken });
//}
//public IActionResult OnPostLoginAjax(Login command)
//{
// var result = _accountApplication.Login(command);
// if (result.IsSuccedded)
// {
// string redirectUrl = string.Empty;
// switch (result.SendId)
// {
// case 1:
// redirectUrl = "/Admin";
// break;
// case 2:
// redirectUrl = "/Client";
// break;
// case 3:
// redirectUrl = "/Camera";
// break;
// case 0:
// result.Message = "امکان ورود با این حساب کاربری وجود ندارد";
// return new JsonResult(new { success = false, message = result.Message });
// }
// return new JsonResult(new { success = true, redirectUrl });
// }
// Mess = result.Message;
// return new JsonResult(new { success = false, message = result.Message });
//}
#endregion
public IActionResult OnPostLogin(Login command)
{
var result = _accountApplication.Login(command);
if (result.IsSuccedded)
return RedirectToPage("/Admin");
ModelState.AddModelError("Username", "اطلاعات وارد شده اشتباه است");
TempData["h"] = "n";
Mess = result.Message;
return null;
}
public IActionResult OnPostEnter(Login command)
{
//bool captchaResult = true;
//if (!_webHostEnvironment.IsDevelopment())
// captchaResult = _googleRecaptcha.IsSatisfy(CaptchaResponse).Result;
//if (captchaResult)
//{
var result = _accountApplication.Login(command);
if (result.IsSuccedded)
{
switch (result.SendId)
{
case 1:
return Redirect($"https://{_appSettingConfiguration.AdminDomain}");
break;
case 2:
return Redirect("/Client");
break;
case 3:
return Redirect("/Camera");
break;
case 0:
result.Message = "امکان ورود با این حساب کاربری وجود ندارد";
break;
}
}
Mess = result.Message;
//}
//else
//{
// Mess = "دستگاه شما ربات تشخیص داده شد";
//}
//ModelState.AddModelError("Username", "اطلاعات وارد شده اشتباه است");
return Page();
}
public async Task<JsonResult> OnPostCheckCaptcha(string response)
{
var result = await _googleRecaptcha.IsSatisfy(response);
return new JsonResult(new
{
isNotRobot = result,
});
}
public IActionResult OnPostRegisterClient(string name, string user, string pass, string phone,
string nationalcode)
{
var command = new RegisterAccount()
{
Fullname = name,
Username = user,
Password = pass,
Mobile = phone,
NationalCode = nationalcode,
};
var result = _accountApplication.RegisterClient(command);
return new JsonResult(new
{
isSucceded = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetLogout()
{
_accountApplication.Logout();
return RedirectToPage("/Index");
}
public async Task<IActionResult> OnPostCheckPhoneValid(string phone)
{
var result = _accountApplication.Search(new AccountSearchModel() { Mobile = phone }).FirstOrDefault();
if (result == null)
{
return new JsonResult(new
{
exist = false,
});
}
else
{
SendSms(phone);
return new JsonResult(new
{
exist = true,
});
}
}
public void SendSms(string phone)
{
var result = _accountApplication.Search(new AccountSearchModel() { Mobile = phone }).FirstOrDefault();
if (result != null)
{
_accountApplication.SetVerifyCode(phone, result.Id);
}
}
public IActionResult OnPostWithMobile(string code, string phone)
{
//bool captchaResult = true;
//if (!_webHostEnvironment.IsDevelopment())
// captchaResult = _googleRecaptcha.IsSatisfy(CaptchaResponse).Result;
//if (captchaResult)
//{
var verfiyResult = _accountApplication.GetByVerifyCode(code, phone);
if (verfiyResult != null)
{
var result = _accountApplication.LoginWithMobile(verfiyResult.Id);
if (result.IsSuccedded && result.SendId == 1)
{
return new JsonResult(new
{
exist = true,
url = "/Admin",
});
}
if (result.IsSuccedded && result.SendId == 2)
{
return new JsonResult(new
{
exist = true,
url = "/Client",
});
}
}
//}
//else
//{
// Mess = "دستگاه شما ربات تشخیص داده شد";
//}
return new JsonResult(new
{
exist = false,
});
}
public IActionResult OnPostVerify(string code, string phone)
{
var result = _accountApplication.GetByVerifyCode(code, phone);
if (result != null)
{
return new JsonResult(new
{
exist = true,
user = result.Username,
verfyId = result.Id
});
}
else
{
return new JsonResult(new
{
exist = false,
});
}
}
public IActionResult OnPostChangePass(long id, string username, string newpass)
{
var result = _accountApplication.GetByUserNameAndId(id, username);
if (result != null)
{
var command = new ChangePassword()
{
Id = id,
Password = newpass,
RePassword = newpass
};
var finalResult = _accountApplication.ChangePassword(command);
if (finalResult.IsSuccedded)
{
return new JsonResult(new
{
exist = true,
changed = true
});
}
else
{
return new JsonResult(new
{
exist = true,
changed = false
});
}
}
else
{
return new JsonResult(new
{
exist = false,
changed = false
});
}
}
}
public class RecaptchaResponse
{
[JsonProperty("success")] public bool Success { get; set; }
[JsonProperty("challenge_ts")] public DateTimeOffset ChallengeTs { get; set; }
[JsonProperty("hostname")] public string HostName { get; set; }
}