黄学彪
2020-07-13 f3e65daca7978b21b5888f49b1bf35e1a6e5d4fd
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/UserCenterLogic.cs
@@ -31,13 +31,6 @@
                ShowNotNetMsg(listNotShowError);
                return false;
            }
            //检测是否已经完成账号信息初始化
            if (UserCenterResourse.UserInfo.InitUserInfoSuccess == false)
            {
                //不管结果怎么样,如果调用了这个函数,需要重头获取Token,所以必须返回失败
                ReInitUserAccoutInfo(listNotShowError);
                return false;
            }
            //获取接口的连接模式
            var connectMode = GetHttpConnectMode(checkAuthority);
            //获取从接口那里取到的比特数据
@@ -76,13 +69,6 @@
            {
                //当前无法访问网络(当需要重新发送时,跳过这个判断)
                ShowNotNetMsg(listNotShowError);
                return "Error";
            }
            //检测是否已经完成账号信息初始化
            if (UserCenterResourse.UserInfo.InitUserInfoSuccess == false)
            {
                //不管结果怎么样,如果调用了这个函数,需要重头获取Token,所以必须返回失败
                ReInitUserAccoutInfo(listNotShowError);
                return "Error";
            }
@@ -127,13 +113,6 @@
            {
                //当前无法访问网络(当需要重新发送时,跳过这个判断)
                ShowNotNetMsg(listNotShowError);
                return null;
            }
            //检测是否已经完成账号信息初始化
            if (UserCenterResourse.UserInfo.InitUserInfoSuccess == false)
            {
                //不管结果怎么样,如果调用了这个函数,需要重头获取Token,所以必须返回失败
                ReInitUserAccoutInfo(listNotShowError);
                return null;
            }
            //获取接口的连接模式
@@ -182,13 +161,6 @@
            {
                //当前无法访问网络(当需要重新发送时,跳过这个判断)
                ShowNotNetMsg(listNotShowError);
                return null;
            }
            //检测是否已经完成账号信息初始化
            if (UserCenterResourse.UserInfo.InitUserInfoSuccess == false)
            {
                //不管结果怎么样,如果调用了这个函数,需要重头获取Token,所以必须返回失败
                ReInitUserAccoutInfo(listNotShowError);
                return null;
            }
            //获取接口的连接模式
@@ -562,6 +534,12 @@
            HdlGatewayLogic.Current.SynchronizeDbGateway();
            //从本地重新加载全部的房间
            HdlRoomLogic.Current.RefreshAllRoomByLocation();
            //断开远程Mqtt连接,重新连接
            HdlThreadLogic.Current.RunThread(async () =>
            {
                HdlGatewayLogic.Current.ClearAllRealGateway();
                await ZigBee.Device.ZbGateway.DisConnectRemoteMqttClient();
            }, ShowErrorMode.NO);
        }
        /// <summary>
@@ -584,62 +562,6 @@
            Config.Instance.Home.Longitude = Longitude;
            Config.Instance.Home.Latitude = Latitude;
            Config.Instance.Home.Save(false);
        }
        #endregion
        #region ■ 删除本地文件_______________________
        /// <summary>
        /// 删除本地所有文件
        /// </summary>
        /// <param name="all">true:全部删除(用于住宅删除) false:重要的文件不删除</param>
        public static void DeleteAllLocationFile(bool all = true)
        {
            string dPath = Config.Instance.FullPath;
            if (System.IO.Directory.Exists(dPath) == false)
            {
                return;
            }
            //然后获取全部的文件
            List<string> listFile = Global.FileListByHomeId();
            foreach (string file in listFile)
            {
                if (all == false && IsNotDeleteFile(file) == true)
                {
                    //这是不能删除的文件
                    continue;
                }
                //删除文件
                Global.DeleteFilebyHomeId(file);
            }
            //如果是把文件全部删除的话,那么文件夹也一起删除掉
            if (all == true)
            {
                //删除文件夹
                System.IO.Directory.Delete(dPath, true);
            }
        }
        /// <summary>
        /// 判断是不是不应该删除的文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsNotDeleteFile(string fileName)
        {
            if (fileName == "Config.json")
            {
                //不能删除Config文件
                return true;
            }
            else if (fileName.StartsWith("House_") == true)
            {
                //不能删除住宅文件
                return true;
            }
            return false;
        }
        #endregion
