user.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { defineStore } from 'pinia';
  2. import api from '@/common/api/index.js'
  3. export const useUserStore = defineStore('user', {
  4. state: () => ({
  5. isRegister: true, // 是否注册成功
  6. isLogin: false, // 用户是否已登录
  7. showLoginModal: false, // 是否显示登录弹框
  8. registerStep: 0, // 注册步骤:0=未注册,1=填写用户信息,2=添加孩子
  9. token:'',
  10. userInfo: null, // 用户信息
  11. inpage:false, //是否当前页面
  12. }),
  13. getters: {
  14. // 方便在模板中直接使用
  15. isLoggedIn: (state) => state.isLogin,
  16. },
  17. actions: {
  18. // 打开登录弹框
  19. openLoginModal() {
  20. this.showLoginModal = true;
  21. },
  22. // 关闭登录弹框
  23. closeLoginModal() {
  24. this.showLoginModal = false;
  25. this.registerStep = 0;
  26. },
  27. async register(userFrom) {
  28. return new Promise(resolve => {
  29. api.post('/wx/register',userFrom).then(({data:res})=>{
  30. if(res.code !== 0){
  31. uni.showToast({ title: res.msg })
  32. resolve(false)
  33. return
  34. }
  35. // 注册成功,重新获取用户信息更新 realName 等字段
  36. api.get('/wx/userWelfareData', {}).then(({data:welfareRes}) => {
  37. if(welfareRes.code === 0) {
  38. this.userInfo = {...this.userInfo, ...welfareRes.data};
  39. uni.setStorageSync('userInfo', JSON.stringify(this.userInfo));
  40. }
  41. // 进入第二步添加孩子
  42. this.registerStep = 2;
  43. resolve(true)
  44. }).catch(() => {
  45. // 即使获取失败也继续流程
  46. this.registerStep = 2;
  47. resolve(true)
  48. })
  49. })
  50. });
  51. },
  52. // 添加孩子完成后调用,完成整个注册流程
  53. completeRegister() {
  54. this.showLoginModal = false;
  55. this.isRegister = true;
  56. this.isLogin = true;
  57. this.registerStep = 0;
  58. uni.showToast({
  59. title: '注册成功',
  60. icon: 'success'
  61. });
  62. },
  63. async login(loginDto) {
  64. await new Promise(resolve => {
  65. api.get('/wx/login',loginDto).then(({data:res})=>{
  66. if(res.code !== 0){
  67. uni.showModal({
  68. title:'温馨提示',
  69. content:res.msg,
  70. showCancel:false
  71. })
  72. return
  73. }
  74. this.token = res.data?.token;
  75. this.userInfo = res.data;
  76. uni.setStorageSync('token',this.userInfo?.token)
  77. const currentDate = new Date();
  78. const futureDate = new Date(currentDate);
  79. futureDate.setDate(currentDate.getDate() + 7);
  80. uni.setStorageSync('expaireTime',Date.parse(futureDate))
  81. uni.setStorageSync('userInfo',JSON.stringify(this.userInfo))
  82. // 新用户(userLevel=0)需要注册流程
  83. if(res.data?.userLevel === 0) {
  84. this.isRegister = false;
  85. this.registerStep = 1;
  86. } else {
  87. this.showLoginModal = false;
  88. this.isLogin = true;
  89. uni.showToast({
  90. title: '登录成功',
  91. icon: 'success'
  92. });
  93. }
  94. })
  95. });
  96. },
  97. // 登出操作
  98. logout() {
  99. this.isRegister = false;
  100. this.isLogin = false;
  101. this.userInfo = null;
  102. uni.showToast({
  103. title: '已退出登录',
  104. icon: 'none'
  105. });
  106. },
  107. },
  108. });