1. 程式人生 > >關於微信分享朋友圈、分享給朋友、隱藏“傳播類”和“保護類”按鈕

關於微信分享朋友圈、分享給朋友、隱藏“傳播類”和“保護類”按鈕

1、引入微信官方的js檔案,將該js檔案放到最前面引入:<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

2、初始化微信所需引數:

2.1 注意:wx.config必須在頁面載入時完成執行,wx.ready可以放到方法內通過觸發執行。

//初始化微信
    var url = null;
    var jsapi_ticket = null;
    var nonceStr = null;
    var timestamp = null;
    var signature = null;

   var showURL = null;

   var appid = null;

    $.ajax({
        cache: false,
        type: "GET",
        url: "../oAuth/configParam",//此處的url是獲取微信引數的後臺方法,也可通過前端實現
        dataType: "json",
        data: {
            currentPageUrl: window.location.href,//url指引方法的引數列表
           
        },
        error: function (error) {
            
            window.location = "../oAuth/error?message" + error.toString()
        },
        success: function (map) {
            //url = map["url"];
            jsapi_ticket = map["jsapi_ticket"];
            nonceStr = map["nonceStr"];
            timestamp = map["timestamp"];
            signature = map["signature"];
            appid = map["appid"];
            //shareUrl = map["redshareurl"];
            alert(appid);
            wx.config({
                debug: true, // 開啟除錯模式,呼叫的所有api的返回值會在客戶端alert出來,若要檢視傳入的引數,可以在pc端開啟,引數資訊會通過log打出,僅在pc端時才會列印。
                appId: appid, // 必填,公眾號的唯一標識
                timestamp: timestamp, // 必填,生成簽名的時間戳
                nonceStr: nonceStr, // 必填,生成簽名的隨機串
                signature: signature,// 必填,簽名,見附錄1
                jsApiList: [//此處列表,用到哪些方法,必須要在此提前宣告,我當時要用到hideMenuItems

,但是因為沒有在此出聲明,一直不起作用,後來查資料才知道,並且這                             //些方法必須放到wx.ready中
                    'checkJsApi',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'hideMenuItems'

                ] // 必填,需要使用的JS介面列表,所有JS介面列表見附錄2
            });
            
            wx.ready(function () {
                //2.3 隱藏不用的按鈕
                alert("隱藏不用的按鈕");
                wx.hideMenuItems({
                    menuList: ['menuItem:share:qq',
                               'menuItem:share:weiboApp',
                               'menuItem:favorite',
                               'menuItem:share:facebook',
                               '/menuItem:share:QZone'], // 要隱藏的選單項,只能隱藏“傳播類”和“保護類”按鈕,所有menu項見附錄3
                    success:function(res){
                        //alert("隱藏");
                    }
                });
               

              wx.checkJsApi({
                                    jsApiList: [
                                        'getNetworkType',
                                        'previewImage'
                                    ],
                                    success: function (res) {
//                                        alert(JSON.stringify(res));
                                    }
                                });
                                /*wx.showMenuItems({
                                    menuList: ['onMenuShareAppMessage',
                                               'onMenuShareTimeline'] // 要顯示的選單項,所有menu項見附錄3
                                });*/
                                // 2. 分享介面
                                // 2.1 監聽“分享給朋友”,按鈕點選、自定義分享內容及分享結果介面
                                wx.onMenuShareAppMessage({
                                    title: '話費購',
                                    desc: '話費紅包,點選領取',
                                    link: shareUrl,
                                    imgUrl: 'http://wuming_ise.ngrok.cc/wcpay/images/tubiao.png',
                                    trigger: function (res) {
                                        // 不要嘗試在trigger中使用ajax非同步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回
                                        //alert("點選分享朋友"+shareUrl);
                                    },
                                    success: function (res) {
                                        dismiss();
                                    },
                                    cancel: function (res) {
                                        dismiss();
                                    },
                                    fail: function (res) {
                                        dismiss();
//                                        alert(JSON.stringify(res));
                                    }
                                });
                                //alert('已註冊獲取“傳送給朋友”狀態事件');

                                // 2.2 監聽“分享到朋友圈”按鈕點選、自定義分享內容及分享結果介面
                                wx.onMenuShareTimeline({
                                    title: '話費購',
                                    link: shareUrl,
                                    imgUrl:  'http://wuming_ise.ngrok.cc/wcpay/images/tubiao.png',
                                    trigger: function (res) {
                                        // 不要嘗試在trigger中使用ajax非同步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回
                                        alert("分享到朋友圈"+shareUrl);
                                    },
                                    success: function (res) {
                                        dismiss();
                                    },
                                    cancel: function (res) {
                                        dismiss();
                                    },
                                    fail: function (res) {
                                        dismiss();
                                        
                                        alert(JSON.stringify(res));
                                    }
                                });

            });
            
        }
    });