예제 #1
0
        public VipEntity GetUserIdByOpenId(LoggingSessionInfo loggingSessionInfo, string OpenId)
        {
            VipEntity vipInfo = new VipEntity();

            try
            {
                string        vipId         = string.Empty;
                string        status        = "0";
                VipBLL        server        = new VipBLL(loggingSessionInfo);
                WXUserInfoBLL wxUserInfoBLL = new WXUserInfoBLL(loggingSessionInfo);
                //var vipObjs = server.QueryByEntityAbsolute(new VipEntity
                //{
                //    WeiXinUserId = OpenId
                //}, null);
                var vipObjs = server.QueryByEntity(new VipEntity   //先从会员表里取
                {
                    WeiXinUserId = OpenId,
                    ClientID     = loggingSessionInfo.ClientID
                }, null);

                if (vipObjs == null || vipObjs.Length == 0 || vipObjs[0] == null)//找不到会员信息
                {
                    //优先从支持多号运营的表中取
                    var wxUserInfo = wxUserInfoBLL.QueryByEntity(new WXUserInfoEntity()
                    {
                        CustomerID = loggingSessionInfo.ClientID, WeiXinUserID = OpenId
                    }, null).FirstOrDefault();
                    if (wxUserInfo != null)
                    {
                        var vipEntity = server.QueryByEntity(new VipEntity()
                        {
                            ClientID = loggingSessionInfo.ClientID, UnionID = wxUserInfo.UnionID
                        }, null).FirstOrDefault();                                                                                                                            //从会员表里取
                        if (vipEntity != null)
                        {
                            vipId   = vipEntity.VIPID;
                            status  = vipEntity.Status.ToString();
                            vipInfo = vipEntity;
                        }
                        else
                        {
                            vipInfo = null;
                        }
                    }
                    else
                    {
                        //请求获取用户信息
                        //Jermyn20130911 从总部导入vip信息
                        bool bReturn  = server.GetVipInfoFromApByOpenId(OpenId, null);
                        var  vipObjs1 = server.QueryByEntityAbsolute(new VipEntity
                        {
                            WeiXinUserId = OpenId
                        }, null);
                        if (vipObjs1 == null || vipObjs1.Length == 0 || vipObjs1[0] == null)
                        {
                            vipInfo = null;
                        }
                        else
                        {
                            vipId   = vipObjs1[0].VIPID;
                            status  = vipObjs1[0].Status.ToString();
                            vipInfo = vipObjs1[0];
                        }
                    }
                }
                else  //查到会员信息了
                {
                    vipId   = vipObjs[0].VIPID;
                    status  = vipObjs[0].Status.ToString();
                    vipInfo = vipObjs[0];
                    //获取UnionID
                    if (string.IsNullOrEmpty(vipInfo.UnionID))
                    {
                        var vipService  = new VipBLL(loggingSessionInfo);
                        var vipEntity   = new VipEntity();
                        var commonBll   = new CommonBLL();
                        var application = new WApplicationInterfaceDAO(loggingSessionInfo);
                        var appEntity   = application.QueryByEntity(new WApplicationInterfaceEntity()
                        {
                            WeiXinID = vipInfo.WeiXin, CustomerId = loggingSessionInfo.ClientID
                        }, null).FirstOrDefault();
                        if (appEntity != null)
                        {
                            //获取调用微信接口的凭证(普通的获取accestoken的地方)
                            var accessToken = commonBll.GetAccessTokenByCache(appEntity.AppID, appEntity.AppSecret, loggingSessionInfo);
                            //通过openID获取用户信息
                            //  (这种情况下,因为已经有会员信息了,并且已经关注了,才能获取到会员信息)
                            var userInfo = commonBll.GetUserInfo(accessToken.access_token, vipInfo.WeiXinUserId);
                            if (!string.IsNullOrEmpty(userInfo.unionid))
                            {
                                var vipEntitys = vipService.QueryByEntity(new VipEntity {
                                    UnionID = userInfo.unionid, ClientID = loggingSessionInfo.ClientID
                                }, null);
                                if (vipEntitys != null && vipEntitys.Length > 0)//已经存在有UnionID的数据
                                {
                                    var wxUserInfo = wxUserInfoBLL.QueryByEntity(new WXUserInfoEntity()
                                    {
                                        CustomerID = loggingSessionInfo.ClientID, VipID = vipEntitys[0].VIPID, WeiXinUserID = OpenId, UnionID = userInfo.unionid
                                    }, null).FirstOrDefault();
                                    if (wxUserInfo == null)
                                    {
                                        var wxuiEntity = new WXUserInfoEntity()
                                        {
                                            WXUserID     = Guid.NewGuid(),
                                            VipID        = vipEntitys[0].VIPID,//vipInfo.VIPID,
                                            WeiXin       = vipInfo.WeiXin,
                                            WeiXinUserID = vipInfo.WeiXinUserId,
                                            UnionID      = userInfo.unionid,
                                            CustomerID   = vipInfo.ClientID,
                                            CreateBy     = "auth",
                                            LastUpdateBy = "auth"
                                        };
                                        wxUserInfoBLL.Create(wxuiEntity);
                                    }

                                    //删除冗余vip记录
                                    vipInfo.LastUpdateBy = "auth-delete";
                                    vipService.Delete(vipInfo);
                                }
                                else
                                {
                                    //更新微信用户信息
                                    vipInfo.VipName    = userInfo.nickname;
                                    vipInfo.City       = userInfo.city;
                                    vipInfo.Gender     = Convert.ToInt32(userInfo.sex);
                                    vipInfo.HeadImgUrl = userInfo.headimgurl;
                                    vipInfo.UnionID    = userInfo.unionid;
                                    server.Update(vipInfo);
                                }
                            }
                        }
                    }
                }
                return(vipInfo);
            }
            catch (Exception ex)
            {
                Loggers.Debug(new DebugLogInfo()
                {
                    Message = string.Format("GetUserIdByOpenId用户用户信息出错: {0}", ex.ToString())
                });
                //Response.Write("GetUserIdByOpenId用户用户信息出错:" + ex.ToString());
                return(vipInfo);
            }
        }