use-user-store.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 APP_VERSION = 'appVersion'
  22. const PROBLEM_DETAILS_KEY = "problemDetails";
  23. const useUserStore = defineStore("userStore", () => {
  24. const userInfo = ref<UserInfo | null>(null);
  25. const accountInfo = ref<AccountInfo | null>(null);
  26. const isLoggedIn = ref(false);
  27. const reasonsOptions = ref<any>(null);
  28. const appVersion = ref<any>(null);
  29. const problemDetails = ref<any>(null);
  30. const initUserInfo = () => {
  31. const encryptedInfo = ls.get(STORAGE_KEY);
  32. if (encryptedInfo) {
  33. const decryptedInfo = crypt.decrypt(encryptedInfo);
  34. if (decryptedInfo) {
  35. userInfo.value = JSON.parse(decryptedInfo);
  36. isLoggedIn.value = true;
  37. }
  38. }
  39. };
  40. const initAppVersion = () => {
  41. const encryptedInfo = ls.get(APP_VERSION);
  42. if (encryptedInfo) {
  43. const decryptedInfo = crypt.decrypt(encryptedInfo);
  44. if (decryptedInfo) {
  45. userInfo.value = JSON.parse(decryptedInfo);
  46. isLoggedIn.value = true;
  47. }
  48. }
  49. };
  50. const initProblemDetails = () => {
  51. const encryptedInfo = ls.get(PROBLEM_DETAILS_KEY);
  52. if (encryptedInfo) {
  53. const decryptedInfo = crypt.decrypt(encryptedInfo);
  54. if (decryptedInfo) {
  55. problemDetails.value = JSON.parse(decryptedInfo);
  56. isLoggedIn.value = true;
  57. }
  58. }
  59. };
  60. const initReasonsOptions = () => {
  61. const encryptedInfo = ls.get(ID_TYPE_OPTIONS_KEY);
  62. if (encryptedInfo) {
  63. const decryptedInfo = crypt.decrypt(encryptedInfo);
  64. if (decryptedInfo) {
  65. reasonsOptions.value = JSON.parse(decryptedInfo);
  66. isLoggedIn.value = true;
  67. }
  68. }
  69. };
  70. const initAccountInfo = () => {
  71. const encryptedInfo = ls.get(ACCOUNT_INFO_KEY);
  72. if (encryptedInfo) {
  73. const decryptedInfo = crypt.decrypt(encryptedInfo);
  74. if (decryptedInfo) {
  75. accountInfo.value = JSON.parse(decryptedInfo);
  76. }
  77. }
  78. };
  79. const saveUserInfo = (info: UserInfo) => {
  80. userInfo.value = info;
  81. isLoggedIn.value = true;
  82. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  83. ls.set(STORAGE_KEY, encryptedInfo);
  84. };
  85. const saveProblemDetails = (info: any) => {
  86. problemDetails.value = info;
  87. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  88. ls.set(PROBLEM_DETAILS_KEY, encryptedInfo);
  89. };
  90. const saveAppVersion = (info: any) => {
  91. appVersion.value = info;
  92. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  93. ls.set(APP_VERSION, encryptedInfo);
  94. };
  95. const saveReasonsOptions = (info: any) => {
  96. reasonsOptions.value = info;
  97. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  98. ls.set(ID_TYPE_OPTIONS_KEY, encryptedInfo);
  99. };
  100. const saveAccountInfo = (info: AccountInfo) => {
  101. accountInfo.value = info;
  102. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  103. ls.set(ACCOUNT_INFO_KEY, encryptedInfo);
  104. };
  105. const clearUserInfo = () => {
  106. userInfo.value = null;
  107. isLoggedIn.value = false;
  108. userToken.value = "";
  109. reasonsOptions.value = null;
  110. ls.remove(STORAGE_KEY);
  111. };
  112. initReasonsOptions();
  113. initUserInfo();
  114. initAccountInfo();
  115. initAppVersion();
  116. initProblemDetails();
  117. return {
  118. userInfo,
  119. accountInfo,
  120. isLoggedIn,
  121. reasonsOptions,
  122. appVersion,
  123. problemDetails,
  124. saveProblemDetails,
  125. saveAppVersion,
  126. saveReasonsOptions,
  127. saveUserInfo,
  128. saveAccountInfo,
  129. clearUserInfo,
  130. };
  131. });
  132. export default useUserStore;