Prerequisite: Already import WeChat SDK
Why share content through WeChat?
Sharing content through WeChat has significant benefits in various aspects, and the following is a detailed analysis from the perspective of dissemination effect:
- Wide coverage: WeChat has a huge user base, with over 1.3 billion monthly active users as of now. When you share content on WeChat, your friends and their friends (through forwarding) may see it, which can quickly spread the information to a large potential audience. For example, after a high-quality science popularization article is shared on WeChat Moments, it may receive tens or even hundreds of thousands of views in a short period of time through multi-level forwarding, greatly expanding the scope of content dissemination.
- Accurate touch: WeChat is a platform established based on social relations, and the shared content will be pushed to your WeChat friends and official account followers in priority. These people have a certain social connection or interest resonance with you, and have a relatively high acceptance of sharing content. For example, if you are a photography enthusiast and share your photography skills and works in a WeChat group, the photography enthusiasts in the group will be more interested and able to reach the target audience more accurately.
- Fast dissemination speed: WeChat's information dissemination has immediacy, and once content is shared, friends can immediately see it. Moreover, through channels such as Moments and WeChat groups, content can be widely shared and spread in a short period of time, creating a viral effect. For example, an interesting and humorous video shared on WeChat may be forwarded to multiple groups and Moments within minutes, quickly spreading on the internet.
The following is a practical example of the HarmonyOS project code shared through WeChat:
Add WeixinUtel
import { GlobalKey, GlobalUtil, Logger } from "@heduohao/bases";
import { bundleManager } from "@kit.AbilityKit";
import { BusinessError } from "@kit.BasicServicesKit";
import { WXApi } from "../sdk/weixin/WXApi";
import * as wxopensdk from '@tencent/wechat_open_sdk';
import { common } from "@kit.AbilityKit";
import { image } from "@kit.ImageKit";
export class WeixinUtil {
/**
* 判断是否已安装微信
* @returns
*/
static isInstalled() {
const isInstalled = WXApi.isWXAppInstalled()
return isInstalled
}
/**
* 判断是否已安装微信
*/
static isInstalled_old() {
try {
let canOpen = bundleManager.canOpenLink('weixin://');
Logger.info(`WeixinUtil.IsInstalled = ${JSON.stringify(canOpen)}`);
return true
} catch (err) {
let message = (err as BusinessError).message;
Logger.error(`WeixinUtil.IsInstalled failed, err = ${message}`);
return false
}
}
/**
* 分享文字
* @param text
*/
static shareText(text: string) {
let textObject = new wxopensdk.WXTextObject()
textObject.text = text
let mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = textObject
let req = new wxopensdk.SendMessageToWXReq()
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
//获取UIAbilityContext
const context = GlobalUtil.getObject(GlobalKey.UIAbilityContext) as common.UIAbilityContext;
WXApi.sendReq(context, req)
}
/**
* 分享网页内容
* @param url
* @param title
* @param description
* @param callbackAbility 微信跳回宿主App时拉起的ability名字,如果不填则默认是'EntryAbility'
*/
static async shareWeb(url: string, title: string, description: string, callbackAbility: string = 'EntryAbility') {
const webpageObject = new wxopensdk.WXWebpageObject()
webpageObject.webpageUrl = url
const mediaMessage = new wxopensdk.WXMediaMessage()
mediaMessage.mediaObject = webpageObject
mediaMessage.title = title
mediaMessage.description = description
//获取UIAbilityContext
const context = GlobalUtil.getObject(GlobalKey.UIAbilityContext) as common.UIAbilityContext;
const thumbData = await context.resourceManager.getMediaContent($rawfile('[bases].drawable-xxhdpi/ic_logo.png'))
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 = callbackAbility
req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
req.message = mediaMessage
WXApi.sendReq(context, req)
}
}
Top comments (0)