PageController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MySystem.Models;
  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. // if (string.IsNullOrEmpty(openid) && !NoAuthUrls().Contains(Url))
  47. // {
  48. // string url = "/p/auth/" + Url;
  49. // if (!string.IsNullOrEmpty(Id))
  50. // {
  51. // url += "?Id=" + Id;
  52. // }
  53. // return Redirect(url);
  54. // }
  55. // Users user = db.Users.FirstOrDefault(m => m.OpenId == openid) ?? new Users();
  56. // Users user = db.Users.FirstOrDefault(m => m.OpenId == "1234567890");
  57. // if (user == null)
  58. // {
  59. // user = db.Users.Add(new Users()
  60. // {
  61. // CreateDate = DateTime.Now,
  62. // UpdateDate = DateTime.Now,
  63. // NickName = "我勒个去",
  64. // Sex = 1,
  65. // HeadPhoto = "",
  66. // Areas = "sichuan,chengdu",
  67. // OpenId = "1234567890",
  68. // }).Entity;
  69. // db.SaveChanges();
  70. // }
  71. // function.WriteCookie(_accessor.HttpContext, "openid", "1234567890");
  72. AppResultJson result = (AppResultJson)new Api.Controllers.PageUpdateInfoController(_accessor, _logger, _setting).CheckDo("{\"Url\":\"" + Url.Replace("_", "#") + "\",\"Kind\":\"" + Kind + "\",\"apptype\":\"web\",\"version\":\"1.00\"}").Value;
  73. Dictionary<string, string> Obj = result.Data as Dictionary<string, string>;
  74. string ModuleContent = Obj["ModuleContent"];
  75. // string Token = function.MD532(Guid.NewGuid().ToString());
  76. // string pretoken = function.GetCookie(_accessor.HttpContext, "Token");
  77. // function.WriteCookie(_accessor.HttpContext, "Token", Token);
  78. // function.WriteCookieNoDES(_accessor.HttpContext, "isWeb", "1");
  79. // ModuleContent = ModuleContent.Replace("getCookieInfo('Token')", "'" + Token + "'");
  80. // if (ModuleContent.Contains("${PictureCodeTag}"))
  81. // {
  82. // string Tag = function.MD5_16(Guid.NewGuid().ToString());
  83. // function.WriteCookie(_accessor.HttpContext, "Tag", Tag);
  84. // ModuleContent = ModuleContent.Replace("${PictureCodeTag}", Tag);
  85. // }
  86. if (Url == "user-maker-policy" && !string.IsNullOrEmpty(Id))
  87. {
  88. ModuleContent = ModuleContent.Replace("getCookieInfo('pageId')", Id);
  89. }
  90. if (Url.StartsWith("user-inviteregist") && !string.IsNullOrEmpty(Id))
  91. {
  92. ModuleContent = ModuleContent.Replace("getCookieInfo('Id')", Id);
  93. }
  94. ViewBag.ModuleContent = ModuleContent;
  95. return View();
  96. }
  97. /// <summary>
  98. /// 授权入口
  99. /// </summary>
  100. /// <param name="EnName"></param>
  101. /// <returns></returns>
  102. [Route("/p/auth/{EnName:minlength(5)}")]
  103. public IActionResult Auth(string EnName = "index", string Id = "")
  104. {
  105. string authurl = "p/auth/" + EnName;
  106. if (!string.IsNullOrEmpty(Id))
  107. {
  108. authurl += "?Id=" + Id;
  109. }
  110. string url = "/p/" + EnName;
  111. if (!string.IsNullOrEmpty(Id))
  112. {
  113. url += "?Id=" + Id;
  114. }
  115. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  116. if (string.IsNullOrEmpty(check_openid))
  117. {
  118. // PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  119. PublicAccountSet set = new PublicAccountSet()
  120. {
  121. WeChatAppId = "wx8e980f3614b8ee6d",
  122. WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  123. };
  124. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  125. {
  126. 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");
  127. }
  128. else
  129. {
  130. string code = _accessor.HttpContext.Request.Query["code"];
  131. 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", "");
  132. function.WriteLog(result, "微信授权返回日志");
  133. if (result.IndexOf("openid") > -1)
  134. {
  135. JsonData json = JsonMapper.ToObject(result);
  136. string openid = json["openid"].ToString();
  137. string access_token = json["access_token"].ToString();
  138. function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  139. return Redirect(url);
  140. }
  141. }
  142. }
  143. return View();
  144. }
  145. [Route("/p/auth2/{EnName:minlength(5)}")]
  146. public IActionResult Auth2(string EnName = "index", string Id = "")
  147. {
  148. string authurl = "p/auth2/" + EnName;
  149. if (!string.IsNullOrEmpty(Id))
  150. {
  151. authurl += "?Id=" + Id;
  152. }
  153. string url = "/p/" + EnName;
  154. if (!string.IsNullOrEmpty(Id))
  155. {
  156. url += "?Id=" + Id;
  157. }
  158. string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid");
  159. if (string.IsNullOrEmpty(check_openid))
  160. {
  161. PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  162. // PublicAccountSet set = new PublicAccountSet()
  163. // {
  164. // WeChatAppId = "wx8e980f3614b8ee6d",
  165. // WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95",
  166. // };
  167. if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"]))
  168. {
  169. 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");
  170. }
  171. else
  172. {
  173. string code = _accessor.HttpContext.Request.Query["code"];
  174. 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", "");
  175. function.WriteLog(result, "微信授权返回日志");
  176. if (result.IndexOf("openid") > -1)
  177. {
  178. JsonData json = JsonMapper.ToObject(result);
  179. string openid = json["openid"].ToString();
  180. string access_token = json["access_token"].ToString();
  181. string userStr = function.GetWebRequest("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid);
  182. function.WriteLog(userStr, "微信授权返回日志");
  183. JsonData userJson = JsonMapper.ToObject(userStr);
  184. string nickname = userJson["nickname"].ToString(); //昵称
  185. string headimgurl = userJson["headimgurl"].ToString(); //头像字
  186. // function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid);
  187. function.WriteCookieNoDES(_accessor.HttpContext, "NickName", nickname);
  188. function.WriteCookieNoDES(_accessor.HttpContext, "HeadPhoto", headimgurl);
  189. return Redirect(url);
  190. }
  191. }
  192. }
  193. else
  194. {
  195. return Redirect(url);
  196. }
  197. return View();
  198. }
  199. public string clear()
  200. {
  201. function.WriteCookieNoDES(_accessor.HttpContext, "openid", "");
  202. return "清除完毕!!!!";
  203. }
  204. public string filterEmoji(string str)
  205. {
  206. string origin = str;
  207. try
  208. {
  209. //关键代码
  210. foreach (var a in str)
  211. {
  212. byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString());
  213. if (bts[0].ToString() == "253" && bts[1].ToString() == "255")
  214. {
  215. str = str.Replace(a.ToString(), "");
  216. }
  217. }
  218. }
  219. catch
  220. {
  221. str = origin;
  222. }
  223. return str;
  224. }
  225. //排除微信授权的页面
  226. public string[] NoAuthUrls()
  227. {
  228. string content = function.ReadInstance("/doc/NoAuthUrls.txt");
  229. if (!string.IsNullOrEmpty(content))
  230. {
  231. return content.Split(',');
  232. }
  233. return new string[] { };
  234. }
  235. }
  236. }