Пример #1
0
        public WeixinMessageHandlerTest()
        {
            //设置日志。
            App.Builder.SetLog4NetLoggerByXmlConfig("./Conf/log4net.conf");

            DevConfiguration devConfig = new DevConfiguration()
            {
                AppID          = "wx5823bf96d3bd56c7",
                Token          = "QDG6eK",
                EncodingAESKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"
            };

            DebugTokenService tokenService = new DebugTokenService()
            {
                Token = "xxxxxxxxx"
            };

            //设置微信SDK相关。
            App.Builder
            .SetWeixinLogger("WeixinSDKLog")
            .SetWeixinDevConfig(devConfig)
            .SetWeixinTokenService(tokenService);

            MessageProcessConfiguration pc = new MessageProcessConfiguration();

            pc.MessageList.Add(new MessageConfiguration <RequestTextMessageProcessFake>(RequestMsgType.Text));

            App.Builder
            .SetWeixinMessageConfig(pc);
        }
Пример #2
0
        public void BuildByJsonFileTest()
        {
            App.Builder.AddSerializer(SerializeType.Json, new JsonNetJsonSerializer());
            MessageProcessConfiguration config = MessageProcessConfigurationBuilder.BuildByJsonFile("./Conf/MessageProcessConfig.json");

            Assert.NotNull(config);
            Assert.True(config.MessageList.Count > 0);
            Assert.True(config.EventMessageList.Count > 0);
            Assert.Equal <string>("Key01", config.EventMessageList[0].EventKey);
        }
Пример #3
0
        /// <summary>
        /// 初始化 <see cref="WeixinApp"/> 类的静态实例。
        /// </summary>
        static WeixinApp()
        {
            //将json 序列化实现改成 JsonNet。
            App.Builder.AddSerializer(SerializeType.Json, new JsonNetJsonSerializer());

            //设置一个空的日子记录者。
            Logger = new EmptyLogger();

            MessageProcessConfig = new MessageProcessConfiguration();
        }
        /// <summary>
        /// 根据 json 配置文件构造。
        /// </summary>
        /// <param name="configFilePath">配置文件路径.</param>
        /// <returns>一个 <see cref="DevConfiguration"/> 实例。</returns>
        public static MessageProcessConfiguration BuildByJsonFile(string configFilePath)
        {
            #region 参数校验

            if (string.IsNullOrEmpty(configFilePath))
            {
                throw new StringNullOrEmptyException(nameof(configFilePath));
            }

            #endregion

            string fileText = File.ReadAllText(configFilePath);
            MessageProcessModel processModel = fileText.ToJsonObject <MessageProcessModel>();

            MessageProcessConfiguration config = new MessageProcessConfiguration();

            if (processModel.MessageList.Any())
            {
                foreach (MessageModel model in processModel.MessageList)
                {
                    #region 类型校验

                    if (!typeof(IWeixinMessageProcess).IsAssignableFrom(model.Type))
                    {
                        throw new Exception($"类型({model.Type.FullName})必须是({typeof(IWeixinMessageProcess).FullName})的派生类。");
                    }

                    #endregion

                    config.MessageList.Add(new MessageConfiguration(model.MsgType, model.Type));
                }
            }

            if (processModel.EventMessageList.Any())
            {
                foreach (EventMessageModel model in processModel.EventMessageList)
                {
                    #region 类型校验

                    if (!typeof(IWeixinMessageProcess).IsAssignableFrom(model.Type))
                    {
                        throw new Exception($"类型({model.Type.FullName})必须是({typeof(IWeixinMessageProcess).FullName})的派生类。");
                    }

                    #endregion

                    config.EventMessageList.Add(new EventMessageConfiguration(model.EventType, model.Type)
                    {
                        EventKey = model.EventKey
                    });
                }
            }
            return(config);
        }
Пример #5
0
        /// <summary>
        /// 设置微信消息处理配置 <see cref="MessageProcessConfiguration"/>。
        /// </summary>
        /// <param name="appBuilder">应用构造。</param>
        /// <param name="config">一个 <see cref="MessageProcessConfiguration"/> 对象。</param>
        /// <returns>应用 构造 静态扩展。</returns>
        public static AppBuilder SetWeixinMessageConfig(this AppBuilder appBuilder, MessageProcessConfiguration config)
        {
            #region 参数校验

            if (config == null)
            {
                throw new ObjectNullException(nameof(config));
            }

            #endregion

            WeixinApp.MessageProcessConfig = config;
            return(appBuilder);
        }
