From 51d8643b8afe6f77065107b57d153e6f5c40ba77 Mon Sep 17 00:00:00 2001 From: 0214 <3614157673@qq.com> Date: Thu, 25 Jan 2024 01:37:04 +0800 Subject: [PATCH 1/6] add a new page to search channels --- src/routes/searchChannel.svelte | 106 +++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/src/routes/searchChannel.svelte b/src/routes/searchChannel.svelte index 24eb70a..035f7ed 100644 --- a/src/routes/searchChannel.svelte +++ b/src/routes/searchChannel.svelte @@ -1 +1,105 @@ -

search channels

+ + +

查找频道

+
+ + +
+ +{#if errorMessage} +

{errorMessage}

+{:else if searchResults.length > 0} + +{:else if name !== ""} +

没有找到相关频道。

+{/if} + + From 85454deae866cc595d9f7add6fb716b1b5de5d18 Mon Sep 17 00:00:00 2001 From: 0214 <3614157673@qq.com> Date: Sat, 27 Jan 2024 16:09:58 +0800 Subject: [PATCH 2/6] Updated createchannel --- src/routes/createChannel.svelte | 21 ++++++++++++++++++--- src/routes/searchChannel.svelte | 4 ++-- src/utils/api/index.js | 2 +- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/routes/createChannel.svelte b/src/routes/createChannel.svelte index 34ca7d6..c32e8e7 100644 --- a/src/routes/createChannel.svelte +++ b/src/routes/createChannel.svelte @@ -1,17 +1,29 @@ -

Create your channel

+

Create Your Channel

-
-
-
- -
-
- -
+ +
+ +
+
+
- + {#if isLoading} +

Loading...

+ {:else} + + {/if} + + \ No newline at end of file diff --git a/src/routes/searchChannel.svelte b/src/routes/searchChannel.svelte index a9fb59b..a154226 100644 --- a/src/routes/searchChannel.svelte +++ b/src/routes/searchChannel.svelte @@ -56,8 +56,7 @@ {#each searchResults as channel}
  • {channel.channelName} - +
  • {/each} diff --git a/src/utils/api/index.js b/src/utils/api/index.js index d8e7e7b..099b31c 100644 --- a/src/utils/api/index.js +++ b/src/utils/api/index.js @@ -1,2 +1,2 @@ -export const PocketBase_URL = "http://10.112.209.162:8090/_/"; +export const PocketBase_URL = "http://10.112.209.162:8090"; //export const PocketBase_URL='http://127.0.0.1:8090'; //test用 From 0a855c0e4e7c6c1192928a1e9a704d7166e079fd Mon Sep 17 00:00:00 2001 From: 0214 <3614157673@qq.com> Date: Mon, 29 Jan 2024 00:28:21 +0800 Subject: [PATCH 4/6] feat: refine createchannel --- src/routes/createChannel.svelte | 117 ++++++++++++++++++-------------- src/routes/searchChannel.svelte | 3 +- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/src/routes/createChannel.svelte b/src/routes/createChannel.svelte index fcf308f..a5ed57b 100644 --- a/src/routes/createChannel.svelte +++ b/src/routes/createChannel.svelte @@ -9,13 +9,17 @@ let isLoading = false; // 新增:用于跟踪提交状态 async function handleCreate() { - isLoading = true; + isLoading = true; // 检查频道名是否已存在 - const existingChannels = await pb.collection("channels").getFullList({filter: `channelName = "${channelName}"`}); + const existingChannels = await pb + .collection("channels") + .getFullList({ filter: `channelName = "${channelName}"` }); if (existingChannels.length > 0) { - alert("A channel with this name already exists. Please choose a different name."); - isLoading = false; + alert( + "A channel with this name already exists. Please choose a different name.", + ); + isLoading = false; return; } @@ -27,11 +31,11 @@ try { const createdChannel = await pb.collection("channels").create(data); alert("Channel created successfully"); - isLoading = false; + isLoading = false; push("/main"); } catch (error) { alert("ERROR: " + error.message); - isLoading = false; + isLoading = false; } } @@ -40,10 +44,19 @@
    - +
    - +
    {#if isLoading}

    Loading...

    @@ -53,50 +66,50 @@
    \ No newline at end of file + .btn-submit:hover { + background-color: #0056b3; + } + diff --git a/src/routes/searchChannel.svelte b/src/routes/searchChannel.svelte index a154226..a9fb59b 100644 --- a/src/routes/searchChannel.svelte +++ b/src/routes/searchChannel.svelte @@ -56,7 +56,8 @@ {#each searchResults as channel}
  • {channel.channelName} - +
  • {/each} From 01a00b28db64db093065ee04c8bbaa89ae889aa1 Mon Sep 17 00:00:00 2001 From: 0214 <3614157673@qq.com> Date: Tue, 30 Jan 2024 01:08:30 +0800 Subject: [PATCH 5/6] feat: update searchChannel --- src/routes/login.svelte | 4 ++ src/routes/searchChannel.svelte | 124 ++++++++++++++++++++++---------- src/store.js | 4 ++ 3 files changed, 96 insertions(+), 36 deletions(-) create mode 100644 src/store.js diff --git a/src/routes/login.svelte b/src/routes/login.svelte index 862869e..a6d5666 100644 --- a/src/routes/login.svelte +++ b/src/routes/login.svelte @@ -3,6 +3,7 @@ import { onDestroy } from "svelte"; import PocketBase from "pocketbase"; import { PocketBase_URL } from "../utils/api/index"; + import { currentUserEmail } from "../store.js"; const pb = new PocketBase(PocketBase_URL); let username = ""; @@ -14,6 +15,9 @@ .collection("users") .authWithPassword(username, password); if (userData) { + currentUserEmail.set(username); + const userEmail = $currentUserEmail; + console.log("当前用户的电子邮件:", userEmail); push("/main"); } } catch (error) { diff --git a/src/routes/searchChannel.svelte b/src/routes/searchChannel.svelte index a9fb59b..6000e68 100644 --- a/src/routes/searchChannel.svelte +++ b/src/routes/searchChannel.svelte @@ -2,20 +2,31 @@ import PocketBase from "pocketbase"; import { push } from "svelte-spa-router"; import { PocketBase_URL } from "../utils/api/index"; + import { currentUserEmail } from "../store.js"; + // 实例化 PocketBase const pb = new PocketBase(PocketBase_URL); - let name = ""; // 绑定到输入框的变量,用于存储用户输入的频道名称 + let channelNameInput = ""; // 用于存储用户输入的频道名称 let searchResults = []; // 存储搜索结果的数组 let errorMessage = ""; // 存储错误信息 + // 搜索频道的函数 async function searchChannel() { try { errorMessage = ""; + const userEmail = $currentUserEmail; const records = await pb .collection("channels") - .getFullList({ filter: `channelName="${name}"` }); - searchResults = records; + .getFullList({ filter: `channelName="${channelNameInput}"` }); + searchResults = await Promise.all( + records.map((channel) => + checkUserJoinedChannel(channel.channelName, userEmail).then( + (isJoined) => ({ ...channel, isJoined }), + ), + ), + ); + if (records.length === 0) { errorMessage = "没有找到相关频道。"; } @@ -25,12 +36,40 @@ } } + // 检查用户是否加入了频道 + async function checkUserJoinedChannel(channelName, userEmail) { + try { + const records = await pb.collection("users_channels").getFullList({ + filter: `useremail="${userEmail}" && channelname="${channelName}"`, + }); + return records.length > 0; + } catch (error) { + console.error("检查用户频道时发生错误:", error); + return false; + } + } + + // 用户加入频道 + async function joinChannel(channelname) { + try { + const userEmail = $currentUserEmail; + await pb.collection("users_channels").create({ + useremail: userEmail, + channelname: channelname, + }); + alert("已成功加入频道"); + } catch (error) { + console.error("加入频道时发生错误:", error); + alert("加入频道时发生错误:" + error.message); + } + } + // 导航到频道详情页 - function ToChannel(channelName) { + function navigateToChannelDetail(channelName) { push(`/channelDetail/${channelName}`); } - // 按下 Enter 键时触发搜索 + // 处理 Enter 键触发的搜索 function handleKeyPress(event) { if (event.key === "Enter") { searchChannel(); @@ -39,11 +78,12 @@

    查找频道

    +
    @@ -52,54 +92,66 @@ {#if errorMessage}

    {errorMessage}

    {:else if searchResults.length > 0} -
      - {#each searchResults as channel} -
    • - {channel.channelName} - + {channel.channelName} - {channel.channelDescription} + {#if channel.isJoined} + 已加入 + {:else} + -
    • - {/each} -
    -{:else if name !== ""} + {/if} + + + {/each} +{:else if channelNameInput !== ""}

    没有找到相关频道。

    {/if} diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..d9c75fe --- /dev/null +++ b/src/store.js @@ -0,0 +1,4 @@ +import { writable } from "svelte/store"; + +export const currentUserEmail = writable(""); +//在不同的组件间共享当前用户的电子邮件地址。 From 2e391041b87d198df400b1f0dc00042fdeafdb7a Mon Sep 17 00:00:00 2001 From: 0214 <3614157673@qq.com> Date: Tue, 30 Jan 2024 01:28:05 +0800 Subject: [PATCH 6/6] refactor: refactor createChannel --- src/routes/createChannel.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/createChannel.svelte b/src/routes/createChannel.svelte index a5ed57b..0318b9f 100644 --- a/src/routes/createChannel.svelte +++ b/src/routes/createChannel.svelte @@ -29,7 +29,7 @@ }; try { - const createdChannel = await pb.collection("channels").create(data); + await pb.collection("channels").create(data); alert("Channel created successfully"); isLoading = false; push("/main");