module.exports = {
  getLastPageRoute: getLastPageRoute,
  goBackHomePage: goBackHomePage,
  goBackRoute: goBackRoute,
  jump: jump,
  jumpParam: jumpParam,
  goBack: goBack,
  redirectTo: redirectTo,
}

///获取最后一个界面的路由名称
function getLastPageRoute() {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  return currentPage.route;
};

///跳转，不摧毁界面
async function jump(url) {
  await wx.navigateTo({
    url: url,
  });
};

///跳转带参，不摧毁界面
async function jumpParam(url, param) {
  if (param) {
    await wx.navigateTo({
      url: url + "?param=" + param,
    });
  } else {
    await jump(url);
  }
};

//跳转,并且摧毁当前界面
async function redirectTo(url) {
  await wx.redirectTo({
    url: url,
  });
};

//返回页面个数
async function goBack(pages) {
  if (pages == null) pages == 1;
  await wx.navigateBack({
    delta: pages,
  });
};

///返回到首页，不关闭当前界面
async function goBackHomePage() {
  const route_constant = require('./route_constant.js');
  await goBackRoute(route_constant.indexRoot);
};

///返回到某个界面，不替换当前界面
///route：pages/index/index
async function goBackRoute(route) {
  const pages = getCurrentPages(); // 获取当前页面栈
  const pageCount = pages.length; // 页面栈中的页面数量
  var pageNuble = 0;
  for (var i = pageCount - 1; i > -1; i--) {
    if (pages[i].route != route) {
      pageNuble++;
    } else {
      break;
    }
  }
  if (pageNuble > 0) {
    await goBack(pageNuble);
  }
};