continuous-integration/drone/tag Build is passing
鸿蒙微信 ArkWeb 下 H5 window.open 常静默失败,仅出现「开始下载…」。 OA/市场附件在小程序 WebView 内改为跳转 pages/oa/file-preview, 用 uni.downloadFile + openDocument 打开,浏览器环境仍用预签名直链。 Refs: #74 Co-authored-by: Cursor <cursoragent@cursor.com>
179 lines
4.8 KiB
JavaScript
179 lines
4.8 KiB
JavaScript
/**
|
|
* 微信 web-view → 小程序附件预览/下载页
|
|
* 对齐 OA 打印 PDF 的宿主跳转;避免 H5 window.open 在鸿蒙 ArkWeb 静默失败。
|
|
*/
|
|
|
|
import {
|
|
getOaPrintHostApis,
|
|
isWeChatMiniWebView,
|
|
printNavLog
|
|
} from '../oa/print/oa-pdf-preview-nav.js'
|
|
|
|
const OPEN_DOCUMENT_EXTS = new Set([
|
|
'pdf',
|
|
'doc',
|
|
'docx',
|
|
'xls',
|
|
'xlsx',
|
|
'ppt',
|
|
'pptx'
|
|
])
|
|
|
|
export function fileExtension(fileName) {
|
|
const match = String(fileName || '')
|
|
.trim()
|
|
.toLowerCase()
|
|
.match(/\.([a-z0-9]+)$/)
|
|
return match ? match[1] : ''
|
|
}
|
|
|
|
/** openDocument 支持的类型;其它扩展返回空串 */
|
|
export function openDocumentFileType(fileName) {
|
|
const ext = fileExtension(fileName)
|
|
return OPEN_DOCUMENT_EXTS.has(ext) ? ext : ''
|
|
}
|
|
|
|
/** query 只用 ASCII 名,避免 navigateTo 被中文撑爆;扩展名保留供宿主识别类型 */
|
|
export function asciiHostFileName(fileName) {
|
|
const ext = fileExtension(fileName) || 'bin'
|
|
return `file.${ext}`
|
|
}
|
|
|
|
/**
|
|
* @returns {{ path: string, queryLen: number, fileUrlLen: number, name: string, fileType: string }}
|
|
*/
|
|
export function buildHostFilePreviewPath({ fileUrl, fileName } = {}) {
|
|
const key = String(fileUrl || '').trim()
|
|
const name = asciiHostFileName(fileName)
|
|
const fileType = openDocumentFileType(name)
|
|
const q =
|
|
'fileUrl=' +
|
|
encodeURIComponent(key) +
|
|
'&f=' +
|
|
encodeURIComponent(key) +
|
|
'&n=' +
|
|
encodeURIComponent(name)
|
|
const path = '/pages/oa/file-preview/index?' + q
|
|
return {
|
|
path,
|
|
queryLen: q.length,
|
|
fileUrlLen: key.length,
|
|
name,
|
|
fileType
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @returns {Promise<{ ok: boolean, via?: string, path?: string, message?: string, assumed?: boolean }>}
|
|
*/
|
|
export function navigateToHostFilePreview({ fileUrl, fileName } = {}, deps = {}) {
|
|
const { path, queryLen, fileUrlLen, name, fileType } = buildHostFilePreviewPath({
|
|
fileUrl,
|
|
fileName
|
|
})
|
|
const win =
|
|
deps.window !== undefined
|
|
? deps.window
|
|
: typeof window !== 'undefined'
|
|
? window
|
|
: undefined
|
|
const log = deps.log || printNavLog
|
|
const { wxApi, uniApi } = getOaPrintHostApis(win)
|
|
|
|
log('file-preview navigate start', {
|
|
fileUrlLen,
|
|
queryLen,
|
|
pathLen: path.length,
|
|
name,
|
|
fileType,
|
|
fileUrlPreview: String(fileUrl || '').slice(0, 120),
|
|
hasWindowWx: !!(win && win.wx),
|
|
hasWxMiniNavigate: !!(wxApi && wxApi.miniProgram && wxApi.miniProgram.navigateTo),
|
|
hasUniWebViewNavigate: !!(uniApi && uniApi.webView && uniApi.webView.navigateTo),
|
|
__wxjs_environment: win && win.__wxjs_environment
|
|
})
|
|
|
|
if (!String(fileUrl || '').trim()) {
|
|
log('file-preview navigate abort: empty fileUrl')
|
|
return Promise.resolve({ ok: false, message: '缺少 fileUrl' })
|
|
}
|
|
|
|
if (path.length > 900) {
|
|
log('file-preview warn path long', { pathLen: path.length })
|
|
}
|
|
|
|
const invokeNavigate = (label, navigateFn) =>
|
|
new Promise((resolve) => {
|
|
if (typeof navigateFn !== 'function') {
|
|
resolve(null)
|
|
return
|
|
}
|
|
let settled = false
|
|
const done = (result) => {
|
|
if (settled) return
|
|
settled = true
|
|
resolve(result)
|
|
}
|
|
try {
|
|
navigateFn({
|
|
url: path,
|
|
success: (res) => {
|
|
log(label + ' success', res)
|
|
done({ ok: true, via: label, path })
|
|
},
|
|
fail: (err) => {
|
|
log(label + ' fail', err)
|
|
done({
|
|
ok: false,
|
|
via: label,
|
|
path,
|
|
message: (err && (err.errMsg || err.message)) || 'navigateTo fail'
|
|
})
|
|
}
|
|
})
|
|
setTimeout(() => {
|
|
if (!settled) {
|
|
log(label + ' no-callback → assume ok', { pathLen: path.length })
|
|
done({ ok: true, via: label, path, assumed: true })
|
|
}
|
|
}, 80)
|
|
} catch (e) {
|
|
log(label + ' throw', String(e && e.message ? e.message : e))
|
|
done({
|
|
ok: false,
|
|
via: label,
|
|
path,
|
|
message: String(e && e.message ? e.message : e)
|
|
})
|
|
}
|
|
})
|
|
|
|
return invokeNavigate(
|
|
'wx.miniProgram.navigateTo',
|
|
wxApi && wxApi.miniProgram && wxApi.miniProgram.navigateTo
|
|
? wxApi.miniProgram.navigateTo.bind(wxApi.miniProgram)
|
|
: null
|
|
).then((wxRes) => {
|
|
if (wxRes && wxRes.ok) return wxRes
|
|
if (wxRes && wxRes.ok === false) {
|
|
log('file-preview wx failed, try uni', wxRes.message)
|
|
}
|
|
return invokeNavigate(
|
|
'uni.webView.navigateTo',
|
|
uniApi && uniApi.webView && uniApi.webView.navigateTo
|
|
? uniApi.webView.navigateTo.bind(uniApi.webView)
|
|
: null
|
|
).then((uniRes) => {
|
|
if (uniRes) return uniRes
|
|
if (wxRes) return wxRes
|
|
log('file-preview navigate unavailable')
|
|
return {
|
|
ok: false,
|
|
message: '无法跳转附件预览页:未找到 wx/uni navigateTo'
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export { isWeChatMiniWebView }
|