微信分享那些事 @劉?

  • 可以再分享的頁面中寫上分享文案
<!-- 分享文案 -->
    <div style="display: none">
        <p id="shareTittle">分享的主標(biāo)題</p>
        <p id="shareDesc">分享的副標(biāo)題</p>
    </div>
  • 單獨(dú)建一個js文件用來寫分享相關(guān)的代碼
// 獲取微信配置
var wxUrl = window.location.href.split('#')[0];
$.ajax({
    type: "GET",
    url: 'http://.......' + '?url=' + wxUrl,//微信分享的接口(后臺給)
    dataType: 'json',
    success: (function(wxMsg) {
        if (wxMsg.retResult == null || wxMsg.retResult == '') {
            return;
        } else {
            console.log(wxMsg);
            console.log(JSON.parse(wxMsg.retResult));
            var wxInformation = JSON.parse(wxMsg.retResult);
            var appId = wxInformation.appId;
            var timestamp = wxInformation.timestamp;
            var nonceStr = wxInformation.noncestr;
            var signature = wxInformation.signature;
            wx.config({
                debug: false,//提示的開關(guān),true的時(shí)候在手機(jī)上可以看到正確或錯誤的提示
                appId: appId,
                timestamp: timestamp,
                nonceStr: nonceStr,
                signature: signature,
                jsApiList: [
                    'checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo'
                ]
            });
        }
    }),
    error: (function() {

    })
});
// 獲取微信配置
var wxSUrl = location.origin+"/giftsNeimeng/enrollNeiMeng/index.html";//當(dāng)前頁面的路徑
console.log(wxSUrl)
var iconImg = location.origin+'/giftsNeimeng/enrollNeiMeng/images/icon.png'; // 分享的圖標(biāo)
var shareTit = $('#shareTittle').text(); // 當(dāng)前頁面標(biāo)題
var shareContent=$('#shareDesc').text();  // 當(dāng)前分享摘要
console.log(shareContent)
wx.ready(function() {
    // 1 判斷當(dāng)前版本是否支持指定 JS 接口,支持批量判斷
    wx.checkJsApi({
        jsApiList: [
            'checkJsApi',
            'onMenuShareAppMessage',
            'onMenuShareTimeline',
            'onMenuShareQQ',
            'onMenuShareWeibo'
        ],
        success: function(res) {

        }
    });

    // 2.1 監(jiān)聽“分享給朋友”,按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口
    wx.onMenuShareAppMessage({
        title: shareTit,
        desc:shareContent,
        link: wxSUrl,
        imgUrl: iconImg,
        trigger: function(res) {
            // alert('用戶點(diǎn)擊發(fā)送給朋友');
        },
        success: function(res) {
            // alert('已分享');
        },
        cancel: function(res) {
            // alert('已取消');
        },
        fail: function(res) {
            // alert(JSON.stringify(res));
        }
    });

    // 2.2 監(jiān)聽“分享到朋友圈”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口
    wx.onMenuShareTimeline({
        title: shareTit,
        desc: shareContent,
        link: wxSUrl,
        imgUrl: iconImg,
        trigger: function(res) {
            //   alert('用戶點(diǎn)擊分享到朋友圈');
        },
        success: function(res) {
            //  alert('已分享');
        },
        cancel: function(res) {
            // alert('已取消');
        },
        fail: function(res) {
            // alert(JSON.stringify(res));
        }
    });

    // 2.3 監(jiān)聽“分享到QQ”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口
    wx.onMenuShareQQ({
        title: shareTit,
        desc:shareContent,
        link: wxSUrl,
        imgUrl: iconImg,
        trigger: function(res) {
            //  alert('用戶點(diǎn)擊分享到QQ');
        },
        complete: function(res) {
            //  alert(JSON.stringify(res));
        },
        success: function(res) {
            //  alert('已分享');
        },
        cancel: function(res) {
            //   alert('已取消');
        },
        fail: function(res) {
            //   alert(JSON.stringify(res));
        }
    });

    // 2.4 監(jiān)聽“分享到微博”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口
    wx.onMenuShareWeibo({
        title: shareTit,
        desc: shareContent,
        link: wxSUrl,
        imgUrl: iconImg,
        trigger: function(res) {
            //   alert('用戶點(diǎn)擊分享到微博');
        },
        complete: function(res) {
            // alert(JSON.stringify(res));
        },
        success: function(res) {
            //  alert('已分享');
        },
        cancel: function(res) {
            //  alert('已取消');
        },
        fail: function(res) {
            //  alert(JSON.stringify(res));
        }
    });

});

wx.error(function(res) {

});

當(dāng)然,微信分享很容易出現(xiàn)以下分享不成功的時(shí)候,各位大佬可以觀看這位簡書作者寫的關(guān)于微信回調(diào)不成功的問題
http://www.itdecent.cn/p/beb48eafc06c

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容