# 鸿蒙应用开发手册
- 如何接入鸿蒙版 openSDK 可参考鸿蒙应用微信登录接入指南,App 中在集成微信 SDK 后,可调用接口实现微信分享的功能
- 当前仅支持分享文字和图片信息到聊天窗
# WXMediaMessage (微信媒体消息内容)说明
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
title | string | 消息标题 | 限制长度不超过 512Bytes |
description | string | 消息描述 | 限制长度不超过 1KB |
thumbData | Uint8Array | 消息略缩图数据 | 大小不能超过 64K |
mediaObject | IMediaObject | 消息对象 | 用于描述一个媒体对象的接口,媒体对象包括: WXTextObject、WXImageObject、WXWebpageObject、WXMiniProgramObject |
# SendMessageToWX.Req(SendMessageToWX请求类)
分享的目标场景,目前仅支持分享到会话
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
message | WXMediaMessage | 发送消息的多媒体内容 | |
scene | number | 发送的目标场景 | 分享到对话: SendMessageToWX.Req.WXSceneSession |
# 示例
一、文字类型分享示例
WXTextObject:多媒体消息中包含的文本数据对象
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
text | string | 文本数据 | 长度需大于 0 且不超过 10KB |
文字类型分享demo
let textObject = new wxopensdk.WXTextObject
textObject.text = "分享的内容"
let mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = textObject
let req = new wxopensdk.SendMessageToWXReq()
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
this.wxApi.sendReq(getContext(this) as common.UIAbilityContext, req
二、图片类型分享示例
WXImageObject:多媒体消息中包含的图片数据对象
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
uri | string | 图片本地路径的uri | 支持jpeg/png类型的图片 |
imageData | string | 图片二进制数据的base64字符串 | 系统跳转限制大小不能超过100KB,uri和imageData同时存在时会优先使用uri字段,忽略imag |
使用uri发送图片
let imageObject = new wxopensdk.WXImageObject
imageObject.uri = fileUri.getUriFromPath(filePath);
let mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = imageObject
let req = new wxopensdk.SendMessageToWXReq()
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
this.wxApi.sendReq(getContext(this) as common.UIAbilityContext, req)
使用imageBase64发送图片
let imageObject = new wxopensdk.WXImageObject
let buf: buffer.Buffer = buffer.from(data);
imageObject.imageData = buf.toString('base64', 0, buf.length);
let mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = imageObject
let req = new wxopensdk.SendMessageToWXReq()
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
this.wxApi.sendReq(getContext(this) as common.UIAbilityContext, req)
三、网页类型分享示例
WXWebpageObject:多媒体消息中包含的网页数据对象
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
webpageUrl | string | 网页链接 | 长度需大于 0 且不超过 10KB |
const webpageObject = new wxopensdk.WXWebpageObject()
webpageObject.webpageUrl = "http://www.qq.com"
const mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = webpageObject
mediaMessage.title = "测试网页链接"
mediaMessage.description = "测试网页摘要"
const thumbData = await getContext(this).resourceManager.getMediaContent($r("app.media.thumb_img"))
const thumbPixel = image.createImageSource(thumbData.buffer).createPixelMapSync()
const thumbBuffer = await image.createImagePacker().packToData(thumbPixel, { format: "image/png", quality: 100 })
mediaMessage.thumbData = new Uint8Array(thumbBuffer)
const req = new wxopensdk.SendMessageToWXReq()
req.callbackAbility = kDemoEntryAbility
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
this.wxApi.sendReq(getContext(this) as common.UIAbilityContext, req)
四、小程序类型分享示例
WXMiniProgramObject:多媒体消息中包含的小程序数据对象
字段 | 类型 | 含义 | 备注 |
---|---|---|---|
userName | string | 小程序的原始 id | 小程序原始 ID 获取方法:登录小程序管理后台-设置-基本设置-账号信息 |
path | string | 小程序的 path | 小程序页面路径 |
miniprogramType | int | 小程序的类型, 默认正式版 | 正式版: WXMiniProgramType.RELEASE; 测试版: WXMiniProgramType.PREVIEW; 预览版: WXMiniProgramType.PREVIEW |
withShareTicket | boolean | 是否使用带 shareTicket 的分享 | 通常开发者希望分享出去的小程序被二次打开时可以获取到更多信息,例如群的标识,可以设置 withShareTicket 为 true,当分享卡片在群聊中被其他用户打开时,可以获取到 shareTicket,用于获取更多分享信息。详见小程序获取更多分享信息 |
const miniProgramObject = new wxopensdk.WXMiniProgramObject()
miniProgramObject.userName = "gh_ac032d0848a9"
miniProgramObject.path = "pages/Home/Home"
miniProgramObject.miniprogramType = wxopensdk.WXMiniProgramType.RELEASE
const mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = miniProgramObject
mediaMessage.title = "测试分享小程序Title"
mediaMessage.description = "分享小程序描述信息"
const thumbData = await getContext(this).resourceManager.getMediaContent($r("app.media.thumb_img2"))
const thumbPixel = image.createImageSource(thumbData.buffer).createPixelMapSync()
const thumbBuffer = await image.createImagePacker().packToData(thumbPixel, { format: "image/png", quality: 100 })
mediaMessage.thumbData = new Uint8Array(thumbBuffer)
const req = new wxopensdk.SendMessageToWXReq()
req.callbackAbility = kDemoEntryAbility
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
this.wxApi.sendReq(getContext(this) as common.UIAbilityContext, req)