如何在微信端html5页面调用分享接口

求助... 求助 展开
 我来答
厦门言成
2018-07-19 · TA获得超过4947个赞
知道小有建树答主
回答量:121
采纳率:97%
帮助的人:134万
展开全部
1. 服务器配置

配置JS接口安全域名

2. 后端SDK下载

源代码下载:https://pic.developgeek.com/o_1ci72pdo81i8fiqf12q0jgt1j8l9.zip

3. 前端调用前端SDK与后端SDK

前端页面调用后端SDK

<?php
    require_once "../../global/jssdk.php";
    $jssdk = new JSSDK("填写微信的APPID", "填写微信的密钥");
    $signPackage = $jssdk->GetSignPackage();
?>

前端页面引入前端SDK

<script src="//res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

Tips:前端页面文件类型为php,这样有利于调用后端SDK。

调用分享接口

<?php
    require_once "../../global/jssdk.php";
    $jssdk = new JSSDK("填写微信的APPID", "填写微信的密钥");
    $signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>言成科技 & HTML5 学堂(码匠)联合出品</title>
</head>
<body>
    <div class="wrap">言成科技</div>
    <script src="//res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript">
        /**
         * [微信分享]
         * @param  {[type]} obj [description]
         * @return {[type]}     [description]
         */
        function share(obj) {
            var mainTitle   = obj.tit,
                mainDesc    = obj.des,
                mainURL     = obj.url,
                mainImgUrl  = obj.icon;

                wx.config({
                    debug: false,
                    appId: '<?php echo $signPackage["appId"];?>',
                    timestamp: <?php echo $signPackage["timestamp"];?>,
                    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
                    signature: '<?php echo $signPackage["signature"];?>',
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage'
                    ]
                });
                wx.ready(function () {
                    wx.onMenuShareTimeline({
                        title: mainTitle,
                        link: mainURL,
                        imgUrl: mainImgUrl,
                        success: function () {
                        },
                        cancel: function () {
                        }
                    });

                    wx.onMenuShareAppMessage({
                        title: mainTitle,
                        desc: mainDesc,
                        link: mainURL,
                        imgUrl: mainImgUrl,
                        type: 'link',
                        dataUrl: '',
                        success: function () {
                        },
                        cancel: function () {
                        }
                });
            });
        };
        share({
            tit: '《言成科技,高品质教育》',
            des: '提供高品质教育服务,让每个人遇见更好的自己',
            url: 'https://www.developgeek.com/index.php',
            icon: 'https://www.developgeek.com/logo.png',
        });
    </script>
</body>
</html>

    $jssdk = new JSSDK("填写微信的APPID", "填写微信的密钥");
    $signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>言成科技 & HTML5 学堂(码匠)联合出品</title>
</head>
<body>
    <div class="wrap">言成科技</div>
    <script src="//res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script type="text/javascript">
        /**
         * [微信分享]
         * @param  {[type]} obj [description]
         * @return {[type]}     [description]
         */
        function share(obj) {
            var mainTitle   = obj.tit,
                mainDesc    = obj.des,
                mainURL     = obj.url,
                mainImgUrl  = obj.icon;

                wx.config({
                    debug: false,
                    appId: '<?php echo $signPackage["appId"];?>',
                    timestamp: <?php echo $signPackage["timestamp"];?>,
                    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
                    signature: '<?php echo $signPackage["signature"];?>',
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage'
                    ]
                });
                wx.ready(function () {
                    wx.onMenuShareTimeline({
                        title: mainTitle,
                        link: mainURL,
                        imgUrl: mainImgUrl,
                        success: function () {
                        },
                        cancel: function () {
                        }
                    });

                    wx.onMenuShareAppMessage({
                        title: mainTitle,
                        desc: mainDesc,
                        link: mainURL,
                        imgUrl: mainImgUrl,
                        type: 'link',
                        dataUrl: '',
                        success: function () {
                        },
                        cancel: function () {
                        }
                });
            });
        };
        share({
            tit: '《言成科技,高品质教育》',
            des: '提供高品质教育服务,让每个人遇见更好的自己',
            url: 'https://www.developgeek.com/index.php',
            icon: 'https://www.developgeek.com/logo.png',
        });
    </script>
</body>
</html>

4. 分享效果

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式