use-user-store.ts 4.5 KB

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