PageController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MySystem.BsModels;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.Extensions.Options;
  9. using Microsoft.Extensions.Logging;
  10. using Library;
  11. using LitJson;
  12. // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  13. namespace MySystem.Areas.Web.Controllers
  14. {
  15. [Area("Web")]
  16. [Route("Web/[controller]/[action]")]
  17. public class PageController : Admin.Controllers.BaseController
  18. {
  19. public PageController(IHttpContextAccessor accessor, ILogger<Admin.Controllers.BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. /// <summary>
  23. /// 页面入口
  24. /// </summary>
  25. /// <param name="Url"></param>
  26. /// <returns></returns>
  27. [Route("/")]
  28. [Route("/p")]
  29. [Route("/p/{Url:minlength(5)}")]
  30. public IActionResult Index(string Url = "index", string Id = "")
  31. {
  32. if (Url.StartsWith("http"))
  33. {
  34. return Redirect(Url);
  35. }
  36. string openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  37. string Kind = "consumer";
  38. if(Id == "creater")
  39. {
  40. Kind = "creater";
  41. }
  42. else if(Id == "merchant")
  43. {
  44. Kind = "creater";
  45. }
  46. AppResultJson result = (AppResultJson)new Api.Controllers.PageUpdateInfoController(_accessor, _logger, _setting).CheckDo("{\"Url\":\"" + Url.Replace("_", "#") + "\",\"Kind\":\"" + Kind + "\",\"apptype\":\"web\",\"version\":\"1.00\"}").Value;
  47. Dictionary<string, string> Obj = result.Data as Dictionary<string, string>;
  48. string ModuleContent = Obj["ModuleContent"];
  49. if (Url == "user-maker-policy" && !string.IsNullOrEmpty(Id))
  50. {
  51. ModuleContent = ModuleContent.Replace("getCookieInfo('pageId')", Id);
  52. }
  53. if (Url.StartsWith("user-inviteregist") && !string.IsNullOrEmpty(Id))
  54. {
  55. ModuleContent = ModuleContent.Replace("getCookieInfo('Id')", Id);
  56. }
  57. ViewBag.ModuleContent = ModuleContent;
  58. return View();
  59. }
  60. /// <summary>
  61. /// 授权入口
  62. /// </summary>
  63. /// <param name="EnName"></param>
  64. /// <returns></returns>
  65. [Route("/p/auth/{EnName:minlength(5)}")]
  66. public IActionResult Auth(string EnName = "index", string Id = "")
  67. {
  68. string authurl = "p/auth/" + EnName;
  69. if (!string.IsNullOrEmpty(Id))
  70. {
  71. authurl += "?Id=" + Id;
  72. }
  73. string url = "/p/" + EnName;
  74. if (!string.IsNullOrEmpty(Id))
  75. {
  76. url += "?Id=" + Id;
  77. }
  78. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  79. if (string.IsNullOrEmpty(check_openid))
  80. {
  81. // PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  82. PublicAccountSet set = new PublicAccountSet()
  83. {
  84. WeChatAppId = "wx8e980f3614b8ee6d",
  85. WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  86. };
  87. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  88. {
  89. return Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + set.WeChatAppId + "&redirect_uri=" + HttpUtility.UrlEncode(Host + authurl) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
  90. }
  91. else
  92. {
  93. string code = _accessor.HttpContext.Request.Query["code"];
  94. string result = function.PostWebRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + set.WeChatAppId + "&secret=" + set.WeChatAppSecret + "&code=" + code + "&grant_type=authorization_code", "");
  95. function.WriteLog(result, "微信授权返回日志");
  96. if (result.IndexOf("openid") > -1)
  97. {
  98. JsonData json = JsonMapper.ToObject(result);
  99. string openid = json["openid"].ToString();
  100. string access_token = json["access_token"].ToString();
  101. function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  102. return Redirect(url);
  103. }
  104. }
  105. }
  106. return View();
  107. }
  108. [Route("/p/auth2/{EnName:minlength(5)}")]
  109. public IActionResult Auth2(string EnName = "index", string Id = "")
  110. {
  111. string authurl = "p/auth2/" + EnName;
  112. if (!string.IsNullOrEmpty(Id))
  113. {
  114. authurl += "?Id=" + Id;
  115. }
  116. string url = "/p/" + EnName;
  117. if (!string.IsNullOrEmpty(Id))
  118. {
  119. url += "?Id=" + Id;
  120. }
  121. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  122. if (string.IsNullOrEmpty(check_openid))
  123. {
  124. PublicAccountSet set = bsdb.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  125. // PublicAccountSet set = new PublicAccountSet()
  126. // {
  127. // WeChatAppId = "wx8e980f3614b8ee6d",
  128. // WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  129. // };
  130. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  131. {
  132. return Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + set.WeChatAppId + "&redirect_uri=" + HttpUtility.UrlEncode(Host + authurl) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
  133. }
  134. else
  135. {
  136. string code = _accessor.HttpContext.Request.Query["code"];
  137. string result = function.PostWebRequest("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + set.WeChatAppId + "&secret=" + set.WeChatAppSecret + "&code=" + code + "&grant_type=authorization_code", "");
  138. function.WriteLog(result, "微信授权返回日志");
  139. if (result.IndexOf("openid") > -1)
  140. {
  141. JsonData json = JsonMapper.ToObject(result);
  142. string openid = json["openid"].ToString();
  143. string access_token = json["access_token"].ToString();
  144. string userStr = function.GetWebRequest("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid);
  145. function.WriteLog(userStr, "微信授权返回日志");
  146. JsonData userJson = JsonMapper.ToObject(userStr);
  147. string nickname = userJson["nickname"].ToString(); //昵称
  148. string headimgurl = userJson["headimgurl"].ToString(); //头像字
  149. // function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  150. function.WriteCookieNoDES(_accessor.HttpContext, "NickName", nickname);
  151. function.WriteCookieNoDES(_accessor.HttpContext, "HeadPhoto", headimgurl);
  152. return Redirect(url);
  153. }
  154. }
  155. }
  156. else
  157. {
  158. return Redirect(url);
  159. }
  160. return View();
  161. }
  162. public string clear()
  163. {
  164. function.WriteCookieNoDES(_accessor.HttpContext, "openid", "");
  165. return "清除完毕!!!!";
  166. }
  167. public string filterEmoji(string str)
  168. {
  169. string origin = str;
  170. try
  171. {
  172. //关键代码
  173. foreach (var a in str)
  174. {
  175. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  176. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  177. {
  178. str = str.Replace(a.ToString(), "");
  179. }
  180. }
  181. }
  182. catch
  183. {
  184. str = origin;
  185. }
  186. return str;
  187. }
  188. //排除微信授权的页面
  189. public string[] NoAuthUrls()
  190. {
  191. string content = function.ReadInstance("/doc/NoAuthUrls.txt");
  192. if (!string.IsNullOrEmpty(content))
  193. {
  194. return content.Split(',');
  195. }
  196. return new string[] { };
  197. }
  198. }
  199. }