微信接口的使用

發送給朋友和分享到朋友圈

1.綁定域名

先登錄微信公衆平臺進入“公衆號設置”的“功能設置”裏填寫“JS接口安全域名”。
備註:登錄後可在“開發者中心”查看對應的接口權限。

2.引入Js文件

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

3.通過config接口注入權限驗證

			/*微信接口的使用(1)注入權限驗證配置*/
			wx.config({
                        debug: false,//開啓調試模式,調用的所有的API的返回值會在客戶端alert出來
                        appId: data.appId,//必填,公衆號的唯一標識
                        timestamp: data.timestamp,//必填,生成簽名時間戳
                        nonceStr: data.nonceStr,//必填,生成簽名的隨機串
                        signature: data.signature,//必填,簽名
                        jsApiList: [
                            'chooseWXPay',//微信支付接口
                            'onMenuShareTimeline',//分享到朋友圈
                            'onMenuShareAppMessage',//分享給好友
                        ]//必填,需要使用的JS接口列表
                    });

4.通過ready接口處理成功驗證

	wx.ready(function () {
            isReady = true;
            //微信接口的使用(2.1)分享到朋友圈
            wx.onMenuShareTimeline({
                title: '👍👍👍👍',//分享標題
                link: window.location.protocol + "//" + window.location.host + '@Url.Content("~/xxxx/qqq")',//分享描述
                imgUrl: window.location.protocol + "//" + window.location.host + '@Url.Content("~/Res/GlobalImg/share.jpg")',//分享圖標
                success: function () {                      
                },
                cancel: function () {
                }
            });
            //微信接口的使用(2.2) 分享給好友
            wx.onMenuShareAppMessage({
                title: '👍👍👍👍',//分享標題
                desc: '!!!!!',//分享描述
                link: window.location.protocol + "//" + window.location.host + '@Url.Content("~/xxxx/qqq")',//分享鏈接
                imgUrl: window.location.protocol + "//" + window.location.host + '@Url.Content("~/Res/GlobalImg/share.jpg")',//分享圖標
                type: '',//分享類型,music、video或link,不填默認爲link
                dataUrl: '',//如果type是music或video,則要提供數據鏈接,默認爲空
                success: function () {//確認分享後執行回調函數
                },
                cancel: function () {//取消分享後執行回調函數
                }
            });
        });

5.1 通過checkJsApi判斷當前客戶端版本是否支持分享參數自定義

wx.checkJsApi({
            jsApiList: [
                'getLocation',
                'onMenuShareTimeline',
                'onMenuShareAppMessage'
            ],
            success: function (res) {
                alert(JSON.stringify(res));
            }
        });

5.2通過error接口處理失敗驗證

wx.error(function () {
            showAlert("頁面初始化失敗");
        });

參考:https://www.w3cschool.cn/weixinkaifawendang/h8ap1qe5.html

https://www.cnblogs.com/txw1958/p/weixin-js.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章