- 实现拍照识别食物功能(集成大语言模型视觉能力) - 实现智能对话功能(集成大语言模型流式输出) - 实现食物记录和卡路里管理功能 - 实现体重记录和统计功能 - 实现健康数据管理页面 - 配置数据库表结构(用户、食物记录、体重记录) - 实现Express后端API路由 - 配置Tab导航和前端页面 - 采用健康运动配色方案
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
// @ts-nocheck
|
|
/**
|
|
* 通用认证上下文
|
|
*
|
|
* 基于固定的 API 接口实现,可复用到其他项目
|
|
* 其他项目使用时,只需修改 @api 的导入路径指向项目的 api 模块
|
|
*
|
|
* 注意:
|
|
* - 如果需要登录/鉴权场景,请扩展本文件,完善 login/logout、token 管理、用户信息获取与刷新等逻辑
|
|
* - 将示例中的占位实现替换为项目实际的接口调用与状态管理
|
|
*/
|
|
import React, { createContext, useContext, ReactNode } from "react";
|
|
|
|
interface UserOut {
|
|
|
|
}
|
|
|
|
interface AuthContextType {
|
|
user: UserOut | null;
|
|
token: string | null;
|
|
isAuthenticated: boolean;
|
|
isLoading: boolean;
|
|
login: (token: string) => Promise<void>;
|
|
logout: () => Promise<void>;
|
|
updateUser: (userData: Partial<UserOut>) => void;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
|
const value: AuthContextType = {
|
|
user: null,
|
|
token: null,
|
|
isAuthenticated: false,
|
|
isLoading: false,
|
|
login: async (token: string) => {},
|
|
logout: async () => {},
|
|
updateUser: () => {},
|
|
};
|
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
|
};
|
|
|
|
export const useAuth = (): AuthContextType => {
|
|
const context = useContext(AuthContext);
|
|
if (context === undefined) {
|
|
throw new Error("useAuth must be used within an AuthProvider");
|
|
}
|
|
return context;
|
|
};
|