use-user-store.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { defineStore } from "pinia";
  2. import ls from "@/utils/store2";
  3. import { ref } from "vue";
  4. import { userToken } from "../composables/config";
  5. import crypt from "../composables/crypt";
  6. export interface UserInfo {
  7. id?: string;
  8. name: string;
  9. email: string;
  10. phone: string;
  11. approveStatus?: number;
  12. }
  13. export interface AccountInfo {
  14. loginName: string;
  15. password: string;
  16. rememberPassword: boolean;
  17. }
  18. const STORAGE_KEY = "user";
  19. const ACCOUNT_INFO_KEY = "accountInfo";
  20. const ID_TYPE_OPTIONS_KEY = "reasonsOptions";
  21. const useUserStore = defineStore("userStore", () => {
  22. const userInfo = ref<UserInfo | null>(null);
  23. const accountInfo = ref<AccountInfo | null>(null);
  24. const isLoggedIn = ref(false);
  25. const reasonsOptions = ref<any>(null);
  26. const initUserInfo = () => {
  27. const encryptedInfo = ls.get(STORAGE_KEY);
  28. if (encryptedInfo) {
  29. const decryptedInfo = crypt.decrypt(encryptedInfo);
  30. if (decryptedInfo) {
  31. userInfo.value = JSON.parse(decryptedInfo);
  32. isLoggedIn.value = true;
  33. }
  34. }
  35. };
  36. const initReasonsOptions = () => {
  37. const encryptedInfo = ls.get(ID_TYPE_OPTIONS_KEY);
  38. if (encryptedInfo) {
  39. const decryptedInfo = crypt.decrypt(encryptedInfo);
  40. if (decryptedInfo) {
  41. reasonsOptions.value = JSON.parse(decryptedInfo);
  42. isLoggedIn.value = true;
  43. }
  44. }
  45. };
  46. const initAccountInfo = () => {
  47. const encryptedInfo = ls.get(ACCOUNT_INFO_KEY);
  48. if (encryptedInfo) {
  49. const decryptedInfo = crypt.decrypt(encryptedInfo);
  50. if (decryptedInfo) {
  51. accountInfo.value = JSON.parse(decryptedInfo);
  52. }
  53. }
  54. };
  55. const saveUserInfo = (info: UserInfo) => {
  56. userInfo.value = info;
  57. isLoggedIn.value = true;
  58. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  59. ls.set(STORAGE_KEY, encryptedInfo);
  60. };
  61. const saveReasonsOptions = (info: any) => {
  62. reasonsOptions.value = info;
  63. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  64. ls.set(ID_TYPE_OPTIONS_KEY, encryptedInfo);
  65. };
  66. const saveAccountInfo = (info: AccountInfo) => {
  67. accountInfo.value = info;
  68. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  69. ls.set(ACCOUNT_INFO_KEY, encryptedInfo);
  70. };
  71. const clearUserInfo = () => {
  72. userInfo.value = null;
  73. isLoggedIn.value = false;
  74. userToken.value = "";
  75. reasonsOptions.value = null;
  76. ls.remove(STORAGE_KEY);
  77. };
  78. initReasonsOptions();
  79. initUserInfo();
  80. initAccountInfo();
  81. return {
  82. userInfo,
  83. accountInfo,
  84. isLoggedIn,
  85. reasonsOptions,
  86. saveReasonsOptions,
  87. initReasonsOptions,
  88. saveUserInfo,
  89. saveAccountInfo,
  90. clearUserInfo,
  91. };
  92. });
  93. export default useUserStore;