在鸿蒙平台使用 uni-app x 做分享时,先要区分两类能力:uni.share 主要对接微信 SDK,需要配置 APPID;uni.shareWithSystem 调用系统原生分享面板,不需要第三方 SDK 的 key。手机上能接收分享的应用会出现在列表中,微信、QQ、微博、钉钉、飞书等都可由用户选择。原文的兼容性结论是:Web 不支持,微信小程序不支持,鸿蒙 4.61 开始支持。因此它更适合不要求定制微信朋友圈、小程序分享的通用分享场景。
API 与参数要点
调用签名是 uni.shareWithSystem(options: ShareWithSystemOptions)。核心参数按分享类型区分:text 使用 summary,链接使用 href;image 使用 imageUrl 或 imagePaths;video 使用 videoPaths;audio 使用 audioPaths;file 使用 filePaths。成功和失败分别走 success、fail 回调。最关键的限制是:所有文件路径参数仅支持本地路径,不支持网络 URL。要分享网络图片,需要先下载到本地,再调用系统分享。
错误码要在 fail 分支中单独处理
原文给出的错误码可用于排障:1310600 表示用户取消分享;1310601 表示分享内容不能为空;1310603 表示图片路径无效;1310604 表示无效链接;1310605 表示 video 路径无效;1310606 表示文件不存在;1310607 表示 audio 路径无效。用户取消不应和真正失败混在一起提示,建议在 fail 中先判断 err.errCode,再决定 Toast 文案或日志。
文本与链接分享
文本分享可直接使用 summary;链接分享仍以 type: 'text' 调用,并传入 href。示例保留原文的错误码判断:- uni.shareWithSystem({
- type: 'text',
- summary: '今天学到了 uni-app x 的系统分享功能',
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- if (err.errCode == 1310600) {
- console.log('用户取消分享')
- } else if (err.errCode == 1310601) {
- console.log('分享内容不能为空')
- } else {
- console.log('分享失败:', err.errCode, err.errMsg)
- }
- }
- })
复制代码
链接分享示例:- uni.shareWithSystem({
- type: 'text',
- href: 'https://uniapp.dcloud.net.cn',
- summary: '推荐 uni-app x,一套代码多端运行',
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- if (err.errCode == 1310604) {
- console.log('无效的链接')
- }
- }
- })
复制代码
图片、视频、音频与文件分享
单图使用 imageUrl,多图使用 imagePaths;视频使用 videoPaths;音频使用 audioPaths;文件使用 filePaths。它们都要求本地路径。原文示例中单图路径为 /static/test-image.jpg,视频为 /static/test-video/demo.mp4,音频为 /static/test-audio/music.mp3,文件为 /static/documents/report.pdf。多文件可把多个本地路径放进 filePaths 数组。对应错误码分别是 1310603、1310605、1310607、1310606,排障时优先检查路径是否存在、是否为本地路径、类型是否匹配。
单图分享:- uni.shareWithSystem({
- type: 'image',
- imageUrl: '/static/test-image.jpg',
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- if (err.errCode == 1310603) {
- console.log('图片路径无效')
- }
- }
- })
复制代码
多图分享:- uni.shareWithSystem({
- type: 'image',
- imagePaths: ['/static/image1.jpg', '/static/image2.jpg'],
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- console.log('分享失败:', err.errCode, err.errMsg)
- }
- })
复制代码
文件分享:- uni.shareWithSystem({
- type: 'file',
- filePaths: ['/static/documents/report.pdf'],
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- if (err.errCode == 1310606) {
- console.log('文件不存在')
- }
- }
- })
复制代码
组合分享与先选择后分享
图文加链接可把 type 设为 image,同时传 summary、href、imageUrl。先选择再分享时,uni.chooseImage 返回的 tempFilePaths 可直接放进 imagePaths;uni.chooseVideo 返回的 tempFilePath 可放进 videoPaths。截图分享则先对目标节点调用 takeSnapshot,成功回调里的 tempFilePath 作为 imageUrl 再调用 shareWithSystem。这样不必额外配置文件选择或截图分享 SDK。
组合分享:- uni.shareWithSystem({
- type: 'image',
- summary: '推荐 uni-app x,一套代码多端运行',
- href: 'https://uniapp.dcloud.net.cn',
- imageUrl: '/static/logo.png',
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- console.log('分享失败:', err.errCode, err.errMsg)
- }
- })
复制代码
选择图片后分享:- uni.chooseImage({
- count: 3,
- sourceType: ['camera', 'album'],
- success: (res) => {
- uni.shareWithSystem({
- type: 'image',
- imagePaths: res.tempFilePaths,
- success: () => {
- console.log('分享成功')
- },
- fail: (err) => {
- console.log('分享失败:', err.errCode, err.errMsg)
- }
- })
- }
- })
复制代码
截图后分享:- uni.getElementById('viewshot')?.takeSnapshot({
- success: (res) => {
- uni.shareWithSystem({
- type: 'image',
- imageUrl: res.tempFilePath,
- success: () => {
- console.log('截图分享成功')
- },
- fail: (err) => {
- console.log('分享失败:', err.errCode, err.errMsg)
- }
- })
- },
- fail: (err) => {
- console.log('截图失败:', err)
- }
- })
复制代码
落地建议
在鸿蒙端接入时,先把分享资源统一转成本地路径,再按类型组装 ShareWithSystemOptions;fail 回调中按错误码区分用户取消、内容为空、链接无效和各类型路径无效。若业务必须分享到微信朋友圈、小程序或依赖微信 SDK 的定制行为,仍应使用 uni.share;如果只是把文本、链接、图片、视频、音频、文件交给系统面板,shareWithSystem 的配置更少,兼容性和用户选择面更符合通用分享需求。 |