123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<Admin.Controllers.BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
- {
- }
- /// <summary>
- /// 页面入口
- /// </summary>
- /// <param name="Url"></param>
- /// <returns></returns>
- [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<string, string> Obj = result.Data as Dictionary<string, string>;
- 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();
- }
- /// <summary>
- /// 授权入口
- /// </summary>
- /// <param name="EnName"></param>
- /// <returns></returns>
- [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[] { };
- }
- }
- }
|