| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using LotteryWebApp.Common;
- using LotteryWebApp.Models;
- using LotteryWebApp.Service;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System;
- using LotteryWebApp.Languages;
- using LotteryWebApp.Extensions;
- using System.Globalization;
- using Microsoft.AspNetCore.Http;
- using System.IO;
- namespace LotteryWebApp.Controllers
- {
- [AutoValidateAntiforgeryToken]
- public class AccountController : BaseController
- {
- private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
- IConfiguration configuration;
- private readonly IWebHostEnvironment webHostEnvironment;
- APIFunctions api = new APIFunctions();
- public AccountController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment)
- {
- configuration = _configuration;
- webHostEnvironment = hostEnvironment;
- }
- public String GetParameter(String key)
- {
- return configuration.GetSection(key).Value;
- }
- public IActionResult Index()
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- AccountIndex_ViewModel model = new AccountIndex_ViewModel();
- return View("Index", model);
- }
- public IActionResult Login(String code, String step, String phonenumber, String message)
- {
- //return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home/Update");
- AccountLogin_ViewModel model = new AccountLogin_ViewModel();
- try
- {
- model.code = code;
- model.step = step != null ? step : Constants.LOGIN_ENTER_MSISDN;
- model.message = message;
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return View("Login", model);
- }
- public IActionResult Logout()
- {
- ClearCache();
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- [ValidateAntiForgeryToken]
- public JsonResult CheckMsisdn_Action(String phonenumber)
- {
- // check Msisdn
- String msisdn = validateMsisdn(phonenumber);
- if (msisdn == "")
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.phone_invalid
- });
- }
- //msisdn = "50940236545";
- HttpContext.Session.SetComplexData("msisdn", msisdn);
- return Json(new
- {
- code = Code.SUCCESS,
- });
- }
- [ValidateAntiForgeryToken]
- public JsonResult ForgotPassword_Action(string phonenumber)
- {
- try
- {
- // resent password to return result
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- //String msisdn = validateMsisdn(phonenumber);
- if (msisdn == null)
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.phone_invalid
- });
- }
- // bo cho test
- RegisterRequest request = new RegisterRequest();
- request.Msisdn = msisdn;
- RegisterResponse reset = api.UserForgotPasswordApi(configuration, request);
- return Json(new
- {
- code = int.Parse(reset.status),
- message = GetLangFromCode(reset.status)
- });
- //return Json(new
- //{
- // code = Code.SUCCESS,
- //});
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- [ValidateAntiForgeryToken]
- public JsonResult Login_Action(String step, String phonenumber, String password)
- {
- try
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- if (msisdn != null)
- {
- //password = "589219";
- RegisterRequest request = new RegisterRequest();
- request.Msisdn = msisdn;
- request.pass = password;
- RegisterResponse login = api.UserLoginApi(configuration, request);
- if (login.status == Code.SUCCESS.ToString())
- {
- // create session
- // create new auth
- CreateAuthToken();
- // login success --> store session
- HttpContext.Session.SetComplexData("msisdn", msisdn);
- HttpContext.Session.SetComplexData("token", login.token);
- // load profile
- // load user status
- UserGetProfileRequest userGetProfileRequest = new UserGetProfileRequest
- {
- users = msisdn,
- token = login.token
- };
- Profile profile = api.UserLoadProfileApi(configuration, userGetProfileRequest);
- HttpContext.Session.SetComplexData("profile", profile);
- // load profile
- UserStatusRequest userStatusRequest = new UserStatusRequest
- {
- users = msisdn,
- token = login.token
- };
- UserStatus userStatus = api.GetUserStatusApi(configuration, userStatusRequest);
- HttpContext.Session.SetComplexData("userStatus", userStatus);
- }
- return Json(new
- {
- code = int.Parse(login.status),
- message = GetLangFromCode(login.status)
- });
- }
- else
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- public IActionResult Register(string code)
- {
- AccountRegister_ViewModel model = new AccountRegister_ViewModel();
- model.code = code;
- return View("Register", model);
- }
- [ValidateAntiForgeryToken]
- public JsonResult Register_Action(string phonenumber, string fullname, string birthday)
- {
- try
- {
- // resent password to return result
- String msisdn = validateMsisdn(phonenumber);
- if (msisdn == "")
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.phone_invalid
- });
- }
- if (fullname == null)
- {
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.fullname_not_valid
- });
- }
- RegisterRequest request = new RegisterRequest();
- request.Msisdn = msisdn;
- RegisterResponse register = api.UserRegisterApi(configuration, request);
- if (register.status == Code.SUCCESS)
- {
- // convert birthday to dd/mm/yyyy
- String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
- Profile profile = new Profile();
- profile.birthday = birthdayFormat;
- profile.fullName = fullname;
- HttpContext.Session.SetComplexData("profile", profile);
- HttpContext.Session.SetComplexData("token", register.token);
- // create profile
- UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest();
- userUpdateProfileRequest.users = msisdn;
- userUpdateProfileRequest.fullName = fullname;
- userUpdateProfileRequest.birthday = birthdayFormat;
- userUpdateProfileRequest.token = register.token;
- UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest);
- if (updateProfile.status == Code.SUCCESS)
- {
- // return login screen
- }
- }
- return Json(new
- {
- code = int.Parse(register.status),
- message = GetLangFromCode(register.status)
- });
- }
- catch (Exception ex)
- {
- log.Error(ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- public IActionResult ForgotPassword(String code)
- {
- if (!CheckAuthToken())
- {
- return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
- }
- AccountForgotPassword_ViewModel model = new AccountForgotPassword_ViewModel();
- model.code = code;
- return View("ForgotPassword", model);
- }
- [ValidateAntiForgeryToken]
- [Produces("application/json")]
- [Consumes("multipart/form-data")]
- public JsonResult UploadAvatar([FromForm] IFormFile user_avatar)
- {
- try
- {
- // check user
- if (!CheckAuthToken())
- {
- log.Info("Must login");
- return Json(new
- {
- code = Code.NOT_AUTHEN,
- message = Lang.must_login
- });
- }
- //var user_avatar = HttpContext.Request.Form.Files;
- if (user_avatar != null)
- {
- String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
- Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
- IFormFile image = user_avatar;
- string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img/" + profile.users);
- Directory.CreateDirectory(uploadsFolder);
- string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
- string filePath = Path.Combine(uploadsFolder, uniqueFileName);
- using (var fileStream = new FileStream(filePath, FileMode.Create))
- {
- image.CopyTo(fileStream);
- }
- profile.realPicture = uniqueFileName;
- profile.picture = Constants.PATH + profile.users + "/" + uniqueFileName;
- HttpContext.Session.SetComplexData("profile", profile);
- return Json(new
- {
- code = Code.SUCCESS,
- });
- }
- else
- {
- log.Error("user avatar error");
- return Json(new
- {
- code = Code.ERROR,
- message = Lang.user_avatar_error
- });
- }
- }
- catch (Exception ex)
- {
- log.Error("Exception ", ex);
- }
- return Json(new
- {
- code = Code.FAILURE,
- message = Lang.error_happened
- });
- }
- }
- }
|