/// <summary> /// 根据节点ID,获取服务实例 /// </summary> /// <param name="nodeID">节点ID</param> internal override NodeInstanceService CreateServicInstance(string nodeID) { //查找 节点ID对应 的配置信息 ResponseNodeConfig configNodeInfo = ConfigTree.GetConfigByNodeID(nodeID); if (configNodeInfo == null) { return(null); } //找不到配置节点时的处理 if (configNodeInfo == null) { NodeInstanceService node_unknown = new NodeInstanceService(ConstString.UNKNOWN_NODE_ID); node_unknown.DealingHandler = DefaultDealingHandler.CreateInstance_Text("抱歉,當前指令現在無法為您提供服務。"); return(node_unknown); } //根据配置文件处理 NodeInstanceService result = new NodeInstanceService(configNodeInfo.NodeID); if (configNodeInfo.DealingHandlerConfig != null) { dynamic dealingHandlerConfig = configNodeInfo.DealingHandlerConfig; result.DealingHandler = dealingHandlerConfig.CreateInstanceFromConfig(); #region 如果是自定义处理器,再注入参数配置的内容 try { if (result.DealingHandler is ICustomHandler && result.DealingHandler is ICustomHandlerConfigable) { Type configClassType = ConfigClassOfCustomHandlerHelper.GetConfigClassType(result.DealingHandler.GetType()); if (configClassType != null) { dynamic configData = ConfigClassOfCustomHandlerHelper.GetConfigClassInstance(configClassType, configNodeInfo.NodeID); dynamic dh = result.DealingHandler; dh.ConfigData = configData; } } } catch (Exception ex) { Logger.Log4Net.Error("自定义处理器的参数配置,注入失败。", ex); } #endregion } if (configNodeInfo.DoneHandlerConfig != null) { dynamic doneHandlerConfig = configNodeInfo.DoneHandlerConfig; result.DonedHandler = doneHandlerConfig.CreateInstanceFromConfig(); } return(result); }
//---------Control--------- #region 读取配置文件 /// <summary> /// 从配置文件,读取 配置信息 。(非线程安全,请外部自己处理。) /// </summary> /// <param name="ConfigNodeDic">结果集合</param> public static void LoadConfig_ResponseChain(Dictionary <string, ResponseNodeConfig> ConfigNodeDic) { //初始化 if (ConfigNodeDic == null) { ConfigNodeDic = new Dictionary <string, ResponseNodeConfig>(); } else { ConfigNodeDic.Clear(); } //读取配置文件 string fullPath = System.Web.Hosting.HostingEnvironment.MapPath(CONFIG_FILEPATH); if (System.IO.File.Exists(fullPath) == false) { return; } //开始解析 XDocument xdoc = XDocument.Load(fullPath); var nodeList = xdoc.Root.Elements("Node"); foreach (XElement item in nodeList) { try { string nodeID = item.Element("NodeID").Value; IHandlerConfig DealingHandlerConfig = ConfigHelper.GetIConfigFromXElement(item.Element("DealingHandler")) as IHandlerConfig; IHandlerConfig DoneHandlerConfig = ConfigHelper.GetIConfigFromXElement(item.Element("DoneHandler")) as IHandlerConfig; //##生成Node节点 ResponseNodeConfig node = new ResponseNodeConfig(nodeID, DealingHandlerConfig, DoneHandlerConfig); //##添加到静态集合 lock (((ICollection)ConfigNodeDic).SyncRoot) ConfigNodeDic[nodeID] = node; } catch (Exception ex) { Logger.Log4Net.Error("LoadConfig_ResponseChain 过程发生异常。", ex); continue; } } }
/* 请保留此测试方法。 */ /// <summary> /// 代替界面,进行配置(TestOnly) /// </summary> internal static void TestOnly_DoConfiguration() { Dictionary <string, ResponseNodeConfig> ConfigNodeDic = new Dictionary <string, ResponseNodeConfig>(); //##RootNode { ResponseNodeConfig rootnode = new ResponseNodeConfig(ConstString.ROOT_NODE_ID); TextMenuHandlerConfig menusHandler = new TextMenuHandlerConfig(); menusHandler.Menus.Add(new DictionaryEntry("1", "1")); menusHandler.Menus.Add(new DictionaryEntry("2", "2")); menusHandler.Menus.Add(new DictionaryEntry("3", "3")); menusHandler.Menus.Add(new DictionaryEntry("9", "9")); ResponseTextMessageConfig readyMessage = new ResponseTextMessageConfig() { Context = @"請輸入以下指令(數位): 1 查詢即時黄金產品資訊; 2 查询/設置 交易帳戶; 3 參與【日日有禮送】的抽獎活動; 9 查詢 集團公開信息。 *溫馨提示您: 在任意時刻輸入數位0,可以隨時返回主功能表。" }; menusHandler.ReadyMessageConfig = readyMessage; rootnode.DealingHandlerConfig = menusHandler; ConfigNodeDic[ConstString.ROOT_NODE_ID] = rootnode; } //##1 { ResponseNodeConfig node_1 = new ResponseNodeConfig("1"); node_1.DealingHandlerConfig = new CustomHandlerConfig() { HandlerTypeName = "EG.WeChat.Web.Service.ResponseChain.Handlers.CustomHandlers.CustomHandler_QueryEGProductPrice" }; node_1.DoneHandlerConfig = new DefaultDoneHandlerConfig() { NodeId = "1", TipText = "查詢其他產品" }; ConfigNodeDic["1"] = node_1; } //##2 { ResponseNodeConfig node_2 = new ResponseNodeConfig("2"); TextMenuHandlerConfig menusHandler = new TextMenuHandlerConfig(); menusHandler.Menus.Add(new DictionaryEntry("1", "2.1")); menusHandler.Menus.Add(new DictionaryEntry("2", "2.2")); menusHandler.Menus.Add(new DictionaryEntry("3", "2.3")); ResponseTextMessageConfig readyMessage = new ResponseTextMessageConfig() { Context = @"請輸入以下指令(數位): 1 查詢 交易帳戶昵称; 2 設置 交易帳戶昵称; 3 查詢 交易帳戶的歷史操作記錄。" }; menusHandler.ReadyMessageConfig = readyMessage; node_2.DealingHandlerConfig = menusHandler; ConfigNodeDic["2"] = node_2; } //##3 { ResponseNodeConfig node_3 = new ResponseNodeConfig("3"); node_3.DealingHandlerConfig = new DefaultDealingHandlerConfig() { DataType = DataTypes.Text, RawData = @"~\(≧▽≦)/~ 恭喜!您獲得1張[消費代金券$1]。" + "\r\n請及時查收您的交易帳戶。" }; ConfigNodeDic["3"] = node_3; } //##2.2 { ResponseNodeConfig node_2_2 = new ResponseNodeConfig("2.2"); node_2_2.DealingHandlerConfig = new CustomHandlerConfig() { HandlerTypeName = "EG.WeChat.Web.Service.ResponseChain.Handlers.CustomHandlers.CustomHandler_UpdateAccountNickname" }; node_2_2.DoneHandlerConfig = new DefaultDoneHandlerConfig() { NodeId = "2", TipText = "返回上一級菜單" }; ConfigNodeDic["2.2"] = node_2_2; } //##2.3 { ResponseNodeConfig node_2_3 = new ResponseNodeConfig("2.3"); node_2_3.DealingHandlerConfig = new CustomHandlerConfig() { HandlerTypeName = "EG.WeChat.Web.Service.ResponseChain.Handlers.CustomHandlers.CustomHandler_QueryAccountActions" }; node_2_3.DoneHandlerConfig = new DefaultDoneHandlerConfig() { NodeId = "2", TipText = "返回上一級菜單" }; ConfigNodeDic["2.3"] = node_2_3; } //##9 { ResponseNodeConfig node_9 = new ResponseNodeConfig("9"); TextMenuHandlerConfig menusHandler = new TextMenuHandlerConfig(); menusHandler.Menus.Add(new DictionaryEntry("1", "9.1")); menusHandler.Menus.Add(new DictionaryEntry("2", "9.2")); menusHandler.Menus.Add(new DictionaryEntry("3", "9.3")); ResponseTextMessageConfig readyMessage = new ResponseTextMessageConfig() { Context = @"請輸入以下指令(數位): 1 查詢 集團網址; 2 查詢 集團總部地址; 3 查詢 集團總部聯繫電話。" }; menusHandler.ReadyMessageConfig = readyMessage; node_9.DealingHandlerConfig = menusHandler; ConfigNodeDic["9"] = node_9; } //##9.1 { ResponseNodeConfig node_9_1 = new ResponseNodeConfig("9.1"); node_9_1.DealingHandlerConfig = new DefaultDealingHandlerConfig() { DataType = DataTypes.Text, RawData = @"www.emperorgroup.com" }; ConfigNodeDic["9.1"] = node_9_1; } //##9.2 { ResponseNodeConfig node_9_2 = new ResponseNodeConfig("9.2"); node_9_2.DealingHandlerConfig = new DefaultDealingHandlerConfig() { DataType = DataTypes.Text, RawData = @"香港灣仔軒尼詩道 288 號" }; ConfigNodeDic["9.2"] = node_9_2; } //##9.3 { ResponseNodeConfig node_9_3 = new ResponseNodeConfig("9.3"); node_9_3.DealingHandlerConfig = new DefaultDealingHandlerConfig() { DataType = DataTypes.Text, RawData = @"852-28356688" }; ConfigNodeDic["9.3"] = node_9_3; } SaveConfig_ResponseChain(ConfigNodeDic.Values.ToArray()); }