export const showToast = (options) => {
    if (typeof options === 'string') {
        uni.showToast({
            title: options,
            icon: 'none',
        }).then();
    } else {
        uni.showToast(options).then();
    }
}

export const isWXWeb = () => {
    const userAgent = navigator.userAgent;
    return userAgent.includes('MicroMessenger');
}

export const toPage = async (url) => {
    try {
        await uni.navigateTo({
            url: url,
        })
    } catch (e) {
        await uni.reLaunch({
            url: url,
        })
    }
}

export const backPage = () => {
    uni.navigateBack().then();
}

export const copy = (context) => {
    try {
        navigator.clipboard.writeText(context)
            .then(() => {
                showToast('已复制');
            })
            .catch(() => {
                showToast('复制失败');
            });
    } catch (e) {
        showToast('复制失败');
    }
}

export const download = (urls) => {
    const promises = urls.map((url, index) => new Promise((resolve, reject) => {
        const iframe = document.createElement('iframe');
        iframe.src = url;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        resolve(true);
    }));
    Promise.all(promises).then(res => {
        console.log(res);
    }).catch(err => {
        console.log(err);
    });
}
