From 3740a30034c8e2944d330bbd0695f90afb4423dd Mon Sep 17 00:00:00 2001 From: renyh1013 Date: Thu, 18 Feb 2016 02:58:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LibraryChannel.cs | 55 -- .../LibDatabase.cs | 557 +++++++++--------- dp2Command.Server/dp2Command.Server.csproj | 12 +- ...2CommandServer.cs => dp2CommandService.cs} | 114 +++- dp2Command.Server/packages.config | 3 + dp2CommandConsole/Instance.cs | 22 +- .../dp2MessageHandler/dp2MessageHandler.cs | 49 +- dp2weixin/Global.asax.cs | 5 +- dp2weixin/Index.aspx.cs | 9 +- .../ApiControllers/LibraryController.cs | 3 +- dp2weixinP2P/Controllers/WeixinController.cs | 8 +- dp2weixinP2P/Global.asax.cs | 6 +- dp2weixinP2P/dp2weixinP2P.csproj | 10 - dp2weixinP2P/packages.config | 3 - 14 files changed, 451 insertions(+), 405 deletions(-) rename {dp2weixinP2P => dp2Command.Server}/LibDatabase.cs (93%) rename dp2Command.Server/{dp2CommandServer.cs => dp2CommandService.cs} (91%) diff --git a/DigitalPlatform.LibraryRestClient/LibraryChannel.cs b/DigitalPlatform.LibraryRestClient/LibraryChannel.cs index 133806a7..da768707 100644 --- a/DigitalPlatform.LibraryRestClient/LibraryChannel.cs +++ b/DigitalPlatform.LibraryRestClient/LibraryChannel.cs @@ -163,26 +163,6 @@ public long GetVersion(out string version, } - /// - /// 专门检索微信用户对应的图书馆账号 - /// - /// - /// - /// - public long SearchReaderByWeiXinId(string strWeiXinId, - out string strError) - { - return this.SearchReader("", - strWeiXinId, - -1, - "email", - "exact", - "zh", - "weixin", - "keyid", - out strError); - } - /// /// 检索读者记录。 /// 请参考 dp2Library API SearchReader() 的详细说明 @@ -247,42 +227,7 @@ public long SearchReader(string strReaderDbNames, return response.SearchReaderResult.Value; } - /// - /// 获取根据微信id检索到的唯一读者记录 - /// - /// 读者记录路径 - /// 读者xml - /// 返回出错信息 - /// - /// -1: 出错 - /// >=0: 结果集内的记录数。注意,不是本次调用返回的结果数 - /// - public long GetSearchResultForWeiXinUser( - out string strPath, - out string strXml, - out string strError) - { - strPath = ""; - strXml = ""; - - Record[] searchresults = null; - long lRet = this.GetSearchResult("weixin", - 0, - -1, - "id,xml", - "zh", - out searchresults, - out strError); - if (searchresults.Length != 1) - { - throw new Exception("获得的记录数不是1"); - } - - strPath = searchresults[0].Path; - strXml = searchresults[0].RecordBody.Xml; - return lRet; - } /// /// 获得检索结果。 diff --git a/dp2weixinP2P/LibDatabase.cs b/dp2Command.Server/LibDatabase.cs similarity index 93% rename from dp2weixinP2P/LibDatabase.cs rename to dp2Command.Server/LibDatabase.cs index 5a531787..95d7dca6 100644 --- a/dp2weixinP2P/LibDatabase.cs +++ b/dp2Command.Server/LibDatabase.cs @@ -1,279 +1,278 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; - -using MongoDB.Bson; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Driver; - -using System.Threading.Tasks; - -namespace dp2weixinP2P -{ - public class LibItem - { - [BsonId] - [BsonRepresentation(BsonType.ObjectId)] - public string id { get; set; } //private todo mongodb怎么取值 - - public string libCode { get; set; } - public string libName { get; set; } - public string libP2PAccount { get; set; } - - - public string comment { get; set; } // 注释 - - public string OperTime { get; set; } // 操作时间 - - } - - /// - /// 用户数据库 - /// - public class LibDatabase - { - private static LibDatabase repo = new LibDatabase(); - public static LibDatabase Current - { - get - { - return repo; - } - } - - MongoClient _mongoClient = null; - IMongoDatabase _database = null; - string _libDbName = ""; - - IMongoCollection _libCollection = null; - public IMongoCollection LibCollection - { - get - { - return this._libCollection; - } - } - - // 初始化 - public void Open( - string strMongoDbConnStr, - string strInstancePrefix) - { - if (string.IsNullOrEmpty(strMongoDbConnStr) == true) - throw new ArgumentException("strMongoDbConnStr 参数值不应为空"); - - if (string.IsNullOrEmpty(strInstancePrefix) == false) - strInstancePrefix = strInstancePrefix + "_"; - _libDbName = strInstancePrefix + "lib"; - - this._mongoClient = new MongoClient(strMongoDbConnStr); - this._database = this._mongoClient.GetDatabase(this._libDbName); - - //图书馆点对点账号 - _libCollection = this._database.GetCollection("item"); - - /* - // todo 创建索引 - bool bExist = false; - var indexes = _libCollection.Indexes.ListAsync().Result.ToListAsync().Result; - foreach (BsonDocument doc in indexes) - { - } - // _logCollection.DropAllIndexes(); - if (bExist == false) - { - //await CreateIndex(); - } - */ - } - - // 创建索引 - public async Task CreateIndex() - { - /* - var options = new CreateIndexOptions() { Unique = true }; - await _libCollection.Indexes.CreateOneAsync( - Builders.IndexKeys.Ascending("libCode"), - options); - */ - } - - // 清除集合内的全部内容 - public async Task Clear() - { - if (_libCollection == null) - { - throw new Exception("访问日志 mongodb 集合尚未初始化"); - } - - // https://docs.mongodb.org/getting-started/csharp/remove/ - var filter = new BsonDocument(); - await _libCollection.DeleteManyAsync(filter); - //await CreateIndex(); - } - - public LibItem GetLibById(string id) - { - var filter = Builders.Filter.Eq("id", id); - - var list = this.LibCollection.Find(new BsonDocument("id", id)).ToListAsync().Result; - if (list.Count > 0) - return list[0]; - - return null; - } - - public List GetLibs() - { - return this.LibCollection.Find(new BsonDocument()).ToListAsync().Result; - } - - /// - /// 根据libCode获得图书馆对象 - /// - /// *获取全部 - /// 0 - /// -1 - /// - public async Task> GetLibs(string libCode, - int start, - int count) - { - IMongoCollection collection = this.LibCollection; - - List results = new List(); - - var filter = Builders.Filter.Eq("libCode", libCode); - var index = 0; - using (var cursor = await collection.FindAsync( - libCode == "*" ? new BsonDocument() : filter - )) - { - while (await cursor.MoveNextAsync()) - { - var batch = cursor.Current; - foreach (var document in batch) - { - if (count != -1 && index - start >= count) - break; - if (index >= start) - results.Add(document); - index++; - } - } - } - return results; - } - - public LibItem Add(LibItem item) - { - item.OperTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); - - this.LibCollection.InsertOne(item); - - return item; - } - - // 更新 - public async Task Update(LibItem item) - { - IMongoCollection collection = this.LibCollection; - - var filter = Builders.Filter.Eq("id", item.id); - //var filter = Builders.Filter.Eq("libCode", item.libCode); - var update = Builders.Update - .Set("libCode", item.libCode) - .Set("libName", item.libName) - .Set("libP2PAccount", item.libP2PAccount) - .Set("comment", item.comment) - .Set("OperTime", item.OperTime); - - UpdateResult ret= await collection.UpdateOneAsync(filter, update); - return ret.ModifiedCount; - } - - - /// - /// 删除 - /// - /// - public void Delete(String id) - { - IMongoCollection collection = this.LibCollection; - - var filter = Builders.Filter.Eq("id", id); - //var filter = Builders.Filter.Eq("libCode", item.libCode); - - collection.DeleteOne(filter); - } - - } - - /* - public class LibraryRespository - { - private static LibraryRespository repo = new LibraryRespository(); - - public static LibraryRespository Current - { - get - { - return repo; - } - } - - private List data = new List { - new LibItem { - id="001", libCode = "lib1", libName = "图书馆1", libP2PAccount = "a1"}, - new LibItem { - id="002",libCode = "lib2", libName = "图书馆2", libP2PAccount = "a2"}, - new LibItem { - id="003",libCode = "lib3", libName = "图书馆3", libP2PAccount = "a3"}, - }; - - public IEnumerable GetAll() - { - return data; - } - - public LibItem Get(string libId) - { - return data.Where(r => r.id == libId).FirstOrDefault(); - } - - public LibItem Add(LibItem item) - { - // id取guid todo - item.id = Guid.NewGuid().ToString(); - - data.Add(item); - return item; - } - - public void Remove(string libId) - { - LibItem item = Get(libId); - if (item != null) - { - data.Remove(item); - } - } - - public bool Update(LibItem item) - { - LibItem storedItem = Get(item.libCode); - if (storedItem != null) - { - storedItem.libName = item.libName; - storedItem.libP2PAccount = item.libP2PAccount; - return true; - } - else - { - return false; - } - } - } - */ -} \ No newline at end of file +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace dp2Command.Service +{ + + public class LibItem + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string id { get; set; } //private todo mongodb怎么取值 + + public string libCode { get; set; } + public string libName { get; set; } + public string libP2PAccount { get; set; } + + + public string comment { get; set; } // 注释 + + public string OperTime { get; set; } // 操作时间 + + } + + /// + /// 用户数据库 + /// + public class LibDatabase + { + private static LibDatabase repo = new LibDatabase(); + public static LibDatabase Current + { + get + { + return repo; + } + } + + MongoClient _mongoClient = null; + IMongoDatabase _database = null; + string _libDbName = ""; + + IMongoCollection _libCollection = null; + public IMongoCollection LibCollection + { + get + { + return this._libCollection; + } + } + + // 初始化 + public void Open( + string strMongoDbConnStr, + string strInstancePrefix) + { + if (string.IsNullOrEmpty(strMongoDbConnStr) == true) + throw new ArgumentException("strMongoDbConnStr 参数值不应为空"); + + if (string.IsNullOrEmpty(strInstancePrefix) == false) + strInstancePrefix = strInstancePrefix + "_"; + _libDbName = strInstancePrefix + "lib"; + + this._mongoClient = new MongoClient(strMongoDbConnStr); + this._database = this._mongoClient.GetDatabase(this._libDbName); + + //图书馆点对点账号 + _libCollection = this._database.GetCollection("item"); + + /* + // todo 创建索引 + bool bExist = false; + var indexes = _libCollection.Indexes.ListAsync().Result.ToListAsync().Result; + foreach (BsonDocument doc in indexes) + { + } + // _logCollection.DropAllIndexes(); + if (bExist == false) + { + //await CreateIndex(); + } + */ + } + + // 创建索引 + public async Task CreateIndex() + { + /* + var options = new CreateIndexOptions() { Unique = true }; + await _libCollection.Indexes.CreateOneAsync( + Builders.IndexKeys.Ascending("libCode"), + options); + */ + } + + // 清除集合内的全部内容 + public async Task Clear() + { + if (_libCollection == null) + { + throw new Exception("访问日志 mongodb 集合尚未初始化"); + } + + // https://docs.mongodb.org/getting-started/csharp/remove/ + var filter = new BsonDocument(); + await _libCollection.DeleteManyAsync(filter); + //await CreateIndex(); + } + + public LibItem GetLibById(string id) + { + var filter = Builders.Filter.Eq("id", id); + + var list = this.LibCollection.Find(new BsonDocument("id", id)).ToListAsync().Result; + if (list.Count > 0) + return list[0]; + + return null; + } + + public List GetLibs() + { + return this.LibCollection.Find(new BsonDocument()).ToListAsync().Result; + } + + /// + /// 根据libCode获得图书馆对象 + /// + /// *获取全部 + /// 0 + /// -1 + /// + public async Task> GetLibs(string libCode, + int start, + int count) + { + IMongoCollection collection = this.LibCollection; + + List results = new List(); + + var filter = Builders.Filter.Eq("libCode", libCode); + var index = 0; + using (var cursor = await collection.FindAsync( + libCode == "*" ? new BsonDocument() : filter + )) + { + while (await cursor.MoveNextAsync()) + { + var batch = cursor.Current; + foreach (var document in batch) + { + if (count != -1 && index - start >= count) + break; + if (index >= start) + results.Add(document); + index++; + } + } + } + return results; + } + + public LibItem Add(LibItem item) + { + item.OperTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); + + this.LibCollection.InsertOne(item); + + return item; + } + + // 更新 + public async Task Update(LibItem item) + { + IMongoCollection collection = this.LibCollection; + + var filter = Builders.Filter.Eq("id", item.id); + //var filter = Builders.Filter.Eq("libCode", item.libCode); + var update = Builders.Update + .Set("libCode", item.libCode) + .Set("libName", item.libName) + .Set("libP2PAccount", item.libP2PAccount) + .Set("comment", item.comment) + .Set("OperTime", item.OperTime); + + UpdateResult ret = await collection.UpdateOneAsync(filter, update); + return ret.ModifiedCount; + } + + + /// + /// 删除 + /// + /// + public void Delete(String id) + { + IMongoCollection collection = this.LibCollection; + + var filter = Builders.Filter.Eq("id", id); + //var filter = Builders.Filter.Eq("libCode", item.libCode); + + collection.DeleteOne(filter); + } + + } + + /* + public class LibraryRespository + { + private static LibraryRespository repo = new LibraryRespository(); + + public static LibraryRespository Current + { + get + { + return repo; + } + } + + private List data = new List { + new LibItem { + id="001", libCode = "lib1", libName = "图书馆1", libP2PAccount = "a1"}, + new LibItem { + id="002",libCode = "lib2", libName = "图书馆2", libP2PAccount = "a2"}, + new LibItem { + id="003",libCode = "lib3", libName = "图书馆3", libP2PAccount = "a3"}, + }; + + public IEnumerable GetAll() + { + return data; + } + + public LibItem Get(string libId) + { + return data.Where(r => r.id == libId).FirstOrDefault(); + } + + public LibItem Add(LibItem item) + { + // id取guid todo + item.id = Guid.NewGuid().ToString(); + + data.Add(item); + return item; + } + + public void Remove(string libId) + { + LibItem item = Get(libId); + if (item != null) + { + data.Remove(item); + } + } + + public bool Update(LibItem item) + { + LibItem storedItem = Get(item.libCode); + if (storedItem != null) + { + storedItem.libName = item.libName; + storedItem.libP2PAccount = item.libP2PAccount; + return true; + } + else + { + return false; + } + } + } + */ +} diff --git a/dp2Command.Server/dp2Command.Server.csproj b/dp2Command.Server/dp2Command.Server.csproj index 62f2c362..a43f213d 100644 --- a/dp2Command.Server/dp2Command.Server.csproj +++ b/dp2Command.Server/dp2Command.Server.csproj @@ -30,6 +30,15 @@ 4 + + ..\packages\MongoDB.Bson.2.2.3\lib\net45\MongoDB.Bson.dll + + + ..\packages\MongoDB.Driver.2.2.3\lib\net45\MongoDB.Driver.dll + + + ..\packages\MongoDB.Driver.Core.2.2.3\lib\net45\MongoDB.Driver.Core.dll + ..\packages\Senparc.Weixin.4.5.1\lib\net45\Senparc.Weixin.dll @@ -52,7 +61,8 @@ - + + diff --git a/dp2Command.Server/dp2CommandServer.cs b/dp2Command.Server/dp2CommandService.cs similarity index 91% rename from dp2Command.Server/dp2CommandServer.cs rename to dp2Command.Server/dp2CommandService.cs index f5484786..46c3d232 100644 --- a/dp2Command.Server/dp2CommandServer.cs +++ b/dp2Command.Server/dp2CommandService.cs @@ -15,20 +15,20 @@ namespace dp2Command.Server { - public class dp2CommandServer + public class dp2CommandService { // 检索限制最大命中数常量 public const int C_Search_MaxCount = 100; //================= // 设为单一实例 - static dp2CommandServer _instance; - private dp2CommandServer() + static dp2CommandService _instance; + private dp2CommandService() { //Thread.Sleep(100); //假设多线程的时候因某种原因阻塞100毫秒 } static object myObject = new object(); - static public dp2CommandServer Instance + static public dp2CommandService Instance { get { @@ -36,7 +36,7 @@ static public dp2CommandServer Instance { if (null == _instance) { - _instance = new dp2CommandServer(); + _instance = new dp2CommandService(); } return _instance; } @@ -52,12 +52,13 @@ static public dp2CommandServer Instance // dp2weixin public string dp2WeiXinUrl = "http://dp2003.com/dp2weixin"; - public string dp2WeiXinLogDir = ""; - + public string dp2WeiXinLogDir = ""; // dp2通道池 public LibraryChannelPool ChannelPool = null; + // 是否使用mongodb存储微信用户与读者关系 + private bool IsUseMongoDb = false; /// /// 构造函数 @@ -71,7 +72,8 @@ public void Init(string strDp2Url, string strDp2UserName, string strDp2Password, string strDp2WeiXinUrl, - string strDp2WeiXinLogDir) + string strDp2WeiXinLogDir, + bool isUseMongoDb) { this.dp2Url = strDp2Url; this.dp2UserName = strDp2UserName; @@ -84,6 +86,8 @@ public void Init(string strDp2Url, ChannelPool.BeforeLogin -= new BeforeLoginEventHandle(Channel_BeforeLogin); ChannelPool.BeforeLogin += new BeforeLoginEventHandle(Channel_BeforeLogin); + // + this.IsUseMongoDb = isUseMongoDb; } @@ -473,8 +477,14 @@ public int Binding(string strBarcode, // 绑定成功,把读者证条码记下来,用于续借 2015/11/7,不要用strbarcode变量,因为可能做的大小写转换 strReaderBarcode = DomUtil.GetNodeText(readerDom.DocumentElement.SelectSingleNode("barcode")); + + // todo 存在mongodb + if (this.IsUseMongoDb == true) + { + + } + return 1; - } strError = "校验读者账号返回未知情况,返回值:" + lRet.ToString() + "-" + strError; @@ -494,7 +504,9 @@ public int Binding(string strBarcode, /// /// /// - public long SearchReaderByWeiXinId(string strWeiXinId, out string strRecPath, out string strXml, + public long SearchReaderByWeiXinId(string strWeiXinId, + out string strRecPath, + out string strXml, out string strError) { strError = ""; @@ -508,7 +520,16 @@ public long SearchReaderByWeiXinId(string strWeiXinId, out string strRecPath, ou try { strWeiXinId = dp2CommandUtility.C_WeiXinIdPrefix + strWeiXinId;//weixinid: - lRet = channel.SearchReaderByWeiXinId(strWeiXinId, out strError); + + // todo 从mongodb + if (this.IsUseMongoDb == true) + { + + return 0; + } + + + lRet = this.SearchReaderByWeiXinId(channel, strWeiXinId, out strError); if (lRet == -1) { strError = "检索微信用户对应的读者出错:" + strError; @@ -526,7 +547,8 @@ public long SearchReaderByWeiXinId(string strWeiXinId, out string strRecPath, ou } else if (lRet == 1) { - lRet = channel.GetSearchResultForWeiXinUser(out strRecPath, + lRet = this.GetSearchResultForWeiXinUser(channel, + out strRecPath, out strXml, out strError); if (lRet != 1) @@ -630,8 +652,12 @@ public int Unbinding(string strReaderBarcode, out string strError) return -1; } - //清掉内存的ReaderBarcode??? - //this.ReaderBarcode = ""; + // todo 从mongodb删除 + if (this.IsUseMongoDb == true) + { + + + } return 1; } @@ -1076,5 +1102,65 @@ public void WriteErrorLog(string strText) } #endregion + + + /// + /// 专门检索微信用户对应的图书馆账号 + /// + /// + /// + /// + public long SearchReaderByWeiXinId(LibraryChannel channel, + string strWeiXinId, + out string strError) + { + return channel.SearchReader("", + strWeiXinId, + -1, + "email", + "exact", + "zh", + "weixin", + "keyid", + out strError); + } + + /// + /// 获取根据微信id检索到的唯一读者记录 + /// + /// 读者记录路径 + /// 读者xml + /// 返回出错信息 + /// + /// -1: 出错 + /// >=0: 结果集内的记录数。注意,不是本次调用返回的结果数 + /// + public long GetSearchResultForWeiXinUser(LibraryChannel channel, + out string strPath, + out string strXml, + out string strError) + { + strPath = ""; + strXml = ""; + + Record[] searchresults = null; + long lRet = channel.GetSearchResult("weixin", + 0, + -1, + "id,xml", + "zh", + out searchresults, + out strError); + if (searchresults.Length != 1) + { + throw new Exception("获得的记录数不是1"); + } + + strPath = searchresults[0].Path; + strXml = searchresults[0].RecordBody.Xml; + + return lRet; + } + } } diff --git a/dp2Command.Server/packages.config b/dp2Command.Server/packages.config index 74eb546c..bc065c96 100644 --- a/dp2Command.Server/packages.config +++ b/dp2Command.Server/packages.config @@ -1,5 +1,8 @@  + + + \ No newline at end of file diff --git a/dp2CommandConsole/Instance.cs b/dp2CommandConsole/Instance.cs index 8cd67a93..b7ddeba6 100644 --- a/dp2CommandConsole/Instance.cs +++ b/dp2CommandConsole/Instance.cs @@ -53,11 +53,11 @@ public Instance() string strDp2WeiXinUrl = "http://dp2003.com/dp2weixin"; // 将命令服务类改为单一实例方式 2015-12-15 - dp2CommandServer.Instance.Init(strDp2Url, + dp2CommandService.Instance.Init(strDp2Url, strDp2UserName, strDp2Password, strDp2WeiXinUrl, - strDp2WeiXinLogDir); + strDp2WeiXinLogDir,false); //命令集合 this.CmdContiner = new CommandContainer(); @@ -195,7 +195,7 @@ private bool DoBinding(string strParam) string strReaderBarcode = ""; string strError = ""; - long lRet = dp2CommandServer.Instance.Binding(bindingCmd.ReaderBarcode, + long lRet = dp2CommandService.Instance.Binding(bindingCmd.ReaderBarcode, bindingCmd.Password, this.WeiXinId, out strReaderBarcode, @@ -243,7 +243,7 @@ private bool DoUnbinding() } // 解除绑定 - lRet = dp2CommandServer.Instance.Unbinding(this.ReaderBarcode, out strError); + lRet = dp2CommandService.Instance.Unbinding(this.ReaderBarcode, out strError); if (lRet == -1 || lRet == 0) { Console.WriteLine(strError); @@ -284,7 +284,7 @@ private bool DoMyInfo() // 获取读者信息 string strMyInfo = ""; - lRet = dp2CommandServer.Instance.GetMyInfo1(this.ReaderBarcode, out strMyInfo, + lRet = dp2CommandService.Instance.GetMyInfo1(this.ReaderBarcode, out strMyInfo, out strError); if (lRet == -1 || lRet == 0) { @@ -323,7 +323,7 @@ private bool DoBorrowInfo() } string strBorrowInfo = ""; - lRet = dp2CommandServer.Instance.GetBorrowInfo1(this.ReaderBarcode, out strBorrowInfo, + lRet = dp2CommandService.Instance.GetBorrowInfo1(this.ReaderBarcode, out strBorrowInfo, out strError); if (lRet == -1) { @@ -366,7 +366,7 @@ private bool DoRenew(string strParam) if (strParam == "" || strParam == "view") { string strBorrowInfo = ""; - lRet = dp2CommandServer.Instance.GetBorrowInfo1(this.ReaderBarcode, out strBorrowInfo, + lRet = dp2CommandService.Instance.GetBorrowInfo1(this.ReaderBarcode, out strBorrowInfo, out strError); if (lRet == -1) { @@ -389,7 +389,7 @@ private bool DoRenew(string strParam) // 认作册条码 BorrowInfo borrowInfo = null; - lRet = dp2CommandServer.Instance.Renew(this.ReaderBarcode, + lRet = dp2CommandService.Instance.Renew(this.ReaderBarcode, strParam, out borrowInfo, out strError); @@ -454,7 +454,7 @@ public bool DoSearch(string strParam) if (nBiblioIndex >= 1) { string strBiblioInfo = ""; - lRet = dp2CommandServer.Instance.GetDetailBiblioInfo(searchCmd, nBiblioIndex, + lRet = dp2CommandService.Instance.GetDetailBiblioInfo(searchCmd, nBiblioIndex, out strBiblioInfo, out strError); if (lRet == -1) @@ -471,7 +471,7 @@ public bool DoSearch(string strParam) // 检索 string strFirstPage = ""; - lRet = dp2CommandServer.Instance.SearchBiblio(strParam, searchCmd, + lRet = dp2CommandService.Instance.SearchBiblio(strParam, searchCmd, out strFirstPage, out strError); if (lRet == -1) @@ -505,7 +505,7 @@ private int CheckIsBinding(out string strError) // 根据openid检索绑定的读者 string strRecPath = ""; string strXml = ""; - long lRet = dp2CommandServer.Instance.SearchReaderByWeiXinId(this.WeiXinId, out strRecPath, + long lRet = dp2CommandService.Instance.SearchReaderByWeiXinId(this.WeiXinId, out strRecPath, out strXml, out strError); if (lRet == -1) diff --git a/dp2weixin.service/dp2MessageHandler/dp2MessageHandler.cs b/dp2weixin.service/dp2MessageHandler/dp2MessageHandler.cs index 1be1c809..264de26d 100644 --- a/dp2weixin.service/dp2MessageHandler/dp2MessageHandler.cs +++ b/dp2weixin.service/dp2MessageHandler/dp2MessageHandler.cs @@ -28,28 +28,34 @@ namespace dp2weixin /// public partial class dp2MessageHandler : MessageHandler { - // 由外面传进来的全局对象 - public dp2CommandServer CmdServer = null; - + // 由外面传进来的CommandServer + private dp2CommandService CmdService = null; // 公众号程序目录,用于获取新书推荐与公告配置文件的路径 - public string dp2WeiXinAppDir = ""; - + private string Dp2WeiXinAppDir = ""; // 是否显示消息路径 - public bool IsDisplayPath = true; + private bool IsDisplayPath = true; /// /// 构造函数 /// /// /// - public dp2MessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0) + public dp2MessageHandler(dp2CommandService cmdServer, + Stream inputStream, PostModel postModel, int maxRecordCount = 0) : base(inputStream, postModel, maxRecordCount) { //这里设置仅用于测试,实际开发可以在外部更全局的地方设置, //比如MessageHandler.GlobalWeixinContext.ExpireMinutes = 3。 WeixinContext.ExpireMinutes = 3; - this.CmdServer = dp2CommandServer.Instance; + this.CmdService = cmdServer; + } + + // 初始化 + public void Init(string dp2weixinAppDir, bool isDisplayPath) + { + this.Dp2WeiXinAppDir = dp2weixinAppDir; + this.IsDisplayPath = isDisplayPath; } /// @@ -255,7 +261,7 @@ private IResponseMessageBase DoSearch(string strParam) if (nBiblioIndex >= 1) { string strBiblioInfo = ""; - lRet = this.CmdServer.GetDetailBiblioInfo(searchCmd, nBiblioIndex, + lRet = this.CmdService.GetDetailBiblioInfo(searchCmd, nBiblioIndex, out strBiblioInfo, out strError); if (lRet == -1) @@ -270,7 +276,7 @@ private IResponseMessageBase DoSearch(string strParam) // 检索 string strFirstPage = ""; - lRet = this.CmdServer.SearchBiblio(strParam, searchCmd, + lRet = this.CmdService.SearchBiblio(strParam, searchCmd, out strFirstPage, out strError); if (lRet == -1) @@ -328,7 +334,7 @@ private IResponseMessageBase DoBinding(string strParam) string strReaderBarcode = ""; string strError = ""; - long lRet = this.CmdServer.Binding(bindingCmd.ReaderBarcode, + long lRet = this.CmdService.Binding(bindingCmd.ReaderBarcode, bindingCmd.Password, this.CurrentMessageContext.UserName, //.WeiXinId out strReaderBarcode, @@ -372,7 +378,7 @@ private IResponseMessageBase DoUnbinding() } // 解除绑定 - lRet = this.CmdServer.Unbinding(this.CurrentMessageContext.ReaderBarcode, out strError); + lRet = this.CmdService.Unbinding(this.CurrentMessageContext.ReaderBarcode, out strError); if (lRet == -1 || lRet == 0) { return this.CreateTextResponseMessage(strError); @@ -409,7 +415,7 @@ private IResponseMessageBase DoMyInfo() // 获取读者信息 string strMyInfo = ""; - lRet = this.CmdServer.GetMyInfo1(this.CurrentMessageContext.ReaderBarcode, out strMyInfo, + lRet = this.CmdService.GetMyInfo1(this.CurrentMessageContext.ReaderBarcode, out strMyInfo, out strError); if (lRet == -1 || lRet==0) { @@ -445,7 +451,7 @@ private IResponseMessageBase DoBorrowInfo() } string strBorrowInfo = ""; - lRet = this.CmdServer.GetBorrowInfo1(this.CurrentMessageContext.ReaderBarcode, out strBorrowInfo, + lRet = this.CmdService.GetBorrowInfo1(this.CurrentMessageContext.ReaderBarcode, out strBorrowInfo, out strError); if (lRet == -1) { @@ -485,7 +491,7 @@ private IResponseMessageBase DoRenew(string strParam) if (strParam == "" || strParam == "view") { string strBorrowInfo = ""; - lRet = this.CmdServer.GetBorrowInfo1(this.CurrentMessageContext.ReaderBarcode, out strBorrowInfo, + lRet = this.CmdService.GetBorrowInfo1(this.CurrentMessageContext.ReaderBarcode, out strBorrowInfo, out strError); if (lRet == -1 || lRet==0) { @@ -501,7 +507,7 @@ private IResponseMessageBase DoRenew(string strParam) // 目前只认作册条码,todo支持序与 BorrowInfo borrowInfo = null; - lRet = this.CmdServer.Renew(this.CurrentMessageContext.ReaderBarcode, + lRet = this.CmdService.Renew(this.CurrentMessageContext.ReaderBarcode, strParam, out borrowInfo, out strError); @@ -576,7 +582,8 @@ private int CheckIsBinding(out string strError) // 根据openid检索绑定的读者 string strRecPath = ""; string strXml = ""; - long lRet = this.CmdServer.SearchReaderByWeiXinId(this.CurrentMessageContext.UserName, out strRecPath, + long lRet = this.CmdService.SearchReaderByWeiXinId(this.CurrentMessageContext.UserName, + out strRecPath, out strXml, out strError); if (lRet == -1) @@ -612,7 +619,7 @@ private IResponseMessageBase DoNewBooks() this.CurrentMessageContext.CurrentCmdName = ""; // 加载新书配置文件 - string fileName = this.dp2WeiXinAppDir + "/newbooks.xml"; + string fileName = this.Dp2WeiXinAppDir + "/newbooks.xml"; if (File.Exists(fileName) == false) { return this.CreateTextResponseMessage("暂无推荐图书。"); @@ -629,7 +636,7 @@ private IResponseMessageBase DoNewBooks() { Title = DomUtil.GetNodeText(node.SelectSingleNode("Title")), Description = DomUtil.GetNodeText(node.SelectSingleNode("Description")), - PicUrl = this.CmdServer.dp2WeiXinUrl + DomUtil.GetNodeText(node.SelectSingleNode("PicUrl")), + PicUrl = this.CmdService.dp2WeiXinUrl + DomUtil.GetNodeText(node.SelectSingleNode("PicUrl")), Url = DomUtil.GetNodeText(node.SelectSingleNode("Url")) }); } @@ -646,7 +653,7 @@ private IResponseMessageBase DoNotice() this.CurrentMessageContext.CurrentCmdName = ""; // 加载公告配置文件 - string fileName = this.dp2WeiXinAppDir + "/notice.xml"; + string fileName = this.Dp2WeiXinAppDir + "/notice.xml"; if (File.Exists(fileName) == false) { return this.CreateTextResponseMessage("暂无公告"); @@ -667,7 +674,7 @@ private IResponseMessageBase DoNotice() article.Description = DomUtil.GetNodeText(node.SelectSingleNode("Description")); string picUrl = DomUtil.GetNodeText(node.SelectSingleNode("PicUrl")); if (String.IsNullOrEmpty(picUrl) == false) - article.PicUrl = this.CmdServer.dp2WeiXinUrl + picUrl; + article.PicUrl = this.CmdService.dp2WeiXinUrl + picUrl; article.Url = DomUtil.GetNodeText(node.SelectSingleNode("Url")); responseMessage.Articles.Add(article); } diff --git a/dp2weixin/Global.asax.cs b/dp2weixin/Global.asax.cs index 9f5e7c17..34ae0562 100644 --- a/dp2weixin/Global.asax.cs +++ b/dp2weixin/Global.asax.cs @@ -29,11 +29,12 @@ protected void Application_Start(object sender, EventArgs e) string strDp2WeiXinUrl = "http://dp2003.com/dp2weixin"; // 初始化命令服务类 - dp2CommandServer.Instance.Init(strDp2Url, + dp2CommandService.Instance.Init(strDp2Url, strDp2UserName, strDp2Password, strDp2WeiXinUrl, - strDp2WeiXinLogDir); + strDp2WeiXinLogDir, + false); } } diff --git a/dp2weixin/Index.aspx.cs b/dp2weixin/Index.aspx.cs index 174571c2..aca79436 100644 --- a/dp2weixin/Index.aspx.cs +++ b/dp2weixin/Index.aspx.cs @@ -67,8 +67,9 @@ protected void Page_Load(object sender, EventArgs e) var maxRecordCount = 10; //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 - var messageHandler = new dp2MessageHandler(Request.InputStream, postModel, maxRecordCount); - messageHandler.dp2WeiXinAppDir = Server.MapPath("~"); + var messageHandler = new dp2MessageHandler(dp2CommandService.Instance, + Request.InputStream, postModel, maxRecordCount); + messageHandler.Init(Server.MapPath("~"), true); try { @@ -89,10 +90,10 @@ protected void Page_Load(object sender, EventArgs e) catch (Exception ex) { //将程序运行中发生的错误记录到日志 - dp2CommandServer.Instance.WriteErrorLog(LibraryChannel.GetExceptionMessage(ex)); + dp2CommandService.Instance.WriteErrorLog(LibraryChannel.GetExceptionMessage(ex)); if (messageHandler.ResponseDocument != null) { - dp2CommandServer.Instance.WriteErrorLog(messageHandler.ResponseDocument.ToString()); + dp2CommandService.Instance.WriteErrorLog(messageHandler.ResponseDocument.ToString()); } // 返回给微信服务器为空内容 diff --git a/dp2weixinP2P/ApiControllers/LibraryController.cs b/dp2weixinP2P/ApiControllers/LibraryController.cs index c3251d9e..1c1cd992 100644 --- a/dp2weixinP2P/ApiControllers/LibraryController.cs +++ b/dp2weixinP2P/ApiControllers/LibraryController.cs @@ -1,4 +1,5 @@ -using System; +using dp2Command.Service; +using System; using System.Collections.Generic; using System.Linq; using System.Net; diff --git a/dp2weixinP2P/Controllers/WeixinController.cs b/dp2weixinP2P/Controllers/WeixinController.cs index 48b57628..4efb43ca 100644 --- a/dp2weixinP2P/Controllers/WeixinController.cs +++ b/dp2weixinP2P/Controllers/WeixinController.cs @@ -30,6 +30,7 @@ using Senparc.Weixin.MP.MvcExtension; using Senparc.Weixin.MP; using dp2weixin; +using dp2Command.Server; namespace dp2weixinP2P.Controllers { @@ -101,8 +102,9 @@ public ActionResult Post(PostModel postModel) */ //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 - var messageHandler = new dp2MessageHandler(Request.InputStream, postModel, maxRecordCount); - messageHandler.dp2WeiXinAppDir = Server.MapPath("~"); + var messageHandler = new dp2MessageHandler(dp2CommandService.Instance, + Request.InputStream, postModel, maxRecordCount); + messageHandler.Init(Server.MapPath("~"), true); try { /* @@ -133,6 +135,8 @@ public ActionResult Post(PostModel postModel) messageHandler.FinalResponseDocument.Save(Path.Combine(logPath, string.Format("{0}_Response_Final_{1}.txt", _getRandomFileName(), messageHandler.RequestMessage.FromUserName))); } */ + + //return Content(messageHandler.ResponseDocument.ToString());//v0.7- //return new FixWeixinBugWeixinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可 return new WeixinResult(messageHandler);//v0.8+ diff --git a/dp2weixinP2P/Global.asax.cs b/dp2weixinP2P/Global.asax.cs index e5bda0fa..5b79d17f 100644 --- a/dp2weixinP2P/Global.asax.cs +++ b/dp2weixinP2P/Global.asax.cs @@ -10,6 +10,7 @@ using System.Web.Configuration; using dp2Command.Server; using DigitalPlatform.IO; +using dp2Command.Service; namespace dp2weixinP2P { @@ -45,11 +46,12 @@ void Application_Start(object sender, EventArgs e) string strDp2WeiXinUrl = "http://dp2003.com/dp2weixin"; // 初始化命令服务类 - dp2CommandServer.Instance.Init(strDp2Url, + dp2CommandService.Instance.Init(strDp2Url, strDp2UserName, strDp2Password, strDp2WeiXinUrl, - strDp2WeiXinLogDir); + strDp2WeiXinLogDir, + false);//todo 使用mongodb } } } \ No newline at end of file diff --git a/dp2weixinP2P/dp2weixinP2P.csproj b/dp2weixinP2P/dp2weixinP2P.csproj index c5b17ebc..caf31131 100644 --- a/dp2weixinP2P/dp2weixinP2P.csproj +++ b/dp2weixinP2P/dp2weixinP2P.csproj @@ -43,15 +43,6 @@ True ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll - - ..\packages\MongoDB.Bson.2.2.3\lib\net45\MongoDB.Bson.dll - - - ..\packages\MongoDB.Driver.2.2.3\lib\net45\MongoDB.Driver.dll - - - ..\packages\MongoDB.Driver.Core.2.2.3\lib\net45\MongoDB.Driver.Core.dll - False ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll @@ -154,7 +145,6 @@ - diff --git a/dp2weixinP2P/packages.config b/dp2weixinP2P/packages.config index 80a77a5e..ab814de4 100644 --- a/dp2weixinP2P/packages.config +++ b/dp2weixinP2P/packages.config @@ -11,9 +11,6 @@ - - -