print.js 3.07 KB
import config from '@/config'
import { getToken } from '@/utils/auth.js'

export async function printFile(id, fileName = '合同.pdf') {
    const bizId = id != null ? String(id) : ''
    if (!bizId) {
        uni.showToast({ title: '未获取到合同ID', icon: 'none' })
        return
    }

    uni.showLoading({ title: '下载中' })
    try {
        const baseUrl = (config && config.baseUrl) ? String(config.baseUrl).replace(/\/$/, '') : ''
        const rawUrl = `${baseUrl}/contract/contractDistributorStandard/printStandardContract?id=${encodeURIComponent(bizId)}`
        const token = getToken()
        const header = {}
        if (token) header['X-Auth-Token'] = token

        let url = rawUrl
        // #ifdef H5
        if (token) {
            const separator = url.includes('?') ? '&' : '?'
            url = `${url}${separator}X-Auth-Token=${encodeURIComponent(token)}`
        }
        // #endif

        const res = await new Promise((resolve, reject) => {
            uni.downloadFile({
                url,
                header,
                success: resolve,
                fail: reject
            })
        })

        if (!(res && res.statusCode === 200)) {
            uni.showToast({ title: '下载失败', icon: 'none' })
            return
        }

        const filePath = res.tempFilePath

        // #ifdef H5
        const ua = navigator.userAgent.toLowerCase()
        const isAndroid = ua.indexOf('android') > -1 || ua.indexOf('adr') > -1
        const isWeChat = ua.indexOf('micromessenger') !== -1
        if (isWeChat && isAndroid) {
            uni.showModal({
                title: '提示',
                content: '微信环境下不支持直接打开,请复制链接后在浏览器打开',
                confirmText: '复制链接',
                showCancel: false,
                success: function (r) {
                    if (r.confirm) {
                        uni.setClipboardData({
                            data: url,
                            success: function () { uni.showToast({ title: '链接已复制', icon: 'success' }) }
                        })
                    }
                }
            })
        } else {
            const win = window.open(filePath, '_blank')
            if (!win) {
                const link = document.createElement('a')
                link.href = filePath
                link.download = fileName || 'download'
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link)
            }
        }
        // #endif

        // #ifndef H5
        const ext = fileName && String(fileName).includes('.') ? String(fileName).split('.').pop().toLowerCase() : 'pdf'
        uni.openDocument({
            filePath,
            fileType: ext || 'pdf',
            showMenu: true,
            fail: function () {
                uni.showToast({ title: '无法打开此文件', icon: 'none' })
            }
        })
        // #endif
    } catch (e) {
        uni.showToast({ title: '下载出错', icon: 'none' })
    } finally {
        uni.hideLoading()
    }
}