user.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { defineStore } from "pinia";
  2. import * as Pinia from 'pinia';
  3. import { login, getUserInfo } from '@/api/user/login';
  4. import { useAppStore } from './app.js';
  5. const appStore = useAppStore(Pinia.createPinia())
  6. export const useUserStore = defineStore("userStore", {
  7. state() {
  8. return {
  9. token: "",
  10. userInfo: null
  11. };
  12. },
  13. getters:{
  14. GET_TOKEN(){
  15. return this.token
  16. },
  17. GET_USER_INFO(){
  18. return this.userInfo
  19. }
  20. },
  21. actions:{
  22. loginAction(formData){
  23. login({data:formData}).then(res=>{
  24. let { token, refreshToken } = res
  25. uni.setStorageSync('token', token)
  26. this.token = token
  27. getUserInfo().then(info=>{
  28. uni.setStorageSync('userInfo', info)
  29. this.userInfo = info
  30. uni.switchTab({
  31. url:'/pages/index/index'
  32. })
  33. })
  34. })
  35. },
  36. async logoutAction(){
  37. uni.removeStorageSync('token')
  38. uni.removeStorageSync('userInfo')
  39. uni.removeStorageSync('version')
  40. appStore.setSysVersion('1.0.0')
  41. this.userInfo = null
  42. this.token = ""
  43. uni.reLaunch({
  44. url:"/pages/login/index"
  45. })
  46. }
  47. },
  48. unistorage: {
  49. // 初始化恢复前触发
  50. beforeRestore(ctx) {},
  51. // 初始化恢复后触发
  52. afterRestore(ctx) {},
  53. },
  54. });