session-guard.js 799 B

12345678910111213141516171819202122232425262728
  1. import store from '@/store'
  2. import { openPage } from '@/core/nav/navigation'
  3. export const LOGIN_ROUTE = '/pages/passport/lobby'
  4. /** 当前是否已登录 */
  5. export function isLoggedIn() {
  6. return !!(store.state.user && store.state.user.isLogin)
  7. }
  8. /**
  9. * 登录守卫:未登录时跳转登录页
  10. * @param {string|{ redirect?: string, message?: string, toast?: boolean }} options
  11. * @returns {boolean} 已登录 true;未登录 false(并已跳转)
  12. */
  13. export function requireLogin(options = {}) {
  14. if (isLoggedIn()) return true
  15. const opts = typeof options === 'string' ? { message: options } : (options || {})
  16. const redirect = opts.redirect || LOGIN_ROUTE
  17. if (opts.toast && opts.message) {
  18. uni.showToast({ title: opts.message, icon: 'none' })
  19. }
  20. openPage(redirect)
  21. return false
  22. }