AccountController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. }
  164. return Json(new
  165. {
  166. code = int.Parse(login.status),
  167. message = GetLangFromCode(login.status)
  168. });
  169. }
  170. else
  171. {
  172. return Json(new
  173. {
  174. code = Code.FAILURE,
  175. message = Lang.error_happened
  176. });
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. log.Error(ex);
  182. }
  183. return Json(new
  184. {
  185. code = Code.FAILURE,
  186. message = Lang.error_happened
  187. });
  188. }
  189. public IActionResult Register(string code)
  190. {
  191. AccountRegister_ViewModel model = new AccountRegister_ViewModel();
  192. model.code = code;
  193. return View("Register", model);
  194. }
  195. [ValidateAntiForgeryToken]
  196. public JsonResult Register_Action(string phonenumber, string fullname, string birthday)
  197. {
  198. try
  199. {
  200. // resent password to return result
  201. String msisdn = validateMsisdn(phonenumber);
  202. if (msisdn == "")
  203. {
  204. return Json(new
  205. {
  206. code = Code.FAILURE,
  207. message = Lang.phone_invalid
  208. });
  209. }
  210. if (fullname == null)
  211. {
  212. return Json(new
  213. {
  214. code = Code.FAILURE,
  215. message = Lang.fullname_not_valid
  216. });
  217. }
  218. RegisterRequest request = new RegisterRequest();
  219. request.Msisdn = msisdn;
  220. RegisterResponse register = api.UserRegisterApi(configuration, request);
  221. if (register.status == Code.SUCCESS)
  222. {
  223. // convert birthday to dd/mm/yyyy
  224. String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
  225. Profile profile = new Profile();
  226. profile.birthday = birthdayFormat;
  227. profile.fullName = fullname;
  228. HttpContext.Session.SetComplexData("profile", profile);
  229. HttpContext.Session.SetComplexData("token", register.token);
  230. // create profile
  231. UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest();
  232. userUpdateProfileRequest.users = msisdn;
  233. userUpdateProfileRequest.fullName = fullname;
  234. userUpdateProfileRequest.birthday = birthdayFormat;
  235. userUpdateProfileRequest.token = register.token;
  236. UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest);
  237. if (updateProfile.status == Code.SUCCESS)
  238. {
  239. // return login screen
  240. }
  241. }
  242. return Json(new
  243. {
  244. code = int.Parse(register.status),
  245. message = GetLangFromCode(register.status)
  246. });
  247. }
  248. catch (Exception ex)
  249. {
  250. log.Error(ex);
  251. }
  252. return Json(new
  253. {
  254. code = Code.FAILURE,
  255. message = Lang.error_happened
  256. });
  257. }
  258. public IActionResult ChooseApp()
  259. {
  260. if (!CheckAuthToken())
  261. {
  262. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  263. }
  264. return View();
  265. }
  266. public IActionResult ForgotPassword(String code)
  267. {
  268. if (!CheckAuthToken())
  269. {
  270. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  271. }
  272. AccountForgotPassword_ViewModel model = new AccountForgotPassword_ViewModel();
  273. model.code = code;
  274. return View("ForgotPassword", model);
  275. }
  276. [ValidateAntiForgeryToken]
  277. [Produces("application/json")]
  278. [Consumes("multipart/form-data")]
  279. public JsonResult UploadAvatar([FromForm] IFormFile user_avatar)
  280. {
  281. try
  282. {
  283. // check user
  284. if (!CheckAuthToken())
  285. {
  286. log.Info("Must login");
  287. return Json(new
  288. {
  289. code = Code.NOT_AUTHEN,
  290. message = Lang.must_login
  291. });
  292. }
  293. //var user_avatar = HttpContext.Request.Form.Files;
  294. if (user_avatar != null)
  295. {
  296. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  297. Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
  298. IFormFile image = user_avatar;
  299. string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img/" + profile.users);
  300. Directory.CreateDirectory(uploadsFolder);
  301. string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
  302. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  303. using (var fileStream = new FileStream(filePath, FileMode.Create))
  304. {
  305. image.CopyTo(fileStream);
  306. }
  307. profile.realPicture = uniqueFileName;
  308. profile.picture = Constants.PATH + profile.users + "/" + uniqueFileName;
  309. HttpContext.Session.SetComplexData("profile", profile);
  310. return Json(new
  311. {
  312. code = Code.SUCCESS,
  313. });
  314. }
  315. else
  316. {
  317. log.Error("user avatar error");
  318. return Json(new
  319. {
  320. code = Code.ERROR,
  321. message = Lang.user_avatar_error
  322. });
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. log.Error("Exception ", ex);
  328. }
  329. return Json(new
  330. {
  331. code = Code.FAILURE,
  332. message = Lang.error_happened
  333. });
  334. }
  335. }
  336. }