using System; using System.Collections.Generic; using System.Linq; using System.Web; using MySystem.BsModels; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Extensions.Logging; using Library; using LitJson; // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace MySystem.Areas.Web.Controllers { [Area("Web")] [Route("Web/[controller]/[action]")] public class PageController : Admin.Controllers.BaseController { public PageController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } /// /// 页面入口 /// /// /// [Route("/")] [Route("/p")] [Route("/p/{Url:minlength(5)}")] public IActionResult Index(string Url = "index", string Id = "") { if (Url.StartsWith("http")) { return Redirect(Url); } string openid = function.GetCookieNoDES(_accessor.HttpContext, "openid"); string Kind = "consumer"; if(Id == "creater") { Kind = "creater"; } else if(Id == "merchant") { Kind = "creater"; } AppResultJson result = (AppResultJson)new Api.Controllers.PageUpdateInfoController(_accessor, _logger, _setting).CheckDo("{\"Url\":\"" + Url.Replace("_", "#") + "\",\"Kind\":\"" + Kind + "\",\"apptype\":\"web\",\"version\":\"1.00\"}").Value; Dictionary Obj = result.Data as Dictionary; string ModuleContent = ""; if(Obj.ContainsKey("ModuleContent")) { ModuleContent = Obj["ModuleContent"]; } if (Url == "user-maker-policy" && !string.IsNullOrEmpty(Id)) { ModuleContent = ModuleContent.Replace("getCookieInfo('pageId')", Id); } if (Url.StartsWith("user-inviteregist") && !string.IsNullOrEmpty(Id)) { ModuleContent = ModuleContent.Replace("getCookieInfo('Id')", Id); } ViewBag.ModuleContent = ModuleContent; return View(); } /// /// 授权入口 /// /// /// [Route("/p/auth/{EnName:minlength(5)}")] public IActionResult Auth(string EnName = "index", string Id = "") { string authurl = "p/auth/" + EnName; if (!string.IsNullOrEmpty(Id)) { authurl += "?Id=" + Id; } string url = "/p/" + EnName; if (!string.IsNullOrEmpty(Id)) { url += "?Id=" + Id; } string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid"); if (string.IsNullOrEmpty(check_openid)) { // PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet(); PublicAccountSet set = new PublicAccountSet() { WeChatAppId = "wx8e980f3614b8ee6d", WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95", }; if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"])) { 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"); } else { string code = _accessor.HttpContext.Request.Query["code"]; 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", ""); function.WriteLog(result, "微信授权返回日志"); if (result.IndexOf("openid") > -1) { JsonData json = JsonMapper.ToObject(result); string openid = json["openid"].ToString(); string access_token = json["access_token"].ToString(); function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid); return Redirect(url); } } } return View(); } [Route("/p/auth2/{EnName:minlength(5)}")] public IActionResult Auth2(string EnName = "index", string Id = "") { string authurl = "p/auth2/" + EnName; if (!string.IsNullOrEmpty(Id)) { authurl += "?Id=" + Id; } string url = "/p/" + EnName; if (!string.IsNullOrEmpty(Id)) { url += "?Id=" + Id; } string check_openid = function.GetCookieNoDES(_accessor.HttpContext, "openid"); if (string.IsNullOrEmpty(check_openid)) { PublicAccountSet set = bsdb.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet(); // PublicAccountSet set = new PublicAccountSet() // { // WeChatAppId = "wx8e980f3614b8ee6d", // WeChatAppSecret = "8ca8ccaaa8d27b30efcc775eb47c3b95", // }; if (string.IsNullOrEmpty(_accessor.HttpContext.Request.Query["code"])) { 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"); } else { string code = _accessor.HttpContext.Request.Query["code"]; 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", ""); function.WriteLog(result, "微信授权返回日志"); if (result.IndexOf("openid") > -1) { JsonData json = JsonMapper.ToObject(result); string openid = json["openid"].ToString(); string access_token = json["access_token"].ToString(); string userStr = function.GetWebRequest("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid); function.WriteLog(userStr, "微信授权返回日志"); JsonData userJson = JsonMapper.ToObject(userStr); string nickname = userJson["nickname"].ToString(); //昵称 string headimgurl = userJson["headimgurl"].ToString(); //头像字 // function.WriteCookieNoDES(_accessor.HttpContext, "openid", openid); function.WriteCookieNoDES(_accessor.HttpContext, "NickName", nickname); function.WriteCookieNoDES(_accessor.HttpContext, "HeadPhoto", headimgurl); return Redirect(url); } } } else { return Redirect(url); } return View(); } public string clear() { function.WriteCookieNoDES(_accessor.HttpContext, "openid", ""); return "清除完毕!!!!"; } public string filterEmoji(string str) { string origin = str; try { //关键代码 foreach (var a in str) { byte[] bts = System.Text.Encoding.UTF32.GetBytes(a.ToString()); if (bts[0].ToString() == "253" && bts[1].ToString() == "255") { str = str.Replace(a.ToString(), ""); } } } catch { str = origin; } return str; } //排除微信授权的页面 public string[] NoAuthUrls() { string content = function.ReadInstance("/doc/NoAuthUrls.txt"); if (!string.IsNullOrEmpty(content)) { return content.Split(','); } return new string[] { }; } } }