New file |
| | |
| | | using Newtonsoft.Json;
|
| | | using Shared.Common;
|
| | | using System;
|
| | | using System.Collections.Generic;
|
| | | using System.Text;
|
| | | using System.Threading.Tasks;
|
| | |
|
| | | namespace Shared.Phone.UserCenter
|
| | | {
|
| | | /// <summary>
|
| | | /// 自动备份的逻辑
|
| | | /// </summary>
|
| | | public class HdlAutoBackupLogic
|
| | | {
|
| | | #region ■ 变量声明___________________________
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 上传备份___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 执行上传自动备份数据(0:没有可上传的自动备份数据 1:成功 -1:失败)
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public static int DoUpLoadAutoBackupData()
|
| | | {
|
| | | //获取app的自动备份
|
| | | var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1);
|
| | | if (data == null)
|
| | | {
|
| | | return -1;
|
| | | }
|
| | |
|
| | | if (data.Count == 0)
|
| | | {
|
| | | //删除全部的自动备份的本地文件
|
| | | DeleteAllAutoBackupFile();
|
| | | //如果没有自动备份数据,则把本地全部东西上传
|
| | | var pathTemp = DirNameResourse.AutoBackupDirectory;
|
| | | //复制本地所有文件过去
|
| | | List<string> listAllFile = HdlFileLogic.Current.GetRootPathListFile();
|
| | | foreach (string fileName in listAllFile)
|
| | | {
|
| | | string oldFile = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
|
| | | string newFile = System.IO.Path.Combine(pathTemp, fileName);
|
| | | //复制文件
|
| | | HdlFileLogic.Current.CopyFile(oldFile, newFile);
|
| | | }
|
| | | }
|
| | |
|
| | | //编辑文件
|
| | | List<string> listEditor = GetAutoBackupEditorFile();
|
| | | //删除文件
|
| | | List<string> listDelete = GetAutoBackupDeleteFile();
|
| | | //没有数据
|
| | | if (listEditor.Count == 0 && listDelete.Count == 0)
|
| | | {
|
| | | return 0;
|
| | | }
|
| | |
|
| | | //开启进度条 正在上传备份文件
|
| | | ProgressFormBar.Current.Start();
|
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
|
| | |
|
| | | //上传文件到云端
|
| | | bool result = UpLoadBackupFileToDB(listEditor);
|
| | | if (result == false)
|
| | | {
|
| | | ProgressFormBar.Current.Close();
|
| | | return -1;
|
| | | }
|
| | |
|
| | | //删除文件
|
| | | result = DoDeleteFileFromDB(listDelete);
|
| | | if (result == false)
|
| | | {
|
| | | ProgressFormBar.Current.Close();
|
| | | return -1;
|
| | | }
|
| | |
|
| | | ProgressFormBar.Current.Close();
|
| | |
|
| | | return 1;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 上传文件到云端
|
| | | /// </summary>
|
| | | /// <param name="listFile"></param>
|
| | | /// <returns></returns>
|
| | | private static bool UpLoadBackupFileToDB(List<string> listFile)
|
| | | {
|
| | | int listFileCount = listFile.Count;
|
| | | string backUpDir = DirNameResourse.AutoBackupDirectory;
|
| | | for (int i = 0; i < listFile.Count; i++)
|
| | | {
|
| | | string file = listFile[i];
|
| | | var datainfo = new FileInfoData();
|
| | | datainfo.BackupFileName = file;
|
| | | datainfo.BackupFileContent = HdlFileLogic.Current.ReadFileByteContent(System.IO.Path.Combine(backUpDir, file));
|
| | | if (datainfo.BackupFileContent == null)
|
| | | {
|
| | | continue;
|
| | | }
|
| | | var list = new List<FileInfoData>();
|
| | | list.Add(datainfo);
|
| | |
|
| | | //执行是上传
|
| | | bool falge = DoUpLoadInfoToDB(list);
|
| | | if (falge == false)
|
| | | {
|
| | | return false;
|
| | | }
|
| | | //设置进度值
|
| | | ProgressFormBar.Current.SetValue(i + 1, listFileCount);
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 云端执行删除指定文件
|
| | | /// </summary>
|
| | | /// <param name="listData">删除的文件</param>
|
| | | /// <returns></returns>
|
| | | private static bool DoDeleteFileFromDB(List<string> listData)
|
| | | {
|
| | | if (listData.Count == 0)
|
| | | {
|
| | | return true;
|
| | | }
|
| | |
|
| | | //获取app的自动备份
|
| | | var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1);
|
| | | if (data == null || data.Count == 0)
|
| | | {
|
| | | return true;
|
| | | }
|
| | | //自动备份只有一个
|
| | | var autoBackupId = data[0].Id;
|
| | | var nowZigbeeNumHomeId = Shared.Common.Config.Instance.Home.Id;
|
| | |
|
| | | var upData = new DeleteFilePra();
|
| | | upData.BackupClassId = autoBackupId;
|
| | | upData.HomeId = nowZigbeeNumHomeId;
|
| | | upData.DeleteFileNameLists = listData;
|
| | | //获取控制主人账号的Token
|
| | | upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
|
| | |
|
| | | bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/DeleteAppBackupFile", true, upData);
|
| | | if (falge == false)
|
| | | {
|
| | | return false;
|
| | | }
|
| | |
|
| | | //删除文件
|
| | | var backPath = DirNameResourse.AutoBackupdeleteDirectory;
|
| | | foreach (var file in listData)
|
| | | {
|
| | | string fullName = System.IO.Path.Combine(backPath, file);
|
| | | HdlFileLogic.Current.DeleteFile(fullName);
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 执行上传文件到云端
|
| | | /// </summary>
|
| | | /// <param name="listData">上传的数据</param>
|
| | | /// <returns></returns>
|
| | | private static bool DoUpLoadInfoToDB(List<FileInfoData> listData)
|
| | | {
|
| | | var nowZigbeeNumHomeId = Shared.Common.Config.Instance.Home.Id;
|
| | |
|
| | | var upData = new UpLoadDataPra();
|
| | | upData.HomeId = nowZigbeeNumHomeId;
|
| | | upData.UploadSubFileLists = listData;
|
| | | //获取控制主人账号的Token
|
| | | upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
|
| | |
|
| | | bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/HomeAppAutoDataBackup", true, upData, null, true);
|
| | | if (falge == false)
|
| | | {
|
| | | return false;
|
| | | }
|
| | |
|
| | | //删除文件
|
| | | var backPath = DirNameResourse.AutoBackupDirectory;
|
| | | foreach (var file in listData)
|
| | | {
|
| | | string fullName = System.IO.Path.Combine(backPath, file.BackupFileName);
|
| | | HdlFileLogic.Current.DeleteFile(fullName);
|
| | | }
|
| | |
|
| | | return true;
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 获取文件___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 获取自动备份目录下的添加或者编辑的文件
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public static List<string> GetAutoBackupEditorFile()
|
| | | {
|
| | | return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 获取自动备份目录下的删除的文件
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public static List<string> GetAutoBackupDeleteFile()
|
| | | {
|
| | | return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupdeleteDirectory);
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 设置文件状态_______________________
|
| | |
|
| | | /// <summary>
|
| | | /// 添加或者修改文件
|
| | | /// </summary>
|
| | | /// <param name="fileName">文件的名字,不含路径</param>
|
| | | public static void AddOrEditorFile(string fileName)
|
| | | {
|
| | | fileName = System.IO.Path.GetFileName(fileName);
|
| | | //自动备份目录
|
| | | string strBackPath = DirNameResourse.AutoBackupDirectory;
|
| | | if (System.IO.Directory.Exists(strBackPath) == false)
|
| | | {
|
| | | //预创建个人中心全部的文件夹
|
| | | HdlFileLogic.Current.CreatAllUserCenterDirectory();
|
| | | }
|
| | |
|
| | | //自动删除备份目录
|
| | | string strdelBackPath = DirNameResourse.AutoBackupdeleteDirectory;
|
| | | //如果删除列表里面有这个东西的话,移除掉
|
| | | string delFile = System.IO.Path.Combine(strdelBackPath, fileName);
|
| | | HdlFileLogic.Current.DeleteFile(delFile);
|
| | |
|
| | | string soureFile = System.IO.Path.Combine(Common.Config.Instance.FullPath, fileName);
|
| | | string newFile = System.IO.Path.Combine(strBackPath, fileName);
|
| | |
|
| | | //原原本本的复制文件到指定文件夹
|
| | | HdlFileLogic.Current.CopyFile(soureFile, newFile);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 删除文件
|
| | | /// </summary>
|
| | | /// <param name="fileName">文件的名字,不含路径</param>
|
| | | public static void DeleteFile(string fileName)
|
| | | {
|
| | | fileName = System.IO.Path.GetFileName(fileName);
|
| | | //自动删除备份目录
|
| | | string strBackPath = DirNameResourse.AutoBackupdeleteDirectory;
|
| | | string newFile = System.IO.Path.Combine(strBackPath, fileName);
|
| | |
|
| | | //创建一个空文件
|
| | | var file = System.IO.File.Create(newFile);
|
| | | file.Close();
|
| | |
|
| | | //自动备份目录
|
| | | strBackPath = DirNameResourse.AutoBackupDirectory;
|
| | | //如果备份列表里面有这个东西的话,移除掉
|
| | | string delFile = System.IO.Path.Combine(strBackPath, fileName);
|
| | |
|
| | | HdlFileLogic.Current.DeleteFile(delFile);
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 同步数据___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 同步云端数据(仅限APP启动之后) -1:异常 0:已经同步过,不需要同步 1:正常同步 2:没有自动备份数据
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public static int SynchronizeDbAutoBackupData()
|
| | | {
|
| | | //判断是否能够同步数据
|
| | | string checkFile = DirNameResourse.AutoDownLoadBackupCheckFile;
|
| | | //如果本地已经拥有了这个文件,则说明不是新手机,不再自动还原
|
| | | if (System.IO.File.Exists(checkFile) == true)
|
| | | {
|
| | | //同步服务器的分享内容
|
| | | HdlShardLogic.Current.SynchronizeDbSharedContent();
|
| | | return 0;
|
| | | }
|
| | |
|
| | | //暂时不支持成员
|
| | | if (UserCenterResourse.UserInfo.AuthorityNo == 3)
|
| | | {
|
| | | //同步服务器的分享内容
|
| | | HdlShardLogic.Current.SynchronizeDbSharedContent();
|
| | | //创建一个空文件(标识已经完成同步)
|
| | | var file = System.IO.File.Create(checkFile);
|
| | | file.Close();
|
| | | return 1;
|
| | | }
|
| | |
|
| | | //获取app的自动备份
|
| | | var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1);
|
| | | if (data == null)
|
| | | {
|
| | | return -1;
|
| | | }
|
| | | if (data.Count == 0)
|
| | | {
|
| | | //同步服务器的分享内容
|
| | | HdlShardLogic.Current.SynchronizeDbSharedContent();
|
| | | //创建一个空文件(标识已经完成同步)
|
| | | var file = System.IO.File.Create(checkFile);
|
| | | file.Close();
|
| | | return 2;
|
| | | }
|
| | | //自动备份只有一个
|
| | | string backId = data[0].Id;
|
| | |
|
| | | //账号数据同步中
|
| | | ProgressFormBar.Current.Start();
|
| | | ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uAccountDataIsSynchronizing));
|
| | |
|
| | | //从云端获取备份的文件,然后存入本地指定的临时文件夹
|
| | | string tempDir = HdlBackupLogic.Current.GetBackFileFromDBAndSetToLocation(backId);
|
| | | if (tempDir == null)
|
| | | {
|
| | | //删除检测文件
|
| | | System.IO.File.Delete(checkFile);
|
| | | //关闭进度条
|
| | | ProgressFormBar.Current.Close();
|
| | | //同步失败
|
| | | return -1;
|
| | | }
|
| | | //如果读取到的文件完全没有问题,则清理本地的文件
|
| | | HdlFileLogic.Current.DeleteAllLocationFile(false);
|
| | |
|
| | | //没有错误的话,则移动到当前住宅文件夹下面
|
| | | HdlFileLogic.Current.MoveDirectoryFileToHomeDirectory(tempDir, true);
|
| | |
|
| | | //创建一个空文件(标识已经完成同步)
|
| | | var file2 = System.IO.File.Create(checkFile);
|
| | | file2.Close();
|
| | |
|
| | | //重新刷新住宅对象
|
| | | UserCenterLogic.RefreshHomeObject();
|
| | |
|
| | | //关闭进度条
|
| | | ProgressFormBar.Current.Close();
|
| | |
|
| | | return 1;
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 一般方法___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
|
| | | /// </summary>
|
| | | public static void DeleteAllAutoBackupFile()
|
| | | {
|
| | | //清空自动备份【文件夹】(编辑,追加)
|
| | | string dirPath = DirNameResourse.AutoBackupDirectory;
|
| | | HdlFileLogic.Current.DeleteDirectory(dirPath);
|
| | | HdlFileLogic.Current.CreateDirectory(dirPath, true);
|
| | |
|
| | | //清空自动备份【文件夹】(删除)
|
| | | dirPath = DirNameResourse.AutoBackupdeleteDirectory;
|
| | | HdlFileLogic.Current.DeleteDirectory(dirPath);
|
| | | HdlFileLogic.Current.CreateDirectory(dirPath, true);
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 备份提醒___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 保存备份提醒设置到本地
|
| | | /// </summary>
|
| | | /// <param name="notPrompted">不再提示</param>
|
| | | /// <param name="day"></param>
|
| | | public static void SaveBackupNotPrompted(bool notPrompted, int day = -1)
|
| | | {
|
| | | //文件全路径
|
| | | string fullName = DirNameResourse.AutoBackupNotPromptedFile;
|
| | | BackupNotPrompted info = null;
|
| | | if (System.IO.File.Exists(fullName) == true)
|
| | | {
|
| | | var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
|
| | | info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data));
|
| | | }
|
| | | if (info == null)
|
| | | {
|
| | | info = new BackupNotPrompted();
|
| | | }
|
| | |
|
| | | info.NotPrompted = notPrompted;
|
| | | if (day != -1)
|
| | | {
|
| | | info.OldDay = DateTime.Now.ToString("yyyy-MM-dd");
|
| | | info.Day = day;
|
| | | }
|
| | | //保存
|
| | | HdlFileLogic.Current.SaveFileContent(fullName, info);
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 显示自动备份的界面
|
| | | /// </summary>
|
| | | public static void ShowAutoBackupPromptedForm()
|
| | | {
|
| | | if (UserCenterResourse.UserInfo.AuthorityNo == 3)
|
| | | {
|
| | | //暂不支持成员
|
| | | return;
|
| | | }
|
| | |
|
| | | List<string> listFile1 = HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory);
|
| | | List<string> listFile2 = GetAutoBackupDeleteFile();
|
| | |
|
| | | if (listFile1.Count == 0 && listFile2.Count == 0)
|
| | | {
|
| | | return;
|
| | | }
|
| | | if (listFile1.Count == 1 && listFile1[0] == "Room_Favorite.json")
|
| | | {
|
| | | //这个东西好像APP启动的时候都会创建的样子
|
| | | return;
|
| | | }
|
| | |
|
| | | //文件全路径
|
| | | string fullName = DirNameResourse.AutoBackupNotPromptedFile;
|
| | | if (System.IO.File.Exists(fullName) == false)
|
| | | {
|
| | | HdlThreadLogic.Current.RunMain(() =>
|
| | | {
|
| | | var form = new HdlBackup.HdlAutoBackupForm();
|
| | | form.AddForm();
|
| | | });
|
| | | return;
|
| | | }
|
| | | BackupNotPrompted info = null;
|
| | | var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
|
| | | info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data));
|
| | | if (info.NotPrompted == true)
|
| | | {
|
| | | //不再提示
|
| | | return;
|
| | | }
|
| | | if (info.Day == 0)
|
| | | {
|
| | | HdlThreadLogic.Current.RunMain(() =>
|
| | | {
|
| | | var form = new HdlBackup.HdlAutoBackupForm();
|
| | | form.AddForm();
|
| | | });
|
| | | return;
|
| | | }
|
| | |
|
| | | DateTime oldTime = Convert.ToDateTime(info.OldDay);
|
| | | int intDay = (DateTime.Now - oldTime).Days;
|
| | | //时间已经超过
|
| | | if (intDay > info.Day)
|
| | | {
|
| | | HdlThreadLogic.Current.RunMain(() =>
|
| | | {
|
| | | var form = new HdlBackup.HdlAutoBackupForm();
|
| | | form.AddForm();
|
| | | });
|
| | | return;
|
| | | }
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 数据结构___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// 自动备份不需要再次提醒的结构体
|
| | | /// </summary>
|
| | | private class BackupNotPrompted
|
| | | {
|
| | | /// <summary>
|
| | | /// 不再提示
|
| | | /// </summary>
|
| | | public bool NotPrompted = false;
|
| | | /// <summary>
|
| | | /// 前一次的日期:2019-01-01(格式)
|
| | | /// </summary>
|
| | | public string OldDay = string.Empty;
|
| | | /// <summary>
|
| | | /// 相隔日期天数
|
| | | /// </summary>
|
| | | public int Day = 0;
|
| | | }
|
| | |
|
| | | #endregion
|
| | | }
|
| | | }
|