AccountController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. }
  50. catch (Exception ex)
  51. {
  52. log.Error(ex);
  53. }
  54. return View("Login", model);
  55. }
  56. public IActionResult Logout()
  57. {
  58. ClearCache();
  59. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  60. }
  61. [ValidateAntiForgeryToken]
  62. public JsonResult CheckMsisdn_Action(String phonenumber)
  63. {
  64. // check Msisdn
  65. String msisdn = validateMsisdn(phonenumber);
  66. if (msisdn == "")
  67. {
  68. return Json(new
  69. {
  70. code = Code.FAILURE,
  71. message = Lang.phone_invalid
  72. });
  73. }
  74. //msisdn = "50940236545";
  75. HttpContext.Session.SetComplexData("msisdn", msisdn);
  76. return Json(new
  77. {
  78. code = Code.SUCCESS,
  79. });
  80. }
  81. [ValidateAntiForgeryToken]
  82. public JsonResult ForgotPassword_Action(string phonenumber)
  83. {
  84. try
  85. {
  86. // resent password to return result
  87. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  88. //String msisdn = validateMsisdn(phonenumber);
  89. if (msisdn == null)
  90. {
  91. return Json(new
  92. {
  93. code = Code.FAILURE,
  94. message = Lang.phone_invalid
  95. });
  96. }
  97. // bo cho test
  98. RegisterRequest request = new RegisterRequest();
  99. request.Msisdn = msisdn;
  100. RegisterResponse reset = api.UserForgotPasswordApi(configuration, request);
  101. return Json(new
  102. {
  103. code = int.Parse(reset.status),
  104. message = GetLangFromCode(reset.status)
  105. });
  106. //return Json(new
  107. //{
  108. // code = Code.SUCCESS,
  109. //});
  110. }
  111. catch (Exception ex)
  112. {
  113. log.Error(ex);
  114. }
  115. return Json(new
  116. {
  117. code = Code.FAILURE,
  118. message = Lang.error_happened
  119. });
  120. }
  121. [ValidateAntiForgeryToken]
  122. public JsonResult Login_Action(String step, String phonenumber, String password)
  123. {
  124. try
  125. {
  126. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  127. if (msisdn != null)
  128. {
  129. //password = "589219";
  130. RegisterRequest request = new RegisterRequest();
  131. request.Msisdn = msisdn;
  132. request.pass = password;
  133. RegisterResponse login = api.UserLoginApi(configuration, request);
  134. if (login.status == Code.SUCCESS.ToString())
  135. {
  136. // create session
  137. // create new auth
  138. CreateAuthToken();
  139. // login success --> store session
  140. HttpContext.Session.SetComplexData("msisdn", msisdn);
  141. HttpContext.Session.SetComplexData("token", login.token);
  142. // load profile
  143. // load user status
  144. UserGetProfileRequest userGetProfileRequest = new UserGetProfileRequest
  145. {
  146. users = msisdn,
  147. token = login.token
  148. };
  149. Profile profile = api.UserLoadProfileApi(configuration, userGetProfileRequest);
  150. HttpContext.Session.SetComplexData("profile", profile);
  151. // load profile
  152. UserStatusRequest userStatusRequest = new UserStatusRequest
  153. {
  154. users = msisdn,
  155. token = login.token
  156. };
  157. UserStatus userStatus = api.GetUserStatusApi(configuration, userStatusRequest);
  158. HttpContext.Session.SetComplexData("userStatus", userStatus);
  159. }
  160. return Json(new
  161. {
  162. code = int.Parse(login.status),
  163. message = GetLangFromCode(login.status)
  164. });
  165. }
  166. else
  167. {
  168. return Json(new
  169. {
  170. code = Code.FAILURE,
  171. message = Lang.error_happened
  172. });
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. log.Error(ex);
  178. }
  179. return Json(new
  180. {
  181. code = Code.FAILURE,
  182. message = Lang.error_happened
  183. });
  184. }
  185. public IActionResult Register(string code)
  186. {
  187. AccountRegister_ViewModel model = new AccountRegister_ViewModel();
  188. model.code = code;
  189. return View("Register", model);
  190. }
  191. [ValidateAntiForgeryToken]
  192. public JsonResult Register_Action(string phonenumber, string fullname, string birthday)
  193. {
  194. try
  195. {
  196. // resent password to return result
  197. String msisdn = validateMsisdn(phonenumber);
  198. if (msisdn == "")
  199. {
  200. return Json(new
  201. {
  202. code = Code.FAILURE,
  203. message = Lang.phone_invalid
  204. });
  205. }
  206. if (fullname == null)
  207. {
  208. return Json(new
  209. {
  210. code = Code.FAILURE,
  211. message = Lang.fullname_not_valid
  212. });
  213. }
  214. RegisterRequest request = new RegisterRequest();
  215. request.Msisdn = msisdn;
  216. RegisterResponse register = api.UserRegisterApi(configuration, request);
  217. if (register.status == Code.SUCCESS)
  218. {
  219. // convert birthday to dd/mm/yyyy
  220. String birthdayFormat = DateTime.ParseExact(birthday, "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
  221. Profile profile = new Profile();
  222. profile.birthday = birthdayFormat;
  223. profile.fullName = fullname;
  224. HttpContext.Session.SetComplexData("profile", profile);
  225. HttpContext.Session.SetComplexData("token", register.token);
  226. // create profile
  227. UserUpdateProfileRequest userUpdateProfileRequest = new UserUpdateProfileRequest();
  228. userUpdateProfileRequest.users = msisdn;
  229. userUpdateProfileRequest.fullName = fullname;
  230. userUpdateProfileRequest.birthday = birthdayFormat;
  231. userUpdateProfileRequest.token = register.token;
  232. UserUpdateProfileResponse updateProfile = api.UserUpdateProfileApi(configuration, userUpdateProfileRequest);
  233. if (updateProfile.status == Code.SUCCESS)
  234. {
  235. // return login screen
  236. }
  237. }
  238. return Json(new
  239. {
  240. code = int.Parse(register.status),
  241. message = GetLangFromCode(register.status)
  242. });
  243. }
  244. catch (Exception ex)
  245. {
  246. log.Error(ex);
  247. }
  248. return Json(new
  249. {
  250. code = Code.FAILURE,
  251. message = Lang.error_happened
  252. });
  253. }
  254. public IActionResult ForgotPassword(String code)
  255. {
  256. if (!CheckAuthToken())
  257. {
  258. return Redirect(GetParameter(Constants.SUB_DOMAIN) + "/Account/Login");
  259. }
  260. AccountForgotPassword_ViewModel model = new AccountForgotPassword_ViewModel();
  261. model.code = code;
  262. return View("ForgotPassword", model);
  263. }
  264. [ValidateAntiForgeryToken]
  265. [Produces("application/json")]
  266. [Consumes("multipart/form-data")]
  267. public JsonResult UploadAvatar([FromForm] IFormFile user_avatar)
  268. {
  269. try
  270. {
  271. // check user
  272. if (!CheckAuthToken())
  273. {
  274. log.Info("Must login");
  275. return Json(new
  276. {
  277. code = Code.NOT_AUTHEN,
  278. message = Lang.must_login
  279. });
  280. }
  281. //var user_avatar = HttpContext.Request.Form.Files;
  282. if (user_avatar != null)
  283. {
  284. String msisdn = HttpContext.Session.GetComplexData<String>("msisdn");
  285. Profile profile = HttpContext.Session.GetComplexData<Profile>("profile");
  286. IFormFile image = user_avatar;
  287. string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "img/" + profile.users);
  288. Directory.CreateDirectory(uploadsFolder);
  289. string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
  290. string filePath = Path.Combine(uploadsFolder, uniqueFileName);
  291. using (var fileStream = new FileStream(filePath, FileMode.Create))
  292. {
  293. image.CopyTo(fileStream);
  294. }
  295. profile.realPicture = uniqueFileName;
  296. profile.picture = Constants.PATH + profile.users + "/" + uniqueFileName;
  297. HttpContext.Session.SetComplexData("profile", profile);
  298. return Json(new
  299. {
  300. code = Code.SUCCESS,
  301. });
  302. }
  303. else
  304. {
  305. log.Error("user avatar error");
  306. return Json(new
  307. {
  308. code = Code.ERROR,
  309. message = Lang.user_avatar_error
  310. });
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. log.Error("Exception ", ex);
  316. }
  317. return Json(new
  318. {
  319. code = Code.FAILURE,
  320. message = Lang.error_happened
  321. });
  322. }
  323. }
  324. }