From fb719c6ea26202e7b7bdb6f262da89ad07a4a732 Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 13 Jan 2026 17:03:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=B9=E6=8E=A5=E9=80=80=E5=87=BA?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(tabs)/my.tsx | 36 +++++++++++++++++++++++++++++++++++- hooks/use-activates.ts | 35 ++++++++--------------------------- lib/auth.ts | 19 +++++++++---------- locales/en-US.json | 3 ++- locales/zh-CN.json | 3 ++- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/app/(tabs)/my.tsx b/app/(tabs)/my.tsx index 3353787..82198db 100644 --- a/app/(tabs)/my.tsx +++ b/app/(tabs)/my.tsx @@ -16,6 +16,8 @@ import { useTranslation } from 'react-i18next' import { PointsIcon, SearchIcon, SettingsIcon } from '@/components/icon' import EditProfileDrawer from '@/components/drawer/EditProfileDrawer' import Dropdown from '@/components/ui/dropdown' +import Toast from '@/components/ui/Toast' +import { signOut } from '@/lib/auth' const { width: screenWidth } = Dimensions.get('window') const GALLERY_GAP = 2 @@ -53,13 +55,44 @@ export default function My() { const [profileName, setProfileName] = useState('乔乔乔乔') // 处理设置菜单选择 - const handleSettingsSelect = (value: string) => { + const handleSettingsSelect = async (value: string) => { if (value === 'changePassword') { router.push('/changePassword' as any) } else if (value === 'language') { // 切换语言 const newLang = i18n.language === 'zh-CN' ? 'en-US' : 'zh-CN' i18n.changeLanguage(newLang) + } else if (value === 'logout') { + // 退出登录 + console.log('🚪 点击退出登录') + const confirmText = i18n.language === 'zh-CN' ? '确定' : 'OK' + const cancelText = i18n.language === 'zh-CN' ? '取消' : 'Cancel' + const message = i18n.language === 'zh-CN' ? '确定要退出登录吗?' : 'Are you sure you want to logout?' + + Toast.showActionSheet({ + itemList: [message, confirmText, cancelText] + }).then(async (index) => { + // index 1 是确定按钮 + if (index === 1) { + console.log('🚪 开始执行退出登录') + try { + Toast.showLoading({ title: i18n.language === 'zh-CN' ? '退出中...' : 'Logging out...' }) + // 调用 better-auth 的 signOut 方法 + await signOut() + Toast.hideLoading() + console.log('✅ 退出登录成功,跳转到登录页') + Toast.show(i18n.language === 'zh-CN' ? '退出登录成功' : 'Logged out successfully') + // 跳转到登录页面(注意:路由是 /auth 不是 /login) + router.replace('/auth') + } catch (error) { + Toast.hideLoading() + console.error('❌ 退出登录失败:', error) + Toast.show(i18n.language === 'zh-CN' ? '退出登录失败,请稍后重试' : 'Failed to logout, please try again later') + } + } + }).catch(() => { + console.log('❌ 取消退出登录') + }) } } @@ -75,6 +108,7 @@ export default function My() { const settingsOptions = [ { label: t('my.changePassword'), value: 'changePassword' }, { label: getLanguageLabel(), value: 'language' }, + { label: t('my.logout'), value: 'logout' }, ] return ( diff --git a/hooks/use-activates.ts b/hooks/use-activates.ts index 595d4b6..6b18a3e 100644 --- a/hooks/use-activates.ts +++ b/hooks/use-activates.ts @@ -1,40 +1,21 @@ -import { loomart } from "@/lib/auth" +import { OWNER_ID } from "@/lib/auth" import { ApiError } from "@/lib/types" +import { root } from '@repo/core' +import { ActivityController, ListActivitiesResult } from "@repo/sdk" import { useState } from "react" export const useActivates = () => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) - const [data, setData] = useState<{ - activities: { - title: string; - titleEn: string; - desc: string; - descEn: string; - coverUrl: string; - link: string; - createdAt: Date; - updatedAt: Date; - id: string; - videoUrl?: string | null | undefined; - isActive?: boolean | null | undefined; - sortOrder?: number | null | undefined; - }[]; - total: number; - page: number; - limit: number; - totalPages: number; - }>() + const [data, setData] = useState() const load = async () => { try { setLoading(true) - const { data, error } = await loomart.activities.list({ isActive: true }) - console.log({ data, error }) - if (error) { - setError(error) - return; - } + const c = root.get(ActivityController) + const data = await c.list({ page: 1, limit: 10, isActive: true, orderBy: 'sortOrder', order: 'desc', ownerId: OWNER_ID }) setData(data) } catch (e) { + setError(e as ApiError) + } finally { setLoading(false) } } diff --git a/lib/auth.ts b/lib/auth.ts index 655dabb..34d7a20 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -3,16 +3,15 @@ import 'reflect-metadata' import { expoClient } from '@better-auth/expo/client' import { createSkerClientPlugin } from '@repo/sdk' import { - adminClient, - emailOTPClient, - genericOAuthClient, - jwtClient, - organizationClient, - phoneNumberClient, - usernameClient, + adminClient, + emailOTPClient, + genericOAuthClient, + jwtClient, + organizationClient, + phoneNumberClient, + usernameClient, } from 'better-auth/client/plugins' import { createAuthClient } from 'better-auth/react' -import Constants from 'expo-constants' import { fetchWithLogger, TOKEN_KEY } from './fetch-logger' import type { Subscription } from './plugins/stripe' @@ -21,7 +20,7 @@ import { storage } from './storage' import type { ApiError } from './types' // 商户ID配置 - 从环境变量或应用配置中读取 -const MERCHANT_ID = Constants.expoConfig?.extra?.merchantId || '' +export const OWNER_ID = 't0m9cketSQdCA6cHXI9mXQLJPM9LDIw5' export interface CreditBalance { tokenUsage: number @@ -92,7 +91,7 @@ export const authClient = createAuthClient({ 'Content-Type': 'application/json', // x-ownerid: 商户ID(如果后端需要) // 如果 MERCHANT_ID 为空,则不添加此 header - ...(MERCHANT_ID && { 'x-ownerid': MERCHANT_ID }), + ...(OWNER_ID && { 'x-ownerid': OWNER_ID }), }, auth: { type: 'Bearer', diff --git a/locales/en-US.json b/locales/en-US.json index 33ca717..a660a77 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -7,7 +7,8 @@ "changePassword": "Change Password", "language": "Language", "languageSwitch": "Language: 中文", - "languageSwitchEn": "Language: English" + "languageSwitchEn": "Language: English", + "logout": "Logout" }, "changePassword": { "title": "Change Password", diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 5cee6df..ceccb0c 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -7,7 +7,8 @@ "changePassword": "修改密码", "language": "语言", "languageSwitch": "语言: 中文", - "languageSwitchEn": "Language: English" + "languageSwitchEn": "Language: English", + "logout": "退出登录" }, "changePassword": { "title": "修改密码",