import { defineStore } from 'pinia'; export const useUserStore = defineStore('user', { state: () => ({ isRegister: false, // 是否注册成功 isLogin: false, // 用户是否已登录 showLoginModal: false, // 是否显示登录弹框 userInfo: null, // 用户信息 }), getters: { // 方便在模板中直接使用 isLoggedIn: (state) => state.isLogin, }, actions: { // 打开登录弹框 openLoginModal() { this.showLoginModal = true; }, // 关闭登录弹框 closeLoginModal() { this.isRegister = false; this.showLoginModal = false; }, async register(userFrom) { await new Promise(resolve => setTimeout(resolve, 1000)); this.isRegister = true; uni.showToast({ title: '注册成功', icon: 'success' }); }, // 模拟登录操作 async login(loginDto) { console.log('正在登录...', loginDto); // --- 这里应该是调用你的后端 API --- // 模拟一个异步请求 await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟登录成功 this.isLogin = true; this.userInfo = { name: '张三', token: 'fake-token-string' }; // 登录成功后关闭弹框 this.closeLoginModal(); uni.showToast({ title: '登录成功', icon: 'success' }); }, // 登出操作 logout() { this.isRegister = false; this.isLogin = false; this.userInfo = null; uni.showToast({ title: '已退出登录', icon: 'none' }); }, }, });