gxc
2019-11-15 05ce435c3b58e53eeab04c672affdeeab75f3036
ZigbeeApp/Shared/Phone/Device/Account/AccountLogin.cs
@@ -1,14 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using uPLibrary.Networking.M2Mqtt;
using Shared.Common;
using Shared.IO;
using Shared.Phone.UserView;
using System.Net.Http;
using Shared.Phone.Device.CommonForm;
using Com.Tencent.MM.Sdk.Openapi;
namespace Shared.Phone.Device.Account
{
@@ -268,7 +261,8 @@
                Y = Application.GetRealHeight(1737),
                Width = Application.GetMinRealAverage(115),
                Height = Application.GetMinRealAverage(115),
                UnSelectedImagePath = "Account/Wechat.png"
                UnSelectedImagePath = "Account/Wechat.png",
                Gravity=Gravity.CenterHorizontal
            };
            midFrameLayout.AddChidren(wechatBtn);
@@ -280,7 +274,7 @@
                Height = Application.GetMinRealAverage(115),
                UnSelectedImagePath = "Account/QQ.png"
            };
            midFrameLayout.AddChidren(qqBtn);
            //midFrameLayout.AddChidren(qqBtn);
            #endregion
@@ -483,16 +477,128 @@
            CommonPage.Instance.AddChidren(loginByCodePage);
            loginByCodePage.Show();
        }
        IWXAPI api;
        /// <summary>
        /// wechat登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="mouseEventArgs"></param>
        private void LoginByWechat(object sender,MouseEventArgs mouseEventArgs)
        private void LoginByWechat(object sender, MouseEventArgs mouseEventArgs)
        {
            var req = new SendAuth.Req { Scope = "snsapi_userinfo", State = "ZigbeeApp" };
            api.SendReq(req);
#if Android
            //var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP),
            //    Language.StringByID(R.MyInternationalizationString.LoginByWechat),
            //    Language.StringByID(R.MyInternationalizationString.Cancel),
            //    Language.StringByID(R.MyInternationalizationString.Confrim));
            //alert.Show();
            //alert.ResultEventHandler += (send, e) =>
            //{
                //if (e)
                //{
                    com.hdl.home.Application.WXLogin();
                    com.hdl.home.WXEntryActivity.RespAction = (authStr) =>
                    {
                        if (authStr == null)
                        {
                        }
                        else
                        {
                            new System.Threading.Thread(async () =>
                            {
                                var re = await isBindAuthAsync(authStr);
                                if (re)
                                {
                                    //直接登录
                                    //this.RemoveFromParent();
                                    //UserPage.Instance.Fresh();
                                    //启动ZigBee
                                    ZigBee.Common.Application.Init();
                                    var resultRegID = await Shared.Common.CommonPage.Instance.PushRegID();
                                    var homes = await House.GetHomeLists();
                                    //刷新个人中心的内存及线程
                                    await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
                                    Shared.Common.Room.CanInitAllRoom = true;
                                    Shared.Common.Room.InitAllRoom();
                                    Application.RunOnMainThread(() =>
                                    {
                                        this.RemoveFromParent();
                                        UserPage.Instance.Fresh();
                                    });
                                }
                                else
                                {
                                    var authRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.ResponseEntity.AuthUserRes>(authStr);
                                    Application.RunOnMainThread(() =>
                                    {
                                        var registerPage = new AccountRegister();
                                        Shared.Common.CommonPage.Instance.AddChidren(registerPage);
                                        registerPage.OpenID = authRes.openid;
                                        registerPage.Show();
                                    });
                                }
                            })
                            { IsBackground = true }.Start();
                        }
                    };
                //}
            //};
#endif
#if iOS
            GateWay.Ios.AppDelegate.WXLogin();
            GateWay.Ios.AppDelegate.RespAction = (authStr) =>
            {
                if (authStr == null)
                {
                }
                else
                {
                    new System.Threading.Thread(async () =>
                    {
                        var re = await isBindAuthAsync(authStr);
                        if (re)
                        {
                            //直接登录
                            //this.RemoveFromParent();
                            //UserPage.Instance.Fresh();
                            //启动ZigBee
                            ZigBee.Common.Application.Init();
                            var resultRegID = await Shared.Common.CommonPage.Instance.PushRegID();
                            var homes = await House.GetHomeLists();
                            //刷新个人中心的内存及线程
                            await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
                            Shared.Common.Room.CanInitAllRoom = true;
                            Shared.Common.Room.InitAllRoom();
                            Application.RunOnMainThread(() =>
                            {
                                this.RemoveFromParent();
                                UserPage.Instance.Fresh();
                            });
                        }
                        else
                        {
                            var authRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.ResponseEntity.AuthUserRes>(authStr);
                            Application.RunOnMainThread(() =>
                            {
                                var registerPage = new AccountRegister();
                                Shared.Common.CommonPage.Instance.AddChidren(registerPage);
                                registerPage.OpenID = authRes.openid;
                                registerPage.Show();
                            });
                        }
                    })
                    { IsBackground = true }.Start();
                }
            };
#endif
        }
        /// <summary>
@@ -506,6 +612,54 @@
        }
        /// <summary>
        /// 是否已绑定
        /// </summary>
        /// <param name="authStr"></param>
        /// <returns></returns>
        private async System.Threading.Tasks.Task<bool> isBindAuthAsync(string authStr)
        {
            var authRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.ResponseEntity.AuthUserRes>(authStr);
            var auth = new SendDataToServer.AuthUser()
            {
                AccessToken = authRes.access_token,
                RefreshToken = authRes.refresh_token,
                OpenID = authRes.openid
            };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(auth);
            var revertObj = await CommonPage.Instance.RequestHttpsZigbeeAsync("ZigbeeUsers/InsertOrUpdateAuthUser", System.Text.Encoding.UTF8.GetBytes(requestJson));
            if (revertObj == null)
            {
                return false;
            }
            var stateCodeStr = revertObj.StateCode.ToUpper();
            if (stateCodeStr == "SUCCESS")
            {
                var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.UserLoginRes>(revertObj.ResponseData.ToString());
                var revertData = responseDataObj;
                Config.Instance.Account = revertData.Account;
                Config.Instance.MD5PWD = revertData.MD5PWD;
                Config.Instance.Guid = revertData.Guid;
                Config.Instance.MqttKey = revertData.MqttKey;
                Config.Instance.LoginDateTime = DateTime.Now;
                Config.Instance.ConnectZigbeeMqttBrokerPwd = revertData.ConnectZigbeeMqttBrokerPwd;
                Config.Instance.ConnectZigbeeMqttClientId = revertData.ConnectZigbeeMqttClientId;
                Config.Instance.ZigbeeMqttBrokerLoadSubDomain = revertData.ZigbeeMqttBrokerLoadSubDomain;
                if (Config.Instance.AccountList.Find((obj) => obj == revertData.Account) == null)
                {
                    Config.Instance.AccountList.Add(revertData.Account);
                }
                Config.Instance.Save();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// phone/email 选择
        /// </summary>
        /// <param name="sender"></param>