user.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { defineStore } from 'pinia';
  2. export const useUserStore = defineStore('user', {
  3. state: () => ({
  4. isRegister: false, // 是否注册成功
  5. isLogin: false, // 用户是否已登录
  6. showLoginModal: false, // 是否显示登录弹框
  7. userInfo: null, // 用户信息
  8. }),
  9. getters: {
  10. // 方便在模板中直接使用
  11. isLoggedIn: (state) => state.isLogin,
  12. },
  13. actions: {
  14. // 打开登录弹框
  15. openLoginModal() {
  16. this.showLoginModal = true;
  17. },
  18. // 关闭登录弹框
  19. closeLoginModal() {
  20. this.isRegister = false;
  21. this.showLoginModal = false;
  22. },
  23. async register(userFrom) {
  24. await new Promise(resolve => setTimeout(resolve, 1000));
  25. this.isRegister = true;
  26. uni.showToast({
  27. title: '注册成功',
  28. icon: 'success'
  29. });
  30. },
  31. // 模拟登录操作
  32. async login(loginDto) {
  33. console.log('正在登录...', loginDto);
  34. // --- 这里应该是调用你的后端 API ---
  35. // 模拟一个异步请求
  36. await new Promise(resolve => setTimeout(resolve, 1000));
  37. // 模拟登录成功
  38. this.isLogin = true;
  39. this.userInfo = { name: '张三', token: 'fake-token-string' };
  40. // 登录成功后关闭弹框
  41. this.closeLoginModal();
  42. uni.showToast({
  43. title: '登录成功',
  44. icon: 'success'
  45. });
  46. },
  47. // 登出操作
  48. logout() {
  49. this.isRegister = false;
  50. this.isLogin = false;
  51. this.userInfo = null;
  52. uni.showToast({
  53. title: '已退出登录',
  54. icon: 'none'
  55. });
  56. },
  57. },
  58. });