import { defineStore } from "pinia"; import ls from "@/utils/store2"; import { ref } from "vue"; import { userToken } from "../composables/config"; import crypt from "../composables/crypt"; import useGlobalStore from "./use-global-store"; export interface UserInfo { id?: string; name: string; email: string; phone: string; approveStatus?: number; } export interface AccountInfo { loginName: string; password: string; rememberPassword: boolean; } const STORAGE_KEY = "USER"; const ACCOUNT_INFO_KEY = "ACCOUNT_INFO_KEY"; const ID_TYPE_OPTIONS_KEY = "ID_TYPE_OPTIONS_KEY"; const APP_VERSION = 'APP_VERSION' const PROBLEM_DETAILS_KEY = "PROBLEM_DETAILS_KEY"; const LOGIN_OPTIONS_KEY = "LOGIN_OPTIONS_KEY"; const PAYMENT_CHANNEL_KEY = "PAYMENT_CHANNEL_KEY"; const CHANNEL_LIST_KEY = "CHANNEL_LIST_KEY"; const useUserStore = defineStore("userStore", () => { const userInfo = ref(null); const accountInfo = ref(null); const isLoggedIn = ref(false); const reasonsOptions = ref(null); const loginOptions = ref(null); const paymentChannel = ref(null); const channelList = ref(null); const appVersion = ref(null); const problemDetails = ref(null); const initChannel = () => { const encryptedInfo = ls.get(PAYMENT_CHANNEL_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { paymentChannel.value = JSON.parse(decryptedInfo); } } }; const initChannelList = () => { const encryptedInfo = ls.get(CHANNEL_LIST_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { channelList.value = JSON.parse(decryptedInfo); } } }; const initUserInfo = () => { const encryptedInfo = ls.get(STORAGE_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { userInfo.value = JSON.parse(decryptedInfo); isLoggedIn.value = true; } } }; const initAppVersion = () => { const encryptedInfo = ls.get(APP_VERSION); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { appVersion.value = JSON.parse(decryptedInfo); } } }; const initLoginOptions = () => { const encryptedInfo = ls.get(LOGIN_OPTIONS_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { loginOptions.value = JSON.parse(decryptedInfo); } } } const initProblemDetails = () => { const encryptedInfo = ls.get(PROBLEM_DETAILS_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { problemDetails.value = JSON.parse(decryptedInfo); } } }; const initReasonsOptions = () => { const encryptedInfo = ls.get(ID_TYPE_OPTIONS_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { reasonsOptions.value = JSON.parse(decryptedInfo); } } }; const initAccountInfo = () => { const encryptedInfo = ls.get(ACCOUNT_INFO_KEY); if (encryptedInfo) { const decryptedInfo = crypt.decrypt(encryptedInfo); if (decryptedInfo) { accountInfo.value = JSON.parse(decryptedInfo); } } }; const saveUserInfo = (info: UserInfo) => { userInfo.value = info; isLoggedIn.value = true; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(STORAGE_KEY, encryptedInfo); }; const savePaymentChannel = (info: any) => { paymentChannel.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(PAYMENT_CHANNEL_KEY, encryptedInfo); }; const saveChannelList = (info: any) => { channelList.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(CHANNEL_LIST_KEY, encryptedInfo); }; const saveLoginOptions = (info: any) => { loginOptions.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(LOGIN_OPTIONS_KEY, encryptedInfo); }; const saveProblemDetails = (info: any) => { problemDetails.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(PROBLEM_DETAILS_KEY, encryptedInfo); }; const saveAppVersion = (info: any) => { appVersion.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(APP_VERSION, encryptedInfo); }; const saveReasonsOptions = (info: any) => { reasonsOptions.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(ID_TYPE_OPTIONS_KEY, encryptedInfo); }; const saveAccountInfo = (info: AccountInfo) => { accountInfo.value = info; const encryptedInfo = crypt.encrypt(JSON.stringify(info)); ls.set(ACCOUNT_INFO_KEY, encryptedInfo); }; const clearUserInfo = () => { userInfo.value = null; isLoggedIn.value = false; userToken.value = ""; reasonsOptions.value = null; loginOptions.value = null; paymentChannel.value = null; channelList.value = null; ls.remove(STORAGE_KEY); ls.remove(PAYMENT_CHANNEL_KEY); ls.remove(CHANNEL_LIST_KEY); ls.remove(LOGIN_OPTIONS_KEY); ls.remove(PROBLEM_DETAILS_KEY); ls.remove(APP_VERSION); ls.remove(ID_TYPE_OPTIONS_KEY); ls.remove(ACCOUNT_INFO_KEY); const globalStore = useGlobalStore(); globalStore.setMode("customer"); }; initReasonsOptions(); initUserInfo(); initAccountInfo(); initAppVersion(); initLoginOptions(); initProblemDetails(); initChannel(); initChannelList(); return { userInfo, accountInfo, isLoggedIn, reasonsOptions, loginOptions, appVersion, problemDetails, paymentChannel, channelList, savePaymentChannel, saveChannelList, saveProblemDetails, saveLoginOptions, saveAppVersion, saveReasonsOptions, saveUserInfo, saveAccountInfo, clearUserInfo, }; }); export default useUserStore;