Пример #6
0
        static TestBase()
        {
            //设置日志。
            App.Builder.SetLog4NetLoggerByXmlConfig("./Conf/log4net.conf");

            GeneralTokenService tokenService = new GeneralTokenService();

            //设置微信SDK相关。
            App.Builder
            .SetWeixinLogger("WeixinSDKLog")
            .SetWeixinDevConfigByAppSettings()
            .SetWeixinTokenService(tokenService);

            MessageProcessConfiguration pc = new MessageProcessConfiguration();

            pc.MessageList.Add(new MessageConfiguration <RequestTextMessageProcessFake>(RequestMsgType.Text));

            pc.EventMessageList.Add(new EventMessageConfiguration <RequestSubscribeEventMessageKey_001ProcessFake>(RequestMsgEventType.Subscribe));

            App.Builder
            .SetWeixinMessageConfig(pc);

            tokenService.Run();
        }
Пример #7
0
        /// <summary>
        /// 根据配置处理消息。
        /// </summary>
        /// <returns>一个字符串,表示要响应给微信服务器的内容。</returns>
        public static string ProcessByConfig(string message)
        {
            #region 参数校验

            if (string.IsNullOrEmpty(message))
            {
                throw new StringNullOrEmptyException(nameof(message));
            }

            #endregion

            MessageProcessConfiguration configInfo = WeixinApp.MessageProcessConfig;

            IRequestMessage requestMessage = Parse(message);

            Type messageProcessType = null;

            //普通消息处理。
            if (requestMessage.MsgType != RequestMsgType.Event)
            {
                if (configInfo.MessageList.Any())
                {
                    MessageConfiguration msgConfig = configInfo.MessageList.FirstOrDefault(m => m.MsgType == requestMessage.MsgType);
                    if (msgConfig != null)
                    {
                        messageProcessType = msgConfig.WeixinMessageProcessType;
                    }
                }
            }
            else //事件消息处理。
            {
                if (configInfo.EventMessageList.Any())
                {
                    RequestEventMessageBase requestEventMessage = (RequestEventMessageBase)requestMessage;

                    //没有事件KEY的事件消息。
                    EventMessageConfiguration eventMsgConfig = configInfo.EventMessageList.FirstOrDefault(e => e.EventType == requestEventMessage.Event && string.IsNullOrEmpty(e.EventKey));
                    if (eventMsgConfig != null)
                    {
                        messageProcessType = eventMsgConfig.WeixinMessageProcessType;
                    }
                    else
                    {
                        //含有事件KEY的事件消息。
                        EventMessageConfiguration hasKeyEventMsgConfig = configInfo.EventMessageList.FirstOrDefault(e => e.EventType == requestEventMessage.Event && e.EventKey == ((IRequestMessageEventKey)requestEventMessage).EventKey);
                        if (hasKeyEventMsgConfig != null)
                        {
                            messageProcessType = hasKeyEventMsgConfig.WeixinMessageProcessType;
                        }
                    }
                }
            }

            //如果类型不为空则,
            if (messageProcessType != null)
            {
                IWeixinMessageProcess wxmsgProess = (IWeixinMessageProcess)Activator.CreateInstance(messageProcessType);
                wxmsgProess.RequestMessage = requestMessage;
                wxmsgProess.Process();

                //如果指定要响应空字符串则返回空字符串给微信服务器。
                if (wxmsgProess.IsResponseEmptyString)
                {
                    return(string.Empty);
                }

                #region 响应消息基本属性赋值

                if (wxmsgProess.ResponseMessage != null)
                {
                    wxmsgProess.ResponseMessage.FromUserName = requestMessage.ToUserName;
                    wxmsgProess.ResponseMessage.ToUserName   = requestMessage.FromUserName;
                    wxmsgProess.ResponseMessage.CreateTime   = DateTime.Now.GetUnixTimestamp();
                }

                #endregion

                return(ResponseMessageProcess.Parse((ResponseMessageBase)wxmsgProess.ResponseMessage));
            }

            //如果没有任何响应则返回给微信服务器一个空字符串。
            return(string.Empty);
        }