RequireLogin.ts 815 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { confirm } from "@/components/utils/DialogAction";
  2. import { navTo } from "@/components/utils/PageAction";
  3. import { useAuthStore } from "@/store/auth";
  4. /**
  5. * 说明:
  6. * * 登录校验组合式代码。
  7. */
  8. export function useReqireLogin() {
  9. const authStore = useAuthStore();
  10. return {
  11. /**
  12. * 校验登录状态
  13. * @param cb 登录成功回调
  14. * @param message 登录提示信息
  15. */
  16. requireLogin(cb: () => void, message: string = '登录后查看') {
  17. if (!authStore.isLogged) {
  18. confirm({
  19. title: '提示',
  20. content: message,
  21. confirmText: '去登录'
  22. }).then((res) => {
  23. if (res) {
  24. navTo('/pages/user/login');
  25. return;
  26. }
  27. })
  28. } else {
  29. cb();
  30. }
  31. }
  32. }
  33. }