// import loginUtil from './login_util.js';

//返回页面个数
async function goBack(pages) {

    const cPages = getCurrentPages();
    const currentPage = cPages[cPages.length - 1];

    if (currentPage.route.lastIndexOf("detail/detail")) {
        setTimeout(() => {
            wx.switchTab({
                url: '../pages/index/index'
            });
        }, 300);
    } else {
        // 执行返回操作
        if (pages == null) pages = 1;
        await wx.navigateBack({
            delta: pages,
        });
    }

};

///跳转界面
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 jumpLogin(url, isNeedLogin) {
    if (isNeedLogin) {
        // if (loginUtil.getUserBean() == null) {
        //     return;
        // }
    }
    await wx.navigateTo({
        url: url,
    });
};

//跳转到Tab页面
async function switchTo(url) {
    console.log(url)
    await wx.switchTab({
        //跳转目标
        url: url,
        //跳转成功后
        success: function () {
            //让多个tabl显示出来
            // wx.showTabBar({
            //     animation: false,
            // });
        }, fail: function (res) {
            console.log(res)
        }
    })
};

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

/**
 * 跳转到其他小程序
 * @param {*} item
 */
function jump2Other(item) {
    wx.navigateToMiniProgram({
        appId: item.app_id,
        path: item.page_path,
        // extraData: {
        //   foo: 'bar'
        // },
        // envVersion: 'develop',
        envVersion: 'release',
        success() { },
        fail() { }
    });
};

module.exports = {
    jump: jump,
    jumpParam: jumpParam,
    goBack: goBack,
    switchTo: switchTo,
    jump2Other: jump2Other,
    redirectTo: redirectTo,
    jumpLogin: jumpLogin,
}