@@ -781,29 +703,6 @@
        #region ■ 拼接信息___________________________
        /// <summary>
        /// 拼接路径(全路径),以住宅ID的文件夹为起点,当没有指定参数时,则返回【住宅ID的文件夹】的全路径
        /// </summary>
        /// <param name="listNames">要拼接的路径</param>
        /// <returns></returns>
        public static string CombinePath(params object[] listNames)
        {
            string rootPath = Config.Instance.FullPath;
            if (listNames == null || listNames.Length == 0)
            {
                return rootPath;
            }
            foreach (var file in listNames)
            {
                if (file == null)
                {
                    continue;
                }
                rootPath = System.IO.Path.Combine(rootPath, file.ToString());
            }
            return rootPath;
        }
        /// <summary>
        /// 拼接网关回复超时的信息
        /// </summary>
        /// <param name="errorMsg">错误信息</param>
@@ -871,7 +770,7 @@
        public static bool InitUserCenterMenmoryAndThread(bool ShowPrompted = true)
        {
            //调用这个方法,都需要重新刷新主页
            UserView.UserPage.Instance.RefreshForm = true;
            UserView.UserPage.Instance.RefreshAllForm = true;
            //添加网络状态监听
            HdlWifiLogic.Current.StartListenNetWork();
@@ -893,12 +792,6 @@
            if (Common.Config.Instance.HomeId != UserCenterResourse.AccountOption.OldHomeStringId
                || Common.Config.Instance.Account != UserCenterResourse.AccountOption.OldAccountId)
            {
                //断开远程Mqtt连接
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    await ZigBee.Device.ZbGateway.DisConnectRemoteMqttClient();
                }, ShowErrorMode.NO);
                //清空所有成员缓存
                ClearAllMemberMemory();
@@ -906,7 +799,7 @@
                var optionInfo = UserCenterResourse.AccountOption.Load();
                UserCenterResourse.AccountOption = optionInfo;
                //变更根用户图片目录路径
                UserCenterResourse.AccountOption.UserPictruePath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, DirNameResourse.UserPictrueDirectory);
                UserCenterResourse.AccountOption.UserPictruePath = DirNameResourse.UserPictrueDirectory;
                //加载住宅配置信息
                UserCenterResourse.ResidenceOption = UserCenterResourse.ResidenceOption.Load();
@@ -921,9 +814,8 @@
                    //读取隐匿配置
                    HdlBackupLogic.Current.LoadHideOption();
                }
                //预创建个人中心全部的文件夹
                CreatAllUserCenterDirectory();
                HdlFileLogic.Current.CreatAllUserCenterDirectory();
                //关闭所有接收
                HdlGatewayReceiveLogic.Current.RemoveAllEvent();
@@ -957,6 +849,13 @@
                    HdlGatewayLogic.Current.SynchronizeDbGateway();
                }
                //断开远程Mqtt连接,重新连接
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    HdlGatewayLogic.Current.ClearAllRealGateway();
                    await ZigBee.Device.ZbGateway.DisConnectRemoteMqttClient();
                }, ShowErrorMode.NO);
                //刷新APP前一次选择的网关ID(可以反复调用,需要在网关初始化完了之后才能调用)
                HdlGatewayLogic.Current.RefreshAppOldSelectGatewayId();
@@ -984,7 +883,7 @@
            //清空所有成员缓存
            ClearAllMemberMemory();
            //预创建个人中心全部的文件夹
            CreatAllUserCenterDirectory();
            HdlFileLogic.Current.CreatAllUserCenterDirectory();
            //关闭所有接收
            HdlGatewayReceiveLogic.Current.RemoveAllEvent();
            //初始化本地的网关信息
