Browse Source

修正微信用户的登录问题

master
yashen 9 years ago
parent
commit
8a72a978b5
6 changed files with 41 additions and 25 deletions
  1. +6
    -2
      B3WeChat.Web/PluginClass.cs
  2. +1
    -1
      B3WeChat/CustomerUserContext.cs
  3. +3
    -1
      B3WeChat/Rpcs/ApproveMessageRpc.cs
  4. +2
    -2
      B3WeChat/Rpcs/WeChatUserRpc.cs
  5. +4
    -4
      B3WeChat/Utils/WeChatPageUtil.cs
  6. +25
    -15
      B3WeChat/WeChatUserContext.cs

+ 6
- 2
B3WeChat.Web/PluginClass.cs View File

@ -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());


+ 1
- 1
B3WeChat/CustomerUserContext.cs View File

@ -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;


+ 3
- 1
B3WeChat/Rpcs/ApproveMessageRpc.cs View File

@ -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);
}


+ 2
- 2
B3WeChat/Rpcs/WeChatUserRpc.cs View File

@ -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<ApproveMessage>().ToArray();


+ 4
- 4
B3WeChat/Utils/WeChatPageUtil.cs View File

@ -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);


+ 25
- 15
B3WeChat/WeChatUserContext.cs View File

@ -27,25 +27,35 @@ namespace BWP.B3WeChat
}
}
[ThreadStatic]
static Lazy<WeChatUserContext> mCurrent = new Lazy<WeChatUserContext>(() =>
{
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<CustomerUser>().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<CustomerUser>().ToArray();
if (httpContext != null)
{
httpContext.Items[key] = context;
}
return context;
}
}
}
}


Loading…
Cancel
Save