codecamp

百度智能小程序 调起分享面板

swan.openShare

解释: 调起分享面板。

Web 态说明:Web 态小程序运行在微信、QQ、QQ 空间、微博、百度 Hi 内时,调用 openShare 会弹出引导浮层引导用户通过平台的分享能力进行分享;在非上述环境时会弹出分享面板提示用户复制链接并分享。

方法参数

Object object

object参数说明

属性名类型必填默认值说明Web 态说明
titleString分享标题暂不支持
contentString分享内容暂不支持
imageUrlString分享图标暂不支持
pathString页面 path,必须是以 / 开头的完整路径。如果 path 中的参数包含中文字符,需通过 encodeURIComponent 对中文字符进行编码。Web 态小程序运行在微信、QQ、QQ 空间、微博、百度 Hi 内时配置的分享 path 不生效,此时分享 path 为当前页面的路径
successFunction接口调用成功的回调函数
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

函数返回值

Boolean result

返回值说明

反馈分享结果,成功或失败。

fail 返回值参数说明

Web 态

错误信息(errMsg)类型说明
url copy failstring分享链接复制到剪切板失败
share canceledstring取消分享面板
sharing guide canceledstring取消分享引导弹层

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <button type="primary" open-type="share">组件方式打开分享</button>
        <button type="primary" bindtap="openShare">API调用打开分享</button>
        <view class="tip-week">不同方式调起分享面板</view>
    </view>
</view>
Page({
    onShareAppMessage() {
        return {
            title: '小程序标题',
            content: '世界很复杂,百度更懂你',
            imageUrl: 'https://b.bdstatic.com/miniapp/images/baidulogo1.jpg',
            path: 'swan-api/open-share/open-share'
        };
    },
    openShare() {
        swan.openShare({
            title: '智能小程序示例',
            content: '世界很复杂,百度更懂你',
            path: 'swan-api/open-share/open-share?key=value',
            imageUrl: 'https://smartprogram.baidu.com/docs/img/logo_new.png',
            success: res => {
                swan.showToast({
                    title: '分享成功',
                    icon: 'none'
                });
                console.log('openShare success', res);
            },
            fail: err => {
                console.log('openShare fail', err);
            }
        });
    }
});

截屏分享二维码说明

自手百 11.23 以来,小程序截屏分享增加了二维码图片标识。

注意:该二维码图片标识 60 天 内失效,请勿在其他场景中使用,如需要长期有效的二维码,请到开发者后台,或使用二维码开放接口生成。

截屏分享图片示例

Bug & Tip

  • tip: 如果入参 path 中的参数包含中文字符,需要通过 encodeURIComponent 对中文字符进行编码,举例:
let path = '/a/b?key=' + encodeURIComponent('中文');
  • bug: 基础库 1.13.43 版本 Android 手机中,点击分享面板的取消时,不会执行 fail 回调。


百度智能小程序 页面的事件处理函数
百度智能小程序 文件分享到其他App
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }