HomeController.cs 52 KB

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