Пример #1
0
        /// <summary>
        /// 校验用户身份,并返回绑定Url
        /// </summary>
        /// <param name="appAccountId"></param>
        /// <param name="redirectUrl"></param>
        /// <param name="type"></param>
        /// <param name="cookie"></param>
        /// <returns>
        /// null:用户身份存在且状态正常
        /// 非null:返回绑定Url,直接跳转
        /// </returns>
        public string GetIdentityUrl(string appAccountId, string redirectUrl, int type, UserCookie cookie, CallbackContentState state = CallbackContentState.Bind)
        {
            ArgumentHelper.AssertIsTrue(!string.IsNullOrWhiteSpace(redirectUrl), "redirectUrl is null or empty");

            AppAccountInfo     appAccountPublic;
            AppAccountInfo     appAccountPrivate = null;
            var                tenantId          = 0;
            AppUserAccountInfo appUserAccountMaster;
            AppUserAccountInfo appUserAccount;
            string             appAccountPublicId;

            //根据类型初始化信息
            if (string.IsNullOrWhiteSpace(appAccountId))
            {
                //公共账户方式
                appAccountPublic     = ProviderGateway.AppAccountProvider.GetPublicByType(type);
                appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPublic.AppId, cookie.OpenIds[appAccountPublic.AppId]);
                appAccountPublicId   = appAccountPublic.AppAccountId;

                if (appUserAccountMaster != null && appUserAccountMaster.State == AppUserAccountState.Activated)
                {
                    cookie.AccountAppId  = appAccountPublic.AppId;
                    cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];
                    return(null);
                }
            }
            else
            {
                //私有账户方式
                appAccountPrivate = ProviderGateway.AppAccountProvider.Get(appAccountId);
                tenantId          = appAccountPrivate.TenantId;

                switch (appAccountPrivate.Type)
                {
                case 11:
                case 12:
                    appAccountPublic     = ProviderGateway.AppAccountProvider.GetPublicByType(appAccountPrivate.Type);
                    appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPublic.AppId, cookie.OpenIds[appAccountPublic.AppId]);
                    appUserAccount       = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPrivate.AppId, cookie.OpenIds[appAccountPrivate.AppId]);
                    appAccountPublicId   = appAccountPublic.AppAccountId;

                    if (appUserAccountMaster == null)
                    {
                        if (appUserAccount != null)
                        {
                            //如果与其他账户冲突
                            var appUserAccoutTemp = ProviderGateway.AppUserAccountProvider.GetByUserId(appUserAccount.TenantId, appUserAccount.UserId, appAccountPublic.AppId);
                            if (appUserAccoutTemp == null)
                            {
                                //补充主账户
                                var appUserAccountMasterNew = new AppUserAccountInfo();
                                appUserAccountMasterNew.AppId           = appAccountPublic.AppId;
                                appUserAccountMasterNew.OpenId          = cookie.OpenIds[appAccountPublic.AppId];
                                appUserAccountMasterNew.TenantId        = appUserAccount.TenantId;
                                appUserAccountMasterNew.UserId          = appUserAccount.UserId;
                                appUserAccountMasterNew.BeisenAccount   = BeisenUserProxy.GetUserEmail(appUserAccount.UserId);
                                appUserAccountMasterNew.Type            = AppUserAccountType.Email;
                                appUserAccountMasterNew.State           = AppUserAccountState.Activated;
                                appUserAccountMasterNew.MasterAccountId = 0;
                                var appUserAccountMasterNewId = ProviderGateway.AppUserAccountProvider.AddOrUpdate(appUserAccountMasterNew.TenantId, appUserAccountMasterNew);
                                ProviderGateway.AppUserAccountProvider.UpdateMasterAccountId(appUserAccount.Id, appUserAccountMasterNewId);

                                cookie.AccountAppId  = appAccountPublic.AppId;
                                cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];

                                return(null);
                            }
                        }
                    }
                    else if (appUserAccountMaster.State == AppUserAccountState.Activated)
                    {
                        if (appUserAccount == null)
                        {
                            //补充账户信息
                            var appUserAccountNew = new AppUserAccountInfo();
                            appUserAccountNew.AppId           = appAccountPrivate.AppId;
                            appUserAccountNew.OpenId          = cookie.OpenIds[appAccountPrivate.AppId];
                            appUserAccountNew.TenantId        = appUserAccountMaster.TenantId;
                            appUserAccountNew.UserId          = appUserAccountMaster.UserId;
                            appUserAccountNew.BeisenAccount   = appUserAccountMaster.BeisenAccount;
                            appUserAccountNew.Type            = appUserAccountMaster.Type;
                            appUserAccountNew.State           = appUserAccountMaster.State;
                            appUserAccountNew.MasterAccountId = appUserAccountMaster.Id;
                            ProviderGateway.AppUserAccountProvider.AddOrUpdate(appUserAccountNew.TenantId, appUserAccountNew);
                        }
                        else if (appUserAccount.MasterAccountId != appUserAccountMaster.Id)
                        {
                            //更新MasterId
                            ProviderGateway.AppUserAccountProvider.UpdateMasterAccountId(appUserAccount.Id, appUserAccountMaster.Id);
                        }
                        cookie.AccountAppId  = appAccountPublic.AppId;
                        cookie.AccountOpenId = cookie.OpenIds[appAccountPublic.AppId];
                        return(null);
                    }
                    break;

                default:
                    appUserAccountMaster = ProviderGateway.AppUserAccountProvider.GetByOpenId(appAccountPrivate.AppId, cookie.OpenIds[appAccountPrivate.AppId]);
                    appAccountPublicId   = appAccountPrivate.AppAccountId;
                    if (appUserAccountMaster != null && appUserAccountMaster.State == AppUserAccountState.Activated)
                    {
                        cookie.AccountAppId  = appAccountPrivate.AppId;
                        cookie.AccountOpenId = cookie.OpenIds[appAccountPrivate.AppId];
                        return(null);
                    }
                    break;
                }
            }
            //记录回调信息
            var batchId         = Guid.NewGuid().ToString();
            var callbackContent = new CallbackContentInfo
            {
                BatchId           = batchId,
                TenantId          = tenantId,
                AppAccountPublic  = appAccountPublicId,
                AppAccountPrivate = appAccountPrivate == null ? null : appAccountPrivate.AppAccountId,
                Content           = redirectUrl,
                State             = state
            };

            ProviderGateway.CallbackContentProvider.Add(callbackContent);

            if (state == CallbackContentState.Bind)
            {
                return(UrlHelper.AddParameter(HostConst.UserBind, "batch", batchId));
            }
            else
            {
                var parameter = redirectUrl.Split('?');
                var query     = parameter.Length > 0 ? string.Format("{0}&batch={1}", parameter[1], batchId) : string.Empty;
                return(UrlHelper.AddQuery(HostConst.RegisterBind, query));
            }
        }