You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.4 KiB

2 months ago
  1. /**
  2. * 路由跳转方法该方法相对于直接使用uni.xxx的好处是使用更加简单快捷
  3. * 并且带有路由拦截功能
  4. */
  5. import { queryParams, deepMerge, page } from '../function/index';
  6. class Router {
  7. constructor() {
  8. // 原始属性定义
  9. this.config = {
  10. type: 'navigateTo',
  11. url: '',
  12. delta: 1, // navigateBack页面后退时,回退的层数
  13. params: {}, // 传递的参数
  14. animationType: 'pop-in', // 窗口动画,只在APP有效
  15. animationDuration: 300, // 窗口动画持续时间,单位毫秒,只在APP有效
  16. intercept: false // 是否需要拦截
  17. }
  18. // 因为route方法是需要对外赋值给另外的对象使用,同时route内部有使用this,会导致route失去上下文
  19. // 这里在构造函数中进行this绑定
  20. this.route = this.route.bind(this)
  21. }
  22. // 判断url前面是否有"/",如果没有则加上,否则无法跳转
  23. addRootPath(url) {
  24. return url[0] === '/' ? url : `/${url}`
  25. }
  26. // 整合路由参数
  27. mixinParam(url, params) {
  28. url = url && this.addRootPath(url)
  29. // 使用正则匹配,主要依据是判断是否有"/","?","="等,如“/page/index/index?name=mary"
  30. // 如果有url中有get参数,转换后无需带上"?"
  31. let query = ''
  32. if (/.*\/.*\?.*=.*/.test(url)) {
  33. // object对象转为get类型的参数
  34. query = queryParams(params, false)
  35. // 因为已有get参数,所以后面拼接的参数需要带上"&"隔开
  36. return url += `&${query}`
  37. }
  38. // 直接拼接参数,因为此处url中没有后面的query参数,也就没有"?/&"之类的符号
  39. query = queryParams(params)
  40. return url += query
  41. }
  42. // 对外的方法名称
  43. async route(options = {}, params = {}) {
  44. // 合并用户的配置和内部的默认配置
  45. let mergeConfig = {}
  46. if (typeof options === 'string') {
  47. // 如果options为字符串,则为route(url, params)的形式
  48. mergeConfig.url = this.mixinParam(options, params)
  49. mergeConfig.type = 'navigateTo'
  50. } else {
  51. mergeConfig = deepMerge(this.config, options)
  52. // 否则正常使用mergeConfig中的url和params进行拼接
  53. mergeConfig.url = this.mixinParam(options.url, options.params)
  54. }
  55. // 如果本次跳转的路径和本页面路径一致,不执行跳转,防止用户快速点击跳转按钮,造成多次跳转同一个页面的问题
  56. if (mergeConfig.url === page()) return
  57. if (params.intercept) {
  58. this.config.intercept = params.intercept
  59. }
  60. // params参数也带给拦截器
  61. mergeConfig.params = params
  62. // 合并内外部参数
  63. mergeConfig = deepMerge(this.config, mergeConfig)
  64. // 判断用户是否定义了拦截器
  65. if (typeof uni.$u.routeIntercept === 'function') {
  66. // 定一个promise,根据用户执行resolve(true)或者resolve(false)来决定是否进行路由跳转
  67. const isNext = await new Promise((resolve, reject) => {
  68. uni.$u.routeIntercept(mergeConfig, resolve)
  69. })
  70. // 如果isNext为true,则执行路由跳转
  71. isNext && this.openPage(mergeConfig)
  72. } else {
  73. this.openPage(mergeConfig)
  74. }
  75. }
  76. // 执行路由跳转
  77. openPage(config) {
  78. // 解构参数
  79. const {
  80. url,
  81. type,
  82. delta,
  83. animationType,
  84. animationDuration
  85. } = config
  86. if (config.type == 'navigateTo' || config.type == 'to') {
  87. uni.navigateTo({
  88. url,
  89. animationType,
  90. animationDuration
  91. })
  92. }
  93. if (config.type == 'redirectTo' || config.type == 'redirect') {
  94. uni.redirectTo({
  95. url
  96. })
  97. }
  98. if (config.type == 'switchTab' || config.type == 'tab') {
  99. uni.switchTab({
  100. url
  101. })
  102. }
  103. if (config.type == 'reLaunch' || config.type == 'launch') {
  104. uni.reLaunch({
  105. url
  106. })
  107. }
  108. if (config.type == 'navigateBack' || config.type == 'back') {
  109. uni.navigateBack({
  110. delta
  111. })
  112. }
  113. }
  114. }
  115. export default (new Router()).route