受欢迎的博客标签

1、用户关注微信公众号事件的逻辑理清

1.1 关注事件

如果用户未关注公众号,当用户扫描带参数的公众号二维码(这个微信提供有接口)时,产生关注事件;

  case EventType.SUBSCRIBE:    //订阅
                                {
                                    var objectResult = CommonHelper.ConverXmlToObject<EventSubscribeMessage>(xmlString);

                                    //EventKey有值(不为空),说明是扫描的带参数的二维码;
                                    //为空值时是扫描的不带参数的二维码

                                    UserSubscribe userSubscribe = new UserSubscribe();
                                    userSubscribe.OriginalId = objectResult.ToUserName; //公众号的ID
                                    userSubscribe.openid = objectResult.FromUserName;
                                    userSubscribe.eventType = EventType.SUBSCRIBE;
                                    userSubscribe.SceneStringId = objectResult.EventKey;//场景id,用户扫描的带参数二维码的场景值字符串类型的ID,为空时说明扫描的是非带参数二维码
                                    userSubscribe.ticket = objectResult.Ticket;//用户扫描的带参数二维码的ticket,为空时说明扫描的是非带参数二维码
                                    userSubscribe.CreatedOnUtc = DateTime.UtcNow;

1.2扫码事件

如果用户已经关注公众号,当用户扫描带参数的公众号二维码(这个微信提供有接口)时,则产生扫码事件而不是关注事件。

 case EventType.SCAN:    //如果用户已经了关注公众号,则扫描二维码时会产生该SCAN事件,不会产生SUBSCRIBE事件;如果用户未关注公众号,则扫描二维码只会产生SUBSCRIBE订阅事件;不会产生SCAN事件
                                {
                                    var objectResult = CommonHelper.ConverXmlToObject<EventScanMessage>(xmlString);

                                    UserSubscribe userSubscribe = new UserSubscribe();
                                    userSubscribe.OriginalId = objectResult.ToUserName; //公众号的ID
                                    userSubscribe.openid = objectResult.FromUserName;
                                    userSubscribe.eventType = EventType.SCAN;
                                    userSubscribe.SceneStringId = objectResult.EventKey;
                                    userSubscribe.ticket = objectResult.Ticket;
                                    userSubscribe.CreatedOnUtc = DateTime.UtcNow;

1.3取消关注事件  .