-
-
-
-
-
-
+
+
+
\ 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 @@
\ 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");