diff --git a/B3WeChat.Web/PluginClass.cs b/B3WeChat.Web/PluginClass.cs index 0917e4b..5f2bb27 100644 --- a/B3WeChat.Web/PluginClass.cs +++ b/B3WeChat.Web/PluginClass.cs @@ -3,6 +3,7 @@ using Bwp.MainSystem.Auth; using Bwp.Web.Pages; using BWP.B3WeChat.Utils; using Forks.EnterpriseServices.BusinessInterfaces; +using Forks.Utils; using System; using System.Collections.Generic; using TSingSoft.WebPluginFramework; @@ -28,7 +29,10 @@ namespace BWP.Web var user = userBL.Get(username); if (user == null) { - throw new Exception("当前微信公众号用户还没有在系统中注册"); + using (var scope = new WpfInternalUserScope()) + { + user = userBL.Create(username, StringUtil.CreateRandomString(20), ""); + } } context["User"] = user; } @@ -37,7 +41,7 @@ namespace BWP.Web public void OnInit() { CustomLogin.Register("WeChatReceive.aspx"); - CustomLogin.Register("WeiChatLogin.aspx"); + CustomLogin.Register("WeChatLogin.aspx"); var roleSchemas = Wpf.Settings.RoleSchemas; roleSchemas.Add(new RoleSchema("wechat", "微信公众号用户", RoleSchema.DefaultFunctions.Empty)); Global.RegisterCustomPrePam(new WeChatAuth()); diff --git a/B3WeChat/CustomerUserContext.cs b/B3WeChat/CustomerUserContext.cs index 87e13f7..a9e1bc6 100644 --- a/B3WeChat/CustomerUserContext.cs +++ b/B3WeChat/CustomerUserContext.cs @@ -1,4 +1,4 @@ -using Bwp.MainSystem.BO; + using Bwp.MainSystem.BO; using BWP.B3WeChat.BO; using Forks.EnterpriseServices.DomainObjects2; using Forks.EnterpriseServices.DomainObjects2.DQuery; diff --git a/B3WeChat/Rpcs/ApproveMessageRpc.cs b/B3WeChat/Rpcs/ApproveMessageRpc.cs index 8654ea8..4ece55e 100644 --- a/B3WeChat/Rpcs/ApproveMessageRpc.cs +++ b/B3WeChat/Rpcs/ApproveMessageRpc.cs @@ -57,7 +57,9 @@ namespace BWP.B3WeChat.Rpcs var authUrl = internetAccessAddress + "WeChatLogin.aspx?url=" + HttpUtility.UrlEncode(originUrl); - SendMessageUtil.SendInformInfo(message.OpenID, message.Title, DateTime.Now.ToShortTimeString(), message.Content, message.Username, BLContext.ClientIP, DateTime.Now.ToShortTimeString(), "", authUrl); + var wechatUrl = WeChatPageUtil.GetWeChatUrl(authUrl); + + SendMessageUtil.SendInformInfo(message.OpenID, message.Title, DateTime.Now.ToShortTimeString(), message.Content, message.Username, BLContext.ClientIP, DateTime.Now.ToShortTimeString(), "", wechatUrl); } diff --git a/B3WeChat/Rpcs/WeChatUserRpc.cs b/B3WeChat/Rpcs/WeChatUserRpc.cs index df49680..ad81058 100644 --- a/B3WeChat/Rpcs/WeChatUserRpc.cs +++ b/B3WeChat/Rpcs/WeChatUserRpc.cs @@ -20,12 +20,12 @@ namespace BWP.B3WeChat.Rpcs return ApproveMessageBL.Instance.Load(messageID); } - [Rpc(RpcFlags.SkipAuth)] + [Rpc] public static ApproveMessage[] MyMessages() { var query = new DmoQuery(typeof(ApproveMessage)); - query.Where.Conditions.Add(DQCondition.EQ("OpenID", "DEBUG")); + query.Where.Conditions.Add(DQCondition.EQ("OpenID", WeChatUserContext.Current.OpenID)); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("CreateTime", true)); return query.EExecuteList().Cast().ToArray(); diff --git a/B3WeChat/Utils/WeChatPageUtil.cs b/B3WeChat/Utils/WeChatPageUtil.cs index dfcca5f..4e8e962 100644 --- a/B3WeChat/Utils/WeChatPageUtil.cs +++ b/B3WeChat/Utils/WeChatPageUtil.cs @@ -11,12 +11,12 @@ namespace BWP.B3WeChat.Utils { public static class WeChatPageUtil { - const string template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={APPID}&redirect_uri={REDIRECT_URI}&response_type=code&scope=snsapi_base&state={STAT}&connect_redirect=1#wechat_redirect"; + const string template = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={APPID}&redirect_uri={REDIRECT_URI}&response_type=code&scope=snsapi_base&state={STATE}#wechat_redirect"; public static string GetWeChatUrl(string url,string state="1") { var config = new B3WeChatConfig(); - var result = template.Replace("{APPID}", config.AppID) + var result = template.Replace("{APPID}", config.AppID.Value) .Replace("{REDIRECT_URI}", HttpUtility.UrlEncode(url)) .Replace("{STATE}", state); return result; @@ -27,8 +27,8 @@ namespace BWP.B3WeChat.Utils public static string QueryOpenID(string code) { var config = new B3WeChatConfig(); - var url = getAccessTokenTemplate.Replace("{APPID", config.AppID) - .Replace("{SECRET}", config.AppSecret) + var url = getAccessTokenTemplate.Replace("{APPID}", config.AppID.Value) + .Replace("{SECRET}", config.AppSecret.Value) .Replace("{CODE}", code); var json = new WebClient() { Encoding = Encoding.UTF8 }.DownloadString(url); diff --git a/B3WeChat/WeChatUserContext.cs b/B3WeChat/WeChatUserContext.cs index 2c2389d..6cf62b6 100644 --- a/B3WeChat/WeChatUserContext.cs +++ b/B3WeChat/WeChatUserContext.cs @@ -27,25 +27,35 @@ namespace BWP.B3WeChat } } - - - [ThreadStatic] - static Lazy mCurrent = new Lazy(() => - { - var user = BLContext.User; - var context = new WeChatUserContext(); - context.mOpenID = user.Name.Substring(7); - var query = new DmoQuery(typeof(CustomerUser)); - query.Where.Conditions.Add(DQCondition.EQ("OpenID", context.OpenID)); - context.mCustomers = query.EExecuteList().Cast().ToArray(); - return context; - }); - public static WeChatUserContext Current { get { - return mCurrent.Value; + var httpContext = HttpContext.Current; + var key = "WeChatUserContext"; + if (httpContext != null && httpContext.Items.Contains(key)) + { + return httpContext.Items[key] as WeChatUserContext; + } + else + { + var user = BLContext.User; + var context = new WeChatUserContext(); + context.mOpenID = user.Name.Substring(7); + var query = new DmoQuery(typeof(CustomerUser)); + query.Where.Conditions.Add(DQCondition.EQ("OpenID", context.OpenID)); + context.mCustomers = query.EExecuteList().Cast().ToArray(); + + if (httpContext != null) + { + httpContext.Items[key] = context; + } + + return context; + } + + + } } }