use-user-store.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 = "ACCOUNT_INFO_KEY";
  20. const ID_TYPE_OPTIONS_KEY = "ID_TYPE_OPTIONS_KEY";
  21. const APP_VERSION = 'APP_VERSION'
  22. const PROBLEM_DETAILS_KEY = "PROBLEM_DETAILS_KEY";
  23. const LOGIN_OPTIONS_KEY = "LOGIN_OPTIONS_KEY";
  24. const PAYMENT_CHANNEL_KEY = "PAYMENT_CHANNEL_KEY";
  25. const CHANNEL_LIST_KEY = "CHANNEL_LIST_KEY";
  26. const useUserStore = defineStore("userStore", () => {
  27. const userInfo = ref<UserInfo | null>(null);
  28. const accountInfo = ref<AccountInfo | null>(null);
  29. const isLoggedIn = ref(false);
  30. const reasonsOptions = ref<any>(null);
  31. const loginOptions = ref<any>(null);
  32. const paymentChannel = ref<any>(null);
  33. const channelList = ref<any>(null);
  34. const appVersion = ref<any>(null);
  35. const problemDetails = ref<any>(null);
  36. const initChannel = () => {
  37. const encryptedInfo = ls.get(PAYMENT_CHANNEL_KEY);
  38. if (encryptedInfo) {
  39. const decryptedInfo = crypt.decrypt(encryptedInfo);
  40. if (decryptedInfo) {
  41. paymentChannel.value = JSON.parse(decryptedInfo);
  42. }
  43. }
  44. };
  45. const initChannelList = () => {
  46. const encryptedInfo = ls.get(CHANNEL_LIST_KEY);
  47. if (encryptedInfo) {
  48. const decryptedInfo = crypt.decrypt(encryptedInfo);
  49. if (decryptedInfo) {
  50. channelList.value = JSON.parse(decryptedInfo);
  51. }
  52. }
  53. };
  54. const initUserInfo = () => {
  55. const encryptedInfo = ls.get(STORAGE_KEY);
  56. if (encryptedInfo) {
  57. const decryptedInfo = crypt.decrypt(encryptedInfo);
  58. if (decryptedInfo) {
  59. userInfo.value = JSON.parse(decryptedInfo);
  60. isLoggedIn.value = true;
  61. }
  62. }
  63. };
  64. const initAppVersion = () => {
  65. const encryptedInfo = ls.get(APP_VERSION);
  66. if (encryptedInfo) {
  67. const decryptedInfo = crypt.decrypt(encryptedInfo);
  68. if (decryptedInfo) {
  69. userInfo.value = JSON.parse(decryptedInfo);
  70. isLoggedIn.value = true;
  71. }
  72. }
  73. };
  74. const initLoginOptions = () => {
  75. const encryptedInfo = ls.get(LOGIN_OPTIONS_KEY);
  76. if (encryptedInfo) {
  77. const decryptedInfo = crypt.decrypt(encryptedInfo);
  78. if (decryptedInfo) {
  79. loginOptions.value = JSON.parse(decryptedInfo);
  80. isLoggedIn.value = true;
  81. }
  82. }
  83. }
  84. const initProblemDetails = () => {
  85. const encryptedInfo = ls.get(PROBLEM_DETAILS_KEY);
  86. if (encryptedInfo) {
  87. const decryptedInfo = crypt.decrypt(encryptedInfo);
  88. if (decryptedInfo) {
  89. problemDetails.value = JSON.parse(decryptedInfo);
  90. isLoggedIn.value = true;
  91. }
  92. }
  93. };
  94. const initReasonsOptions = () => {
  95. const encryptedInfo = ls.get(ID_TYPE_OPTIONS_KEY);
  96. if (encryptedInfo) {
  97. const decryptedInfo = crypt.decrypt(encryptedInfo);
  98. if (decryptedInfo) {
  99. reasonsOptions.value = JSON.parse(decryptedInfo);
  100. isLoggedIn.value = true;
  101. }
  102. }
  103. };
  104. const initAccountInfo = () => {
  105. const encryptedInfo = ls.get(ACCOUNT_INFO_KEY);
  106. if (encryptedInfo) {
  107. const decryptedInfo = crypt.decrypt(encryptedInfo);
  108. if (decryptedInfo) {
  109. accountInfo.value = JSON.parse(decryptedInfo);
  110. }
  111. }
  112. };
  113. const saveUserInfo = (info: UserInfo) => {
  114. userInfo.value = info;
  115. isLoggedIn.value = true;
  116. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  117. ls.set(STORAGE_KEY, encryptedInfo);
  118. };
  119. const savePaymentChannel = (info: any) => {
  120. paymentChannel.value = info;
  121. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  122. ls.set(PAYMENT_CHANNEL_KEY, encryptedInfo);
  123. };
  124. const saveChannelList = (info: any) => {
  125. channelList.value = info;
  126. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  127. ls.set(CHANNEL_LIST_KEY, encryptedInfo);
  128. };
  129. const saveLoginOptions = (info: any) => {
  130. loginOptions.value = info;
  131. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  132. ls.set(LOGIN_OPTIONS_KEY, encryptedInfo);
  133. };
  134. const saveProblemDetails = (info: any) => {
  135. problemDetails.value = info;
  136. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  137. ls.set(PROBLEM_DETAILS_KEY, encryptedInfo);
  138. };
  139. const saveAppVersion = (info: any) => {
  140. appVersion.value = info;
  141. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  142. ls.set(APP_VERSION, encryptedInfo);
  143. };
  144. const saveReasonsOptions = (info: any) => {
  145. reasonsOptions.value = info;
  146. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  147. ls.set(ID_TYPE_OPTIONS_KEY, encryptedInfo);
  148. };
  149. const saveAccountInfo = (info: AccountInfo) => {
  150. accountInfo.value = info;
  151. const encryptedInfo = crypt.encrypt(JSON.stringify(info));
  152. ls.set(ACCOUNT_INFO_KEY, encryptedInfo);
  153. };
  154. const clearUserInfo = () => {
  155. userInfo.value = null;
  156. isLoggedIn.value = false;
  157. userToken.value = "";
  158. reasonsOptions.value = null;
  159. loginOptions.value = null;
  160. paymentChannel.value = null;
  161. channelList.value = null;
  162. ls.remove(STORAGE_KEY);
  163. ls.remove(PAYMENT_CHANNEL_KEY);
  164. ls.remove(CHANNEL_LIST_KEY);
  165. ls.remove(LOGIN_OPTIONS_KEY);
  166. ls.remove(PROBLEM_DETAILS_KEY);
  167. ls.remove(APP_VERSION);
  168. ls.remove(ID_TYPE_OPTIONS_KEY);
  169. ls.remove(ACCOUNT_INFO_KEY);
  170. };
  171. initReasonsOptions();
  172. initUserInfo();
  173. initAccountInfo();
  174. initAppVersion();
  175. initLoginOptions();
  176. initProblemDetails();
  177. initChannel();
  178. initChannelList();
  179. return {
  180. userInfo,
  181. accountInfo,
  182. isLoggedIn,
  183. reasonsOptions,
  184. loginOptions,
  185. appVersion,
  186. problemDetails,
  187. paymentChannel,
  188. channelList,
  189. savePaymentChannel,
  190. saveChannelList,
  191. saveProblemDetails,
  192. saveLoginOptions,
  193. saveAppVersion,
  194. saveReasonsOptions,
  195. saveUserInfo,
  196. saveAccountInfo,
  197. clearUserInfo,
  198. };
  199. });
  200. export default useUserStore;