From 6929aa8a47203487d284072a121fc77313ec4342 Mon Sep 17 00:00:00 2001 From: maojindao55 Date: Tue, 1 Apr 2025 07:26:40 +0800 Subject: [PATCH] support user --- friends.sql | 13 +++++++++++++ functions/api/user/upload.ts | 24 ++++++++++++++++++++++++ src/store/userStore.ts | 25 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 friends.sql create mode 100644 functions/api/user/upload.ts create mode 100644 src/store/userStore.ts diff --git a/friends.sql b/friends.sql new file mode 100644 index 0000000..872897a --- /dev/null +++ b/friends.sql @@ -0,0 +1,13 @@ +PRAGMA defer_foreign_keys=TRUE; +CREATE TABLE users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + phone VARCHAR(11) NOT NULL UNIQUE, + nickname VARCHAR(50), + avatar_url TEXT, + status INTEGER DEFAULT 1, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + last_login_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +INSERT INTO users VALUES(1,'13800138000','测试用户',NULL,1,'2025-03-26 08:39:15','2025-03-26 08:39:15','2025-03-26 08:39:15'); +INSERT INTO users VALUES(2,'13391881866','Hobby',NULL,1,'2025-03-26 09:42:30','2025-03-27 05:50:04','2025-03-27 03:21:40'); \ No newline at end of file diff --git a/functions/api/user/upload.ts b/functions/api/user/upload.ts new file mode 100644 index 0000000..698688d --- /dev/null +++ b/functions/api/user/upload.ts @@ -0,0 +1,24 @@ +export const onRequestPost: PagesFunction = async (context) =>{ + const { env, data } = context; + + // 创建 FormData 对象 + const formData = new FormData(); + formData.append('requireSignedURLs', 'false'); + formData.append('metadata', JSON.stringify({ + user: data.user.userId + })); + + // 调用 Cloudflare Images API 获取直接上传 URL + const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CF_ACCOUNT_ID}/images/v2/direct_upload`, { + method: "POST", + headers: { + "Authorization": `Bearer ${env.CF_API_TOKEN}`, + }, + body: formData + }); + + const data1 = await response.json(); + console.log('上传头像', data1); + //下发image前缀 + return Response.json(data1.result); + } \ No newline at end of file diff --git a/src/store/userStore.ts b/src/store/userStore.ts new file mode 100644 index 0000000..ff7ce01 --- /dev/null +++ b/src/store/userStore.ts @@ -0,0 +1,25 @@ +import { create } from 'zustand' + +interface UserInfo { + id: number; + phone: string; + nickname: string; + avatar_url: string | null; + status: number; +} + +interface UserStore { + userInfo: UserInfo; + setUserInfo: (userInfo: UserInfo) => void; +} + +export const useUserStore = create((set) => ({ + userInfo: { + id: 0, + phone: '', + nickname: '', + avatar_url: null, + status: 0 + }, + setUserInfo: (userInfo: UserInfo) => set({ userInfo }) +})); \ No newline at end of file