Example #1
0
        /// <summary>
        /// 验证是否合法
        /// </summary>
        /// <param name="handler">IHandler处理器</param>
        public static bool IsValid(Type CheckedType, DataTypes currentTypes)
        {
            //##获取特性
            InputDataTypeLimitAttribute targetAttribute = Attribute.GetCustomAttribute(CheckedType, typeof(InputDataTypeLimitAttribute)) as InputDataTypeLimitAttribute;

            //默认允许全部格式
            if (targetAttribute == null)
            {
                return(true);
            }

            //检查(currentTypes需要在targetAttribute范围内)
            return(targetAttribute.LimitedDataTypes.HasFlag(currentTypes));
        }
Example #2
0
        /// <summary>
        /// 处理数据(内部)
        /// </summary>
        /// <param name="openId">OpenID</param>
        /// <param name="inputType">输入的类型</param>
        /// <param name="rawData">Raw格式的数据</param>
        /// <returns>响应结果</returns>
        private IResponseMessage ProcessData(string openId, DataTypes inputType, object rawData)
        {
            #region 输入的数据格式

            {
                DataTypes allowTypes  = DataTypes.InputAll;     //默认为接收全部类型
                Type      handlerType = null;

                //获取目标处理器的类型
                switch (CurrentNode.NodeStatus)
                {
                case NodeStatusTypes.Dealing:
                    if (CurrentNode.DealingHandler != null)
                    {
                        handlerType = CurrentNode.DealingHandler.GetType();
                    }
                    break;

                case NodeStatusTypes.Doned:
                    if (CurrentNode.DonedHandler != null)
                    {
                        handlerType = CurrentNode.DealingHandler.GetType();
                    }
                    break;
                }

                //根据类型,获得 InputDataTypeLimit 特性
                if (handlerType != null)
                {
                    InputDataTypeLimitAttribute attribute = Attribute.GetCustomAttribute(handlerType, typeof(InputDataTypeLimitAttribute)) as InputDataTypeLimitAttribute;
                    if (attribute != null)  //有标记具体的特性,则使用具体的类型;否则默认为全部格式。
                    {
                        allowTypes = attribute.LimitedDataTypes;
                    }
                }

                //执行此次输入数据的类型检查
                if (allowTypes.HasFlag(inputType) == false)     //允许列表不包含“此次类型”
                {
                    return(new ResponseTextMessage(String.Format(@"您輸入的內容,其類型不符合要求,請重新輸入。
預期的類型有:{0}
此次的類型是:{1}
", allowTypes.GetDescription(),
                                                                 inputType.GetDescription()
                                                                 )));
                }
            }

            #endregion


            switch (CurrentNode.NodeStatus)
            {
            case NodeStatusTypes.Created:
            {        //##刚创建成功,切换状态并显示“处理的提示消息”
                CurrentNode.MarkSuccess_ThisNodeStatus();
                return(CurrentNode.DealingHandler.ReadyMessage);
            }

            case NodeStatusTypes.Dealing:
            {        //##进行“处理中”状态
                if (CurrentNode.DealingHandler == null)
                {
                    return(null);
                }

                //##检查类型,允许指定多种格式
                if (DataTypeValidator.IsValid(CurrentNode.DealingHandler.GetType(), inputType) == false)
                {
                    return(DealFailResponseResult(CurrentNode.DealingHandler, "輸入的資料類型不正確,請重新輸入。"));
                }

                //##处理
                HandlerResult ret = CurrentNode.DealingHandler.HandlerData(openId, inputType, rawData);
                if (ret == HandlerResult.Success)
                {
                    CurrentNode.MarkSuccess_ThisNodeStatus();
                    return(DealSuccessResponseResult(CurrentNode.DealingHandler, CurrentNode.DonedHandler));
                }
                else
                {
                    return(DealFailResponseResult(CurrentNode.DealingHandler));
                }
            }

            case NodeStatusTypes.Doned:
            {        //##处理完成,等待用户选择其他处理。
                if (CurrentNode.DonedHandler == null)
                {
                    //返回主菜单
                    return(new ResponseJumpNode(ConstString.ROOT_NODE_ID));
                }

                //##检查类型
                if (DataTypeValidator.IsValid(CurrentNode.DonedHandler.GetType(), inputType) == false)
                {
                    return(DealFailResponseResult(CurrentNode.DonedHandler, "輸入的資料類型不正確,請重新輸入。"));
                }

                //##处理
                HandlerResult ret = CurrentNode.DonedHandler.HandlerData(openId, inputType, rawData);
                if (ret == HandlerResult.Success)
                {
                    CurrentNode.MarkSuccess_ThisNodeStatus();
                    return(CurrentNode.DonedHandler.SuccessResponseResult);
                }
                else
                {
                    return(CurrentNode.DonedHandler.FailResponseResult);
                }
            }

            case NodeStatusTypes.Finalized:
            {
                throw new InvalidOperationException("CurrentNode.NodeStatus状态异常", new ArgumentOutOfRangeException("状态为Finalized是不合理的应用结果。"));
            }
            }

            return(null);
        }