@@ -1050,7 +949,7 @@
            //序列化对象
            var requestJson = JsonConvert.SerializeObject(pra);
            //访问接口
            byte[] byteData = CommonPage.Instance.RequestHttpsZigbeeBytesResultAsync("ZigbeeUsers/GetAccountInfo", Encoding.UTF8.GetBytes(requestJson));
            byte[] byteData = CommonPage.Instance.RequestHttpsZigbeeBytesResultAsync("ZigbeeUsers/GetAccountInfo", Encoding.UTF8.GetBytes(requestJson), "POST", 4);
            if (byteData == null)
            {
                return -1;
@@ -1089,29 +988,27 @@
                userInfo.AuthorityText = Language.StringByID(R.MyInternationalizationString.uMember);
            }
            if (UserCenterResourse.UserInfo.AuthorityNo != userInfo.AuthorityNo)
            //UserInfo.AuthorityNo==0代表本地还没有生成文件,这个时候不需要处理
            if (UserCenterResourse.UserInfo.AuthorityNo != userInfo.AuthorityNo
                && UserCenterResourse.UserInfo.AuthorityNo != 0)
            {
                //如果登陆的账号的权限和上一次的不一样,则删除本地这个住宅全部的文件,从头再来
                string dirPath = CombinePath();
                string dirPath = Config.Instance.FullPath;
                if (System.IO.Directory.Exists(dirPath) == true)
                {
                    try
                    {
                        //同步数据的判断文件(以防万一删除整个文件夹失败的时候,这个文件被删的话,应该没什么大问题)
                        string SynchronizeFile = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.AutoDownLoadBackupCheckFile);
                        string SynchronizeFile = DirNameResourse.AutoDownLoadBackupCheckFile;
                        //如果本地已经拥有了这个文件,则说明不是新手机,不再自动还原
                        if (System.IO.File.Exists(SynchronizeFile) == true)
                        {
                            System.IO.File.Delete(SynchronizeFile);
                        }
                        //删除整个文件夹
                        System.IO.Directory.Delete(dirPath, true);
                        HdlFileLogic.Current.DeleteFile(SynchronizeFile);
                        HdlFileLogic.Current.DeleteDirectory(dirPath);
                    }
                    catch { }
                    //创建住宅文件夹
                    Global.CreateHomeDirectory(Config.Instance.HomeId);
                    //预创建个人中心全部的文件夹
                    CreatAllUserCenterDirectory();
                    HdlFileLogic.Current.CreatAllUserCenterDirectory();
                }
            }
            if (string.IsNullOrEmpty(userInfo.UserName) == true)
@@ -1134,6 +1031,8 @@
            //密码验证
            UserCenterResourse.AccountOption.PswAuthentication = UserCenterResourse.UserInfo.StringPwd == null ? string.Empty : UserCenterResourse.UserInfo.StringPwd;
            UserCenterResourse.UserInfo.StringPwd = null;
            //保存缓存
            UserCenterResourse.AccountOption.Save();
            //初始化管理员控制主人的连接地址(因为这个连接Token是不会改变的,所以只需要初始化一次)
            var flage = InitAdminConnectMainInfo();
@@ -1149,8 +1048,7 @@
        /// <returns></returns>
        private static UserInformation GetUserInformationFromLocation()
        {
            string fileName = CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.UserInfoFile);
            var value = LoadFileContent(fileName);
            var value = HdlFileLogic.Current.ReadFileTextContent(DirNameResourse.UserInfoFile);
            if (value == null)
            {
                return new UserInformation();
@@ -1163,25 +1061,10 @@
        /// 重新初始化登陆账号的信息(旨在对应那一瞬间,网络不好,导致误判的情况)
        /// </summary>
        /// <returns></returns>
        private static bool ReInitUserAccoutInfo(List<string> listNotShowError)
        public static bool ReInitUserAccoutInfo()
        {
            //重新初始化账号信息
            var result = InitUserAccoutInfo(false);
            //连接不了外网的时候
            if (result == -1)
            {
                if (listNotShowError != null && listNotShowError.Contains("NotCheck") == true)
                {
                    //有这个标识时,不提示错误
                    return false;
                }
                Application.RunOnMainThread(() =>
                {
                    //当前无网络连接,请确认网络
                    var alert = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uNowIsDonotNetworkAndCheckNetwork));
                    alert.Show();
                });
            }
            if (result == 1)
            {
                //同步云端的网关id,如果本地拥有云端不存在的id,则表示应该被换绑了,直接删除
@@ -1199,9 +1082,8 @@
        /// </summary>
        private static void SaveUserInformationToLocation()
        {
            string fullName = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.UserInfoFile);
            //写入内容
            SaveFileContent(fullName, UserCenterResourse.UserInfo);
            HdlFileLogic.Current.SaveFileContent(DirNameResourse.UserInfoFile, UserCenterResourse.UserInfo);
            //搞一下主人的默认头像
            string defultFile = IO.FileUtils.GetImageFilePath("Center/Admin.png");
@@ -1265,54 +1147,6 @@
        #endregion
        #region ■ 预创建个人中心全部的文件夹_________
        /// <summary>
        /// 预创建个人中心全部的文件夹
        /// </summary>
        public static void CreatAllUserCenterDirectory()
        {
            //本地缓存的根目录
            string LocalDirectory = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory);
            Global.CreateEmptyDirectory(LocalDirectory);
            //自动备份【文件夹】(编辑,追加)
            string directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.AutoBackupDirectory);
            Global.CreateEmptyDirectory(directory);
            //自动备份【文件夹】(删除)
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.AutoBackupdeleteDirectory);
            Global.CreateEmptyDirectory(directory);
            //下载备份的时候所使用的临时【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.DownLoadBackupTempDirectory);
            Global.CreateEmptyDirectory(directory);
            //保存安防记录的【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.SafeguardAlarmDirectory);
            Global.CreateEmptyDirectory(directory);
            //上传网关备份文件到云端的临时【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.GatewayBackupDirectory);
            Global.CreateEmptyDirectory(directory);
            //下载分享文件的临时【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.DownLoadShardDirectory);
            Global.CreateEmptyDirectory(directory);
            //LOG出力【文件夹】
            directory = System.IO.Path.Combine(LocalDirectory, DirNameResourse.LogDirectory);
            Global.CreateEmptyDirectory(directory);
            //用户图片目录路径【文件夹】
            if (UserCenterResourse.AccountOption.UserPictruePath != string.Empty && !System.IO.Directory.Exists(UserCenterResourse.AccountOption.UserPictruePath))
            {
                System.IO.Directory.CreateDirectory(UserCenterResourse.AccountOption.UserPictruePath);
            }
        }
        #endregion
        #region ■ 获取控制主人账号的Token____________
        /// <summary>
