PageController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = "";
  49. if(Obj.ContainsKey("ModuleContent"))
  50. {
  51. ModuleContent = Obj["ModuleContent"];
  52. }
  53. if (Url == "user-maker-policy" && !string.IsNullOrEmpty(Id))
  54. {
  55. ModuleContent = ModuleContent.Replace("getCookieInfo('pageId')", Id);
  56. }
  57. if (Url.StartsWith("user-inviteregist") && !string.IsNullOrEmpty(Id))
  58. {
  59. ModuleContent = ModuleContent.Replace("getCookieInfo('Id')", Id);
  60. }
  61. ViewBag.ModuleContent = ModuleContent;
  62. return View();
  63. }
  64. /// <summary>
  65. /// 授权入口
  66. /// </summary>
  67. /// <param name="EnName"></param>
  68. /// <returns></returns>
  69. [Route("/p/auth/{EnName:minlength(5)}")]
  70. public IActionResult Auth(string EnName = "index", string Id = "")
  71. {
  72. string authurl = "p/auth/" + EnName;
  73. if (!string.IsNullOrEmpty(Id))
  74. {
  75. authurl += "?Id=" + Id;
  76. }
  77. string url = "/p/" + EnName;
  78. if (!string.IsNullOrEmpty(Id))
  79. {
  80. url += "?Id=" + Id;
  81. }
  82. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  83. if (string.IsNullOrEmpty(check_openid))
  84. {
  85. // PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  86. PublicAccountSet set = new PublicAccountSet()
  87. {
  88. WeChatAppId = "wx8e980f3614b8ee6d",
  89. WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  90. };
  91. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  92. {
  93. 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");
  94. }
  95. else
  96. {
  97. string code = _accessor.HttpContext.Request.Query["code"];
  98. 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", "");
  99. function.WriteLog(result, "微信授权返回日志");
  100. if (result.IndexOf("openid") > -1)
  101. {
  102. JsonData json = JsonMapper.ToObject(result);
  103. string openid = json["openid"].ToString();
  104. string access_token = json["access_token"].ToString();
  105. function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  106. return Redirect(url);
  107. }
  108. }
  109. }
  110. return View();
  111. }
  112. [Route("/p/auth2/{EnName:minlength(5)}")]
  113. public IActionResult Auth2(string EnName = "index", string Id = "")
  114. {
  115. string authurl = "p/auth2/" + EnName;
  116. if (!string.IsNullOrEmpty(Id))
  117. {
  118. authurl += "?Id=" + Id;
  119. }
  120. string url = "/p/" + EnName;
  121. if (!string.IsNullOrEmpty(Id))
  122. {
  123. url += "?Id=" + Id;
  124. }
  125. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  126. if (string.IsNullOrEmpty(check_openid))
  127. {
  128. PublicAccountSet set = bsdb.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  129. // PublicAccountSet set = new PublicAccountSet()
  130. // {
  131. // WeChatAppId = "wx8e980f3614b8ee6d",
  132. // WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  133. // };
  134. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  135. {
  136. 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");
  137. }
  138. else
  139. {
  140. string code = _accessor.HttpContext.Request.Query["code"];
  141. 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", "");
  142. function.WriteLog(result, "微信授权返回日志");
  143. if (result.IndexOf("openid") > -1)
  144. {
  145. JsonData json = JsonMapper.ToObject(result);
  146. string openid = json["openid"].ToString();
  147. string access_token = json["access_token"].ToString();
  148. string userStr = function.GetWebRequest("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid);
  149. function.WriteLog(userStr, "微信授权返回日志");
  150. JsonData userJson = JsonMapper.ToObject(userStr);
  151. string nickname = userJson["nickname"].ToString(); //昵称
  152. string headimgurl = userJson["headimgurl"].ToString(); //头像字
  153. // function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  154. function.WriteCookieNoDES(_accessor.HttpContext, "NickName", nickname);
  155. function.WriteCookieNoDES(_accessor.HttpContext, "HeadPhoto", headimgurl);
  156. return Redirect(url);
  157. }
  158. }
  159. }
  160. else
  161. {
  162. return Redirect(url);
  163. }
  164. return View();
  165. }
  166. public string clear()
  167. {
  168. function.WriteCookieNoDES(_accessor.HttpContext, "openid", "");
  169. return "清除完毕!!!!";
  170. }
  171. public string filterEmoji(string str)
  172. {
  173. string origin = str;
  174. try
  175. {
  176. //关键代码
  177. foreach (var a in str)
  178. {
  179. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  180. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  181. {
  182. str = str.Replace(a.ToString(), "");
  183. }
  184. }
  185. }
  186. catch
  187. {
  188. str = origin;
  189. }
  190. return str;
  191. }
  192. //排除微信授权的页面
  193. public string[] NoAuthUrls()
  194. {
  195. string content = function.ReadInstance("/doc/NoAuthUrls.txt");
  196. if (!string.IsNullOrEmpty(content))
  197. {
  198. return content.Split(',');
  199. }
  200. return new string[] { };
  201. }
  202. }
  203. }