AccountController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using LotteryWebApp.Common;
  2. using LotteryWebApp.Models;
  3. using LotteryWebApp.Service;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using System;
  8. using LotteryWebApp.Languages;
  9. using LotteryWebApp.Extensions;
  10. using System.Globalization;
  11. using Microsoft.AspNetCore.Http;
  12. using System.IO;
  13. namespace LotteryWebApp.Controllers
  14. {
  15. [AutoValidateAntiforgeryToken]
  16. public class AccountController : BaseController
  17. {
  18. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  19. IConfiguration configuration;
  20. private readonly IWebHostEnvironment webHostEnvironment;
  21. APIFunctions api = new APIFunctions();
  22. public AccountController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment)
  23. {
  24. configuration = _configuration;
  25. webHostEnvironment = hostEnvironment;
  26. }
  27. public String GetParameter(String key)
  28. {
  29. return configuration.GetSection(key).Value;
  30. }
  31. public IActionResult Index()
  32. {
  33. if (!CheckAuthToken())
  34. {
  35. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  36. }
  37. AccountIndex_ViewModel model = new AccountIndex_ViewModel();
  38. return View("Index", model);
  39. }
  40. public IActionResult Login(String code, String step, String phonenumber, String message)
  41. {
  42. //return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Home/Update");
  43. AccountLogin_ViewModel model = new AccountLogin_ViewModel();
  44. try
  45. {
  46. model.code = code;
  47. model.step = step != null ? step : Constants.LOGIN_ENTER_MSISDN;
  48. model.message = message;
  49. model.phonenumber = phonenumber;
  50. }
  51. catch (Exception ex)
  52. {
  53. log.Error(ex);
  54. }
  55. return View("Login", model);
  56. }
  57. public IActionResult Logout()
  58. {
  59. ClearCache();
  60. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  61. }
  62. [ValidateAntiForgeryToken]
  63. public JsonResult CheckMsisdn_Action(String phonenumber)
  64. {
  65. // check Msisdn
  66. String msisdn = validateMsisdn(phonenumber);
  67. if (msisdn == "")
  68. {
  69. return Json(new
  70. {
  71. code = Code.FAILURE,
  72. message = Lang.phone_invalid
  73. });
  74. }
  75. //msisdn = "50940236545";
  76. HttpContext.Session.SetComplexData("msisdn", msisdn);
  77. return Json(new
  78. {
  79. code = Code.SUCCESS,
  80. });
  81. }
  82. [ValidateAntiForgeryToken]
  83. public JsonResult ForgotPassword_Action(string phonenumber)
  84. {
  85. try
  86. {
  87. // resent password to return result
  88. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  89. if (msisdn == null && !string.IsNullOrEmpty(phonenumber))
  90. {
  91. msisdn = validateMsisdn(phonenumber);
  92. }
  93. if (msisdn == null || msisdn == "")
  94. {
  95. return Json(new
  96. {
  97. code = Code.FAILURE,
  98. message = Lang.phone_invalid
  99. });
  100. }
  101. // bo cho test
  102. RegisterRequest request = new RegisterRequest();
  103. request.Msisdn = msisdn;
  104. RegisterResponse reset = api.UserForgotPasswordApi(configuration, request);
  105. return Json(new
  106. {
  107. code = int.Parse(reset.status),
  108. message = GetLangFromCode(reset.status)
  109. });
  110. //return Json(new
  111. //{
  112. // code = Code.SUCCESS,
  113. //});
  114. }
  115. catch (Exception ex)
  116. {
  117. log.Error(ex);
  118. }
  119. return Json(new
  120. {
  121. code = Code.FAILURE,
  122. message = Lang.error_happened
  123. });
  124. }
  125. [ValidateAntiForgeryToken]
  126. public JsonResult Login_Action(String step, String phonenumber, String password)
  127. {
  128. try
  129. {
  130. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  131. if (msisdn != null)
  132. {
  133. //password = "589219";
  134. RegisterRequest request = new RegisterRequest();
  135. request.Msisdn = msisdn;
  136. request.pass = password;
  137. RegisterResponse login = api.UserLoginApi(configuration, request);
  138. if (login.status == Code.SUCCESS.ToString())
  139. {
  140. // create session
  141. // create new auth
  142. CreateAuthToken();
  143. // login success --> store session
  144. HttpContext.Session.SetComplexData("msisdn", msisdn);
  145. HttpContext.Session.SetComplexData("token", login.token);
  146. // load profile
  147. // load user status
  148. UserGetProfileRequest userGetProfileRequest = new UserGetProfileRequest
  149. {
  150. users = msisdn,
  151. token = login.token
  152. };
  153. Profile profile = api.UserLoadProfileApi(configuration, userGetProfileRequest);
  154. HttpContext.Session.SetComplexData("profile", profile);
  155. // load profile
  156. UserStatusRequest userStatusRequest = new UserStatusRequest
  157. {
  158. users = msisdn,
  159. token = login.token
  160. };
  161. UserStatus userStatus = api.GetUserStatusApi(configuration, userStatusRequest);
  162. HttpContext.Session.SetComplexData("userStatus", userStatus);
  163. HttpContext.Session.SetString("ShowChooseAppAfterLogin", "1");
  164. }
  165. return Json(new
  166. {
  167. code = int.Parse(login.status),
  168. message = GetLangFromCode(login.status)
  169. });
  170. }
  171. else
  172. {
  173. return Json(new
  174. {
  175. code = Code.FAILURE,
  176. message = Lang.error_happened
  177. });
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. log.Error(ex);
  183. }
  184. return Json(new
  185. {
  186. code = Code.FAILURE,
  187. message = Lang.error_happened
  188. });
  189. }
  190. public IActionResult Register(string code)
  191. {
  192. AccountRegister_ViewModel model = new AccountRegister_ViewModel();
  193. model.code = code;
  194. return View("Register", model);
  195. }
  196. [ValidateAntiForgeryToken]
  197. public JsonResult Register_Action(string phonenumber, string fullname, string birthday)
  198. {
  199. try
  200. {
  201. // resent password to return result
  202. String msisdn = validateMsisdn(phonenumber);
  203. if (msisdn == "")
  204. {
  205. return Json(new
  206. {
  207. code = Code.FAILURE,
  208. message = Lang.phone_invalid
  209. });
  210. }
  211. if (fullname == null)
  212. {
  213. return Json(new
  214. {
  215. code = Code.FAILURE,
  216. message = Lang.fullname_not_valid
  217. });
  218. }
  219. RegisterRequest request = new RegisterRequest();
  220. request.Msisdn = msisdn;
  221. RegisterResponse register = api.UserRegisterApi(configuration, request);
  222. if (register.status == Code.SUCCESS)
  223. {
  224. // convert birthday to dd/mm/yyyy
  225. String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
  226. Profile profile = new Profile();
  227. profile.birthday = birthdayFormat;
  228. profile.fullName = fullname;
  229. HttpContext.Session.SetComplexData("profile", profile);
  230. HttpContext.Session.SetComplexData("token", register.token);
  231. // create profile
  232. UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest();
  233. userUpdateProfileRequest.users = msisdn;
  234. userUpdateProfileRequest.fullName = fullname;
  235. userUpdateProfileRequest.birthday = birthdayFormat;
  236. userUpdateProfileRequest.token = register.token;
  237. UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest);
  238. if (updateProfile.status == Code.SUCCESS)
  239. {
  240. // return login screen
  241. }
  242. }
  243. return Json(new
  244. {
  245. code = int.Parse(register.status),
  246. message = GetLangFromCode(register.status)
  247. });
  248. }
  249. catch (Exception ex)
  250. {
  251. log.Error(ex);
  252. }
  253. return Json(new
  254. {
  255. code = Code.FAILURE,
  256. message = Lang.error_happened
  257. });
  258. }
  259. public IActionResult ChooseApp()
  260. {
  261. if (!CheckAuthToken())
  262. {
  263. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  264. }
  265. HttpContext.Session.Remove("ShowChooseAppAfterLogin");
  266. return View();
  267. }
  268. public IActionResult ForgotPassword(String code)
  269. {
  270. if (!CheckAuthToken())
  271. {
  272. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  273. }
  274. AccountForgotPassword_ViewModel model = new AccountForgotPassword_ViewModel();
  275. model.code = code;
  276. return View("ForgotPassword", model);
  277. }
  278. [ValidateAntiForgeryToken]
  279. [Produces("application/json")]
  280. [Consumes("multipart/form-data")]
  281. public JsonResult UploadAvatar([FromForm] IFormFile user_avatar)
  282. {
  283. try
  284. {
  285. // check user
  286. if (!CheckAuthToken())
  287. {
  288. log.Info("Must login");
  289. return Json(new
  290. {
  291. code = Code.NOT_AUTHEN,
  292. message = Lang.must_login
  293. });
  294. }
  295. //var user_avatar = HttpContext.Request.Form.Files;
  296. if (user_avatar != null)
  297. {
  298. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  299. Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
  300. IFormFile image = user_avatar;
  301. string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img/" + profile.users);
  302. Directory.CreateDirectory(uploadsFolder);
  303. string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
  304. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  305. using (var fileStream = new FileStream(filePath, FileMode.Create))
  306. {
  307. image.CopyTo(fileStream);
  308. }
  309. profile.realPicture = uniqueFileName;
  310. profile.picture = Constants.PATH + profile.users + "/" + uniqueFileName;
  311. HttpContext.Session.SetComplexData("profile", profile);
  312. return Json(new
  313. {
  314. code = Code.SUCCESS,
  315. });
  316. }
  317. else
  318. {
  319. log.Error("user avatar error");
  320. return Json(new
  321. {
  322. code = Code.ERROR,
  323. message = Lang.user_avatar_error
  324. });
  325. }
  326. }
  327. catch (Exception ex)
  328. {
  329. log.Error("Exception ", ex);
  330. }
  331. return Json(new
  332. {
  333. code = Code.FAILURE,
  334. message = Lang.error_happened
  335. });
  336. }
  337. }
  338. }