@@ -1339,7 +1173,7 @@
        /// <param name="result">同步结果 -1:异常   0:已经同步过,不需要同步  1:正常同步  2:没有自动备份数据</param>
        private static void ShowGuideForm(int result)
        {
            var checkFile = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, DirNameResourse.GuideFile);
            var checkFile = DirNameResourse.GuideFile;
            if (System.IO.File.Exists(checkFile) == true)
            {
                //不需要显示
@@ -1482,89 +1316,6 @@
        {
            var utcTime = Convert.ToDateTime(timeText);
            return TimeZoneInfo.ConvertTimeFromUtc(utcTime, TimeZoneInfo.Local);
        }
        #endregion
        #region ■ 文件保存和读取_____________________
        /// <summary>
        /// 文件保存(整天忘记,所以建一个函数来玩玩)
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <param name="obj">需要序列化的东西</param>
        public static void SaveFileContent(string fullName, object obj)
        {
            var data = JsonConvert.SerializeObject(obj);
            SaveTexToFile(fullName, data);
        }
        /// <summary>
        /// 文件保存(整天忘记,所以建一个函数来玩玩)
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <param name="obj">需要序列化的东西</param>
        public static void SaveTexToFile(string fullName, string textValue)
        {
            var byteData = Encoding.UTF8.GetBytes(textValue);
            //写入内容
            System.IO.FileStream fileStream = null;
            try
            {
                fileStream = new System.IO.FileStream(fullName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                fileStream.Write(byteData, 0, byteData.Length);
                fileStream.Flush();
            }
            catch { }
            finally
            {
                fileStream?.Close();
            }
        }
        /// <summary>
        /// 读取文件,不限制住宅(文件不存在返回null,整天忘记,所以建一个函数来玩玩)
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <returns></returns>
        public static string LoadFileContent(string fullName)
        {
            //读取文件
            var varByte = ReadFileContent(fullName);
            if (varByte == null)
            {
                return null;
            }
            return Encoding.UTF8.GetString(varByte);
        }
        /// <summary>
        /// 读取文件,不限制住宅,文件不存在返回null
        /// </summary>
        /// <param name="fullName">全路径</param>
        /// <returns></returns>
        public static byte[] ReadFileContent(string fullName)
        {
            if (System.IO.File.Exists(fullName) == false)
            {
                return null;
            }
            System.IO.FileStream fileStream = null;
            try
            {
                fileStream = new System.IO.FileStream(fullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] array = new byte[fileStream.Length];
                fileStream.Read(array, 0, array.Length);
                return array;
            }
            catch
            {
                return null;
            }
            finally
            {
                fileStream?.Close();
            }
        }
        #endregion