john 2 жил өмнө
parent
commit
e7e1967894

+ 14 - 4
README.md

@@ -6,10 +6,8 @@ This template should help get you started developing with Tauri, React and Types
 
 - [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)
 
-
-
-
 ## 开发笔记
+
 ```
 隐藏标题栏
     "windows": [
@@ -26,4 +24,16 @@ This template should help get you started developing with Tauri, React and Types
 ```
 
 ### 设计
-1、所有的状态存放本地 sqlite
+
+1、所有的状态存放本地 sqlite
+
+#### 参考项目
+
+```
+1、https://github.com/rrkeji/rrai-desktop
+2、https://github.com/rrkeji/rrai-desktop-sdk
+```
+
+## 致谢
+
+1、ChatGpt

+ 4 - 1
src-tauri/src/main.rs

@@ -8,15 +8,18 @@ mod self_plugin;
 mod common;
 mod utils;
 
+
 use crate::menus::default::use_memu;
 use crate::menus::event::m_event;
 use crate::event_loop::{greet, file_path, file_sort};
-use crate::self_plugin::tauri_plugin_sqlite;
+use self_plugin::tauri_plugin_sqlite;
+use self_plugin::tauri_plugin_file;
 
 
 fn main() {
     tauri::Builder::default()
         .plugin(tauri_plugin_sqlite::init())
+        .plugin(tauri_plugin_file::init())
         .menu(use_memu())
         .on_menu_event(|event| {
             // 处理菜单事件

+ 2 - 1
src-tauri/src/self_plugin/mod.rs

@@ -1 +1,2 @@
-pub mod tauri_plugin_sqlite;
+pub mod tauri_plugin_sqlite;
+pub mod tauri_plugin_file;

+ 46 - 0
src-tauri/src/self_plugin/tauri_plugin_file/files.rs

@@ -0,0 +1,46 @@
+use std::fs;
+use std::path::{Path, PathBuf};
+use tauri::command;
+use serde::{Serialize, Serializer};
+// use tauri::api::file::IntoInvokeHandler;
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+    #[error(transparent)]
+    Io(#[from] std::io::Error)
+}
+
+impl Serialize for Error {
+    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
+    where
+        S: Serializer,
+    {
+        serializer.serialize_str(self.to_string().as_ref())
+    }
+}
+
+type Result<T> = std::result::Result<T, Error>;
+
+fn read_files_in_directory(directory: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
+    if let Ok(entries) = fs::read_dir(directory) {
+        for entry in entries {
+            if let Ok(entry) = entry {
+                let path = entry.path();
+                if path.is_file() {
+                    files.push(path.clone());
+                } else if path.is_dir() {
+                    read_files_in_directory(&path, files)?;
+                }
+            }
+        }
+    }
+    Ok(())
+}
+
+#[command]
+pub fn get_all_directory(path: String) -> Result<Vec<PathBuf>> {
+    let directory = Path::new(&path);
+    let mut files = Vec::new();
+    read_files_in_directory(directory, &mut files)?;
+    Ok(files)
+}

+ 21 - 0
src-tauri/src/self_plugin/tauri_plugin_file/mod.rs

@@ -0,0 +1,21 @@
+pub(crate) mod files;
+
+use tauri::{
+    plugin::{Builder, TauriPlugin},
+    Manager, Runtime,
+};
+
+use self::files::*;
+
+/// Initializes the plugin.
+pub fn init<R: Runtime>() -> TauriPlugin<R> {
+    Builder::new("st-files")
+        .invoke_handler(tauri::generate_handler![
+            get_all_directory
+        ])
+        .setup(|app| {
+            // app.manage(SqliteMap::default());
+            Ok(())
+        })
+        .build()
+}

+ 1 - 1
src-tauri/src/self_plugin/tauri_plugin_sqlite/mod.rs

@@ -10,7 +10,7 @@ use self::sqlite::*;
 
 /// Initializes the plugin.
 pub fn init<R: Runtime>() -> TauriPlugin<R> {
-    Builder::new("rrai-sqlite")
+    Builder::new("st-sqlite")
         .invoke_handler(tauri::generate_handler![
             open,
             open_with_flags,

+ 8 - 9
src-tauri/src/self_plugin/tauri_plugin_sqlite/sqlite.rs

@@ -4,9 +4,8 @@ use tauri::command;
 
 use serde::{Serialize, Serializer};
 use serde_json::Value as JsonValue;
-// use tauri_app::rrai_desktop_sdk_common;
 
-use crate::common as rrai_desktop_sdk_common;
+use crate::common as st_common;
 #[derive(Debug, thiserror::Error)]
 pub enum Error {
     #[error(transparent)]
@@ -36,7 +35,7 @@ type Result<T> = std::result::Result<T, Error>;
 
 #[command]
 pub async fn open(path: String) -> Result<bool> {
-    rrai_desktop_sdk_common::sqlite::open(&path).await?;
+    st_common::sqlite::open(&path).await?;
     Ok(true)
 }
 
@@ -44,31 +43,31 @@ pub async fn open(path: String) -> Result<bool> {
 pub async fn open_with_flags(path: String, iflags: i32) -> Result<bool> {
     let flags = OpenFlags::default();
 
-    rrai_desktop_sdk_common::sqlite::open_with_flags(&path, flags).await?;
+    st_common::sqlite::open_with_flags(&path, flags).await?;
     Ok(true)
 }
 
 #[command]
 pub async fn close(path: String) -> Result<bool> {
-    rrai_desktop_sdk_common::sqlite::close(&path).await?;
+    st_common::sqlite::close(&path).await?;
     Ok(true)
 }
 
 #[command]
 pub async fn execute_sql(path: String, sql: String) -> Result<usize> {
-    let res = rrai_desktop_sdk_common::sqlite::execute_sql(&path, &sql).await?;
+    let res = st_common::sqlite::execute_sql(&path, &sql).await?;
     Ok(res)
 }
 
 #[command]
 pub async fn execute_batch(path: String, sql: String) -> Result<bool> {
-    let res = rrai_desktop_sdk_common::sqlite::execute_batch(&path, &sql).await?;
+    let res = st_common::sqlite::execute_batch(&path, &sql).await?;
     Ok(res)
 }
 
 #[command]
 pub async fn execute(path: String, sql: String, args: JsonValue) -> Result<usize> {
-    let res = rrai_desktop_sdk_common::sqlite::execute(&path, &sql, &args).await?;
+    let res = st_common::sqlite::execute(&path, &sql, &args).await?;
     Ok(res)
 }
 
@@ -78,6 +77,6 @@ pub async fn query_with_args(
     sql: String,
     args: JsonValue,
 ) -> Result<Vec<HashMap<String, JsonValue>>> {
-    let res = rrai_desktop_sdk_common::sqlite::query_with_args(&path, &sql, &args).await?;
+    let res = st_common::sqlite::query_with_args(&path, &sql, &args).await?;
     Ok(res)
 }

+ 1 - 1
src/Router.tsx

@@ -12,7 +12,7 @@ export default function Router() {
   return (
     <Routes>
       <Route path="/" element={<Layout />}>
-        <Route index element={<FileSort />} />
+        <Route index element={<DuplicateFile />} />
         <Route path={"about"} element={<About />} />
         <Route path={"finder"} element={<Finder />} />
         <Route path={"setting"} element={<Setting />} />

+ 25 - 0
src/databases/index.ts

@@ -0,0 +1,25 @@
+import { SQLite } from '@/plugins/tauri-plugin-sqlite';
+
+export const getVersion = async (dbName: string): Promise<number> => {
+    const dbversion = await SQLite.open(dbName);
+
+    //查询是否有该表
+    const rows = await dbversion.queryWithArgs<Array<{ count: number }>>("SELECT count(1) count FROM sqlite_master WHERE type='table' and name = 'select_history' ");
+
+    console.log(rows);
+    if (!!rows && rows.length > 0 && rows[0].count > 0) {
+
+    } else {
+        //创建表
+        await dbversion.execute(`CREATE TABLE select_history (name TEXT, version INTEGER, unique(name));`)
+    }
+
+    //查询
+    const versions = await dbversion.queryWithArgs<Array<{ version: number }>>("SELECT version FROM databases_version WHERE name = :name", { ':name': dbName });
+
+    if (!!versions && versions.length > 0) {
+        return versions[0].version;
+    }
+
+    return 0;
+}

+ 44 - 2
src/pages/DuplicateFile/DuplicateFile.tsx

@@ -1,9 +1,51 @@
 import styles from "./DuplicateFile.module.less";
+import { invoke } from "@tauri-apps/api/tauri";
+import { open } from "@tauri-apps/api/dialog";
+import { Col, Row, Button } from "antd";
+import { appDataDir } from "@tauri-apps/api/path";
+import File from "@/plugins/tauri-plugin-file/file";
+import { useState } from "react";
+import {SQLite} from '@/plugins/tauri-plugin-sqlite'
+
 export default function DuplicateFile() {
+  const [usePath, setUsePath] = useState<string>("111111111111111");
+  async function sort() {
+    // 打开本地的系统目录,暂时不支持多选
+    const selected = await open({
+      directory: true,
+      multiple: false,
+      defaultPath: await appDataDir(),
+    });
+
+    if (selected && selected.length) {
+      setUsePath(`${selected}`);
+      // 最多记录 100 条用户操作的历史数据
+      const files = await File.getAllList(`${selected}`);
+      console.log(20202020, files);
+    }
+    // await invoke("file_sort", { path: selected });
+    // setFile([...fileStr, await invoke("file_sort", { path: selected })]);
+  }
+
+  // 存储用户的历史选择记录
+  async function opens() {
+    const sql = await SQLite.open('./files.db3')
+    // console.log(sql.execute);
+
+    // SQLite.execute
+  }
+
   return (
     <div className={styles.DuplicateFileBox}>
-      DuplicateFile DuplicateFile
-      <div style={{ backgroundColor: "green", height: "200vh" }}>DuplicateFile</div>
+      <Row>
+        <Col>
+          <Button onClick={() => sort()}>选择文件路径</Button>
+        </Col>
+
+        <Col>设置文件路径</Col>
+      </Row>
+      <Row>已选择路径:{usePath}</Row>
+      {usePath && <Row><Button onClick={() => opens()}>开始</Button></Row>}
     </div>
   );
 }

+ 32 - 0
src/plugins/tauri-plugin-file/file.ts

@@ -0,0 +1,32 @@
+import { invoke } from "@tauri-apps/api/tauri";
+
+export class File {
+  path: string;
+
+  constructor(path: string) {
+    this.path = path;
+  }
+
+  static async getAllList(path: string): Promise<string[]> {
+    console.log(11,path);
+    return await invoke<string[]>("plugin:st-files|get_all_directory", {
+      path,
+    });
+    // console.log(15,path);
+    // return new Promise(() => res);
+  }
+
+  // async close(): Promise<boolean> {
+  //     return await invoke('plugin:st-sqlite|close', { path: this.path })
+  // }
+
+  // async execute(sql: string, values?: Record<string, any>): Promise<boolean> {
+  //     return values ? await invoke('plugin:st-sqlite|execute', { path: this.path, sql, args: values }) : await invoke('plugin:st-sqlite|execute_sql', { path: this.path, sql })
+  // }
+
+  // async queryWithArgs<T>(sql: string, values?: Record<string, any>): Promise<T> {
+  //     return await invoke('plugin:st-sqlite|query_with_args', { path: this.path, sql, args: values ?? {} })
+  // }
+}
+
+export default File;

+ 1 - 0
src/plugins/tauri-plugin-file/index.ts

@@ -0,0 +1 @@
+export * from './file';

+ 4 - 4
src/plugins/tauri-plugin-sqlite/sqlite.ts

@@ -8,21 +8,21 @@ export class SQLite {
     }
 
     static async open(path: string): Promise<SQLite> {
-        let res = await invoke<string>('plugin:rrai-sqlite|open', { path });
+        let res = await invoke<string>('plugin:st-sqlite|open', { path });
         console.log(res);
         return new SQLite(path);
     }
 
     async close(): Promise<boolean> {
-        return await invoke('plugin:rrai-sqlite|close', { path: this.path })
+        return await invoke('plugin:st-sqlite|close', { path: this.path })
     }
 
     async execute(sql: string, values?: Record<string, any>): Promise<boolean> {
-        return values ? await invoke('plugin:rrai-sqlite|execute', { path: this.path, sql, args: values }) : await invoke('plugin:rrai-sqlite|execute_sql', { path: this.path, sql })
+        return values ? await invoke('plugin:st-sqlite|execute', { path: this.path, sql, args: values }) : await invoke('plugin:st-sqlite|execute_sql', { path: this.path, sql })
     }
 
     async queryWithArgs<T>(sql: string, values?: Record<string, any>): Promise<T> {
-        return await invoke('plugin:rrai-sqlite|query_with_args', { path: this.path, sql, args: values ?? {} })
+        return await invoke('plugin:st-sqlite|query_with_args', { path: this.path, sql, args: values ?? {} })
     }
 }