index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. export class Router {
  2. constructor() {
  3. this.interceptor = {
  4. before: null,
  5. after: null
  6. }
  7. }
  8. /**
  9. * 保留当前页面,跳转到应用内的某个页面,使用navigateBack可以返回到原页面。
  10. * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
  11. * @param {*} options
  12. */
  13. navigateTo(
  14. options = {
  15. url: '',
  16. animationType: 'pop-in',
  17. animationDuration: 300,
  18. success: () => {},
  19. fail: () => {},
  20. complete: () => {}
  21. }
  22. ) {
  23. if (this.interceptor.before) {
  24. let newOptions = this.interceptor.before(options)
  25. if (newOptions) {
  26. options = newOptions
  27. } else {
  28. return
  29. }
  30. }
  31. uni.navigateTo({
  32. url: options.url,
  33. animationType: options.animationType,
  34. animationDuration: options.animationDuration,
  35. success: options.success,
  36. fail: options.fail,
  37. complete: options.complete
  38. })
  39. if (this.interceptor.after) {
  40. let newOptions = this.interceptor.after(options)
  41. if (newOptions) {
  42. options = newOptions
  43. }
  44. }
  45. }
  46. /**
  47. * 关闭当前页面,跳转到应用内的某个页面。
  48. * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
  49. * @param {*} options
  50. */
  51. redirectTo(
  52. options = {
  53. url: '',
  54. success: () => {},
  55. fail: () => {},
  56. complete: () => {}
  57. }
  58. ) {
  59. if (this.interceptor.before) {
  60. let newOptions = this.interceptor.before(options)
  61. if (newOptions) {
  62. options = newOptions
  63. } else {
  64. return
  65. }
  66. }
  67. uni.redirectTo({
  68. url: options.url,
  69. success: options.success,
  70. fail: options.fail,
  71. complete: options.complete
  72. })
  73. if (this.interceptor.after) {
  74. let newOptions = this.interceptor.after(options)
  75. if (newOptions) {
  76. options = newOptions
  77. }
  78. }
  79. }
  80. /**
  81. * 关闭所有页面,打开到应用内的某个页面。
  82. * @description reLaunch 可以打开任意页面。
  83. * @param {*} options
  84. */
  85. reLaunch(
  86. options = {
  87. url: '',
  88. success: () => {},
  89. fail: () => {},
  90. complete: () => {}
  91. }
  92. ) {
  93. if (this.interceptor.before) {
  94. let newOptions = this.interceptor.before(options)
  95. if (newOptions) {
  96. options = newOptions
  97. } else {
  98. return
  99. }
  100. }
  101. uni.reLaunch({
  102. url: options.url,
  103. success: options.success,
  104. fail: options.fail,
  105. complete: options.complete
  106. })
  107. if (this.interceptor.after) {
  108. let newOptions = this.interceptor.after(options)
  109. if (newOptions) {
  110. options = newOptions
  111. }
  112. }
  113. }
  114. /**
  115. * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
  116. * @description switchTab 只能打开 tabBar 页面。
  117. * @param {*} options
  118. */
  119. switchTab(
  120. options = {
  121. url: '',
  122. success: () => {},
  123. fail: () => {},
  124. complete: () => {}
  125. }
  126. ) {
  127. if (this.interceptor.before) {
  128. let newOptions = this.interceptor.before(options)
  129. if (newOptions) {
  130. options = newOptions
  131. } else {
  132. return
  133. }
  134. }
  135. uni.switchTab({
  136. url: options.url,
  137. success: options.success,
  138. fail: options.fail,
  139. complete: options.complete
  140. })
  141. if (this.interceptor.after) {
  142. let newOptions = this.interceptor.after(options)
  143. if (newOptions) {
  144. options = newOptions
  145. }
  146. }
  147. }
  148. /**
  149. * 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
  150. * @param {*} options
  151. */
  152. navigateBack(
  153. options = {
  154. delta: 1,
  155. animationType: 'pop-out',
  156. animationDuration: 300
  157. }
  158. ) {
  159. if (this.interceptor.before) {
  160. let newOptions = this.interceptor.before(options)
  161. if (newOptions) {
  162. options = newOptions
  163. } else {
  164. return
  165. }
  166. }
  167. uni.navigateBack({
  168. url: options.url,
  169. animationType: options.animationType,
  170. animationDuration: options.animationDuration,
  171. complete: options.complete
  172. })
  173. if (this.interceptor.after) {
  174. let newOptions = this.interceptor.after(options)
  175. if (newOptions) {
  176. options = newOptions
  177. }
  178. }
  179. }
  180. }