async Task FillHeaders(HttpRequestHeaders headers, Dictionary <string, object> additionalHeaders = null)
        {
            // 额外 headers
            if (additionalHeaders != null)
            {
                foreach (KeyValuePair <string, object> kv in additionalHeaders)
                {
                    headers.Add(kv.Key, kv.Value.ToString());
                }
            }
            // 签名
            long   timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            string data      = $"{timestamp}{appKey}";
            string hash      = GetMd5Hash(md5, data);
            string sign      = $"{hash},{timestamp}";

            headers.Add("X-LC-Sign", sign);
            // 当前用户 Session Token
            LCUser currentUser = await LCUser.GetCurrent();

            if (currentUser != null)
            {
                headers.Add("X-LC-Session", currentUser.SessionToken);
            }
        }
Exemple #2
0
        public async Task LoginWithAuthDataWithUnionId()
        {
            string uuid = Guid.NewGuid().ToString();
            Dictionary <string, object> authData = new Dictionary <string, object> {
                { "expires_in", 7200 },
                { "openid", uuid },
                { "access_token", uuid }
            };
            string unionId = Guid.NewGuid().ToString();

            LCUserAuthDataLoginOption option = new LCUserAuthDataLoginOption();

            option.AsMainAccount = true;
            LCUser currentUser = await LCUser.LoginWithAuthDataAndUnionId(authData, "weixin_app", unionId, option : option);

            TestContext.WriteLine(currentUser.SessionToken);
            Assert.NotNull(currentUser.SessionToken);
            string userId = currentUser.ObjectId;

            TestContext.WriteLine($"userId: {userId}");
            TestContext.WriteLine(currentUser.AuthData);

            await LCUser.Logout();

            currentUser = await LCUser.GetCurrent();

            Assert.IsNull(currentUser);

            currentUser = await LCUser.LoginWithAuthDataAndUnionId(authData, "weixin_mini_app", unionId, option : option);

            TestContext.WriteLine(currentUser.SessionToken);
            Assert.NotNull(currentUser.SessionToken);
            Assert.AreEqual(currentUser.ObjectId, userId);
            TestContext.WriteLine(currentUser.AuthData);
        }
Exemple #3
0
        public async Task UserReadAndWrite()
        {
            await LCUser.Login("hello", "world");

            LCObject account     = new LCObject("Account");
            LCUser   currentUser = await LCUser.GetCurrent();

            LCACL acl = LCACL.CreateWithOwner(currentUser);

            account.ACL        = acl;
            account["balance"] = 512;
            await account.Save();

            Assert.IsTrue(acl.GetUserReadAccess(currentUser));
            Assert.IsTrue(acl.GetUserWriteAccess(currentUser));

            LCQuery <LCObject> query  = new LCQuery <LCObject>("Account");
            LCObject           result = await query.Get(account.ObjectId);

            TestContext.WriteLine(result.ObjectId);
            Assert.NotNull(result.ObjectId);

            await LCUser.Logout();

            result = await query.Get(account.ObjectId);

            Assert.IsNull(result);
        }
Exemple #4
0
        public async Task LoginWithAuthData()
        {
            string uuid = Guid.NewGuid().ToString();
            Dictionary <string, object> authData = new Dictionary <string, object> {
                { "expires_in", 7200 },
                { "openid", uuid },
                { "access_token", uuid }
            };
            LCUser currentUser = await LCUser.LoginWithAuthData(authData, "weixin");

            TestContext.WriteLine(currentUser.SessionToken);
            Assert.NotNull(currentUser.SessionToken);
            string userId = currentUser.ObjectId;

            TestContext.WriteLine($"userId: {userId}");
            TestContext.WriteLine(currentUser.AuthData);

            await LCUser.Logout();

            currentUser = await LCUser.GetCurrent();

            Assert.IsNull(currentUser);

            currentUser = await LCUser.LoginWithAuthData(authData, "weixin");

            TestContext.WriteLine(currentUser.SessionToken);
            Assert.NotNull(currentUser.SessionToken);
            Assert.AreEqual(currentUser.ObjectId, userId);
            TestContext.WriteLine(currentUser.AuthData);
        }
Exemple #5
0
        public async Task UserReadAndWrite()
        {
            await LCUser.Login(TestPhone, TestPhone);

            account = new Account();
            LCUser currentUser = await LCUser.GetCurrent();

            account.ACL     = LCACL.CreateWithOwner(currentUser);
            account.Balance = 512;
            await account.Save();

            Assert.IsTrue(account.ACL.GetUserReadAccess(currentUser));
            Assert.IsTrue(account.ACL.GetUserWriteAccess(currentUser));

            LCQuery <LCObject> query  = new LCQuery <LCObject>("Account");
            LCObject           result = await query.Get(account.ObjectId);

            TestContext.WriteLine(result.ObjectId);
            Assert.NotNull(result.ObjectId);

            await LCUser.Logout();

            try {
                await query.Get(account.ObjectId);
            } catch (LCException e) {
                Assert.AreEqual(e.Code, 403);
            }
        }
        private async void UploadScore()
        {
            try {
                int homeBaseCount          = m_LevelManager.numberOfHomeBases;
                PlayerHomeBase[] homeBases = m_LevelManager.playerHomeBases;

                float totalRemainingHealth = 0f;
                for (int i = 0; i < homeBaseCount; i++)
                {
                    Damageable config = homeBases[i].configuration;
                    totalRemainingHealth += config.currentHealth;
                }

                LCUser user = await LCUser.GetCurrent();

                if (user != null)
                {
                    Dictionary <string, double> statistics = new Dictionary <string, double> {
                        { "GameScore", totalRemainingHealth }
                    };
                    await LCLeaderboard.UpdateStatistics(user, statistics, false);
                }
            } catch (LCException e) {
                Debug.LogError($"{e.Code} : {e.Message}");
            }
        }
