HttpController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using NEducation.Content.Texts;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using NEducation.Code;
  8. using System.Configuration;
  9. namespace NEducation.Controllers
  10. {
  11. public class HttpController : Controller
  12. {
  13. // GET: Http
  14. public ActionResult Index()
  15. {
  16. return View();
  17. }
  18. [ValidateAntiForgeryToken]
  19. public JsonResult Signup(String phoneNumber)
  20. {
  21. String msisdn = UtilsController.validateMsisdn(phoneNumber);
  22. if (msisdn != "")
  23. {
  24. UserInfoRequest user = new UserInfoRequest();
  25. user.users = msisdn;
  26. user.msisdn = msisdn;
  27. user.command = "REGIST";
  28. user.channel = "WEB";
  29. String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.UsersRegister);
  30. UserActionResult res = new UserActionResult(rs);
  31. if (res.status == "0")
  32. {
  33. // login success --> store session
  34. Session["msisdn"] = msisdn;
  35. // get profile
  36. GetUserProfileReq req = new GetUserProfileReq();
  37. req.users = msisdn;
  38. rs = UtilsController.SendPost(req, Session.SessionID, UtilsController.WsType.UsersGetProfile);
  39. UserProfile profile = new UserProfile(rs);
  40. Session["profile"] = profile;
  41. // success
  42. return Json(new
  43. {
  44. error_code = res.status,
  45. error_content = res.message,
  46. msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
  47. href = "/Home/MyProfile"
  48. });
  49. }
  50. else
  51. {
  52. // sign up fail
  53. return Json(new
  54. {
  55. error_code = res.status,
  56. error_content = UtilsController.ErrorContent(res.status),
  57. msisdn = msisdn.Substring(UtilsController.CountryCode.Length)
  58. });
  59. }
  60. }
  61. else
  62. {
  63. return Json(new
  64. {
  65. error_code = "-1",
  66. error_content = Lang.InvalidMsisdn
  67. });
  68. }
  69. }
  70. [ValidateAntiForgeryToken]
  71. public JsonResult Login(String phoneNumber, String password)
  72. {
  73. try
  74. {
  75. System.Diagnostics.Debug.WriteLine("before " + phoneNumber);
  76. String msisdn = UtilsController.validateMsisdn(phoneNumber);
  77. System.Diagnostics.Debug.WriteLine("after " + msisdn);
  78. if (msisdn != "" && msisdn != null)
  79. {
  80. UserInfoRequest user = new UserInfoRequest();
  81. user.users = msisdn;
  82. user.msisdn = msisdn;
  83. user.pass = password;
  84. user.command = "LOGIN";
  85. user.channel = "WEB";
  86. String rs = UtilsController.SendPost(user, Session.SessionID, UtilsController.WsType.UsersRegister);
  87. UserActionResult res = new UserActionResult(rs);
  88. if (res.status == "0")
  89. {
  90. // login success --> store session
  91. Session["msisdn"] = msisdn;
  92. // get profile
  93. ReloadProfileInfo();
  94. // get sub
  95. ReloadSubInfo();
  96. System.Diagnostics.Debug.WriteLine(Session);
  97. UserProfile profile = Session["profile"] as UserProfile;
  98. return Json(new
  99. {
  100. error_code = res.status,
  101. error_content = res.message,
  102. msisdn = msisdn.Substring(UtilsController.CountryCode.Length),
  103. //href = (profile == null || profile.fullName == null || profile.fullName == "" || profile.picture == null || profile.picture == "") ?
  104. // "/Home/AccountProfile" : "/Home/Profiles"
  105. href = "/Home/AccountProfile"
  106. });
  107. }
  108. else
  109. {
  110. return Json(new
  111. {
  112. error_code = res.status,
  113. //error_content = CommonController.ErrorContent(res.status),
  114. error_content = res.message,
  115. msisdn = msisdn.Substring(UtilsController.CountryCode.Length)
  116. });
  117. }
  118. }
  119. else
  120. {
  121. return Json(new
  122. {
  123. error_code = "-1",
  124. error_content = Lang.InvalidMsisdn
  125. });
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. return Json(new
  131. {
  132. error_code = "-2",
  133. error_content = ex.Message
  134. }); ;
  135. }
  136. }
  137. public void ReloadProfileInfo()
  138. {
  139. String msisdn = Session["msisdn"] as String;
  140. if (msisdn != null)
  141. {
  142. GetUserProfileReq req = new GetUserProfileReq();
  143. req.users = msisdn;
  144. String rs = UtilsController.SendPost(req, Session.SessionID, UtilsController.WsType.UsersGetProfile);
  145. UserProfile profile = new UserProfile(rs);
  146. System.Diagnostics.Debug.WriteLine("profile: " + profile);
  147. Session["profile"] = profile;
  148. }
  149. }
  150. public void ReloadSubInfo()
  151. {
  152. String msisdn = Session["msisdn"] as String;
  153. if (msisdn != null)
  154. {
  155. GetSubReq reqSub = new GetSubReq();
  156. reqSub.msisdn = msisdn;
  157. String rs = UtilsController.SendPost(reqSub, Session.SessionID, UtilsController.WsType.SubGetListsubServiceCode);
  158. GetSubRes resSub = new GetSubRes(rs);
  159. Session["subInfo"] = resSub.listSubServiceCode;
  160. }
  161. }
  162. }
  163. }