您的位置:首页 > 运维架构 > 网站架构

[c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现

2014-04-11 08:48 681 查看
上篇已经设计出比较完善的数据库了,这篇开始进入代码。 首先把上篇设计的数据库脚本在数据库中执行下,生成数据库,然后在VS中建立项目,为了方便理解和查看,我设计的都是很直白的类名和文件名,没有命名空间前缀。

采用接口方式,共8个项目:7个类库和一个MVC项目, 分别为:

显示层——MVC项目

业务逻辑层——访问接口IBLL、具体实现BLL

数据访问层——访问接口IDAL、具体实现DAL

数据(模型)——DataModel

通用方法——Common

仓储——Factory

这里的仓储并不为了生产业务逻辑层和数据访问层的接口,而是为了存放EntityFramework上下文对象和一些缓存管理,业务逻辑层和数据访问层的接口生产(实现)工作我会交给Spring.NET注入实现。 框架搭建好之后如下:

private IBLL.IDoWei BLLWei { set; get; }
public DbContext dbHome { get; set; }
private string token { get; set; }
Dictionary<string, string> xmlModel = new Dictionary<string, string>();
public void Index()
{
dbHome=FContext.WeiXinDbContext();
//xml字符串
string xmlData = string.Empty;
//请求类型
string method=Request.HttpMethod.ToLower();
string signature = Request.QueryString["signature"];
string timestamp = Request.QueryString["timestamp"];
string nonce = Request.QueryString["nonce"];
//验证接入和每次请求验证真实性
if (method == "get")
{
if (CheckSign(signature,timestamp,nonce))
{
Often.ResponseToEnd(Request.QueryString["echostr"]);
}
else
{
Response.Status = "403";
Often.ResponseToEnd("");
}
}
//处理接收到的POST消息
else if (method == "post")
{
using (Stream stream = Request.InputStream)
{
Byte[] byteData = new Byte[stream.Length];
stream.Read(byteData, 0, (Int32)stream.Length);
xmlData = Encoding.UTF8.GetString(byteData);
}
if (!string.IsNullOrEmpty(xmlData))
{
try
{
xmlModel = ReadXml.GetXmlModel(xmlData);
}
catch
{
//未能正确处理 给微信服务器回复默认值
Often.ResponseToEnd("");
}
}
if (xmlModel.Count > 0)
{
string msgType = ReadXml.ReadModel("MsgType", xmlModel);
LookMsgType(msgType);
}
}
else//除了post和get外 如head皆视为非法请求
{
Response.Status = "403";
Often.ResponseToEnd("");
}
dbHome.Dispose();
}


View Code
这里用到的验证方法:

/// <summary>
/// 验证签名
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <returns></returns>
public bool CheckSign(string signature, string timestamp, string nonce)
{
List<string> list = new List<string>();
list.Add(token);
list.Add(timestamp);
list.Add(nonce);
//默认排序
list.Sort();
string tmpStr = string.Empty;
list.All(l => { tmpStr += l; return true; });
tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
//验证
if (tmpStr == signature)
{
return true;
}
return false;
}


仓储中的EF上下文:

public static DbContext WeiXinDbContext()
{
DbContext dbcontext =new WeiXinEntities();  //创建
dbcontext.Configuration.AutoDetectChangesEnabled = false;//自动检测配置更改
dbcontext.Configuration.LazyLoadingEnabled = true;//延迟加载
dbcontext.Configuration.ValidateOnSaveEnabled = false;//自动跟踪
return dbcontext;
}


Common中的解析微信发来的XML方法

//把接收到的XML转为字典
public static Dictionary<string, string> GetXmlModel(string xmlStr)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlStr);
Dictionary<string, string> mo = new Dictionary<string, string>();
var data = doc.DocumentElement.ChildNodes;
for (int i = 0; i < data.Count; i++)
{
mo.Add(data.Item(i).LocalName, data.Item(i).InnerText);
}
return mo;
}

////从字典中读取指定的值
public static string ReadModel(string key, Dictionary<string, string> model)
{
string str = "";
model.TryGetValue(key, out str);
if (str== null)
str = "";
return str;
}


好了,入口以及验证相关的都解决了,下一篇开始微信消息处理LookMsgType方法实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