HomeController.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. using NEducation.Code;
  2. using NEducation.Content.Texts;
  3. using NEducation.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.ServiceModel;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. using System.IO;
  12. using System.Globalization;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Web.SessionState;
  16. using Newtonsoft.Json;
  17. using System.Net.Sockets;
  18. using System.Net;
  19. using System.Security.Policy;
  20. using NEducation.GenKeyWs;
  21. using Microsoft.Extensions.Configuration;
  22. using System.Threading.Tasks;
  23. namespace NEducation.Controllers
  24. {
  25. public class HomeController : BaseController
  26. {
  27. private static log4net.ILog log { get; set; } = log4net.LogManager.GetLogger(typeof(HomeController));
  28. GenKeyWs.WsGenKeyClient wsClient = new GenKeyWs.WsGenKeyClient();
  29. public ActionResult Index(String uuid)
  30. {
  31. Session["navitab"] = "Home";
  32. HomeModel model = new HomeModel();
  33. // nhan dien thue bao
  34. String msisdnAuto = null;
  35. // check auto login ?uuid=681471d23b83f0a463e2dd5671ea0fc5
  36. // ?uuid=681471d23b83f0a463e2dd5671ea0fc5&mcuid=6c6948a89908d83aec8c05be256970a1&mcapp=mocha
  37. //uuid = "681471d23b83f0a463e2dd5671ea0fc5";
  38. model.uuid = uuid;
  39. log.Info("uuid: " + uuid);
  40. //string url = Request.Url.AbsoluteUri;
  41. //log.Info("url: " + url);
  42. //string path = Request.Url.AbsolutePath;
  43. //log.Info("path: " + path);
  44. //string host = Request.Url.Host;
  45. //log.Info("host: " + host);
  46. HttpCookie reqCookies = Request.Cookies["onapp"];
  47. if (reqCookies != null)
  48. {
  49. string isOnApp = reqCookies["isonapp"].ToString();
  50. if (isOnApp == "1")
  51. {
  52. Session["WHICHDEVICE"] = UtilsController.Constant.REGISTER_ON_APP;
  53. }
  54. }
  55. if (uuid != null)
  56. {
  57. String resDetect = CheckAutoLogin(log, uuid);
  58. log.Info("uuid: " + uuid + " res: " + resDetect);
  59. if (resDetect != null)
  60. {
  61. dynamic json = JsonConvert.DeserializeObject(resDetect);
  62. log.Info("res json: " + json);
  63. String code = json["code"];
  64. if (code == "200")
  65. {
  66. String errorCode = json["errorCode"];
  67. if (errorCode == "200")
  68. {
  69. msisdnAuto = json["data"]["msisdn"];
  70. log.Info("msisdnAuto: " + msisdnAuto);
  71. String msisdn = UtilsController.validateMsisdn(msisdnAuto.Substring(1));
  72. //String msisdn = UtilsController.validateMsisdn("+8562096841080".Substring(1));
  73. log.Info("msisdn: " + msisdn);
  74. if (msisdn != "")
  75. {
  76. // auto login
  77. // reload user info
  78. HttpContext.Session.Remove("regInfos");
  79. Session["msisdnDetect"] = msisdn;
  80. // dang ky tren app
  81. Session["WHICHDEVICE"] = UtilsController.Constant.REGISTER_ON_APP;
  82. HttpCookie onApp = new HttpCookie("onapp");
  83. onApp["isonapp"] = "1";
  84. onApp.Expires = DateTime.Now.AddDays(30);
  85. Response.Cookies.Add(onApp);
  86. // check user has account
  87. GetUserProfileReq req = new GetUserProfileReq();
  88. req.users = msisdn;
  89. String rs = UtilsController.SendPost(req, System.Web.HttpContext.Current.Session.SessionID, UtilsController.WsType.UsersGetProfile);
  90. UserProfile profile = new UserProfile(rs);
  91. if (profile.id != null)
  92. {
  93. System.Web.HttpContext.Current.Session["profile"] = profile;
  94. // create new auth
  95. CreateAuthToken();
  96. String sessionId = Session.SessionID;
  97. // login success --> store session
  98. Session["msisdn"] = msisdn;
  99. // get sub
  100. UtilsController.ReloadSubInfo();
  101. // all info of user was loaded from server
  102. //UserProfile profile = Session["profile"] as UserProfile;
  103. String typeLanguage = Session["typeLanguage"] as String;
  104. }
  105. else
  106. {
  107. // create free account for user
  108. // check user has account
  109. UserRequest userRequest = new UserRequest();
  110. userRequest.users = msisdn;
  111. userRequest.msisdn = msisdn;
  112. userRequest.command = "REGIST";
  113. userRequest.channel = "WEB";
  114. String rsRegister = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
  115. UserActionResult resRegister = new UserActionResult(rsRegister);
  116. Session["isSub"] = "false";
  117. if (resRegister.status == UtilsController.Constant.SUCCESS)
  118. {
  119. // load profile
  120. GetUserProfileReq reqProfile = new GetUserProfileReq();
  121. reqProfile.users = msisdn;
  122. String rsProfile = UtilsController.SendPost(reqProfile, Session.SessionID, UtilsController.WsType.UsersGetProfile);
  123. UserProfile profileGet = new UserProfile(rsProfile);
  124. if (profileGet.id != null)
  125. {
  126. System.Web.HttpContext.Current.Session["profile"] = profile;
  127. UserInfoRequest reqStatus = new UserInfoRequest();
  128. reqStatus.users = msisdn;
  129. String rsStatus = UtilsController.SendPost(reqStatus, Session.SessionID, UtilsController.WsType.UsersCheckStatus);
  130. UserActionResult resStatus = new UserActionResult(rsStatus);
  131. if (resStatus.status == UtilsController.Constant.SUCCESS)
  132. {
  133. UserInfo userInfo = new UserInfo(rsStatus);
  134. System.Web.HttpContext.Current.Session["userInfo"] = userInfo;
  135. // create new auth
  136. CreateAuthToken();
  137. String sessionId = Session.SessionID;
  138. // login success --> store session
  139. Session["msisdn"] = msisdn;
  140. // get sub
  141. UtilsController.ReloadSubInfo();
  142. // all info of user was loaded from server
  143. //UserProfile profile = Session["profile"] as UserProfile;
  144. String typeLanguage = Session["typeLanguage"] as String;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. return View(model);
  155. }
  156. public ActionResult VietnameseIndex()
  157. {
  158. // check login
  159. try
  160. {
  161. if (!CheckAuthToken())
  162. {
  163. // show login dialog
  164. return Redirect("/Home/Index");
  165. }
  166. }
  167. catch (Exception exp)
  168. {
  169. System.Diagnostics.Debug.WriteLine("Exception: " + exp);
  170. }
  171. Session["COURSE_NOW"] = UtilsController.Constant.VIETNAMESE;
  172. return View("VietnameseIndex");
  173. }
  174. public ActionResult LaosIndex()
  175. {
  176. if (!CheckAuthToken())
  177. {
  178. // show login dialog
  179. return Redirect("/Home/Index");
  180. }
  181. Session["COURSE_NOW"] = UtilsController.Constant.LAOS;
  182. return View("LaosIndex");
  183. }
  184. public ActionResult EnglishIndex()
  185. {
  186. if (!CheckAuthToken())
  187. {
  188. // show login dialog
  189. return Redirect("/Home/Index");
  190. }
  191. Session["COURSE_NOW"] = UtilsController.Constant.ENGLISH;
  192. return View("EnglishIndex");
  193. }
  194. public JsonResult CheckAccount(String typeCourse)
  195. {
  196. Session["typeLanguage"] = typeCourse;
  197. if (!CheckAuthToken())
  198. {
  199. return Json(new
  200. {
  201. code = "-1",
  202. });
  203. }
  204. return Json(new
  205. {
  206. code = "0",
  207. });
  208. }
  209. public JsonResult CourseSet(String courseId)
  210. {
  211. Session["COURSE_NOW"] = courseId;
  212. return Json(new
  213. {
  214. code = "0",
  215. });
  216. }
  217. public ActionResult ChangeLanguage(String LanguageAbbrevation)
  218. {
  219. if (CheckAuthToken())
  220. {
  221. System.Diagnostics.Debug.WriteLine("LanguageAbbrevation: " + LanguageAbbrevation);
  222. if (LanguageAbbrevation != null)
  223. {
  224. Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);
  225. Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageAbbrevation);
  226. }
  227. //Response.Cookies.Remove("Language");
  228. HttpCookie cookie = new HttpCookie("Language");
  229. cookie.Value = LanguageAbbrevation;
  230. Response.Cookies.Add(cookie);
  231. return View();
  232. }
  233. else
  234. {
  235. Session.Clear();
  236. return Redirect("/Home/Index");
  237. }
  238. }
  239. [ValidateAntiForgeryToken]
  240. public async Task<JsonResult> RegisterSub(string captcha, string phone, string otp)
  241. {
  242. try
  243. {
  244. log.Debug("RegisterSub: " + phone);
  245. String packageName = ConfigurationManager.AppSettings["packname"];
  246. String fee = ConfigurationManager.AppSettings["fee"];
  247. // Tạo đối tượng mpsparams với các giá trị từ tham số
  248. MpsParams mpsparams = new MpsParams
  249. {
  250. CMD = "REGISTER",
  251. PRICE = fee,
  252. MOBILE = phone,
  253. CATE = "BLANK",
  254. SUB = packageName
  255. };
  256. // Gọi phương thức bất đồng bộ để đăng ký
  257. GenKeyWs.response makeUrl = await WapController.UserRegisteringAsync(wsClient, mpsparams);
  258. // Lưu thông tin vào session
  259. Session["mps-params"] = mpsparams;
  260. log.Debug("mpsparams_RegisterSub: " + mpsparams);
  261. log.Debug("makeUrl_RegisterSub: " + makeUrl);
  262. // Kiểm tra mã lỗi và trả về kết quả tương ứng
  263. if (makeUrl.errorCode == UtilsController.WapConstant.SUCCESS)
  264. {
  265. return Json(new
  266. {
  267. success = true,
  268. redirectUrl = makeUrl.enscrypt
  269. });
  270. }
  271. else
  272. {
  273. return Json(new
  274. {
  275. success = false,
  276. error_code = makeUrl.errorCode,
  277. error_content = UtilsController.GetErrorCodeCharging(makeUrl.errorCode)
  278. });
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. // Log lỗi nếu cần
  284. return Json(new
  285. {
  286. success = false,
  287. error_code = "UNKNOWN_ERROR",
  288. error_content = "An unexpected error occurred: "
  289. });
  290. }
  291. }
  292. //[ValidateAntiForgeryToken]
  293. //public async Task<ActionResult> RegisterSub(string captcha, string phone, string otp)
  294. //{
  295. // try
  296. // {
  297. // string packageName = ConfigurationManager.AppSettings["packname"];
  298. // string fee = ConfigurationManager.AppSettings["fee"];
  299. // // Tạo đối tượng mpsparams với các giá trị từ tham số
  300. // MpsParams mpsparams = new MpsParams
  301. // {
  302. // CMD = "REGISTER",
  303. // PRICE = fee,
  304. // MOBILE = phone,
  305. // CATE = "BLANK",
  306. // SUB = packageName
  307. // };
  308. // // Gọi phương thức bất đồng bộ để đăng ký
  309. // GenKeyWs.response makeUrl = await WapController.UserRegisteringAsync(wsClient, mpsparams);
  310. // // Lưu thông tin vào session
  311. // Session["mps-params"] = mpsparams;
  312. // // Kiểm tra mã lỗi và trả về kết quả tương ứng
  313. // if (makeUrl.errorCode == UtilsController.WapConstant.SUCCESS)
  314. // {
  315. // return Redirect(makeUrl.enscrypt); // Điều hướng đến URL thành công
  316. // }
  317. // else
  318. // {
  319. // // Nếu không thành công, trả về một lỗi hoặc thông báo
  320. // return Json(new
  321. // {
  322. // success = false,
  323. // error_code = makeUrl.errorCode,
  324. // error_content = UtilsController.GetErrorCodeCharging(makeUrl.errorCode)
  325. // }, JsonRequestBehavior.AllowGet);
  326. // }
  327. // }
  328. // catch (Exception ex)
  329. // {
  330. // // Log lỗi nếu cần
  331. // return Json(new
  332. // {
  333. // success = false,
  334. // error_code = "UNKNOWN_ERROR",
  335. // error_content = "An unexpected error occurred."
  336. // }, JsonRequestBehavior.AllowGet);
  337. // }
  338. //}
  339. [ValidateAntiForgeryToken]
  340. public JsonResult RegisterSubWithoutCaptcha()
  341. {
  342. if (!CheckAuthToken())
  343. {
  344. return Json(new
  345. {
  346. error_code = UtilsController.Constant.EXCEPTION,
  347. error_content = ErrCode.SystemErr
  348. });
  349. }
  350. String msisdn = Session["msisdn"] as string;
  351. String whichDevice = Session["WHICHDEVICE"] as String;
  352. if (msisdn != "" && msisdn != null)
  353. {
  354. // get package
  355. String packageId = Session["COURSE_NOW"] as String;
  356. String package = UtilsController.GetPackagename(packageId, whichDevice);
  357. // check account existed
  358. var checkSub = UtilsController.checkAccountSub(msisdn, package);
  359. if (checkSub == false)
  360. {
  361. //AbandonSession();
  362. UserRequest userRequest = new UserRequest();
  363. userRequest.users = msisdn;
  364. userRequest.msisdn = msisdn;
  365. userRequest.subServiceCode = package;
  366. userRequest.requestId = "-1";
  367. userRequest.otp = "-1";
  368. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.MpsRequest);
  369. UserActionResult res = new UserActionResult(rs);
  370. if (res.responseCode == UtilsController.Constant.WAITING_OTP)
  371. //if (res.responseCode == UtilsController.Constant.SUCCESS) // for test
  372. {
  373. Charging charging = new Charging(rs);
  374. Session["charging"] = charging;
  375. Session["msisdn"] = msisdn;
  376. Session["package"] = package;
  377. Session["CHARGE_ACTION"] = UtilsController.Constant.REGISTER;
  378. Session["CHARGE_DATA"] = userRequest;
  379. }
  380. return Json(new
  381. {
  382. error_code = res.responseCode,
  383. error_content = UtilsController.GetErrorCodeCharging(res.responseCode)
  384. });
  385. }
  386. else
  387. {
  388. return Json(new
  389. {
  390. error_code = UtilsController.Constant.USER_EXISTED,
  391. error_content = UtilsController.GetErrorCodeCharging(UtilsController.Constant.USER_EXISTED)
  392. });
  393. }
  394. }
  395. else
  396. {
  397. return Json(new
  398. {
  399. error_code = UtilsController.Constant.INVALID_MSISDN,
  400. error_content = Lang.InvalidMsisdn,
  401. });
  402. }
  403. }
  404. [ValidateAntiForgeryToken]
  405. public JsonResult ResentOtp()
  406. {
  407. String msisdn = Session["msisdn"] as string;
  408. if (msisdn != "" && msisdn != null)
  409. {
  410. String chargeAction = Session["CHARGE_ACTION"] as String;
  411. UserRequest chargerequest = Session["CHARGE_DATA"] as UserRequest;
  412. if (chargeAction == UtilsController.Constant.REGISTER)
  413. {
  414. // get package
  415. String whichDevice = Session["WHICHDEVICE"] as String;
  416. String packageId = Session["COURSE_NOW"] as String;
  417. String package = UtilsController.GetPackagename(packageId, whichDevice);
  418. String rs = UtilsController.SendPost(chargerequest, Session.SessionID, UtilsController.WsType.MpsRequest);
  419. UserActionResult res = new UserActionResult(rs);
  420. if (res.responseCode == UtilsController.Constant.WAITING_OTP)
  421. //if (res.responseCode == UtilsController.Constant.SUCCESS) // for test
  422. {
  423. Charging charging = new Charging(rs);
  424. Session["charging"] = charging;
  425. Session["msisdn"] = msisdn;
  426. Session["package"] = package;
  427. }
  428. return Json(new
  429. {
  430. error_code = res.responseCode,
  431. error_content = Lang.otpSentAgain
  432. });
  433. }
  434. else
  435. {
  436. String rs = UtilsController.SendPost(chargerequest, Session.SessionID, UtilsController.WsType.buyCourse);
  437. UserActionResult res = new UserActionResult(rs);
  438. System.Diagnostics.Debug.WriteLine(rs);
  439. if (res.status == UtilsController.Constant.WAITING_OTP)
  440. //if (res.status == UtilsController.Constant.SUCCESS) // for test
  441. {
  442. Charging charging = new Charging(rs);
  443. Session["charging"] = charging;
  444. }
  445. return Json(new
  446. {
  447. error_code = res.status,
  448. error_content = UtilsController.GetErrorCodeCharging(res.status),
  449. });
  450. }
  451. }
  452. else
  453. {
  454. return Json(new
  455. {
  456. error_code = UtilsController.Constant.EXCEPTION,
  457. });
  458. }
  459. }
  460. [HttpPost]
  461. public ActionResult UploadImage(HttpPostedFileBase file)
  462. {
  463. if (CheckAuthToken())
  464. {
  465. System.Diagnostics.Debug.WriteLine("file: " + file);
  466. // check type of file
  467. if (file != null && file.ContentLength > 0)
  468. try
  469. {
  470. var supportedTypes = new[] { "jpg", "png" };
  471. var fileExt = Path.GetExtension(file.FileName).Substring(1);
  472. System.Diagnostics.Debug.WriteLine("fileExt: " + fileExt);
  473. System.Diagnostics.Debug.WriteLine("file.ContentLength: " + file.ContentLength);
  474. if (!supportedTypes.Contains(fileExt))
  475. {
  476. //return Json(new
  477. //{
  478. // error_content = "File Extension Is InValid - Only Upload WORD/PDF/EXCEL/TXT File",
  479. //});
  480. ViewBag.Message = "File Extension Is InValid - Only Upload WORD/PDF/EXCEL/TXT File";
  481. return View("ProfileView");
  482. }
  483. else if (file.ContentLength > (5 * 1024 * 1024))
  484. {
  485. //return Json(new
  486. //{
  487. // error_content = "File size Should Be UpTo " + 5 + "MB",
  488. //});
  489. ViewBag.Message = "File size Should Be UpTo 5MB";
  490. return View("ProfileView");
  491. }
  492. else
  493. {
  494. //string path = Path.Combine(Server.MapPath("~/Images"),
  495. // Path.GetFileName(file.FileName));
  496. var msisdn = Session["msisdn"] as string;
  497. System.Diagnostics.Debug.WriteLine("msisdn: " + msisdn);
  498. if (msisdn != null)
  499. {
  500. System.Diagnostics.Debug.WriteLine("FileName: " + Path.GetFileName(file.FileName));
  501. var fileName = msisdn + "_" + Path.GetFileName(file.FileName);
  502. var path = Path.Combine(Server.MapPath("~/Content/assets/imgs/post/"), fileName);
  503. System.Diagnostics.Debug.WriteLine("path: " + path);
  504. file.SaveAs(path);
  505. ViewBag.Message = "File uploaded successfully";
  506. ProfileModel model = new ProfileModel();
  507. UserProfile userProfile = Session["profile"] as UserProfile;
  508. // update icon
  509. userProfile.picture = UtilsController.GetContentPath.IndividualContentPath + fileName;
  510. String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
  511. UserActionResult res = new UserActionResult(rs);
  512. if (res.status == UtilsController.Constant.SUCCESS)
  513. {
  514. // update success --> store session
  515. Session["profile"] = userProfile;
  516. }
  517. else
  518. {
  519. return Redirect("/Common/Error");
  520. }
  521. model.profileDetail = userProfile;
  522. return View("ProfileView", model);
  523. }
  524. else
  525. {
  526. ViewBag.Message = "File uploaded failure";
  527. return Redirect("/Common/Error");
  528. }
  529. }
  530. }
  531. catch (Exception ex)
  532. {
  533. ViewBag.Message = "ERROR:" + ex.Message.ToString();
  534. return Redirect("/Common/Error");
  535. }
  536. else
  537. {
  538. ViewBag.Message = "You have not specified a file.";
  539. return Redirect("/Common/Error");
  540. }
  541. }
  542. else
  543. {
  544. Session.Clear();
  545. return Redirect("/Home/Index");
  546. }
  547. }
  548. [HttpPost]
  549. public ActionResult UploadExistedImage(String picture)
  550. {
  551. if (CheckAuthToken())
  552. {
  553. // check type of file
  554. if (picture != null)
  555. {
  556. UserProfile userProfile = Session["profile"] as UserProfile;
  557. // update icon
  558. userProfile.picture = picture;
  559. String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
  560. UserActionResult res = new UserActionResult(rs);
  561. if (res.status == UtilsController.Constant.SUCCESS)
  562. {
  563. // update success --> store session
  564. Session["profile"] = userProfile;
  565. }
  566. return Json(new
  567. {
  568. error_code = res.status,
  569. error_content = UtilsController.GetErrorCodeCharging(res.status),
  570. });
  571. }
  572. else
  573. {
  574. ViewBag.Message = "You have not specified a file.";
  575. return Redirect("/Common/Error");
  576. }
  577. }
  578. else
  579. {
  580. Session.Clear();
  581. return Redirect("/Home/Index");
  582. }
  583. }
  584. [ValidateAntiForgeryToken]
  585. public JsonResult CreateFree(String captcha, String phone)
  586. {
  587. try
  588. {
  589. if (!ReCaptcha.Validate(captcha))
  590. {
  591. return Json(new
  592. {
  593. error_code = UtilsController.Constant.USER_EXISTED,
  594. error_content = "Your captcha is not valid"
  595. });
  596. }
  597. String msisdn = UtilsController.validateMsisdn(phone);
  598. if (msisdn != "")
  599. {
  600. //AbandonSession();
  601. UserRequest userRequest = new UserRequest();
  602. userRequest.users = msisdn;
  603. userRequest.msisdn = msisdn;
  604. userRequest.command = "REGIST";
  605. userRequest.channel = "WEB";
  606. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
  607. System.Diagnostics.Debug.WriteLine("res: " + rs);
  608. UserActionResult res = new UserActionResult(rs);
  609. Session["isSub"] = "false";
  610. return Json(new
  611. {
  612. error_code = res.status,
  613. error_content = UtilsController.GetErrorCodeCharging(res.status),
  614. href = res.status == UtilsController.Constant.SUCCESS ? "/Home/Login" : null,
  615. //href = res.status == UtilsController.Constant.SUCCESS ? "/Home/Login" : null // for test
  616. });
  617. }
  618. else
  619. {
  620. return Json(new
  621. {
  622. error_code = UtilsController.Constant.INVALID_MSISDN,
  623. error_content = Lang.InvalidMsisdn,
  624. });
  625. }
  626. }
  627. catch (Exception ex)
  628. {
  629. return Json(new
  630. {
  631. error_code = UtilsController.Constant.EXCEPTION,
  632. error_content = UtilsController.GetErrorCodeCharging("-10"),
  633. });
  634. }
  635. }
  636. public ActionResult LoginSuccess(String code)
  637. {
  638. try
  639. {
  640. // verify
  641. String tempCode = code;
  642. log.Info("Login success: code = " + code + ", tempCode = " + tempCode);
  643. Verify.VerifyReq req = new Verify.VerifyReq();
  644. req.code = code;
  645. req.secretKey = ConfigurationManager.AppSettings["secretKey"];
  646. string url = ConfigurationManager.AppSettings["uniIdVerifier"];
  647. String result = UtilsController.SendPostWithAuthen(req, url, ConfigurationManager.AppSettings["secretKey"]);
  648. Verify.VerifyRes res = Verify.VerifyRes.Parse(result);
  649. if (res.status == 100)
  650. {
  651. // success -> check token
  652. String token = res.data.accessToken;
  653. Dictionary<String, String> headers = new Dictionary<string, string>();
  654. headers.Add("Authorization", "Bearer " + token);
  655. result = UtilsController.SendGetWithAuthen(ConfigurationManager.AppSettings["uniIdUsers"], token);
  656. Verify.UserRes users = Verify.UserRes.Parse(result);
  657. if (users.status == 100)
  658. {
  659. // success
  660. // login success --> store session
  661. String msisdn = UtilsController.validateMsisdn(users.data.phone);
  662. // create new auth
  663. CreateAuthToken();
  664. String sessionId = Session.SessionID;
  665. // login success --> store session
  666. Session["msisdn"] = msisdn;
  667. // get profile
  668. UtilsController.ReloadProfileInfo();
  669. UserProfile profile = Session["profile"] as UserProfile;
  670. if (profile != null)
  671. {
  672. // get sub
  673. UtilsController.ReloadSubInfo();
  674. }
  675. else
  676. {
  677. // create account
  678. UserRequest userRequest = new UserRequest();
  679. userRequest.users = msisdn;
  680. userRequest.msisdn = msisdn;
  681. userRequest.command = "REGIST";
  682. userRequest.channel = "WEB";
  683. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
  684. System.Diagnostics.Debug.WriteLine("res: " + rs);
  685. UtilsController.ReloadSubInfo();
  686. }
  687. }
  688. }
  689. return Redirect("/Home");
  690. }
  691. catch (Exception ex)
  692. {
  693. log.Error("Exception login success: " + ex.Message, ex);
  694. return Redirect("/Error");
  695. }
  696. }
  697. [ValidateAntiForgeryToken]
  698. public JsonResult Login(String phoneNumber, String password, String typeLang)
  699. {
  700. try
  701. {
  702. String msisdn = UtilsController.validateMsisdn(phoneNumber);
  703. if (msisdn != "" && msisdn != null)
  704. {
  705. //AbandonSession();
  706. UserInfoRequest user = new UserInfoRequest();
  707. user.users = msisdn;
  708. user.msisdn = msisdn;
  709. user.pass = password;
  710. user.command = "LOGIN";
  711. user.channel = "WEB";
  712. String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.UsersRegister);
  713. UserActionResult res = new UserActionResult(rs);
  714. // fake
  715. if (msisdn == "8562098376035")
  716. {
  717. // create new auth
  718. CreateAuthToken();
  719. String sessionId = Session.SessionID;
  720. // login success --> store session
  721. Session["msisdn"] = msisdn;
  722. // get profile
  723. UtilsController.ReloadProfileInfo();
  724. // get sub
  725. UtilsController.ReloadSubInfo();
  726. // all info of user was loaded from server
  727. //UserProfile profile = Session["profile"] as UserProfile;
  728. String typeLanguage = Session["typeLanguage"] as String;
  729. // check sub
  730. String isSub = Session["isSub"] as String;
  731. //HttpCookie onApp = new HttpCookie("onapp");
  732. //onApp["isonapp"] = "1";
  733. //onApp.Expires = DateTime.Now.AddDays(30);
  734. //Response.Cookies.Add(onApp);
  735. return Json(new
  736. {
  737. error_code = "0",
  738. error_content = "Success",
  739. msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
  740. href = typeLanguage == UtilsController.Constant.LAOS ? "/Home/LaosIndex" :
  741. typeLanguage == UtilsController.Constant.VIETNAMESE ? "/Home/VietnameseIndex" :
  742. typeLanguage == UtilsController.Constant.ENGLISH ? "/Home/EnglishIndex" : "/Home/Index"
  743. });
  744. }
  745. else if (res.status == UtilsController.Constant.SUCCESS)
  746. {
  747. // create new auth
  748. CreateAuthToken();
  749. String sessionId = Session.SessionID;
  750. // login success --> store session
  751. Session["msisdn"] = msisdn;
  752. // get profile
  753. UtilsController.ReloadProfileInfo();
  754. // get sub
  755. UtilsController.ReloadSubInfo();
  756. // all info of user was loaded from server
  757. //UserProfile profile = Session["profile"] as UserProfile;
  758. String typeLanguage = Session["typeLanguage"] as String;
  759. //HttpCookie onApp = new HttpCookie("onapp");
  760. //onApp["isonapp"] = "1";
  761. //onApp.Expires = DateTime.Now.AddDays(30);
  762. //Response.Cookies.Add(onApp);
  763. return Json(new
  764. {
  765. error_code = res.status,
  766. error_content = UtilsController.GetErrorCodeCharging(res.status),
  767. msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
  768. href = typeLanguage == UtilsController.Constant.LAOS ? "/Home/LaosIndex" :
  769. typeLanguage == UtilsController.Constant.VIETNAMESE ? "/Home/VietnameseIndex" :
  770. typeLanguage == UtilsController.Constant.ENGLISH ? "/Home/EnglishIndex" : "/Home/Index"
  771. });
  772. }
  773. else
  774. {
  775. return Json(new
  776. {
  777. error_code = res.status,
  778. //error_content = CommonController.ErrorContent(res.status),
  779. error_content = UtilsController.GetErrorCodeCharging(res.status),
  780. msisdn = msisdn.Substring(UtilsController.CountryCode.Length)
  781. });
  782. }
  783. }
  784. else
  785. {
  786. return Json(new
  787. {
  788. error_code = UtilsController.Constant.INVALID_MSISDN,
  789. error_content = Lang.InvalidMsisdn,
  790. });
  791. }
  792. }
  793. catch (Exception ex)
  794. {
  795. return Json(new
  796. {
  797. error_code = UtilsController.Constant.EXCEPTION,
  798. error_content = UtilsController.GetErrorCodeCharging("-10"),
  799. });
  800. }
  801. }
  802. public ActionResult About()
  803. {
  804. ViewBag.Message = "Your application description page.";
  805. return View();
  806. }
  807. public ActionResult AccountProfile()
  808. {
  809. if (CheckAuthToken())
  810. {
  811. ProfileModel model = new ProfileModel();
  812. model.profileDetail = Session["profile"] as UserProfile;
  813. return View("AccountProfileView", model);
  814. }
  815. else
  816. {
  817. Session.Clear();
  818. return Redirect("/Home/Index");
  819. }
  820. }
  821. public ActionResult Profile()
  822. {
  823. if (CheckAuthToken())
  824. {
  825. ProfileModel model = new ProfileModel();
  826. model.profileDetail = Session["profile"] as UserProfile;
  827. return View("ProfileView", model);
  828. }
  829. else
  830. {
  831. Session.Clear();
  832. return Redirect("/Home/Index");
  833. }
  834. }
  835. public ActionResult ChangePass()
  836. {
  837. if (CheckAuthToken())
  838. {
  839. ProfileModel model = new ProfileModel();
  840. model.profileDetail = Session["profile"] as UserProfile;
  841. return View("ChangePassView", model);
  842. }
  843. else
  844. {
  845. Session.Clear();
  846. return Redirect("/Home/Index");
  847. }
  848. }
  849. [ValidateAntiForgeryToken]
  850. public JsonResult UpdateProfile(String fullname, String birthday)
  851. {
  852. try
  853. {
  854. if (CheckAuthToken())
  855. {
  856. UserProfile userProfile = Session["profile"] as UserProfile;
  857. userProfile.fullName = fullname;
  858. userProfile.birthday = birthday;
  859. String rs = UtilsController.SendPost(userProfile, Session.SessionID, UtilsController.WsType.UsersUpdateProfile);
  860. UserActionResult res = new UserActionResult(rs);
  861. if (res.status == UtilsController.Constant.SUCCESS)
  862. {
  863. // update success --> store session
  864. Session["profile"] = userProfile;
  865. return Json(new
  866. {
  867. error_code = res.status,
  868. error_content = res.message,
  869. href = "/Home/Profile"
  870. });
  871. }
  872. else
  873. {
  874. return Json(new
  875. {
  876. error_code = res.status,
  877. error_content = UtilsController.GetErrorCodeCharging(res.status),
  878. });
  879. }
  880. }
  881. else
  882. {
  883. Session.Clear();
  884. return Json(new
  885. {
  886. error_code = UtilsController.Constant.EXCEPTION,
  887. error_content = UtilsController.GetErrorCodeCharging("-10"),
  888. });
  889. }
  890. }
  891. catch (Exception ex)
  892. {
  893. return Json(new
  894. {
  895. error_code = UtilsController.Constant.EXCEPTION,
  896. error_content = UtilsController.GetErrorCodeCharging("-10"),
  897. });
  898. }
  899. }
  900. [ValidateAntiForgeryToken]
  901. public JsonResult UpdatePass(String oldPassword, String password)
  902. {
  903. try
  904. {
  905. if (CheckAuthToken())
  906. {
  907. UserInfoRequest userRequest = new UserInfoRequest();
  908. userRequest.users = Session["msisdn"] as string;
  909. userRequest.passnew = password;
  910. userRequest.pass = oldPassword;
  911. userRequest.command = "CHANGEPASS";
  912. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
  913. UserActionResult res = new UserActionResult(rs);
  914. if (res.status == UtilsController.Constant.SUCCESS)
  915. {
  916. // update success --> store session
  917. return Json(new
  918. {
  919. error_code = res.status,
  920. error_content = res.message,
  921. href = "/Home/Profile"
  922. });
  923. }
  924. else
  925. {
  926. return Json(new
  927. {
  928. error_code = res.status,
  929. error_content = UtilsController.GetErrorCodeCharging(res.status),
  930. });
  931. }
  932. }
  933. else
  934. {
  935. Session.Clear();
  936. return Json(new
  937. {
  938. error_code = UtilsController.Constant.EXCEPTION,
  939. error_content = UtilsController.GetErrorCodeCharging("-10"),
  940. });
  941. }
  942. }
  943. catch (Exception ex)
  944. {
  945. return Json(new
  946. {
  947. error_code = UtilsController.Constant.EXCEPTION,
  948. error_content = UtilsController.GetErrorCodeCharging("-10"),
  949. });
  950. }
  951. }
  952. [ValidateAntiForgeryToken]
  953. public JsonResult ResetPassword(String phoneNumber)
  954. {
  955. //if (CheckAuthToken())
  956. //{
  957. String msisdn = UtilsController.validateMsisdn(phoneNumber);
  958. if (msisdn != "")
  959. {
  960. UserInfoRequest userRequest = new UserInfoRequest();
  961. userRequest.users = msisdn;
  962. userRequest.msisdn = msisdn;
  963. userRequest.command = "RESETPASS";
  964. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.UsersRegister);
  965. UserActionResult res = new UserActionResult(rs);
  966. if (res.status == UtilsController.Constant.SUCCESS)
  967. {
  968. // update success --> store session
  969. return Json(new
  970. {
  971. error_code = res.status,
  972. error_content = UtilsController.GetErrorCodeCharging(res.status),
  973. //href = "/Home/Profile"
  974. });
  975. }
  976. else
  977. {
  978. return Json(new
  979. {
  980. error_code = res.status,
  981. error_content = UtilsController.GetErrorCodeCharging(res.status),
  982. });
  983. }
  984. }
  985. else
  986. {
  987. return Json(new
  988. {
  989. error_code = UtilsController.Constant.INVALID_MSISDN,
  990. error_content = Lang.InvalidMsisdn,
  991. });
  992. }
  993. }
  994. [ValidateAntiForgeryToken]
  995. public JsonResult BuyCourse(String courseId, String phone, int courseChoose, String otpConfirm)
  996. {
  997. try
  998. {
  999. if (CheckAuthToken())
  1000. {
  1001. if (otpConfirm == null)
  1002. {
  1003. UserRequest userRequest = new UserRequest();
  1004. userRequest.users = phone;
  1005. userRequest.courseId = courseId;
  1006. userRequest.codeBuy = courseChoose == 7 ? "EDU_CHARGE_50" : "EDU_CHARGE_200";
  1007. userRequest.requestId = "-1";
  1008. userRequest.otp = "-1";
  1009. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.buyCourse);
  1010. UserActionResult res = new UserActionResult(rs);
  1011. System.Diagnostics.Debug.WriteLine(rs);
  1012. if (res.status == UtilsController.Constant.WAITING_OTP)
  1013. //if (res.status == UtilsController.Constant.SUCCESS) // for test
  1014. {
  1015. Charging charging = new Charging(rs);
  1016. Session["charging"] = charging;
  1017. Session["CHARGE_ACTION"] = UtilsController.Constant.BUY_COURSE;
  1018. Session["CHARGE_DATA"] = userRequest;
  1019. }
  1020. return Json(new
  1021. {
  1022. error_code = res.status,
  1023. error_content = UtilsController.GetErrorCodeCharging(res.status),
  1024. });
  1025. }
  1026. else
  1027. {
  1028. String msisdn = UtilsController.validateMsisdn(phone);
  1029. if (msisdn != "")
  1030. {
  1031. UserRequest userRequest = new UserRequest();
  1032. userRequest.users = phone;
  1033. userRequest.courseId = courseId;
  1034. userRequest.codeBuy = courseChoose == 7 ? "EDU_CHARGE_50" : "EDU_CHARGE_200";
  1035. // get requestId on buying course
  1036. Charging charging = Session["charging"] as Charging;
  1037. userRequest.requestId = charging.requestId;
  1038. userRequest.otp = otpConfirm.ToString();
  1039. String rs = UtilsController.SendPost(userRequest, Session.SessionID, UtilsController.WsType.buyCourse);
  1040. UserActionResult res = new UserActionResult(rs);
  1041. System.Diagnostics.Debug.WriteLine(rs);
  1042. return Json(new
  1043. {
  1044. error_code = res.status,
  1045. error_content = UtilsController.GetErrorCodeCharging(res.status),
  1046. });
  1047. }
  1048. else
  1049. {
  1050. return Json(new
  1051. {
  1052. error_code = UtilsController.Constant.INVALID_MSISDN,
  1053. error_content = Lang.InvalidMsisdn,
  1054. });
  1055. }
  1056. }
  1057. }
  1058. else
  1059. {
  1060. Session.Clear();
  1061. return Json(new
  1062. {
  1063. error_code = UtilsController.Constant.EXCEPTION,
  1064. error_content = UtilsController.GetErrorCodeCharging("-10"),
  1065. });
  1066. }
  1067. }
  1068. catch (Exception ex)
  1069. {
  1070. return Json(new
  1071. {
  1072. error_code = UtilsController.Constant.EXCEPTION,
  1073. error_content = UtilsController.GetErrorCodeCharging("-10"),
  1074. });
  1075. }
  1076. }
  1077. public ActionResult GetRanking()
  1078. {
  1079. if (CheckAuthToken())
  1080. {
  1081. RankingModel model = new RankingModel();
  1082. UserInfoRequest user = new UserInfoRequest();
  1083. user.users = Session["msisdn"] as string;
  1084. user.msisdn = Session["msisdn"] as String;
  1085. String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.GetRanking);
  1086. System.Diagnostics.Debug.WriteLine("rs: " + rs);
  1087. UserActionResult res = new UserActionResult(rs);
  1088. if (res.status == UtilsController.Constant.SUCCESS)
  1089. {
  1090. Ranking ranking = new Ranking(rs);
  1091. model.ranking = ranking;
  1092. // get current ranking
  1093. for (int i = 0; i < ranking.listRanking.Count; i++)
  1094. {
  1095. if (ranking.listRanking[i].users == Session["msisdn"] as string)
  1096. {
  1097. model.currentRanking = ranking.listRanking[i];
  1098. }
  1099. }
  1100. return PartialView("_ModalRanking", model);
  1101. }
  1102. else
  1103. {
  1104. return Redirect("/Common/Error");
  1105. }
  1106. }
  1107. else
  1108. {
  1109. Session.Clear();
  1110. return Redirect("/Home/Index");
  1111. }
  1112. }
  1113. private void CreateAuthToken()
  1114. {
  1115. // create session authen
  1116. SessionIDManager manager = new SessionIDManager();
  1117. string newSessionId = manager.CreateSessionID(System.Web.HttpContext.Current);
  1118. Response.Cookies["AuthToken"].Value = newSessionId;
  1119. Session["AuthToken"] = newSessionId;
  1120. }
  1121. private bool CheckAuthToken()
  1122. {
  1123. if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
  1124. {
  1125. if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
  1126. {
  1127. return false;
  1128. }
  1129. else
  1130. {
  1131. return true;
  1132. }
  1133. }
  1134. else
  1135. {
  1136. return false;
  1137. }
  1138. }
  1139. public RedirectResult Logout()
  1140. {
  1141. Session.Clear();
  1142. return Redirect("/Home/Index");
  1143. }
  1144. }
  1145. }