黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/UserCenterLogic.cs
old mode 100755 new mode 100644
@@ -47,7 +47,7 @@
            }
            var revertObj = JsonConvert.DeserializeObject<ResponsePack>(Encoding.UTF8.GetString(byteData));
            //检测是否存在错误信息
            return CheckNotEorrorMsg(revertObj, RequestName, listNotShowError);
            return CheckNotEorrorMsg(revertObj, RequestName, listNotShowError, obj);
        }
        /// <summary>
@@ -114,7 +114,7 @@
            }
            var revertObj = JsonConvert.DeserializeObject<ResponsePack>(Encoding.UTF8.GetString(byteData));
            //检测错误
            bool notError = CheckNotEorrorMsg(revertObj, RequestName, listNotShowError);
            bool notError = CheckNotEorrorMsg(revertObj, RequestName, listNotShowError, obj);
            if (notError == false)
            {
                return null;
@@ -165,7 +165,7 @@
                    var data = JsonConvert.DeserializeObject<ResponsePack>(data2);
                    if (data != null && string.IsNullOrEmpty(data.StateCode) == false)
                    {
                        bool notError = CheckNotEorrorMsg(data, RequestName, listNotShowError);
                        bool notError = CheckNotEorrorMsg(data, RequestName, listNotShowError, obj);
                        if (notError == false)
                        {
                            return null;
@@ -256,7 +256,8 @@
        /// <param name="revertObj">从接口接收到的数据</param>
        /// <param name="RequestName">请求接口</param>
        /// <param name="listNotShowError">不需要显示错误的错误类别(接口返回的错误类别)</param>
        public static bool CheckNotEorrorMsg(ResponsePack revertObj, string RequestName, List<string> listNotShowError = null)
        /// <param name="pra">请求的参数</param>
        public static bool CheckNotEorrorMsg(ResponsePack revertObj, string RequestName, List<string> listNotShowError = null, object pra = null)
        {
            if (revertObj == null)
            {
@@ -284,14 +285,17 @@
                        //如果用户已经退出了登陆,则不处理
                        return;
                    }
                    string msg = IMessageCommon.Current.GetMsgByRequestName(RequestName, revertObj.StateCode);
                    var control = new ShowMsgControl(ShowMsgType.Tip, msg);
                    control.Show();
                    //无效登录Token
                    if (revertObj.StateCode == "NoLogin")
                    string msg = IMessageCommon.Current.GetMsgByRequestName(RequestName, revertObj.StateCode, pra);
                    if (msg != null)
                    {
                        UserCenterLogic.ReLoginAgain(Config.Instance.Account, false);
                        var control = new ShowMsgControl(ShowMsgType.Tip, msg);
                        control.Show();
                        //无效登录Token
                        if (revertObj.StateCode == "NoLogin")
                        {
                            UserCenterLogic.ReLoginAgain(Config.Instance.Account, false);
                        }
                    }
                });
@@ -305,7 +309,7 @@
        /// </summary>
        /// <param name="checkAuthority">是否检测权限</param>
        /// <returns></returns>
        private static HttpConnectMode GetHttpConnectMode(bool checkAuthority)
        public static HttpConnectMode GetHttpConnectMode(bool checkAuthority)
        {
            if (checkAuthority == false)
            {
@@ -393,10 +397,32 @@
                        {
                            //设置当前激活的画面ID
                            UserCenterResourse.NowActionFormID = UserCenterResourse.listActionFormId[index];
                            if (UserCenterResourse.DicActionForm.ContainsKey(UserCenterResourse.NowActionFormID) == true)
                            //追加条件一:关闭的界面为EditorCommonForm的时候才处理
                            if ((form is EditorCommonForm) && UserCenterResourse.DicActionForm.ContainsKey(UserCenterResourse.NowActionFormID) == true)
                            {
                                //触发界面再次激活的事件
                                UserCenterResourse.DicActionForm[UserCenterResourse.NowActionFormID]?.FormActionAgainEvent();
                                try
                                {
                                    var Myform = UserCenterResourse.DicActionForm[UserCenterResourse.NowActionFormID];
                                    if (Myform is EditorCommonForm)
                                    {
                                        //触发界面再次激活的事件
                                        int value = ((EditorCommonForm)Myform).FormActionAgainEvent();
                                        if (value == 1)
                                        {
                                            //Log出力
                                            HdlLogLogic.Current.WriteLog(1, Myform.FormID + " 被激活");
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //出现未知错误,数据丢失
                                    var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnknownErrorAndDataLost));
                                    alert.Show();
                                    //Log出力
                                    HdlLogLogic.Current.WriteLog(ex);
                                }
                            }
                        }
                        else if (actionForm != null && actionForm is UserView.UserPage)
@@ -406,10 +432,32 @@
                            {
                                //设置当前激活的画面ID
                                UserCenterResourse.NowActionFormID = UserCenterResourse.listActionFormId[index];
                                if (UserCenterResourse.DicActionForm.ContainsKey(UserCenterResourse.NowActionFormID) == true)
                                //追加条件一:关闭的界面为EditorCommonForm的时候才处理
                                if ((form is EditorCommonForm) && UserCenterResourse.DicActionForm.ContainsKey(UserCenterResourse.NowActionFormID) == true)
                                {
                                    //触发界面再次激活的事件
                                    UserCenterResourse.DicActionForm[UserCenterResourse.NowActionFormID]?.FormActionAgainEvent();
                                    try
                                    {
                                        var Myform = UserCenterResourse.DicActionForm[UserCenterResourse.NowActionFormID];
                                        if (Myform is EditorCommonForm)
                                        {
                                            //触发界面再次激活的事件
                                            int value = ((EditorCommonForm)Myform).FormActionAgainEvent();
                                            if (value == 1)
                                            {
                                                //Log出力
                                                HdlLogLogic.Current.WriteLog(1, Myform.FormID + " 被激活");
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        //出现未知错误,数据丢失
                                        var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnknownErrorAndDataLost));
                                        alert.Show();
                                        //Log出力
                                        HdlLogLogic.Current.WriteLog(ex);
                                    }
                                }
                            }
                        }
@@ -494,7 +542,7 @@
        /// <param name="all">true:全部删除(用于住宅删除) false:重要的文件不删除</param>
        public static void DeleteAllLocationFile(bool all = true)
        {
            string dPath = UserCenterResourse.LocalRootPath;
            string dPath = Config.Instance.FullPath;
            if (System.IO.Directory.Exists(dPath) == false)
            {
                return;
@@ -532,11 +580,6 @@
                //不能删除Config文件
                return true;
            }
            else if (fileName.StartsWith("DeviceUI_") == true)
            {
                //不能删除设备UI文件
                return true;
            }
            else if (fileName.StartsWith("House_") == true)
            {
                //不能删除住宅文件
@@ -556,17 +599,17 @@
        /// <param name="noticeDb">是否通知云端</param>
        public static void ReLoginAgain(string account = "", bool noticeDb = true)
        {
            UserCenterResourse.oldAccountId = string.Empty;
            UserCenterResourse.Option.OldAccountId = string.Empty;
            //关闭所有接收
            HdlDeviceAttributeLogic.Current.RemoveAllEvent();
            //清除升级列表
            FirmwareUpdateResourse.dicDeviceUpdateList.Clear();
            FirmwareUpdateResourse.dicGatewayUpdateList.Clear();
            new System.Threading.Thread(async () =>
            HdlThreadLogic.Current.RunThread(async () =>
            {
                //检测APP是否能够退出
                while (UserCenterResourse.AppCanSignout == false)
                while (UserCenterResourse.Option.AppCanSignout == false)
                {
                    await Task.Delay(500);
                }
@@ -577,19 +620,14 @@
                //清空当前住宅id
                Shared.Common.Config.Instance.HomeId = string.Empty;
                HdlGatewayLogic.Current.ClearAllRealGateway();
                try
                {
                    ZigBee.Device.ZbGateway.RemoteMqttClient?.DisconnectAsync();
                    ZigBee.Device.ZbGateway.RemoteMqttClient = null;
                }
                catch { }
                if (noticeDb == true)
                {
                    //通知云端,已经退出登陆
                    var result = await CommonPage.Instance.RequestHttpsZigbeeBytesResultAsync("ZigbeeUsers/SignOut", null, "GET");
                }
                Application.RunOnMainThread(() =>
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //关闭所有打开了的界面
                    CloseAllOpenForm();
@@ -599,8 +637,7 @@
                    Shared.Common.CommonPage.Instance.AddChidren(formLogin);
                    formLogin.Show(account);
                });
            })
            { IsBackground = true }.Start();
            });
        }
        /// <summary>
@@ -677,7 +714,7 @@
        /// <returns></returns>
        public static string CombinePath(params object[] listNames)
        {
            string rootPath = UserCenterResourse.LocalRootPath;
            string rootPath = Config.Instance.FullPath;
            if (listNames == null || listNames.Length == 0)
            {
                return rootPath;
@@ -748,22 +785,20 @@
        public async static Task<bool> InitUserCenterMenmoryAndThread()
        {
            //APP缓存加载开始
            UserCenterResourse.AppCanSignout = false;
            UserCenterResourse.Option.AppCanSignout = false;
            //强制指定不关闭进度条
            ProgressBar.SetCloseBarFlag(true);
            //只有在住宅ID不一样的时候才做这个操作
            if (Common.Config.Instance.HomeId != UserCenterResourse.oldHomeStringId
                || Common.Config.Instance.Account != UserCenterResourse.oldAccountId)
            if (Common.Config.Instance.HomeId != UserCenterResourse.Option.OldHomeStringId
                || Common.Config.Instance.Account != UserCenterResourse.Option.OldAccountId)
            {
                //变更根目录路径
                UserCenterResourse.LocalRootPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, Config.Instance.Home.Id);
                UserCenterResourse.UserPictruePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, DirNameResourse.UserPictrueDirectory);
                //加载账号配置信息
                var optionInfo = UserCenterResourse.Option.Load();
                UserCenterResourse.Option = optionInfo;
                //变更根用户图片目录路径
                UserCenterResourse.Option.UserPictruePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, DirNameResourse.UserPictrueDirectory);
                //初始化登陆账号的信息
                await InitUserAccoutInfo();
@@ -789,8 +824,8 @@
                //保存用户的登陆信息到本地
                SaveUserInformationToLocation();
                UserCenterResourse.oldHomeStringId = Common.Config.Instance.HomeId;
                UserCenterResourse.oldAccountId = Common.Config.Instance.Account;
                UserCenterResourse.Option.OldHomeStringId = Common.Config.Instance.HomeId;
                UserCenterResourse.Option.OldAccountId = Common.Config.Instance.Account;
                //同步数据(二次调用没关系)
                var result = await HdlAutoBackupLogic.SynchronizeDbAutoBackupData();
@@ -803,6 +838,9 @@
                //同步云端的网关id,如果本地拥有云端不存在的id,则表示应该被换绑了,直接删除
                HdlGatewayLogic.Current.SynchronizeDbGateway();
                //初始化住宅对象
                Common.Config.Instance.Home = House.GetHouseByHouseId(Common.Config.Instance.HomeId);
                //初始化房间(郭雪城那边不做处理,需要这里特殊执行一步)
                Room.RefreshAllRoomByLocation();
@@ -813,17 +851,17 @@
                ProgressBar.SetAppendText(string.Empty);
                //0:已经同步过,不需要同步,这个时候需要提示备份
                if (result == 0)
                {
                    //开启自动备份提示
                    //HdlAutoBackupLogic.ShowAutoBackupPromptedForm();
                }
                //if (result == 0)
                //{
                //    //开启自动备份提示
                //    HdlAutoBackupLogic.ShowAutoBackupPromptedForm();
                //}
            }
            //恢复可关闭进度条
            ProgressBar.SetCloseBarFlag(false);
            //APP缓存加载完成
            UserCenterResourse.AppCanSignout = true;
            UserCenterResourse.Option.AppCanSignout = true;
            return true;
        }
@@ -840,7 +878,7 @@
        {
            //获取本地记录的用户信息
            UserCenterResourse.UserInfo = GetUserInformationFromLocation();
            UserCenterResourse.UserInfo.UserIconFile = System.IO.Path.Combine(UserCenterResourse.UserPictruePath, "Admin.png");
            UserCenterResourse.UserInfo.UserIconFile = System.IO.Path.Combine(UserCenterResourse.Option.UserPictruePath, "Admin.png");
            //获取登录账号的信息
            var pra = new AccountInfoPra();
@@ -911,7 +949,19 @@
            }
            UserCenterResourse.UserInfo = userInfo;
            UserCenterResourse.UserInfo.UserIconFile = System.IO.Path.Combine(UserCenterResourse.UserPictruePath, "Admin.png");
            UserCenterResourse.UserInfo.UserIconFile = System.IO.Path.Combine(UserCenterResourse.Option.UserPictruePath, "Admin.png");
            if (UserCenterResourse.UserInfo.HeadImage != null)
            {
                //写入头像内容
                Shared.IO.FileUtils.WriteFileByBytes(UserCenterResourse.UserInfo.UserIconFile, UserCenterResourse.UserInfo.HeadImage);
            }
            UserCenterResourse.UserInfo.HeadImage = null;
            //手势密码
            UserCenterResourse.Option.GestureAuthentication = UserCenterResourse.UserInfo.GesturePwd == null ? string.Empty : UserCenterResourse.UserInfo.GesturePwd;
            UserCenterResourse.UserInfo.GesturePwd = null;
            //密码验证
            UserCenterResourse.Option.PswAuthentication = UserCenterResourse.UserInfo.StringPwd == null ? string.Empty : UserCenterResourse.UserInfo.StringPwd;
            UserCenterResourse.UserInfo.StringPwd = null;
            //初始化管理员控制主人的连接地址(因为这个连接Token是不会改变的,所以只需要初始化一次)
            await InitAdminConnectMainInfo();
@@ -926,12 +976,12 @@
        private static UserInformation GetUserInformationFromLocation()
        {
            string fileName = CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.UserInfoFile);
            if (System.IO.File.Exists(fileName) == false)
            var value = LoadFileContent(fileName);
            if (value == null)
            {
                return new UserInformation();
            }
            var varByte = Shared.IO.FileUtils.ReadFile(fileName);
            var info = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInformation>(System.Text.Encoding.UTF8.GetString(varByte));
            var info = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInformation>(value);
            return info;
        }
@@ -940,12 +990,9 @@
        /// </summary>
        private static void SaveUserInformationToLocation()
        {
            var data = Newtonsoft.Json.JsonConvert.SerializeObject(UserCenterResourse.UserInfo);
            var byteData = System.Text.Encoding.UTF8.GetBytes(data);
            string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.UserInfoFile);
            //写入内容
            Shared.IO.FileUtils.WriteFileByBytes(fullName, byteData);
            SaveFileContent(fullName, UserCenterResourse.UserInfo);
            //搞一下主人的默认头像
            string defultFile = IO.FileUtils.GetImageFilePath("Center/Admin.png");
@@ -953,13 +1000,12 @@
            {
                return;
            }
            string nowFile = System.IO.Path.Combine(UserCenterResourse.UserPictruePath, "Admin.png");
            if (System.IO.File.Exists(nowFile) == true)
            if (System.IO.File.Exists(UserCenterResourse.UserInfo.UserIconFile) == true)
            {
                return;
            }
            //复制过去
            System.IO.File.Copy(defultFile, nowFile);
            System.IO.File.Copy(defultFile, UserCenterResourse.UserInfo.UserIconFile);
        }
        #endregion
