查看: 165|回复: 3

鸿蒙平台 uni-app x 系统分享本地路径限制与错误码排障开发实践

[复制链接]
发表于 1 小时前 | 显示全部楼层 |阅读模式
在鸿蒙平台使用 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。示例保留原文的错误码判断:
  1. uni.shareWithSystem({
  2.   type: 'text',
  3.   summary: '今天学到了 uni-app x 的系统分享功能',
  4.   success: () => {
  5.     console.log('分享成功')
  6.   },
  7.   fail: (err) => {
  8.     if (err.errCode == 1310600) {
  9.       console.log('用户取消分享')
  10.     } else if (err.errCode == 1310601) {
  11.       console.log('分享内容不能为空')
  12.     } else {
  13.       console.log('分享失败:', err.errCode, err.errMsg)
  14.     }
  15.   }
  16. })
复制代码

链接分享示例:
  1. uni.shareWithSystem({
  2.   type: 'text',
  3.   href: 'https://uniapp.dcloud.net.cn',
  4.   summary: '推荐 uni-app x,一套代码多端运行',
  5.   success: () => {
  6.     console.log('分享成功')
  7.   },
  8.   fail: (err) => {
  9.     if (err.errCode == 1310604) {
  10.       console.log('无效的链接')
  11.     }
  12.   }
  13. })
复制代码

图片、视频、音频与文件分享
单图使用 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,排障时优先检查路径是否存在、是否为本地路径、类型是否匹配。

单图分享:
  1. uni.shareWithSystem({
  2.   type: 'image',
  3.   imageUrl: '/static/test-image.jpg',
  4.   success: () => {
  5.     console.log('分享成功')
  6.   },
  7.   fail: (err) => {
  8.     if (err.errCode == 1310603) {
  9.       console.log('图片路径无效')
  10.     }
  11.   }
  12. })
复制代码

多图分享:
  1. uni.shareWithSystem({
  2.   type: 'image',
  3.   imagePaths: ['/static/image1.jpg', '/static/image2.jpg'],
  4.   success: () => {
  5.     console.log('分享成功')
  6.   },
  7.   fail: (err) => {
  8.     console.log('分享失败:', err.errCode, err.errMsg)
  9.   }
  10. })
复制代码

文件分享:
  1. uni.shareWithSystem({
  2.   type: 'file',
  3.   filePaths: ['/static/documents/report.pdf'],
  4.   success: () => {
  5.     console.log('分享成功')
  6.   },
  7.   fail: (err) => {
  8.     if (err.errCode == 1310606) {
  9.       console.log('文件不存在')
  10.     }
  11.   }
  12. })
复制代码

组合分享与先选择后分享
图文加链接可把 type 设为 image,同时传 summary、href、imageUrl。先选择再分享时,uni.chooseImage 返回的 tempFilePaths 可直接放进 imagePaths;uni.chooseVideo 返回的 tempFilePath 可放进 videoPaths。截图分享则先对目标节点调用 takeSnapshot,成功回调里的 tempFilePath 作为 imageUrl 再调用 shareWithSystem。这样不必额外配置文件选择或截图分享 SDK。

组合分享:
  1. uni.shareWithSystem({
  2.   type: 'image',
  3.   summary: '推荐 uni-app x,一套代码多端运行',
  4.   href: 'https://uniapp.dcloud.net.cn',
  5.   imageUrl: '/static/logo.png',
  6.   success: () => {
  7.     console.log('分享成功')
  8.   },
  9.   fail: (err) => {
  10.     console.log('分享失败:', err.errCode, err.errMsg)
  11.   }
  12. })
复制代码

选择图片后分享:
  1. uni.chooseImage({
  2.   count: 3,
  3.   sourceType: ['camera', 'album'],
  4.   success: (res) => {
  5.     uni.shareWithSystem({
  6.       type: 'image',
  7.       imagePaths: res.tempFilePaths,
  8.       success: () => {
  9.         console.log('分享成功')
  10.       },
  11.       fail: (err) => {
  12.         console.log('分享失败:', err.errCode, err.errMsg)
  13.       }
  14.     })
  15.   }
  16. })
复制代码