Exemple #7
0
        public async Task LoginByEmail()
        {
            await LCUser.LoginByEmail("*****@*****.**", "world");

            LCUser current = await LCUser.GetCurrent();

            Assert.NotNull(current.ObjectId);
        }
Exemple #8
0
        async Task <ReadOnlyCollection <LCObject> > GetFriends()
        {
            LCUser user = await LCUser.GetCurrent();

            LCQuery <LCObject> query = new LCQuery <LCObject>("_Followee")
                                       .WhereEqualTo("user", user)
                                       .WhereEqualTo("friendStatus", true);

            return(await query.Find());
        }
Exemple #9
0
        async Task <LCFriendshipRequest> GetRequest()
        {
            LCUser user = await LCUser.GetCurrent();

            LCQuery <LCFriendshipRequest> query = new LCQuery <LCFriendshipRequest>("_FriendshipRequest")
                                                  .WhereEqualTo("friend", user)
                                                  .WhereEqualTo("status", "pending");

            return(await query.First());
        }
    public override async void Show()
    {
        base.Show();
        try {
            LCUser user = await LCUser.GetCurrent();

            nameText.text = user.GetNickname();
        } catch (LCException e) {
            LCUtils.LogException(e);
        }
    }
Exemple #11
0
        public async Task LoginBySessionToken()
        {
            await LCUser.Logout();

            string sessionToken = "luo2fpl4qij2050e7enqfz173";
            await LCUser.BecomeWithSessionToken(sessionToken);

            LCUser current = await LCUser.GetCurrent();

            Assert.NotNull(current.ObjectId);
        }
Exemple #12
0
        public async Task Login()
        {
            await LCUser.Login("hello", "world");

            LCUser current = await LCUser.GetCurrent();

            Assert.NotNull(current.ObjectId);
            Assert.IsFalse(current.EmailVerified);
            Assert.IsFalse(current.MobileVerified);
            Assert.AreEqual(current.Mobile, "15101006008");
        }
    public async void FakePlay()
    {
        try {
            LCUser user = await LCUser.GetCurrent();

            int score = Random.Range(1, 10);
            Dictionary <string, double> statistics = new Dictionary <string, double> {
                { LeaderboadName, score }
            };
            await LCLeaderboard.UpdateStatistics(user, statistics, false);

            Reload();
        } catch (LCException e) {
            Debug.Log($"{e.Code} : {e.Message}");
        }
    }
Exemple #14
0
    public async void OnOKClicked()
    {
        string nickname = nicknameInputField.text;

        if (!LCUtils.IsValidString(nickname))
        {
            LCUtils.ShowToast(this, "Please input nickname");
            return;
        }

        try {
            LCUser user = await LCUser.GetCurrent();

            user.SetNickname(nickname);
            await user.Save();

            SendMessageUpwards("BackToLCMainMenu", SendMessageOptions.RequireReceiver);
        } catch (LCException e) {
            LCUtils.LogException(e);
            LCUtils.ShowToast(this, e);
        }
    }
Exemple #15
0
        public async Task Subscribe()
        {
            // TODO 判断当前连接情况
            if (connection == null)
            {
                connection = new LCLiveQueryConnection(DeviceId)
                {
                    OnReconnected  = OnReconnected,
                    OnNotification = OnNotification
                };
                await connection.Connect();
                await Login();
            }
            Dictionary <string, object> queryData = new Dictionary <string, object> {
                { "className", Query.ClassName },
                { "where", Query.Condition.Encode() }
            };
            Dictionary <string, object> data = new Dictionary <string, object> {
                { "query", queryData },
                { "id", DeviceId },
                { "clientTimestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds() }
            };
            LCUser user = await LCUser.GetCurrent();

            if (user != null && !string.IsNullOrEmpty(user.SessionToken))
            {
                data.Add("sessionToken", user.SessionToken);
            }
            string path = "LiveQuery/subscribe";
            Dictionary <string, object> result = await LCCore.HttpClient.Post <Dictionary <string, object> >(path,
                                                                                                             data : data);

            if (result.TryGetValue("query_id", out object id))
            {
                Id = id as string;
                WeakReference <LCLiveQuery> weakRef = new WeakReference <LCLiveQuery>(this);
                liveQueries[Id] = weakRef;
            }
        }
Exemple #16
0
        public static void Initialize(string appId,
                                      string appKey,
                                      string server    = null,
                                      string masterKey = null)
        {
            LCCore.Initialize(appId, appKey, server, masterKey);

            LCCore.HttpClient.AddRuntimeHeaderTask(SessionHeaderKey, async() => {
                LCUser currentUser = await LCUser.GetCurrent();
                if (currentUser == null)
                {
                    return(null);
                }
                return(currentUser.SessionToken);
            });

            // 注册 LeanCloud 内部子类化类型
            LCObject.RegisterSubclass(LCUser.CLASS_NAME, () => new LCUser());
            LCObject.RegisterSubclass(LCRole.CLASS_NAME, () => new LCRole());
            LCObject.RegisterSubclass(LCFile.CLASS_NAME, () => new LCFile());
            LCObject.RegisterSubclass(LCStatus.CLASS_NAME, () => new LCStatus());
            LCObject.RegisterSubclass(LCFriendshipRequest.CLASS_NAME, () => new LCFriendshipRequest());
        }