分享
Android、iOS平台从 HBuilderX5.08 版本开始支持微信分享
| Web | 微信小程序 | Android | iOS | HarmonyOS |
|---|---|---|---|---|
| x | - | 5.08 | 5.08 | 4.81 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | ShareOptions | 是 | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | 错误码 | ||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | |||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | |||||||||||||||||||||||||||||||||||||||||
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1">
<!-- #endif -->
<page-head :title="title"></page-head>
<view class="uni-list-cell-padding status-box">
<view class="uni-title uni-common-mt">
<text class="uni-title-text">分享内容:</text>
</view>
<textarea style="max-height: 100px;padding: 10px;background-color:aliceblue;border: 1px solid black;"
:auto-height=" true" :value="shareContent"></textarea>
</view>
<view class="uni-list-cell-padding status-box">
<view class="uni-title uni-common-mt">
<text class="uni-title-text">分享图片:</text>
</view>
<view style="flex-wrap: wrap;">
<image v-if="imageURL != null" style="width: 104px; height: 104px;" :src="imageURL">
</image>
<image v-else class="uni-uploader__input-box" @tap="chooseImage" src="/static/plus.png"></image>
</view>
</view>
<view class="uni-list-cell-padding status-box">
<view class="uni-title uni-common-mt">
<text class="uni-title-text">分享类型:</text>
</view>
<radio-group class="uni-row" @change="typeChange">
<radio class="uni-common-mt" value="0" :checked="true">
图文
</radio>
<radio class="uni-common-mt" value="1">纯文字</radio>
<radio class="uni-common-mt" value="2">纯图片</radio>
</radio-group>
</view>
<view class="uni-list-cell-padding status-box">
<view class="uni-title uni-common-mt">
<text class="uni-title-text">分享到:</text>
</view>
<radio-group class="uni-row" style="flex-wrap: wrap;" @change="sceneChange">
<radio class="uni-common-mt" value="WXSceneSession" :checked="true">
聊天界面
</radio>
<radio class="uni-common-mt" value="WXSceneTimeline">朋友圈</radio>
<radio class="uni-common-mt" value="WXSceneFavorite" :disabled="true" style="color: gray;">微信收藏(暂不支持)</radio>
</radio-group>
</view>
<view class="uni-padding-wrap uni-common-mt">
<button type="primary" @click="share">分享</button>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup>
const scene = ref('WXSceneSession')
const type = ref(0)
const typeChange = (e : UniRadioGroupChangeEvent) => {
type.value = parseInt(e.detail.value)
}
const sceneChange = (e : UniRadioGroupChangeEvent) => {
scene.value = e.detail.value
}
const title = ref('share')
const shareContent = ref('uni-app x 可以分享内容到微信了!')
const imageURL = ref<string | null>(null)
function chooseImage() {
uni.chooseImage({
count: 1,
success: (res) => {
imageURL.value = res.tempFilePaths[0]
},
fail: (err) => {
console.log("err: ", JSON.stringify(err));
uni.showToast({
title: "choose image error.code:" + err.errCode + ";message:" + err.errMsg,
position: "bottom"
})
}
})
}
function share() {
uni.showLoading({
title: "正在分享......"
})
console.log('scene.value: ', scene.value);
uni.share({
provider: 'weixin',
title: '微信分享',
scene: scene.value,
type: type.value,
href: "https://uniapp.dcloud.net.cn/api/plugins/share.html",
summary: shareContent.value,
imageUrl: imageURL.value,
success(res) {
uni.hideLoading()
uni.showToast({
title: '分享成功'
})
},
fail(e) {
uni.hideLoading()
uni.showToast({
title: e.errMsg
})
}
})
}
</script>
<style>
.uni-uploader__input-box {
margin: 5px;
width: 104px;
height: 104px;
border: 1px solid #D9D9D9;
}
.status-box {
background-color: #FFFFFF;
margin: 0 20px;
}
</style>
UniShareWeixinProvider(微信分享)继承自 UniProvider
| 名称 | 类型 | 必备 | 默认值 | 描述 |
|---|---|---|---|---|
| isWeChatInstalled | boolean | 是 | - | 判断微信是否安装 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |
背景:目前uni-app x引擎已经内置了微信分享。但分享SDK还有很多,比如微博、抖音分享。
以往这些SDK可以通过独立插件的方式集成到uni-app x中,但需要提供单独的API给开发者使用。
uni-app x从4.25起,开放了provider自接入机制,让三方SDK可以以provider方式被开发者集成。
开发一个UTS插件,对接uni规范化的API、错误信息描述等实现自己的分享插件,这样插件使用者就可以通过uni的标准API使用三方SDK。
举个例子,开发者想使用uni.share()的方式调用XX分享,但是内置分享api不支持,
那只需要按照下面四个步骤实现即可:
第一步,新建一个UTS插件,在interface.uts 中定义接口,UniShareProvider,代码如下
export interface UniShareWeixinProvider extends UniShareProvider{}
第二步,在app-android或者app-ios的index.uts中实现接口,代码如下
import { UniShareWeixinProvider } from '../interface.uts'
export class UniShareWeixinProviderImpl implements UniShareWeixinProvider {
override id : String = "XX" // id必须有插件作者前缀,避免冲突,避免不同插件作者的插件id重名
override description : String = "XX的描述"
override isAppExist : boolean | null = null
constructor(){}
override share(options : ShareOptions) {
//todo 具体逻辑,接收uni规范的入参,进行业务处理,返回uni规范的返回值。如遇到错误,按uni的规范返回错误码
}
}
第三步,在manifest.json中配置
"app": {
"distribute": {
/* android打包配置 */
"modules": {
"uni-share":{
"XX":{}
}
}
}
}
第四步,打包自定义基座然后运行
uni_modules__uni_share_weixin.har,放到 项目根目录/harmony-configs/libs/ 目录下重新编译运行到手机。高版本不存在此问题uni.getProvider的方式,详见uni.getProvider uni.getProvider({
service: "share",
success: (e) => {
const provider = e.providers.find((item): boolean => {
return item.id == 'weixin'
})
// #ifdef APP-ANDROID
if (provider != null && provider instanceof UniPaymentWxpayProvider && !((provider as UniPaymentWxpayProvider).isWeChatInstalled)) {
console.log('WeChat 没有安装')
} else {
console.log('WeChat 已安装')
}
// #endif
// #ifdef APP-IOS
if (provider != null && ((provider as UniPaymentWxpayProvider).isWeChatInstalled == undefined || ((provider as UniPaymentWxpayProvider).isWeChatInstalled != null && (provider as UniPaymentWxpayProvider).isWeChatInstalled == false))) {
console.log('WeChat 没有安装')
} else {
console.log('WeChat 已安装')
}
// #endif
},
fail: (e) => {
console.log("获取分享通道失败:", e);
}
})
uni-share节点,详见 https://doc.dcloud.net.cn/uni-app-x/collocation/manifest-modules.html#uni-share模块配置