share.ets 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { UTSHarmony } from '@dcloudio/uni-app-x-runtime';
  2. import { systemShare } from '@kit.ShareKit';
  3. import { uniformTypeDescriptor as utd } from '@kit.ArkData';
  4. import { common } from '@kit.AbilityKit';
  5. import { fileUri } from '@kit.CoreFileKit';
  6. import { BusinessError } from '@kit.BasicServicesKit';
  7. /**
  8. * 分享类型枚举
  9. */
  10. enum ShareType {
  11. TEXT = "text", // 纯文本分享
  12. IMAGE = "image", // 图片分享
  13. VIDEO = "video", // 视频分享
  14. AUDIO = "audio", // 音频分享
  15. FILE = "file", // 文件分享
  16. LINK = "link" // 链接分享
  17. }
  18. /**
  19. * 根据文件路径获取统一数据类型标识符
  20. * @param filePath 文件路径
  21. * @param defaultType 默认数据类型
  22. * @returns 统一数据类型标识符
  23. */
  24. function getUtdTypeByPath(filePath: string, defaultType: string): string {
  25. const ext = filePath?.split('.')?.pop()?.toLowerCase() ?? '';
  26. if (ext === '') {
  27. return defaultType;
  28. }
  29. return utd.getUniformDataTypeByFilenameExtension('.' + ext, defaultType);
  30. }
  31. /**
  32. * 创建图片分享数据
  33. * @param url 图片路径(支持本地路径和网络 URL)
  34. * @param title 分享标题
  35. * @param summary 分享描述
  36. * @returns 分享数据对象
  37. */
  38. function createImageShareData(url: string, title: string, summary: string): systemShare.SharedData | null {
  39. if (url === '') {
  40. return null;
  41. }
  42. const filePath = UTSHarmony.getResourcePath(url);
  43. const utdTypeId = getUtdTypeByPath(filePath, utd.UniformDataType.IMAGE);
  44. return new systemShare.SharedData({
  45. utd: utdTypeId,
  46. uri: fileUri.getUriFromPath(filePath),
  47. title: title,
  48. description: summary,
  49. });
  50. }
  51. /**
  52. * 创建视频分享数据
  53. * @param url 视频路径(支持本地路径和网络 URL)
  54. * @param title 分享标题
  55. * @param summary 分享描述
  56. * @returns 分享数据对象
  57. */
  58. function createVideoShareData(url: string, title: string, summary: string): systemShare.SharedData | null {
  59. if (url === '') {
  60. return null;
  61. }
  62. const filePath = UTSHarmony.getResourcePath(url);
  63. const utdTypeId = getUtdTypeByPath(filePath, utd.UniformDataType.VIDEO);
  64. return new systemShare.SharedData({
  65. utd: utdTypeId,
  66. uri: fileUri.getUriFromPath(filePath),
  67. title: title,
  68. description: summary,
  69. });
  70. }
  71. /**
  72. * 创建音频分享数据
  73. * @param url 音频路径(支持本地路径和网络 URL)
  74. * @param title 分享标题
  75. * @param summary 分享描述
  76. * @returns 分享数据对象
  77. */
  78. function createAudioShareData(url: string, title: string, summary: string): systemShare.SharedData | null {
  79. if (url === '') {
  80. return null;
  81. }
  82. const filePath = UTSHarmony.getResourcePath(url);
  83. const utdTypeId = getUtdTypeByPath(filePath, utd.UniformDataType.AUDIO);
  84. return new systemShare.SharedData({
  85. utd: utdTypeId,
  86. uri: fileUri.getUriFromPath(filePath),
  87. title: title,
  88. description: summary,
  89. });
  90. }
  91. /**
  92. * 创建文件分享数据
  93. * @param filePath 文件路径(支持本地路径和网络 URL)
  94. * @param title 分享标题
  95. * @param summary 分享描述
  96. * @returns 分享数据对象
  97. */
  98. function createFileShareData(filePath: string, title: string, summary: string): systemShare.SharedData | null {
  99. if (filePath === '') {
  100. return null;
  101. }
  102. const resourcePath = UTSHarmony.getResourcePath(filePath);
  103. const ext = resourcePath?.split('.')?.pop()?.toLowerCase() ?? '';
  104. // 根据文件扩展名确定数据类型
  105. let utdType = utd.UniformDataType.FILE;
  106. let utdTypeId = '';
  107. // 支持常见的文件类型
  108. switch (ext) {
  109. case 'zip':
  110. case 'rar':
  111. case '7z':
  112. case 'tar':
  113. case 'gz':
  114. utdType = utd.UniformDataType.ARCHIVE;
  115. break;
  116. case 'pdf':
  117. utdType = utd.UniformDataType.PDF;
  118. break;
  119. case 'doc':
  120. case 'docx':
  121. utdType = utd.UniformDataType.WORD_DOC;
  122. break;
  123. case 'xls':
  124. case 'xlsx':
  125. utdType = utd.UniformDataType.EXCEL;
  126. break;
  127. case 'ppt':
  128. case 'pptx':
  129. utdType = utd.UniformDataType.PPT;
  130. break;
  131. default:
  132. utdType = utd.UniformDataType.FILE;
  133. break;
  134. }
  135. utdTypeId = utd.getUniformDataTypeByFilenameExtension('.' + ext, utdType);
  136. return new systemShare.SharedData({
  137. utd: utdTypeId,
  138. uri: fileUri.getUriFromPath(resourcePath),
  139. title: title,
  140. description: summary,
  141. });
  142. }
  143. /**
  144. * 创建链接分享数据
  145. * @param url 链接地址
  146. * @param title 分享标题
  147. * @param summary 分享描述
  148. * @returns 分享数据对象
  149. */
  150. function createLinkShareData(url: string, title: string, summary: string): systemShare.SharedData {
  151. return new systemShare.SharedData({
  152. utd: utd.UniformDataType.HYPERLINK,
  153. title: title,
  154. content: url,
  155. description: summary
  156. });
  157. }
  158. /**
  159. * 创建文本分享数据
  160. * @param title 分享标题
  161. * @param summary 分享内容
  162. * @returns 分享数据对象
  163. */
  164. function createTextShareData(title: string, summary: string): systemShare.SharedData {
  165. return new systemShare.SharedData({
  166. utd: utd.UniformDataType.TEXT,
  167. title: title,
  168. content: summary
  169. });
  170. }
  171. /**
  172. * 系统分享功能
  173. * @param options 分享参数
  174. * @param options.type 分享类型: text(文本) | image(图片) | video(视频) | audio(音频) | file(文件) | link(链接)
  175. * @param options.title 分享标题
  176. * @param options.summary 分享描述/内容
  177. * @param options.url 资源路径(图片/视频/音频/文件路径或链接地址,支持本地路径和网络 URL)
  178. * @param options.success 成功回调
  179. * @param options.fail 失败回调
  180. */
  181. export function share(type: string, title: string, summary: string, url: string, success: () => void, fail: (error: string) => void): void {
  182. // 获取UI上下文
  183. const uiContext: UIContext = UTSHarmony.getCurrentWindow()?.getUIContext();
  184. const context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
  185. // 根据分享类型创建分享数据
  186. let shareData: systemShare.SharedData | null = null;
  187. let errorMsg = '';
  188. switch (type) {
  189. case ShareType.IMAGE:
  190. shareData = createImageShareData(url, title, summary);
  191. errorMsg = '图片路径不能为空';
  192. break;
  193. case ShareType.VIDEO:
  194. shareData = createVideoShareData(url, title, summary);
  195. errorMsg = '视频路径不能为空';
  196. break;
  197. case ShareType.AUDIO:
  198. shareData = createAudioShareData(url, title, summary);
  199. errorMsg = '音频路径不能为空';
  200. break;
  201. case ShareType.FILE:
  202. shareData = createFileShareData(url, title, summary);
  203. errorMsg = '文件路径不能为空';
  204. break;
  205. case ShareType.LINK:
  206. shareData = createLinkShareData(url, title, summary);
  207. break;
  208. default:
  209. // 默认为文本分享
  210. shareData = createTextShareData(title, summary);
  211. errorMsg = '分享内容不能为空';
  212. break;
  213. }
  214. // 验证分享数据
  215. if (shareData === null) {
  216. fail(errorMsg);
  217. return;
  218. }
  219. // 创建分享控制器
  220. const controller: systemShare.ShareController = new systemShare.ShareController(shareData);
  221. // 显示分享面板,配置分享选项
  222. controller.show(context, {
  223. selectionMode: systemShare.SelectionMode.SINGLE, // 单选模式
  224. previewMode: systemShare.SharePreviewMode.DEFAULT, // 默认预览模式
  225. })
  226. .then(() => {
  227. // 分享成功
  228. success();
  229. })
  230. .catch((error: BusinessError) => {
  231. // 分享失败,返回错误信息
  232. const errorMessage = error?.message ?? '分享失败';
  233. fail(errorMessage);
  234. });
  235. }