weibo_har鴻蒙微博分享,單例二次封裝,鴻蒙微博
HarmonyOS 5.0.3 Beta2 SDK,原樣包含OpenHarmony SDK Ohos_sdk_public 5.0.3.131 (API Version 15 Beta2)
🏆簡介
@zyl/weibo_har是微博封裝使用,支持原生core使用
🌞下載安裝
ohpm install @zyl/weibo_har // 微博分享類ohpm i @zyl/commonlibhar // 可搭配使用操作數據,如網絡圖片->沙箱路徑->微博分享類
效果如下
圖片
視頻
🙏使用說明
前期準備
有關資料可參考
sdk接入流程
官方開發文檔
產物,APP_KEY_WB,REDIRECT_URL_WB,SCOPE_WB
(1)module.json5 配置文件修改
// module.json5 的"module"節點下配置 querySchemes
"querySchemes": ["sinaweibo",
]
(1)EntryAbility.ets 配置文件修改
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {// 初始化微博sdkShareWeiBoSdk.getInstance().initSdkCreate(new AuthInfo(this.context, APP_KEY_WB, // 申請REDIRECT_URL_WB,// 申請SCOPE_WB// 申請))// 接受回調的ShareWeiBoSdk.mWBAPI?.doWBAsResult(want,this.context);
}
//....onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {// 接受回調的ShareWeiBoSdk.mWBAPI?.doWBAsResult(want,this.context);
}
微博是否安裝
checkWeiBoApp()
const flag = checkWeiBoApp() // 布爾值
微博分享
處理數據可查看har包,如網絡圖片存沙箱路徑,base63等點擊跳轉
doWeiboShare
- @param text 文本
- @param string[] // 沙箱路徑
- @returns Promise<WeiboShareCallBack>
import { doWeiboShare, ShareWeiBoSdk, WeiboShareCallBack } from "@zyl/weibo_har";doWeiboShare(text, [this.pixmapUri])
分享類完整分享代碼
由于限制,分享了視頻就覆蓋了圖片,切視頻只能傳一組。
await doWeiboShare(‘分享的文字’, strs) // 分享圖片
await doWeiboShare(‘分享的文字’, [],[{videoPath: videoPath, coverPath: strs[0]}]) // 分享視頻
ohpm i @zyl/commonlibhar
ohpm install @zyl/weibo_har
//.......
import { doWeiboShare, ShareWeiBoSdk, WeiboShareCallBack } from '@zyl/weibo_har';
import { downloadImageWithUrlsToFile, downloadVideoWithFiles } from '@zyl/commonlibhar';
// .......Button('微博分享').onClick(async () => {try {const strs: string[] = await downloadImageWithUrlsToFile(["https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1","https://i-blog.csdnimg.cn/direct/55a24ef3c6e1481286dc46b452316d4f.png"])const videoPath = await downloadVideoWithFiles('http://47.122.120.244/dist/video.MP4')console.log('doWeiboShare1doWeiboSharethenpixmapUri1',videoPath)// await doWeiboShare('分享的文字', strs) // 分享圖片// await doWeiboShare('分享的文字', [],[{videoPath: videoPath, coverPath: strs[0]}]) // 分享視頻const res: WeiboShareCallBack<string> = await doWeiboShare('分享的文字', strs,[{videoPath: videoPath,coverPath: strs[0] // 視頻封面} ])if (res.code === 200) {//成功,拿到數據//登錄成功調用用戶數據的接口} else if (res.code === 499) {// 用戶取消} else if (res.code === 400) {// 登錄失敗}}catch (e) {console.log('eeeeeee',e)}})
微博登錄(web登錄,授權登錄)
微博登錄(web登錄)
import { Oauth2AccessToken,UiError,WbASListener,ShareWeiBoSdk } from '@zyl/weibo_har';let listener: WbASListener = {onComplete: (token: Oauth2AccessToken) => {console.log('微博授權成功',token)},onError: (error: UiError) => {console.log('微博授權出錯',error)},onCancel: () => {console.log('微博授權取消')}};
if (ShareWeiBoSdk.mWBAPI != null) {ShareWeiBoSdk.mWBAPI?.authorizeWeb(listener);
}
微博登錄(客戶端授權登錄)
import { Oauth2AccessToken,UiError,WbASListener,ShareWeiBoSdk } from '@zyl/weibo_har';const contexts: common.UIAbilityContext = getContext(this) as common.UIAbilityContextButton('微博登錄(客戶端授權登錄)').onClick(()=>{let listener: WbASListener = {onComplete: (token: Oauth2AccessToken) => {// 微博授權成功},onError: (error: UiError) => {//微博授權出錯},onCancel: () => {//微博授權取消}};ShareWeiBoSdk.mWBAPI?.authorizeClient(contexts, listener);})
原生方法便于擴展
ShareWeiBoSdk.mWBAPI