浏览代码

feat: 新增 util - toLoginPage 跳转到登录页, 带防抖处理

Utopia 6 月之前
父节点
当前提交
25c229869c
共有 1 个文件被更改,包括 42 次插入0 次删除
  1. 42 0
      src/utils/toLoginPage.ts

+ 42 - 0
src/utils/toLoginPage.ts

@@ -0,0 +1,42 @@
+import { LOGIN_PAGE } from '@/router/config'
+import { getLastPage } from '@/utils'
+import { debounce } from '@/utils/debounce'
+
+interface ToLoginPageOptions {
+  /**
+   * 跳转模式, uni.navigateTo | uni.reLaunch
+   * @default 'navigateTo'
+   */
+  mode?: 'navigateTo' | 'reLaunch'
+  /**
+   * 查询参数
+   * @example '?redirect=/pages/home/index'
+   */
+  queryString?: string
+}
+
+/**
+ * 跳转到登录页, 带防抖处理
+ *
+ * 如果要立即跳转,不做延时,可以使用 `toLoginPage.flush()` 方法
+ */
+export const toLoginPage = debounce((options: ToLoginPageOptions = {}) => {
+  const { mode = 'navigateTo', queryString = '' } = options
+
+  const url = `${LOGIN_PAGE}${queryString}`
+
+  // 获取当前页面路径
+  const currentPage = getLastPage()
+  const currentPath = `/${currentPage.route}`
+  // 如果已经在登录页,则不跳转
+  if (currentPath === LOGIN_PAGE) {
+    return
+  }
+
+  if (mode === 'navigateTo') {
+    uni.navigateTo({ url })
+  }
+  else {
+    uni.reLaunch({ url })
+  }
+}, 500)