@@ -1114,10 +1160,14 @@
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.DownLoadShardDirectory);
            Global.CreateEmptyDirectory(directory);
            //LOG出力【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.LogDirectory);
            Global.CreateEmptyDirectory(directory);
            //用户图片目录路径【文件夹】
            if (!System.IO.Directory.Exists(UserCenterResourse.UserPictruePath))
            if (UserCenterResourse.Option.UserPictruePath != string.Empty && !System.IO.Directory.Exists(UserCenterResourse.Option.UserPictruePath))
            {
                System.IO.Directory.CreateDirectory(UserCenterResourse.UserPictruePath);
                System.IO.Directory.CreateDirectory(UserCenterResourse.Option.UserPictruePath);
            }
        }
@@ -1242,5 +1292,37 @@
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }
        #endregion
        #region ■ 文件保存和读取_____________________
        /// <summary>
        /// 文件保存(整天忘记,所以建一个函数来玩玩)
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <param name="obj">需要序列化的东西</param>
        public static void SaveFileContent(string fullName, object obj)
        {
            var data = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            var byteData = System.Text.Encoding.UTF8.GetBytes(data);
            //写入内容
            Shared.IO.FileUtils.WriteFileByBytes(fullName, byteData);
        }
        /// <summary>
        /// 读取文件(文件不存在返回null,整天忘记,所以建一个函数来玩玩)
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <returns></returns>
        public static string LoadFileContent(string fullName)
        {
            if (System.IO.File.Exists(fullName) == false)
            {
                return null;
            }
            var varByte = Shared.IO.FileUtils.ReadFile(fullName);
            return System.Text.Encoding.UTF8.GetString(varByte);
        }
        #endregion
    }
}