routes.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { autoSubcrebe } from './utils'
  2. const tabRoutes = {
  3. // home: "/pages/index/index",
  4. // my: "/pages/my/my",
  5. // chatList: "/pages/chat-list/chat-list",
  6. }
  7. const routes = {
  8. select: "/pages/select/index",
  9. example: "/pages/example/index",
  10. camera: "/pages/camera/index",
  11. index: "/pages/index/index",
  12. start: "/pages/start/index",
  13. work: "/pages/work/index"
  14. }
  15. function sortQuery (query) {
  16. return Object.keys(query).map(key => `${key}=${query[key]}`).join('&')
  17. }
  18. function toUrl (url, query) {
  19. if (query) {
  20. query = sortQuery(query)
  21. }
  22. if (tabRoutes[url]) {
  23. wx.switchTab({
  24. url: `${tabRoutes[url]}?${query}`,
  25. })
  26. } else {
  27. wx.navigateTo({
  28. url: `${routes[url]}?${query}`,
  29. })
  30. }
  31. }
  32. export default {
  33. tabRoutes,
  34. push (options) {
  35. // autoSubcrebe()
  36. if (typeof options === 'string') {
  37. return toUrl(options)
  38. }
  39. const { url, query } = options
  40. return toUrl(url, query)
  41. },
  42. redirectTo(options){
  43. const { url, query } = options
  44. let q = query
  45. if (q) {
  46. q = sortQuery(q)
  47. }
  48. return (()=>{
  49. wx.redirectTo({
  50. url: `${routes[url]}?${q}`,
  51. })
  52. })()
  53. },
  54. back () {
  55. wx.navigateBack()
  56. }
  57. }