管理中心
API参考快应用API第三方平台第三方分享

第三方分享

注意

该功能依赖第三方SDK提供服务,第三方SDK可能会收集用户的个人数据。应用中请务必在使用该功能前,告知用户将搜集个人数据,并引导用户阅读相应的隐私政策。

在接入 第三方分享 前,请了解以下关键点:

  1. 支持微信分享,暂时无法获取分享结果。
  2. 推荐使用图文分享类型,其他类型个别平台无法支持,将使用图文类型分享。
  3. 不同厂商引擎的处理会有不同,使用华为引擎请参见华为文档接入。

接口声明

在manifest.json文件的 features属性中增加如下配置。

{
    "name": "service.share"
    "params":{
    }
}

导入模块

在调用接口页面的<script>部分增加如下配置。

import share from '@service.share'

或

var share = require("@service.share")

使用限制

展开

限制条件

说明

适用终端

手机、平板

适用区域

中国大陆

接口定义

展开

接口

描述

share.getProvider(OBJECT)

获取服务提供商,如厂商的英文品牌名称。

share.share(OBJECT)

分享内容。

share.getAvailablePlatforms(OBJECT)

获取当前可用的支持分享的平台列表。

share.getProvider(OBJECT)

描述

获取服务提供商,如厂商的英文品牌名称。

返回值

字符串,服务提供商的代号,如厂商的英文品牌名称,如果无此服务则返回空字符串。返回空字符串时,请勿调用该服务其他接口,否则将抛出异常。

示例代码

share.getProvider();

share.share(OBJECT)

描述

分享内容。

OBJECT参数

展开

参数

类型

是否必填

说明

shareType

int

是

分享类型。

  • 默认图文0
  • 纯文字1
  • 纯图片2
  • 音频3
  • 视频4

title

string

分享类型为0,1,3,4时必填

分享的标题。

summary

string

否

分享的摘要。

targetUrl

string

分享类型为0,3,4时必填

点击后的跳转URL。

说明

URL是否能正常打开依赖被分享的第三方平台的能力。

imagePath

string

分享类型为2,3,4时必填

分享图片/缩略图的本地地址或网络地址。

当分享类型为0时,imagePath可以选填,如果填写则为分享图片的本地路径,不填的话,则分享后没有图片。

微信分享图片时,图片大小不能超过32KB。

mediaUrl

string

分享类型为3,4时必填

分享的音乐/视频数据URL。

platforms

array

否

分享到的平台,不填则默认分享所有平台。

可用值包括:WEIXIN(微信好友)、WEIXIN_CIRCLE(微信朋友圈)、SYSTEM(系统分享)。

success

function

否

成功回调。

fail

function

否

失败回调。

cancel

function

否

取消回调。

示例代码

share.share({
    shareType:0,
    title:"我是标题",
    summary:"我是摘要",
    imagePath:"xxx/xxx/xxx/share.jpg",
    targetUrl:"https://www.example.com",
    success:function() {
        console.log(“message”);
    },
    fail: function(data, code) {
        console.log("handling fail, code=" + code);
    }
})

share.getAvailablePlatforms(OBJECT)(1030+)

描述

获取当前可用的支持分享的平台列表。

OBJECT参数

展开

参数

类型

是否必填

说明

success

function

否

成功回调。

fail

function

否

失败回调。

cancel

function

否

取消回调。

success返回值:

展开

参数

类型

说明

platforms

array

当前可用的支持分享的平台列表,可用值包括:WEIXIN、WEIXIN_CIRCLE(微信朋友圈)、SYSTEM(系统分享)。

示例代码

share.getAvailablePlatforms({
    success:function(data) {
        For(const i in data.platforms) {
             console.log(“platforms: ” + data.platforms[i]);
        }
    },
    Fail:function(data, code) {
         console.log(“handling fail, code = ” + code);
    }
})

Demo

<template>
    <div>
        <div>
            <text>{{interface}}</text>
        </div>
        <div class="btn-wrap-column ">
        <input type="button" value="设置分享的SD卡图片" onclick="pickSDPhoto" />
            <input type="button" value="设置分享的应用内部图片" onclick="pickDataPhoto" />
            <input type="button" value="设置分享的网络图片" onclick="pickNetPhoto" />
            <input type="button" value="服务提供商" onclick="getProvider" />
            <input type="button" value="可分享平台列表" onclick="getAvailablePlatforms" />
            <input type="button" value="微信好友分享" onclick="wxFriendShare" />
            <input type="button" value="微信朋友圈分享" onclick="wxCircleShare" />
            <input type="button" value="系统分享" onclick="systemShare" />
            <input type="button" value="多平台分享" onclick="morePlatforms" />
            <input type="button" value="全平台分享" onclick="allPlatforms" />
        </div>
    </div>
</template>
<style>
    @import '../Common/css/common.css';
    .item-container {
        margin-bottom: 50px;
        margin-right: 60px;
        margin-left: 60px;
        justify-content: center;
    }
    .item-title {
        padding-left: 30px;
        padding-bottom: 20px;
    }
    .item-content {
        height: 300px;
        justify-content: center;
    }
    .btn-wrap-column {
        margin-left: 60px;
        flex-direction: column;
    }
