Browse Source

新增批量导出

Suxue 1 year ago
parent
commit
eb4d4fa3fa
7 changed files with 171 additions and 23 deletions
  1. 10 1
      Main2.xaml
  2. 14 1
      Main2.xaml.cs
  3. 18 0
      Pages/Manager.xaml
  4. 90 0
      Pages/Manager.xaml.cs
  5. 5 2
      Pages/Workspace.xaml.cs
  6. 20 17
      ViewModel/WorkspaceViewModel.cs
  7. 14 2
      WXUserReader.cs

+ 10 - 1
Main2.xaml

@@ -122,8 +122,17 @@
                 </EventTrigger>
             </Image.Triggers>
         </Image>
+        
         <Grid Width="230" Background="#2775b6" HorizontalAlignment="Left" IsHitTestVisible="True">
-            <ListView BorderThickness="0" Background="Transparent" Margin="0,0,0,85" Name="list_workspace" ItemTemplate="{DynamicResource ListViewItemContentTemplate}" SelectionChanged="list_workspace_SelectionChanged"/>
+
+            <ListView BorderThickness="0" Background="Transparent" Margin="0,0,0,85" Name="list_workspace" ItemTemplate="{DynamicResource ListViewItemContentTemplate}" SelectionChanged="list_workspace_SelectionChanged">
+                <ListView.ContextMenu>
+                    <ContextMenu>
+                        <MenuItem Header="查看" Click="MenuItem_Click"  />
+                        <MenuItem Header="管理" Click="MenuItem_Click_1" />
+                    </ContextMenu>
+                </ListView.ContextMenu>
+            </ListView>
             <Grid Name="new_workspace" Width="170" Height="40" VerticalAlignment="Bottom" Margin="30,45" IsHitTestVisible="True">
                 <Rectangle Name="new_workspace_fill" Fill="Transparent"  RadiusX="0" RadiusY="0" Stroke="White" StrokeDashArray="5" MouseDown="new_workspace_fill_MouseDown">
                     <Rectangle.Triggers>

+ 14 - 1
Main2.xaml.cs

@@ -27,8 +27,9 @@ namespace WechatBakTool
         public Main2()
         {
             InitializeComponent();
+            // 获取文件版本
             lab_version.Content += $" {Application.ResourceAssembly.GetName().Version}";
-            // list_workspace.Items.Add(new { Name = "sxcoder", Friends_Number=23, Msg_Number=102302, Decrypt="已解密" });
+            //加载工作区
             LoadWorkspace();
         }
 
@@ -40,10 +41,12 @@ namespace WechatBakTool
         public void LoadWorkspace()
         {
             userBakConfigs.Clear();
+            // 根目录worksapce读工作区
             string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "workspace");
             if (Directory.Exists(path))
             {
                 string[] files = Directory.GetFiles(path);
+                //目录内json文件为各工作区配置文件
                 foreach (string file in files)
                 {
                     string type = file.Substring(file.Length - 5, 5);
@@ -105,5 +108,15 @@ namespace WechatBakTool
                 DragMove();
             }
         }
+
+        private void MenuItem_Click(object sender, RoutedEventArgs e)
+        {
+            MainFrame.Navigate(new Uri("pack://application:,,,/Pages/Workspace.xaml?datatime=" + DateTime.Now.Ticks));
+        }
+
+        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
+        {
+            MainFrame.Navigate(new Uri("pack://application:,,,/Pages/Manager.xaml?datatime=" + DateTime.Now.Ticks));
+        }
     }
 }

+ 18 - 0
Pages/Manager.xaml

@@ -0,0 +1,18 @@
+<Page x:Class="WechatBakTool.Pages.Manager"
+      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+      xmlns:local="clr-namespace:WechatBakTool.Pages"
+      mc:Ignorable="d" 
+      d:DesignHeight="450" d:DesignWidth="720"
+      Title="Manager" Background="White">
+    <Grid>
+        <Label FontSize="20" Margin="30,15" Content="管理" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        <Label Margin="30,55" Content="批量导出聊天记录" HorizontalAlignment="Left" VerticalAlignment="Top" FontWeight="Bold" />
+        <CheckBox Name="cb_group" Margin="35,85" Content="群聊" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        <CheckBox Name="cb_user" Margin="90,85" Content="好友聊天" HorizontalAlignment="Left" VerticalAlignment="Top" />
+        <Button Name="btn_export_all" Margin="35,110" Height="26" Width="60" Content="导出" HorizontalAlignment="Left" VerticalAlignment="Top" Background="#2775b6" Foreground="White" BorderThickness="0" Click="btn_export_all_Click"></Button>
+        <Label Content="{Binding LabelStatus}" HorizontalAlignment="Left" Margin="110,110,0,0" VerticalAlignment="Top"/>
+    </Grid>
+</Page>

+ 90 - 0
Pages/Manager.xaml.cs

