? ? ?上篇已經(jīng)設(shè)計(jì)出比較完善的資料庫了,這篇開始進(jìn)入程式碼。 ?先把上篇設(shè)計(jì)的資料庫腳本在資料庫中執(zhí)行下,產(chǎn)生資料庫,然後在VS中建立項(xiàng)目,為了方便理解和查看,我設(shè)計(jì)的都是很直白的類名和檔名,沒有命名空間前綴。
? ? ?採用介面方式,共8個項(xiàng)目:7個類別庫與一個MVC項(xiàng)目, ?分別為: ?
? ? ? ? ? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?顯示器層-MVC項(xiàng)目
##? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?事業(yè)層方面-存取介面IBLL、特定實(shí)現(xiàn)BLL? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ?資料存取層-存取介面IDAL、特定實(shí)作DAL##? ? ? ? ? ? ? ? ? ?與 ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 模型)-DataModel
? ? ? ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?一般法-Common
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ? ? ?倉儲-Factory
##這裡的倉儲並不是為了生產(chǎn)業(yè)務(wù)邏輯層和資料存取層的接口,而是為了存放EntityFramework上下文物件和一些快取管理,業(yè)務(wù)邏輯層和資料存取層的介面生產(chǎn)(實(shí)作)工作我會交給Spring.NET注入實(shí)作。 框架搭建好之後如下:?
?
選DbContext Generator, ?然後儲存一下edmx, 之後把edmx和weixinmodel.tt複製到DataModel,刪除DAL中的edmx和weixinmodel.tt, 在datamodel中打開weixinmodel.tt保存一下即可, 另外需要在DAL中保留的WeiXinModel.Context.cs中聲明datamodel命名空間。
//添加 public T AddEntity<T>(DbContext db,T entity) where T : class { db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } //修改 public bool UpdateEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Modified; db.SaveChanges(); return true; } //刪除 public bool DeleteEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Deleted; db.SaveChanges(); return true; } // 返回一個對象 public T InfoEntities<T>(DbContext db, Expression<Func<T, bool>> whereLambda) where T : class { return db.Set<T>().Where<T>(whereLambda).FirstOrDefault(); }對應(yīng)的把介面、業(yè)務(wù)邏輯層都寫上。
現(xiàn)在來到顯示層,默認(rèn)的MVC項(xiàng)目是返回VIEW, 這里我們不需要返回頁面, 把home中的index改成Void返回類型, 接下去就是接收微信發(fā)來的請求進(jìn)行判斷了,驗(yàn)證請求----接收POST數(shù)據(jù)---分析XML----解析成自己想要的數(shù)據(jù)
入口:首先驗(yàn)證消息來源是微信服務(wù)器,然后解析收到的xml,解析成功有數(shù)據(jù)則執(zhí)行LookMsgType方法來進(jìn)行處理
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"]; //驗(yàn)證接入和每次請求驗(yàn)證真實(shí)性 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 { //未能正確處理 給微信服務(wù)器回復(fù)默認(rèn)值 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(); }
這里用到的驗(yàn)證方法:
/// <summary> /// 驗(yàn)證簽名 /// </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); //默認(rèn)排序 list.Sort(); string tmpStr = string.Empty; list.All(l => { tmpStr += l; return true; }); tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); //驗(yàn)證 if (tmpStr == signature) { return true; } return false; }
倉儲中的EF上下文:
public static DbContext WeiXinDbContext() { DbContext dbcontext =new WeiXinEntities(); //創(chuàng)建 dbcontext.Configuration.AutoDetectChangesEnabled = false;//自動檢測配置更改 dbcontext.Configuration.LazyLoadingEnabled = true;//延遲加載 dbcontext.Configuration.ValidateOnSaveEnabled = false;//自動跟蹤 return dbcontext; }
Common中的解析微信發(fā)來的XML方法
//把接收到的XML轉(zhuǎn)為字典 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; }
好了,入口以及驗(yàn)證相關(guān)的都解決了,下一篇開始微信消息處理LookMsgType方法實(shí)現(xiàn)
更多asp.net開發(fā)微信公眾平臺(2)多層架構(gòu)框架建構(gòu)與入口實(shí)現(xiàn)相關(guān)文章請關(guān)注PHP中文網(wǎng)!

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)