</style>
<script>
    import share from '@service.share'
    import media from '@system.media'
    import prompt from '@system.prompt'
    export default {
        data: {
            interface: "第三方分享",
            //默认是本地应用内部图片
            photoUri: '/Common/logo.png'
        },
        onInit() {
            this.$page.setTitleBar({ text: 'service_share' })
        },
        getProvider: function () {
            prompt.showToast({
                message: 'message provider :' + share.getProvider(),
                duration: 0
            })
        },
        getAvailablePlatforms: function () {
            share.getAvailablePlatforms({
                success: function (data) {
                    for (const i in data.platforms) {
                        console.log("share platforms: " + data.platforms[i]);
                    }
                },
                fail: function (data, code) {
                    console.log("handling fail, code = " + code);
                }
            })
        },
        wxFriendShare: function () {
            share.share({
                shareType: 2,
                title: '微信的标题',
                summary: '微信的摘要',
                imagePath: this.photoUri,
                targetUrl: 'https://www.baidu.com/',
                mediaUrl: 'https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4',
                platforms: ["WEIXIN"],
                fail: function (data, code) {
                    prompt.showToast({
                        message: 'message fail message :' + data + " code " + code,
                        duration: 0
                    })
                },
                cancel: function () {
                    prompt.showToast({
                        message: 'message cancel :',
                        duration: 0
                    })
                }
            })
        },
        wxCircleShare: function () {
            share.share({
                shareType: 4,
                title: '微信朋友圈的标题',
                summary: '微信朋友圈的摘要',
                imagePath: this.photoUri,
                targetUrl: 'https://www.baidu.com/',
                mediaUrl: 'https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4',
                platforms: ["WEIXIN_CIRCLE"],
                fail: function (data, code) {
                    prompt.showToast({
                        message: 'message fail message :' + data + " code " + code,
                        duration: 0
                    })
                },
                cancel: function () {
                    prompt.showToast({
                        message: 'message cancel :',
                        duration: 0
                    })
                }
            })
        },        
        systemShare: function () {
            share.share({
                shareType: 4,
                title: '系统的标题',
                summary: '系统的摘要',
                imagePath: this.photoUri,
                targetUrl: 'https://www.baidu.com/',
                mediaUrl: 'https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4',
                platforms: ["SYSTEM"],
                fail: function (data, code) {
                    prompt.showToast({
                        message: 'message fail message :' + data + " code " + code,
                        duration: 0
                    })
                },
                cancel: function () {
                    prompt.showToast({
                        message: 'message cancel :',
                        duration: 0
                    })
                }
            })
        },
        morePlatforms: function () {
            share.share({
                shareType: 4,
                title: '多平台的标题',
                summary: '多平台的摘要',
                imagePath: this.photoUri,
                targetUrl: 'https://www.baidu.com/',
                mediaUrl: 'https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4',
                platforms: ["WEIXIN", "WEIBO", "SYSTEM"],
                fail: function (data, code) {
                    prompt.showToast({
                        message: 'message fail message :' + data + " code " + code,
                        duration: 0
                    })
                },
                cancel: function () {
                    prompt.showToast({
                        message: 'message cancel :',
                        duration: 0
                    })
                }
            })
        },
        allPlatforms: function () {
            share.share({
                shareType: 4,
                title: '全平台的标题',
                summary: '全平台的摘要',
                imagePath: this.photoUri,
                targetUrl: 'https://www.baidu.com/',
                mediaUrl: 'https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4',
                platforms: "",
                fail: function (data, code) {
                    prompt.showToast({
                        message: 'message fail message :' + data + " code " + code,
                        duration: 0
                    })
                },
                cancel: function () {
                    prompt.showToast({
                        message: 'message cancel :',
                        duration: 0
                    })
                }
            })
        },
        pickSDPhoto: function () {
            var that = this;
            media.pickImage({
                success: function (ret) {
                    that.photoUri = ret.uri;
                },
                fail: function (erromsg, errocode) {
                    prompt.showToast({ message: "pickImage fail------" + errocode + ': ' + erromsg })
                    console.log('media.pickImage----------' + errocode + ': ' + erromsg)
                },
                cancel: function () {
                    console.log('media.pickImage----------cancel')
                },
                complete: function () {
                    console.log('media.pickImage----------complete')
                }
            })
        },
        pickDataPhoto: function () {
            var that = this;
            that.photoUri = '/Common/logo.png';
        },
        pickNetPhoto: function () {
            var that = this;
            that.photoUri = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542175176&di=948a7a2a8cec225c82741c773fcd8e64&imgtype=jpg&er=1&src=http%3A%2F%2Fpic9.nipic.com%2F20100813%2F3525627_135840092720_2.jpg';
        }
    }
</script>

版本更新说明

展开

版本

发布日期

描述

1030

2018-10-31

新增getAvailablePlatforms方法,用于获取当前可用的支持分享的平台列表。

相关链接

在 API参考 中进行搜索
请输入您想要搜索的关键词