routes.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. let q = query
  43. if (q) {
  44. q = sortQuery(q)
  45. }
  46. return (()=>{
  47. wx.redirectTo({
  48. url: `${routes[url]}?${q}`,
  49. })
  50. })()
  51. },
  52. back () {
  53. wx.navigateBack()
  54. }
  55. }