routes.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. work: "/pages/work/index"
  12. }
  13. function sortQuery (query) {
  14. return Object.keys(query).map(key => `${key}=${query[key]}`).join('&')
  15. }
  16. function toUrl (url, query) {
  17. if (query) {
  18. query = sortQuery(query)
  19. }
  20. if (tabRoutes[url]) {
  21. wx.switchTab({
  22. url: `${tabRoutes[url]}?${query}`,
  23. })
  24. } else {
  25. wx.navigateTo({
  26. url: `${routes[url]}?${query}`,
  27. })
  28. }
  29. }
  30. export default {
  31. tabRoutes,
  32. push (options) {
  33. autoSubcrebe()
  34. if (typeof options === 'string') {
  35. return toUrl(options)
  36. }
  37. const { url, query } = options
  38. return toUrl(url, query)
  39. },
  40. redirectTo(options){
  41. const { url, query } = options
  42. return (()=>{
  43. wx.redirectTo({
  44. url: `${routes[url]}?${query}`,
  45. })
  46. })()
  47. },
  48. back () {
  49. wx.navigateBack()
  50. }
  51. }