Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

87 lignes
2.7 KiB

  1. const LOGIN_RETURN_KEY = 'login_redirect'
  2. const DEFAULT_RETURN_URL = '/pages/toolbox/toolbox'
  3. const LOGIN_ROUTES = ['pages/login/login', 'pages/login/phone']
  4. function routePath(url = '') {
  5. return String(url).replace(/^\/+/, '').split(/[?#]/)[0]
  6. }
  7. function isLoginRoute(url = '') {
  8. return LOGIN_ROUTES.includes(routePath(url))
  9. }
  10. function normalizeReturnUrl(value = '') {
  11. if (!value) return ''
  12. let url = String(value)
  13. try {
  14. if (/%[0-9a-f]{2}/i.test(url)) url = decodeURIComponent(url)
  15. } catch (e) {}
  16. if (!url.startsWith('/')) url = `/${url}`
  17. if (!url.startsWith('/pages/') || isLoginRoute(url)) return ''
  18. return url
  19. }
  20. function getPageUrl(page) {
  21. if (!page) return ''
  22. const fullPath = page.$page && page.$page.fullPath
  23. const normalizedFullPath = normalizeReturnUrl(fullPath)
  24. if (normalizedFullPath) return normalizedFullPath
  25. const route = routePath(page.route || '')
  26. if (!route || isLoginRoute(route)) return ''
  27. const options = page.options || (page.$page && page.$page.options) || {}
  28. const query = Object.keys(options)
  29. .filter((key) => options[key] !== undefined && options[key] !== null && options[key] !== '')
  30. .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(String(options[key]))}`)
  31. .join('&')
  32. return normalizeReturnUrl(`/${route}${query ? `?${query}` : ''}`)
  33. }
  34. /**
  35. * 记录发起登录的页面。普通端返回时优先使用原页面栈;该记录主要用于 H5 微信授权后页面栈丢失的场景。
  36. */
  37. export function rememberLoginSource(explicitUrl = '') {
  38. const directUrl = normalizeReturnUrl(explicitUrl)
  39. if (directUrl) {
  40. uni.setStorageSync(LOGIN_RETURN_KEY, directUrl)
  41. return directUrl
  42. }
  43. const pages = getCurrentPages()
  44. for (let index = pages.length - 1; index >= 0; index -= 1) {
  45. const url = getPageUrl(pages[index])
  46. if (!url) continue
  47. uni.setStorageSync(LOGIN_RETURN_KEY, url)
  48. return url
  49. }
  50. return normalizeReturnUrl(uni.getStorageSync(LOGIN_RETURN_KEY))
  51. }
  52. /**
  53. * 登录成功后回到发起页:
  54. * - 页面栈仍存在时 navigateBack,保留表单/滚动位置,并触发来源页 onShow;
  55. * - H5 微信授权导致页面栈丢失时 reLaunch 到已记录路径;
  56. * - 没有来源页时才回到工具箱首页。
  57. */
  58. export function returnAfterLogin() {
  59. const pages = getCurrentPages()
  60. const firstLoginIndex = pages.findIndex((page) => isLoginRoute(page.route || ''))
  61. if (firstLoginIndex > 0) {
  62. uni.removeStorageSync(LOGIN_RETURN_KEY)
  63. uni.navigateBack({ delta: pages.length - firstLoginIndex })
  64. return
  65. }
  66. const returnUrl = normalizeReturnUrl(uni.getStorageSync(LOGIN_RETURN_KEY))
  67. uni.removeStorageSync(LOGIN_RETURN_KEY)
  68. if (returnUrl) {
  69. uni.reLaunch({ url: returnUrl })
  70. return
  71. }
  72. uni.switchTab({ url: DEFAULT_RETURN_URL })
  73. }