黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 备份业务的逻辑
    /// </summary>
    public class HdlBackupLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 备份业务的逻辑
        /// </summary>
        private static HdlBackupLogic m_Current = null;
        /// <summary>
        /// 备份业务的逻辑
        /// </summary>
        public static HdlBackupLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlBackupLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 获取备份名字列表___________________
 
        /// <summary>
        /// 从云端获取备份数据的名字列表
        /// </summary>
        /// <param name="ZigbeeBackupType">0:App手动备份 1: App 自动备份 2:网关手动备份 3:网关自动备份</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,获取网关备份需要</param>
        /// <param name="getOptionBackup">是否获取功能备份</param>
        /// <returns></returns>
        public List<BackupListNameInfo> GetBackupListNameFromDB(int ZigbeeBackupType = 0, string ZigbeeUniqueId = null, bool getOptionBackup = false)
        {
            //备份名字很少的内存,可以直接一次性全部取完
            var pra = new GetBackupListPra();
            pra.BackupType = ZigbeeBackupType;
            pra.GatewayUniqueId = ZigbeeUniqueId;
            pra.PageSetting.Page = 1;
            pra.PageSetting.PageSize = 99999;
            //获取控制主人账号的Token
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            //访问接口
            string result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupPagger", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
 
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult>(result);
            var list = new List<BackupListNameInfo>();
            foreach (var data2 in data.PageData)
            {
                if (ZigbeeUniqueId != null && data2.GatewayUniqueId != ZigbeeUniqueId)
                {
                    continue;
                }
                if (getOptionBackup == false && data2.BackupName == DirNameResourse.OptionBackupName)
                {
                    //不获取功能备份
                    continue;
                }
                list.Add(data2);
            }
            return list;
        }
 
        /// <summary>
        /// <para>从云端获取备份的文件,然后存入本地指定的临时文件夹</para>
        /// <para>返回文件夹名字(里面存放着全部的文件),返回null时,代表失败</para>
        /// </summary>
        /// <param name="BackupClassId">BackupClassId</param>
        /// <param name="IsGatewayAutoBackup">当前获取是否为网关自动备份</param>
        /// <param name="ZigbeeUniqueId">ZigbeeUniqueId 网关的唯一Id(当IsGatewayBackup=true时,此参数必须提供</param>
        /// <returns></returns>
        public string GetBackFileFromDBAndSetToLocation(string BackupClassId, bool IsGatewayAutoBackup = false, string ZigbeeUniqueId = null)
        {
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.AppCanSignout = false;
 
            //首先先创建一个临时文件夹,存在文件则清空
            string newDir = System.IO.Path.Combine(DirNameResourse.LocalMemoryDirectory, DirNameResourse.DownLoadBackupTempDirectory);
            Global.CreateEmptyDirectory(newDir, true);
 
            //获取这个备份下面有多少个文件
            List<string> listFile = GetBackFileIDFromDB(BackupClassId, IsGatewayAutoBackup, ZigbeeUniqueId);
            if (listFile == null)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                //允许按系统的返回键
                Shared.Common.CommonPage.BackKeyCanClick = true;
                UserCenterResourse.AccountOption.AppCanSignout = true;
                return null;
            }
 
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
            pra.IsGatewayAutoBackup = IsGatewayAutoBackup;
            pra.GatewayUniqueId = ZigbeeUniqueId;
            //获取控制主人账号的Token
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            //一个个的下载文件
            int listFileCount = listFile.Count;
            for (int i = 0; i < listFile.Count; i++)
            {
                string fileName = listFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return null;
                }
 
                //★设置需要获取的文件名字★
                pra.BackupFileName = fileName;
                var result = UserCenterLogic.GetByteResponseDataByRequestHttps("App/DownloadSomeDataBackup", true, pra, null, true);
                if (result == null)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return null;
                }
                //将输入写入本地的临时文件夹
                Global.WriteFileToDirectoryByBytes(newDir, fileName, result);
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
 
            return newDir;
        }
 
        /// <summary>
        /// 从云端获取全部的备份文件的名字
        /// </summary>
        /// <param name="BackupClassId">BackupClassId</param>
        /// <param name="IsGatewayAutoBackup">当前获取是否为网关自动备份</param>
        /// <param name="ZigbeeUniqueId">ZigbeeUniqueId 网关的唯一Id(当IsGatewayBackup=true时,此参数必须提供</param>
        /// <returns></returns>
        private List<string> GetBackFileIDFromDB(string BackupClassId, bool IsGatewayAutoBackup, string ZigbeeUniqueId)
        {
            var pra = new LoadBackInfoPra();
            pra.BackupClassId = BackupClassId;
            pra.PageSetting.PageSize = 999999;
            pra.PageSetting.Page = 1;
            pra.IsGatewayAutoBackup = IsGatewayAutoBackup;
            pra.GatewayUniqueId = ZigbeeUniqueId;
            //获取控制主人账号的Token
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            //首先先获取云端有什么文件
            string result = UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomeDataBackupUploadListPagger", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
            var fileListData = Newtonsoft.Json.JsonConvert.DeserializeObject<LoadBackInfoResult>(result);
 
            //获取文件名字
            List<string> listFileName = new List<string>();
            foreach (LoadBackupFileNameInfo file in fileListData.PageData)
            {
                listFileName.Add(file.BackupFileName);
            }
            return listFileName;
        }
 
        #endregion
 
        #region ■ 创建备份___________________________
 
        /// <summary>
        /// 创建一个备份名字(成功时返回备份的主键ID,失败时返回null)
        /// </summary>
        /// <param name="backupName">备份名字</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="ZigbeeUniqueId">网关的唯一Id,当ManualBackupType=2时,此值不能为空!</param>
        /// <returns></returns>
        public string CreatNewBackupNameToDB(string backupName, int ManualBackupType = 0, string ZigbeeUniqueId = null)
        {
            var pra = new AddBackUpNamePra();
            pra.BackupName = backupName;
            pra.ManualBackupType = ManualBackupType;
            pra.GatewayUniqueId = ZigbeeUniqueId;
            //获取控制主人账号的Token
            pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            var result = UserCenterLogic.GetResponseDataByRequestHttps("App/AddHomeAppGatewayName", true, pra);
            if (string.IsNullOrEmpty(result) == true)
            {
                return null;
            }
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBackupNameResult>(result);
 
            return data.BackupClassId;
        }
 
        #endregion
 
        #region ■ 上传备份___________________________
 
        /// <summary>
        /// 上传本地所有文件到云端(函数内部有进度条)
        /// </summary>
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <param name="upPath">指定上传的是哪个文件夹的文件(全路径),不指定时,上传的是本地路径下的文件</param>
        /// <param name="showBar">是否设置显示进度条</param>
        /// <returns></returns>
        public bool UpLoadBackupFileToDB(string BackupClassId, int ManualBackupType = 0, string upPath = "", bool showBar = true)
        {
            List<string> listAllFile = null;
            //文件夹的全路径
            string fullDir = string.Empty;
            if (upPath == string.Empty)
            {
                //获取本地文件
                listAllFile = Global.FileListByHomeId();
                fullDir = UserCenterLogic.CombinePath();
            }
            else
            {
                listAllFile = HdlAutoBackupLogic.GetFileFromDirectory(upPath);
                fullDir = upPath;
            }
            if (listAllFile.Count == 0)
            {
                return true;
            }
 
            //普通文件,可以一次性上传多个的
            List<string> listNormalFile = new List<string>();
            //图片文件,这个东西很大,需要独立出来
            List<string> listPicFile = new List<string>();
 
            foreach (string fileName in listAllFile)
            {
                if (fileName.EndsWith(".png") == true)
                {
                    //图片文件
                    listPicFile.Add(fileName);
                    continue;
                }
                listNormalFile.Add(fileName);
            }
 
            //开启进度条
            int listFileCount = listAllFile.Count;
            if (showBar == true)
            {
                //开启进度条 正在上传备份文件
                ProgressFormBar.Current.Start();
                ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
            }
 
            //不允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = false;
            UserCenterResourse.AccountOption.AppCanSignout = false;
 
            //你说为什么这里不分一个函数出来(理论上是分函数出来的)
            //是因为如果分函数的话,会出现双重异步,导致主线程的进度条的百分比永远定死0%
            for (int i = 0; i < listNormalFile.Count; i++)
            {
                string file = listNormalFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //判断是否是应该上传的文件
                if (IsNotUpLoadFile(file) == true && upPath == string.Empty)
                {
                    continue;
                }
                //整理数据
                var datainfo = new FileInfoData();
                datainfo.BackupFileName = file;
                string fullFileName = System.IO.Path.Combine(fullDir, file);
                datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile(fullFileName);
 
                var list = new List<FileInfoData>();
                list.Add(datainfo);
 
                //执行上传
                bool falge = DoUpLoadInfoToDB(BackupClassId, list, ManualBackupType);
                if (falge == false)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
 
            for (int i = 0; i < listPicFile.Count; i++)
            {
                string file = listPicFile[i];
                //账号已经退出
                if (HdlCheckLogic.Current.IsAccountLoginOut() == true)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
 
                var listPic = new List<string>();
                listPic.Add(file);
 
                //上传本地图片文件到云端
                bool result = UpLoadBigBackupFileToDB(fullDir, listPic, BackupClassId, ManualBackupType);
                if (result == false)
                {
                    //关闭进度条
                    ProgressFormBar.Current.Close();
                    //允许按系统的返回键
                    Shared.Common.CommonPage.BackKeyCanClick = true;
                    UserCenterResourse.AccountOption.AppCanSignout = true;
                    return false;
                }
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1 + listNormalFile.Count, listFileCount);
            }
 
            //进度条关闭
            ProgressFormBar.Current.Close();
            //允许按系统的返回键
            Shared.Common.CommonPage.BackKeyCanClick = true;
            UserCenterResourse.AccountOption.AppCanSignout = true;
 
            return true;
        }
 
        /// <summary>
        /// 执行上传到云端
        /// </summary>
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="listData">上传的数据</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <returns></returns>
        private bool DoUpLoadInfoToDB(string BackupClassId, List<FileInfoData> listData, int ManualBackupType = 0)
        {
            var upData = new UpLoadDataPra();
            upData.ManualBackupType = ManualBackupType;
            upData.BackupClassId = BackupClassId;
            upData.UploadSubFileLists = listData;
            //获取控制主人账号的Token
            upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/UploadHomeAppGatewaySubFiles", true, upData, null, true);
            if (falge == false)
            {
                return false;
            }
            return true;
        }
 
        /// <summary>
        /// 上传本地图片文件到云端
        /// </summary>
        /// <param name="fullDir">文件夹的全路径</param>
        /// <param name="listPicFile">图片文件列表</param>
        /// <param name="BackupClassId">备份主键ID</param>
        /// <param name="ManualBackupType">手动备份类型(0:App手动备份,2:网关手动备份)</param>
        /// <returns></returns>
        private bool UpLoadBigBackupFileToDB(string fullDir, List<string> listPicFile, string BackupClassId, int ManualBackupType = 0)
        {
            foreach (string fileName in listPicFile)
            {
                string fullName = System.IO.Path.Combine(fullDir, fileName);
 
                NameValueCollection nvc = new NameValueCollection();
                nvc.Add("RequestVersion", Common.CommonPage.RequestVersion);
                nvc.Add("LoginAccessToken", UserCenterLogic.GetConnectMainToken());
                nvc.Add("HomeId", Shared.Common.Config.Instance.Home.Id);
                nvc.Add("BackupClassId", BackupClassId);
                nvc.Add("ManualBackupType", ManualBackupType.ToString());
                nvc.Add("IsOtherAccountCtrl", Common.Config.Instance.Home.IsOthreShare.ToString());
 
                string base64 = GetBase64Text(fullName);
                nvc.Add("UploadSubFileLists[0].BackupFileName", fileName);
                nvc.Add("UploadSubFileLists[0].BackupFileContent", base64);
 
                bool result = UpLoadBigBackupFileToDB("App/UploadHomeAppGatewaySubFiles", fullName, nvc);
                if (result == false)
                {
                    return false;
                }
            }
            return true;
        }
 
        /// <summary>
        /// 上传大文件
        /// </summary>
        /// <param name="RequestName">请求接口</param>
        /// <param name="fullFileName">文件名字(含路径)</param>
        /// <param name="nvc"></param>
        /// <returns></returns>
        private bool UpLoadBigBackupFileToDB(string RequestName, string fullFileName, NameValueCollection nvc)
        {
            string paramName = "file";
            string contentType = "application/octet-stream";
            string requestUrl = string.Empty;
            //启用管理员权限
            if (Config.Instance.Home.IsOthreShare == true)
            {
                requestUrl = $"{Config.Instance.AdminRequestBaseUrl}/{RequestName}";
            }
            else
            {
                requestUrl = $"{CommonPage.RequestHttpsHost}/{RequestName}";
            }
 
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
 
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(requestUrl);
            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            wr.KeepAlive = true;
            wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
            Stream rs = wr.GetRequestStream();
 
            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
            foreach (string key in nvc.Keys)
            {
                rs.Write(boundarybytes, 0, boundarybytes.Length);
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                rs.Write(formitembytes, 0, formitembytes.Length);
            }
            rs.Write(boundarybytes, 0, boundarybytes.Length);
 
            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string header = string.Format(headerTemplate, paramName, fullFileName, contentType);
            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);
 
            FileStream fileStream = new FileStream(fullFileName, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);
            }
            fileStream.Close();
 
            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);
            rs.Close();
 
            WebResponse wresp = null;
            try
            {
                wresp = wr.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
 
                string result = reader2.ReadToEnd();
                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResponsePack>(result);
 
                return UserCenterLogic.CheckNotEorrorMsg(data, requestUrl);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
                wr = null;
            }
        }
 
        #endregion
 
        #region ■ 上传Log备份________________________
 
        /// <summary>
        /// 上传Log备份(隐匿功能)
        /// </summary>
        /// <returns></returns>
        public bool UpLoadLogBackup()
        {
            string upPath = UserCenterLogic.CombinePath(DirNameResourse.LocalMemoryDirectory, DirNameResourse.LogDirectory);
            if (HdlAutoBackupLogic.GetFileFromDirectory(upPath).Count == 0)
            {
                //没有Log文件
                return true;
            }
 
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg("正在上传Log文件");
 
            //从云端获取数据
            var pageData = HdlBackupLogic.Current.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                ProgressFormBar.Current.Close();
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            if (backId == string.Empty)
            {
                //创建新的备份
                backId = HdlBackupLogic.Current.CreatNewBackupNameToDB(DirNameResourse.OptionBackupName);
                if (backId == null)
                {
                    ProgressFormBar.Current.Close();
                    return false;
                }
            }
            //上传Log文件
            bool result = this.UpLoadBackupFileToDB(backId, 0, upPath, false);
            if (result == true)
            {
                try
                {
                    var listAllFile = HdlAutoBackupLogic.GetFileFromDirectory(upPath);
                    if (listAllFile.Count > 10)
                    {
                        listAllFile.Sort();
                        while (listAllFile.Count >= 10)
                        {
                            System.IO.File.Delete(listAllFile[0]);
                            listAllFile.RemoveAt(0);
                        }
                    }
                }
                catch { }
            }
            ProgressFormBar.Current.Close();
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, "Log上传成功");
                contr.Show();
            });
 
            return result;
        }
 
        #endregion
 
        #region ■ 读取隐匿功能配置___________________
 
        /// <summary>
        /// 读取隐匿功能配置(不要在意返回值)
        /// </summary>
        /// <returns></returns>
        public bool LoadHideOption()
        {
            //先初始化
            UserCenterResourse.HideOption = new HideOptionInfo();
            if (UserCenterResourse.UserInfo.AuthorityNo != 1 && UserCenterResourse.UserInfo.AuthorityNo != 2)
            {
                return true;
            }
 
            //无视错误
            IMessageCommon.Current.IgnoreError = true;
            //从云端获取数据
            var pageData = this.GetBackupListNameFromDB(0, null, true);
            if (pageData == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            string backId = string.Empty;
            for (int i = 0; i < pageData.Count; i++)
            {
                if (pageData[i].BackupName == DirNameResourse.OptionBackupName)
                {
                    //获取功能备份的ID
                    backId = pageData[i].Id;
                    break;
                }
            }
            if (backId == string.Empty)
            {
                //没有功能配置
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
            //获取这个备份下面有多少个文件
            List<string> listFile = GetBackFileIDFromDB(backId, false, null);
            if (listFile == null)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return false;
            }
            if (listFile.Count == 0)
            {
                //恢复
                IMessageCommon.Current.IgnoreError = false;
                return true;
            }
 
            //检测
            string checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.ShowOptionMenuFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示主页隐藏菜单(Debug用)
                UserCenterResourse.HideOption.CenterHideMenu = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DetailedLogFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //出力详细Log(Debug用)
                UserCenterResourse.HideOption.DetailedLog = 1;
            }
            checkKeys = UserCenterLogic.EncryptPassword(UserCenterResourse.FileEncryptKey, DirNameResourse.DeviceHistoryFile + UserCenterResourse.UserInfo.Account);
            if (listFile.Contains(checkKeys) == true)
            {
                //显示设备历史版本(Debug用)
                UserCenterResourse.HideOption.DeviceHistory = 1;
            }
 
            //恢复
            IMessageCommon.Current.IgnoreError = false;
            return true;
        }
 
        #endregion
 
        #region ■ 读取备份___________________________
 
        /// <summary>
        /// 读取APP备份文档(函数内部有进度条)
        /// </summary>
        /// <param name="BackupClassId"></param>
        public bool LoadAppBackupInfo(string BackupClassId)
        {
            //打开进度条 正在获取备份文件列表
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileListGetting));
 
            //从云端获取备份的文件,然后存入本地的临时文件夹
            string tempDirectory = GetBackFileFromDBAndSetToLocation(BackupClassId);
            if (tempDirectory == null)
            {
                //关闭进度条
                ProgressFormBar.Current.Close();
                return false;
            }
            //关闭进度条
            ProgressFormBar.Current.Close();
 
            //如果读取到的文件完全没有问题,则清理本地的文件
            UserCenterLogic.DeleteAllLocationFile(false);
 
            //没有错误的话,则移动到当前住宅文件夹下面
            Global.MoveDirectoryFileToHomeDirectory(tempDirectory, true);
 
            //删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
            HdlAutoBackupLogic.DeleteAllAutoBackupFile();
 
            //刷新本地缓存
            UserCenterLogic.RefreshAllMemory();
 
            return true;
        }
 
        #endregion
 
        #region ■ 删除备份___________________________
 
        /// <summary>
        /// 删除云端备份
        /// </summary>
        /// <param name="BackupClassId">备份的主键</param>
        /// <returns></returns>
        public bool DeleteDbBackupData(string BackupClassId)
        {
            var Pra = new LoadBackInfoPra();
            Pra.BackupClassId = BackupClassId;
            //获取控制主人账号的Token
            Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            bool success = UserCenterLogic.GetResultStatuByRequestHttps("App/DelHomeAppGatewayName", true, Pra);
 
            return success;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 判断是否是应该上传的文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public bool IsNotUpLoadFile(string file)
        {
            if (file == "Config.json")
            {
                //不能覆盖Config文件
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 将文件转为Base64字符串
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private string GetBase64Text(string fileName)
        {
            string path = Config.Instance.FullPath;
            string fullName = System.IO.Path.Combine(path, fileName);
 
            FileStream fs = File.OpenRead(fullName);
            byte[] buff = new byte[fs.Length];
            fs.Read(buff, 0, buff.Length);
            fs.Close();
 
            string base64 = Convert.ToBase64String(buff);
            return base64;
        }
 
        #endregion
    }
}