2024-02-08 10:29:42 +08:00
|
|
|
import { SupabaseClient } from "@supabase/supabase-js";
|
|
|
|
// import { cookies } from "next/headers";
|
|
|
|
// import { createClient } from "@/utils/supabase/server";
|
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { Reference } from "@/utils/global";
|
|
|
|
//supabase
|
|
|
|
const supabase = createClient();
|
|
|
|
import { createClient } from "@/utils/supabase/client";
|
2024-02-14 23:16:03 +08:00
|
|
|
//sentry
|
|
|
|
import * as Sentry from "@sentry/nextjs";
|
|
|
|
|
2024-02-08 10:29:42 +08:00
|
|
|
//获取用户id
|
2024-02-08 14:14:50 +08:00
|
|
|
export async function getUser() {
|
2024-02-08 10:29:42 +08:00
|
|
|
const { data, error } = await supabase.auth.getSession();
|
|
|
|
if (data.session) {
|
|
|
|
const user = data.session.user;
|
|
|
|
if (user) {
|
|
|
|
// console.log("User UUID in getUser:", user.id);
|
|
|
|
return user;
|
|
|
|
} else {
|
|
|
|
console.log("No user in getUser");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("No session in getUser");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//将论文保存到服务器
|
|
|
|
export async function submitPaper(
|
|
|
|
supabase: SupabaseClient,
|
2024-02-09 23:01:01 +08:00
|
|
|
editorContent?: string, // 使得editorContent成为可选参数
|
|
|
|
references?: Reference[], // 使得references成为可选参数
|
2024-02-08 21:57:21 +08:00
|
|
|
paperNumber = "1"
|
2024-02-08 10:29:42 +08:00
|
|
|
) {
|
|
|
|
const user = await getUser(supabase);
|
|
|
|
if (user) {
|
|
|
|
try {
|
2024-02-09 23:01:01 +08:00
|
|
|
// 构造请求体,只包含提供的参数
|
|
|
|
const requestBody: any = {
|
|
|
|
userId: user.id,
|
|
|
|
paperNumber,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (editorContent !== undefined) {
|
|
|
|
requestBody.paperContent = editorContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (references !== undefined) {
|
|
|
|
requestBody.paperReference = references;
|
|
|
|
}
|
|
|
|
|
2024-02-08 10:29:42 +08:00
|
|
|
const response = await fetch("/api/supa/data", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2024-02-09 23:01:01 +08:00
|
|
|
body: JSON.stringify(requestBody),
|
2024-02-08 10:29:42 +08:00
|
|
|
});
|
2024-02-09 23:01:01 +08:00
|
|
|
|
2024-02-08 10:29:42 +08:00
|
|
|
const data = await response.json();
|
2024-02-13 16:01:16 +08:00
|
|
|
// console.log(
|
|
|
|
// "Response data in submitPaper:",
|
|
|
|
// data,
|
|
|
|
// `此次更新的是第${paperNumber}篇论文,` +
|
|
|
|
// `${editorContent !== undefined ? "更新内容为" + editorContent : ""}` +
|
|
|
|
// `${
|
|
|
|
// references !== undefined
|
|
|
|
// ? "更新引用为" + JSON.stringify(references)
|
|
|
|
// : ""
|
|
|
|
// }`
|
|
|
|
// );
|
2024-02-08 10:29:42 +08:00
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error submitting paper in submitPaper:", error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
"No user found. User must be logged in to submit a paper. in submitPaper"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-02-09 23:01:01 +08:00
|
|
|
|
2024-02-08 10:29:42 +08:00
|
|
|
//添加某指定用户id下的论文
|
|
|
|
|
|
|
|
//删除指定用户下paperNumber的论文
|
2024-02-08 14:14:50 +08:00
|
|
|
export async function deletePaper(
|
|
|
|
supabase: SupabaseClient,
|
|
|
|
userId: string,
|
|
|
|
paperNumber: string
|
|
|
|
) {
|
|
|
|
const { data, error } = await supabase
|
|
|
|
.from("user_paper")
|
|
|
|
.delete()
|
|
|
|
.eq("user_id", userId)
|
|
|
|
.eq("paper_number", paperNumber);
|
|
|
|
console.log("删除的数据", data);
|
|
|
|
if (error) {
|
|
|
|
console.error("删除出错", error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2024-02-08 10:29:42 +08:00
|
|
|
//获取用户论文
|
2024-02-08 14:14:50 +08:00
|
|
|
export async function getUserPapers(userId: string, supabase: SupabaseClient) {
|
|
|
|
const { data, error } = await supabase
|
|
|
|
.from("user_paper") // 指定表名
|
|
|
|
.select("*") // 选择所有列
|
|
|
|
.eq("user_id", userId); // 筛选特定user_id的记录
|
2024-02-08 10:29:42 +08:00
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
if (error) {
|
|
|
|
console.error("查询出错", error);
|
|
|
|
return null;
|
|
|
|
}
|
2024-02-08 10:29:42 +08:00
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
return data; // 返回查询结果
|
|
|
|
}
|
2024-02-08 10:29:42 +08:00
|
|
|
// 获取用户论文的序号
|
2024-02-08 14:14:50 +08:00
|
|
|
export async function getUserPaperNumbers(
|
|
|
|
userId: string,
|
|
|
|
supabase: SupabaseClient
|
|
|
|
) {
|
|
|
|
const { data, error } = await supabase
|
|
|
|
.from("user_paper") // 指定表名
|
|
|
|
.select("paper_number") // 仅选择paper_number列
|
|
|
|
.eq("user_id", userId); // 筛选特定user_id的记录
|
2024-02-08 10:29:42 +08:00
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
if (error) {
|
|
|
|
console.error("查询出错", error);
|
2024-02-08 10:29:42 +08:00
|
|
|
return null;
|
|
|
|
}
|
2024-02-08 14:14:50 +08:00
|
|
|
console.log("获取到的用户论文数量:", data);
|
|
|
|
// 返回查询结果,即所有论文的序号
|
|
|
|
return data.map((paper) => paper.paper_number);
|
2024-02-08 10:29:42 +08:00
|
|
|
}
|
|
|
|
// 获取用户指定序号论文的内容
|
2024-02-08 14:14:50 +08:00
|
|
|
export async function getUserPaper(
|
|
|
|
userId: string,
|
|
|
|
paperNumber: string,
|
|
|
|
supabase: SupabaseClient
|
|
|
|
) {
|
|
|
|
const { data, error } = await supabase
|
|
|
|
.from("user_paper") // 指定表名
|
|
|
|
.select("paper_content,paper_reference") // 仅选择paper_content列
|
|
|
|
.eq("user_id", userId) // 筛选特定user_id的记录
|
|
|
|
.eq("paper_number", paperNumber)
|
|
|
|
.single(); // 筛选特定paper_number的记录
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
console.error("查询出错", error);
|
|
|
|
return null;
|
|
|
|
}
|
2024-02-08 10:29:42 +08:00
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
// 返回查询结果,即指定论文的内容
|
|
|
|
return data;
|
|
|
|
}
|
2024-02-08 10:29:42 +08:00
|
|
|
|
2024-02-08 14:14:50 +08:00
|
|
|
// 使用Supabase客户端实例来查询vip_statuses表
|
|
|
|
export async function fetchUserVipStatus(userId: string) {
|
|
|
|
const { data, error } = await supabase
|
|
|
|
.from("vip_statuses")
|
|
|
|
.select("is_vip")
|
|
|
|
.eq("user_id", userId)
|
|
|
|
.single();
|
|
|
|
if (error) {
|
|
|
|
console.error("Error fetching VIP status:", error);
|
2024-02-08 15:47:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ("is_vip" in data) {
|
|
|
|
console.log("VIP status:", data.is_vip);
|
|
|
|
return data.is_vip;
|
|
|
|
} else {
|
|
|
|
return false;
|
2024-02-08 10:29:42 +08:00
|
|
|
}
|
2024-02-08 14:14:50 +08:00
|
|
|
}
|
2024-02-14 23:16:03 +08:00
|
|
|
|
|
|
|
//profiles表 插入用户信息
|
|
|
|
export async function insertUserProfile(data: any, supabase: SupabaseClient) {
|
2024-03-07 10:08:17 +08:00
|
|
|
let user;
|
|
|
|
if (data.user) {
|
|
|
|
user = data.user;
|
|
|
|
} else {
|
|
|
|
user = data;
|
|
|
|
}
|
2024-03-07 10:47:55 +08:00
|
|
|
|
2024-02-14 23:16:03 +08:00
|
|
|
if (user) {
|
2024-03-13 20:51:58 +08:00
|
|
|
// console.log("user in insertUserProfile:", user);
|
2024-03-07 10:47:55 +08:00
|
|
|
const currentTime = new Date().toISOString(); // 生成ISO格式的时间字符串
|
|
|
|
|
2024-02-14 23:16:03 +08:00
|
|
|
const { data, error: profileError } = await supabase
|
|
|
|
.from("profiles")
|
2024-03-07 10:47:55 +08:00
|
|
|
.upsert([
|
|
|
|
{
|
|
|
|
id: user.id,
|
|
|
|
email: user.email,
|
|
|
|
created_at: currentTime, // 添加创建时间
|
|
|
|
},
|
|
|
|
]);
|
2024-02-14 23:16:03 +08:00
|
|
|
|
|
|
|
if (profileError) {
|
|
|
|
console.error("Failed to create user profile:", profileError);
|
2024-03-07 10:08:17 +08:00
|
|
|
Sentry.captureException(profileError);
|
2024-02-14 23:16:03 +08:00
|
|
|
}
|
2024-03-07 10:47:55 +08:00
|
|
|
|
2024-02-14 23:16:03 +08:00
|
|
|
Sentry.setUser({
|
|
|
|
email: user.email,
|
|
|
|
id: user.id,
|
2024-03-07 10:47:55 +08:00
|
|
|
ip_address: "{{auto}}",
|
2024-02-14 23:16:03 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|