const tabRoutes = {
  home: "/pages/index/index",
  my: "/pages/my/my",
  chatList: "/pages/chat-list/chat-list",
  
}

const routes = {
  search: "/pages/search/search",
  exhibitionDetail: "/pages/exhibition-detail/exhibition-detail",
  exhibitionRegister: "/pages/exhibition-register/exhibition-register",
  exhibitionEnterprise :"/pages/exhibition-enterprise/exhibition-enterprise",
  goodsTabs: "/pages/goods-tabs/goods-tabs",
  userInfo: "/pages/user-info/user-info",
  collectGoods: "/pages/collect-goods/collect-goods",
  collectEnterprise: "/pages/collect-enterprise/collect-enterprise",
  businessCard: "/pages/business-card/business-card",
  businessCardDetail: '/pages/business-card/detail/business-card-detail',
  goodsDetail: "/pages/goods-detail/goods-detail",
  dataReport: "/dataReport/pages/data-report/data-report",
  find: '/pages/find/find',
  scene: '/pages/scene/scene',
  login: '/pages/login/login',
  chat: "/pages/chat/chat",
  logout: "/pages/logout/logout",
  scan: "/pages/business-card/scan/scan",
  scanResult: "/pages/business-card/scan-result/scan-result"
}

function sortQuery (query) {
  return Object.keys(query).map(key => `${key}=${query[key]}`).join('&')
}
function toUrl (url, query) {
  if (query) {
    query = sortQuery(query)
  }
  if (tabRoutes[url]) {
    wx.switchTab({
      url: `${tabRoutes[url]}?${query}`,
    })
  } else {
    wx.navigateTo({
      url: `${routes[url]}?${query}`,
    })
  }
}
export default {
  tabRoutes,
  push (options) {
    if (typeof options === 'string') {
      return toUrl(options)
    }
    const { url, query } = options
    return toUrl(url, query)
  },
  back () {
    wx.navigateBack()
  }
}