use-user-store.ts 6.0 KB

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