截图后分享:
  1. uni.getElementById('viewshot')?.takeSnapshot({
  2.   success: (res) => {
  3.     uni.shareWithSystem({
  4.       type: 'image',
  5.       imageUrl: res.tempFilePath,
  6.       success: () => {
  7.         console.log('截图分享成功')
  8.       },
  9.       fail: (err) => {
  10.         console.log('分享失败:', err.errCode, err.errMsg)
  11.       }
  12.     })
  13.   },
  14.   fail: (err) => {
  15.     console.log('截图失败:', err)
  16.   }
  17. })
复制代码

落地建议
在鸿蒙端接入时,先把分享资源统一转成本地路径,再按类型组装 ShareWithSystemOptions;fail 回调中按错误码区分用户取消、内容为空、链接无效和各类型路径无效。若业务必须分享到微信朋友圈、小程序或依赖微信 SDK 的定制行为,仍应使用 uni.share;如果只是把文本、链接、图片、视频、音频、文件交给系统面板,shareWithSystem 的配置更少,兼容性和用户选择面更符合通用分享需求。
回复

使用道具 举报

发表于 1 小时前 | 显示全部楼层

Re: 鸿蒙平台 uni-app x 系统分享本地路径限制与错误码排障开发实践

这篇整理挺实用的,把 `uni.share` 和 `uni.shareWithSystem` 的定位区分得很清楚,也把系统分享不需要第三方 key、但文件参数仅支持本地路径这个关键限制点出来了。错误码放在 fail 分支里单独判断这一点很重要,尤其是 1310600 用户取消不该和真正失败混在一起提示,实际排障时能少很多误判。 文本、链接、图片、视频、音频、文件的参数对应关系也写得很直观,组合分享和先选择后分享的思路对落地很有帮助。后续如果补充的话,感觉可以再讲讲多图或多文件数组在鸿蒙上的实际数量、大小限制,以及路径不存在时除了错误码还有没有更明确的日志建议;不过就首帖内容看,已经是一篇比较完整的开发实践总结了。
回复 支持 反对

使用道具 举报

发表于 1 小时前 | 显示全部楼层

Re: 鸿蒙平台 uni-app x 系统分享本地路径限制与错误码排障开发实践

感谢楼主整理,内容很实用。我之前对 uni.share 和 uni.shareWithSystem 的区别有点模糊,这篇把微信 SDK 和系统分享面板的边界讲清楚了。几个关键点印象比较深:文件路径只支持本地路径,网络图片要先下载;错误码要在 fail 里单独判断,尤其 1310600 用户取消不要当成失败提示;图文加链接组合分享,以及 chooseImage、chooseVideo、takeSnapshot 后直接拿临时路径再分享的链路也很顺手。排障时按路径是否存在、是否本地、类型是否匹配来查,思路很清晰。收藏了,后面鸿蒙端做分享时按这个实践来。
回复 支持 反对

使用道具 举报

发表于 1 小时前 | 显示全部楼层

Re: 鸿蒙平台 uni-app x 系统分享本地路径限制与错误码排障开发实践

这篇把 uni.share 和 uni.shareWithSystem 的区别讲得很清楚,尤其是系统分享不依赖第三方 SDK 的 key,以及文件路径参数只支持本地路径这个限制,实际开发中很容易踩坑。错误码在 fail 分支里单独判断也很实用,像 1310600 用户取消不应该当成真正失败来提示,建议按首帖说的先判断 err.errCode 再决定 Toast 或日志。排障时优先检查路径是否存在、是否为本地路径、类型是否匹配,网络图片先下载到本地再分享;先选择再分享和截图后分享的思路也很顺手。感谢整理,收藏备用。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

指导单位

江苏省公安厅

江苏省通信管理局

浙江省台州刑侦支队

DEFCON GROUP 86025

Hacking Group 021A

旗下站点

态势感知中心

应急响应中心

红盟安全

联系我们

官方QQ群:112851260

官方邮箱:security#ihonker.org(#改成@)

官方核心成员

关注微信公众号

Archiver|手机版|小黑屋| ( 沪ICP备2021026908号 )

GMT+8, 2026-10-30 04:44 , Processed in 0.024342 second(s), 28 queries , Gzip On.

Powered by ihonker.com

Copyright © 2015-现在.

  • 返回顶部