示例#1
0
    public override void OnEnable()
    {
        base.OnEnable();

        ShareSDK.authHandler         = OnAuthResultHandler;
        ShareSDK.shareHandler        = OnShareResultHandler;
        ShareSDK.showUserHandler     = OnGetUserInfoResultHandler;
        ShareSDK.getFriendsHandler   = OnGetFriendsResultHandler;
        ShareSDK.followFriendHandler = OnFollowFriendResultHandler;

        WeChatComponents.OnAdd().Subscribe(entity =>
        {
            var wechatComponent = entity.GetComponent <WeChatComponent>();
            var animator        = entity.GetComponent <Animator>();

            IsAuthorized.DistinctUntilChanged().Subscribe(isOn =>
            {
                animator.SetBool(IsOn, isOn);
                if (isOn)
                {
                    ShareSDK.GetUserInfo(PlatformType.WeChat);
                    wechatComponent.NickName.text = authInfo[NickName].ToString();
                    StartCoroutine(LoadImage(authInfo[HeadImgURL].ToString(), wechatComponent.UserIcon));
                }
                else
                {
                    IsGetUserInfo.Value             = false;
                    wechatComponent.NickName.text   = Empty;
                    wechatComponent.UserIcon.sprite = null;
                }
            }).AddTo(this.Disposer).AddTo(wechatComponent.Disposer);

            IsGetUserInfo.DistinctUntilChanged().Where(b => b).Subscribe(_ =>
            {
                wechatComponent.NickName.text = userInfo[NickName].ToString();
                StartCoroutine(LoadImage(userInfo[HeadImgURL].ToString(), wechatComponent.UserIcon));
            }).AddTo(this.Disposer).AddTo(wechatComponent.Disposer);

            wechatComponent.SignInButton.OnPointerClickAsObservable().Subscribe(_ =>
            {
                ShareSDK.Authorize(PlatformType.WeChat);
            }).AddTo(this.Disposer).AddTo(wechatComponent.Disposer);

            wechatComponent.SignOutButton.OnPointerClickAsObservable().Subscribe(_ =>
            {
                ShareSDK.CancelAuthorize(PlatformType.WeChat);
                IsAuthorized.Value = false;
            }).AddTo(this.Disposer).AddTo(wechatComponent.Disposer);
        }).AddTo(this.Disposer);

        WeChatWriter.OnAdd().Subscribe(writer =>
        {
            if (ShareSDK.IsAuthorized(PlatformType.WeChat) && writer.HasKey(AuthInfo))
            {
                authInfo           = writer.Get <string>(AuthInfo).hashtableFromJson();
                IsAuthorized.Value = true;
            }
            else
            {
                if (ShareSDK.IsAuthorized(PlatformType.WeChat))
                {
                    ShareSDK.CancelAuthorize(PlatformType.WeChat);
                }
                IsAuthorized.DistinctUntilChanged().TakeWhile(_ => !writer.HasKey(AuthInfo)).Where(b => b).Subscribe(_ =>
                {
                    writer.Set(AuthInfo, authInfo.toJson());
                }).AddTo(this.Disposer);
            }

            IsGetUserInfo.DistinctUntilChanged().TakeWhile(_ => !writer.HasKey(UserInfo)).Where(b => b).Subscribe(_ =>
            {
                writer.Set(UserInfo, userInfo.toJson());
            }).AddTo(this.Disposer);
        }).AddTo(this.Disposer);
    }