useLogout.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import { getLogoutApi } from "@/api/user";
  2. import { useRouter } from "@/i18n/routing";
  3. import { useSearchStore } from "@/stores/useSearchStore";
  4. import { useUserInfoStore } from "@/stores/useUserInfoStore";
  5. import { useWalletStore } from "@/stores/useWalletStore";
  6. import dayjs from "dayjs";
  7. import Cookies from "js-cookie";
  8. export const useLogout = () => {
  9. const restGlobal = useUserInfoStore((state) => state.reset);
  10. const resetSearch = useSearchStore((state) => state.reset);
  11. const restWalletStore = useWalletStore((state) => state.reset);
  12. const router = useRouter();
  13. const logout = async () => {
  14. let res = await getLogoutApi();
  15. const isClearPromotion = dayjs().isSame(sessionStorage.getItem("isClosePromotion"), "days");
  16. if (res.code == 200) {
  17. restGlobal();
  18. resetSearch();
  19. restWalletStore();
  20. localStorage.clear();
  21. Cookies.remove("Token");
  22. router.replace("/login");
  23. }
  24. if (!isClearPromotion) {
  25. sessionStorage.clear();
  26. }
  27. return Promise.resolve(res);
  28. };
  29. return {
  30. logout,
  31. };
  32. };