@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using WechatBakTool.Export;
+using WechatBakTool.Model;
+using WechatBakTool.ViewModel;
+
+namespace WechatBakTool.Pages
+{
+    /// <summary>
+    /// Manager.xaml 的交互逻辑
+    /// </summary>
+    public partial class Manager : Page
+    {
+        private WorkspaceViewModel workspaceViewModel = new WorkspaceViewModel();
+        public WXUserReader? UserReader;
+        public Manager()
+        {
+            DataContext = workspaceViewModel;
+            InitializeComponent();
+            UserBakConfig? config = Main2.CurrentUserBakConfig;
+            if (config != null)
+            {
+                UserReader = new WXUserReader(config);
+                if (!config.Decrypt)
+                {
+                    MessageBox.Show("请先解密数据库", "错误");
+                    return;
+                }
+            }
+        }
+
+        private void btn_export_all_Click(object sender, RoutedEventArgs e)
+        {
+            Task.Run(() =>
+            {
+                bool group = false, user = false;
+                Dispatcher.Invoke(() =>
+                {
+                    if (cb_group.IsChecked == null || cb_user.IsChecked == null)
+                        return;
+
+                    group = (bool)cb_group.IsChecked;
+                    user = (bool)cb_user.IsChecked;
+                });
+                if (UserReader != null)
+                {
+                    List<WXContact>? contacts = UserReader.GetWXContacts().ToList();
+                    foreach (var contact in contacts)
+                    {
+                        if (group && contact.UserName.Contains("@chatroom"))
+                        {
+                            workspaceViewModel.WXContact = contact;
+                            ExportMsg(contact);
+                        }
+                        if (user)
+                        {
+                            workspaceViewModel.WXContact = contact;
+                            ExportMsg(contact);
+                        }
+                    }
+                    MessageBox.Show("批量导出完成", "提示");
+                }
+            });
+        }
+
+        private void ExportMsg(WXContact contact)
+        {
+            workspaceViewModel.ExportCount = "";
+            string path = Path.Combine(Main2.CurrentUserBakConfig!.UserWorkspacePath, contact.UserName + ".html");
+            IExport export = new HtmlExport();
+            export.InitTemplate(contact, path);
+            export.SetMsg(UserReader!, contact, workspaceViewModel);
+            export.SetEnd();
+            export.Save(path);
+        }
+    }
+}

+ 5 - 2
Pages/Workspace.xaml.cs

@@ -27,8 +27,8 @@ namespace WechatBakTool.Pages
     /// </summary>
     public partial class Workspace : Page
     {
-        public WXUserReader? UserReader { get; set; }
-        private WorkspaceViewModel ViewModel { get; set; } = new WorkspaceViewModel();
+        public WXUserReader? UserReader;
+        private WorkspaceViewModel ViewModel = new WorkspaceViewModel();
         public Workspace()
         {
             ViewModel.ExportItems = new System.Collections.ObjectModel.ObservableCollection<ExportItem> {
@@ -37,6 +37,9 @@ namespace WechatBakTool.Pages
             };
             ViewModel.SelectExportItem = ViewModel.ExportItems[0];
             InitializeComponent();
+
+            list_users.Items.Clear();
+
             DataContext = ViewModel;
             UserBakConfig? config = Main2.CurrentUserBakConfig;
             if (config != null)

+ 20 - 17
ViewModel/WorkspaceViewModel.cs

@@ -11,18 +11,29 @@ namespace WechatBakTool.ViewModel
 {
     public partial class WorkspaceViewModel : ObservableObject
     {
+        [ObservableProperty]
+        [NotifyPropertyChangedFor(nameof(SelectContact))]
+        [NotifyPropertyChangedFor(nameof(LabelStatus))]
         private WXContact? wXContact = null;
-        public WXContact? WXContact {
-            get { return wXContact; }
-            set {  
-                wXContact = value;
-                OnPropertyChanged("WXContact");
-                OnPropertyChanged("SelectContact");
-            }
-        }
 
         [ObservableProperty]
-        public string exportCount = "";
+        [NotifyPropertyChangedFor(nameof(LabelStatus))]
+        private string exportCount = "";
+
+        public string LabelStatus
+        {
+            get
+            {
+                if (WXContact == null)
+                    return ExportCount;
+
+                string name = WXContact.NickName;
+                if(WXContact.Remark != "")
+                    name = WXContact.Remark;
+
+                return string.Format("{0}:{1}", name, ExportCount);
+            }
+        }
 
         public bool SelectContact
         {
@@ -62,13 +73,5 @@ namespace WechatBakTool.ViewModel
                 return searchString;
             }
         }
-
-        public string SearchRealString
-        {
-            get
-            {
-                return searchString;
-            }
-        }
     }
 }

+ 14 - 2
WXUserReader.cs

@@ -380,12 +380,24 @@ namespace WechatBakTool
                 if(!Directory.Exists(video_dir))
                     Directory.CreateDirectory(video_dir);
                 FileInfo fileInfo = new FileInfo(path);
+                // 目标视频路径
                 string video_file_path = Path.Combine(video_dir, fileInfo.Name);
                 // 视频的路径是相对路径,需要加上资源目录
                 path = Path.Combine(UserBakConfig.UserResPath, path);
-                if(!File.Exists(video_file_path))
+                // 原文件存在,目标不存在
+                if (!File.Exists(video_file_path) && File.Exists(path))
+                {
+                    // 复制
                     File.Copy(path, video_file_path);
-                path = video_file_path;
+                    path = video_file_path;
+                }
+                else if (File.Exists(video_file_path))
+                {
+                    path = video_file_path;
+                }
+                else
+                    return null;
+                    
             }
 
             if (path == null)