1. 程式人生 > >微信jssdk分享功能開發

微信jssdk分享功能開發

獲取地理位置 開放平臺 ont 1.3 千萬 sage hide ict gbk

先理解下分享:

  技術分享圖片

在app端 ,經常能看見 分享按鈕的功能,(分享給朋友,分享到朋友圈,分享到QQ空間等等);

https://open.weixin.qq.com/(微信開發平臺),這需要到開放平臺註冊賬號,綁定自己的應用,才能實現其權限;

在微信公眾號開發過程中,目前,現在只能,實現,自定義分享內容,而不能自己開發分享按鈕;

技術分享圖片

我們通過

技術分享圖片

一些分享的方法,只是獲取“*******” 按鈕點擊狀態及 自定義分享內容接口,請註意

我們,仍然,需要點擊,微信自帶的頭部菜單按鈕裏面(分享朋友,分享朋友圈按鈕)

技術分享圖片

大家註意這個;千萬別理解錯了;

jsdk 的分享代碼:

完整代碼:

頁面:

技術分享圖片
<!DOCTYPE html>
<html>
<head>
    <meta content="IE=7.0000" http-equiv="X-UA-Compatible">
    <title>微信JS-SDK Demo</title>
    <meta charset=utf-8>
    <meta name=viewport
          content="width=device-width, initial-scale=1, user-scalable=0">
    <link rel=stylesheet href="微信JS-SDK%20Demo_files/style.css">
    <meta name=GENERATOR content="MSHTML 11.00.10240.16590">
</head>
<body ontouchstart="">
    <div class=wxapi_container>
        <div class="lbox_close wxapi_form">
            <h3 id=menu-basic>基礎接口</h3><span class=desc>判斷當前客戶端是否支持指定JS接口</span><button id=checkJsApi class="btn btn_primary">
                checkJsApi
            </button>
            <h3 id=menu-share>分享接口</h3><span class=desc>獲取“分享到朋友圈”按鈕點擊狀態及自定義分享內容接口</span><button id=onMenuShareTimeline
                                                                                                 class="btn btn_primary">
                onMenuShareTimeline
            </button> <span class=desc>獲取“分享給朋友”按鈕點擊狀態及自定義分享內容接口</span><button id=onMenuShareAppMessage
                                                                               class="btn btn_primary">
                onMenuShareAppMessage
            </button> <span class=desc>獲取“分享到QQ”按鈕點擊狀態及自定義分享內容接口</span><button id=onMenuShareQQ
                                                                               class="btn btn_primary">
                onMenuShareQQ
            </button> <span class=desc>獲取“分享到騰訊微博”按鈕點擊狀態及自定義分享內容接口</span><button id=onMenuShareWeibo
                                                                                 class="btn btn_primary">
                onMenuShareWeibo
            </button> <span class=desc>獲取“分享到QZone”按鈕點擊狀態及自定義分享內容接口</span><button id=onMenuShareQZone
                                                                                  class="btn btn_primary">
                onMenuShareQZone
            </button>
            <h3 id=menu-image>圖像接口</h3><span class=desc>拍照或從手機相冊中選圖接口</span><button id=chooseImage class="btn btn_primary">
                chooseImage
            </button> <span class=desc>預覽圖片接口</span><button id=previewImage
                                                            class="btn btn_primary">
                previewImage
            </button> <span class=desc>上傳圖片接口</span><button id=uploadImage
                                                            class="btn btn_primary">
                uploadImage
            </button> <span class=desc>下載圖片接口</span><button id=downloadImage
                                                            class="btn btn_primary">
                downloadImage
            </button>
            <h3 id=menu-voice>音頻接口</h3><span class=desc>開始錄音接口</span><button id=startRecord
                                                                             class="btn btn_primary">
                startRecord
            </button> <span class=desc>停止錄音接口</span><button id=stopRecord
                                                            class="btn btn_primary">
                stopRecord
            </button> <span class=desc>播放語音接口</span><button id=playVoice
                                                            class="btn btn_primary">
                playVoice
            </button> <span class=desc>暫停播放接口</span><button id=pauseVoice class="btn btn_primary">
                pauseVoice
            </button> <span class=desc>停止播放接口</span><button id=stopVoice
                                                            class="btn btn_primary">
                stopVoice
            </button> <span class=desc>上傳語音接口</span><button id=uploadVoice class="btn btn_primary">
                uploadVoice
            </button> <span class=desc>下載語音接口</span><button id=downloadVoice
                                                            class="btn btn_primary">
                downloadVoice
            </button>
            <h3 id=menu-smart>智能接口</h3><span class=desc>識別音頻並返回識別結果接口</span><button id=translateVoice class="btn btn_primary">
                translateVoice
            </button>
            <h3 id=menu-device>設備信息接口</h3><span class=desc>獲取網絡狀態接口</span><button id=getNetworkType class="btn btn_primary">
                getNetworkType
            </button>
            <h3 id=menu-location>地理位置接口</h3><span class=desc>使用微信內置地圖查看位置接口</span><button id=openLocation class="btn btn_primary">
                openLocation
            </button> <span class=desc>獲取地理位置接口</span><button id=getLocation
                                                              class="btn btn_primary">
                getLocation
            </button>
            <h3 id=menu-webview>界面操作接口</h3><span class=desc>隱藏右上角菜單接口</span><button id=hideOptionMenu class="btn btn_primary">
                hideOptionMenu
            </button> <span class=desc>顯示右上角菜單接口</span><button id=showOptionMenu
                                                               class="btn btn_primary">
                showOptionMenu
            </button> <span class=desc>關閉當前網頁窗口接口</span><button id=closeWindow
                                                                class="btn btn_primary">
                closeWindow
            </button> <span class=desc>批量隱藏功能按鈕接口</span><button id=hideMenuItems
                                                                class="btn btn_primary">
                hideMenuItems
            </button> <span class=desc>批量顯示功能按鈕接口</span><button id=showMenuItems
                                                                class="btn btn_primary">
                showMenuItems
            </button> <span class=desc>隱藏所有非基礎按鈕接口</span><button id=hideAllNonBaseMenuItem
                                                                 class="btn btn_primary">
                hideAllNonBaseMenuItem
            </button> <span class=desc>顯示所有功能按鈕接口</span><button id=showAllNonBaseMenuItem
                                                                class="btn btn_primary">
                showAllNonBaseMenuItem
            </button>
            <h3 id=menu-scan>微信掃一掃</h3><span class=desc>調起微信掃一掃接口</span><button id=scanQRCode0 class="btn btn_primary">
                scanQRCode(微信處理結果)
            </button><button id=scanQRCode1 class="btn btn_primary">
                scanQRCode(直接返回結果)
            </button>
            <h3 id=menu-shopping>微信小店接口</h3><span class=desc>跳轉微信商品頁接口</span><button id=openProductSpecificView
                                                                                     class="btn btn_primary">
                openProductSpecificView
            </button>
            <h3 id=menu-card>微信卡券接口</h3><span class=desc>批量添加卡券接口</span><button id=addCard
                                                                                class="btn btn_primary">
                addCard
            </button> <span class=desc>調起適用於門店的卡券列表並獲取用戶選擇列表</span><button id=chooseCard
                                                                           class="btn btn_primary">
                chooseCard
            </button> <span class=desc>查看微信卡包中的卡券接口</span><button id=openCard
                                                                  class="btn btn_primary">
                openCard
            </button>
            <h3 id=menu-pay>微信支付接口</h3><span class=desc>發起一個微信支付請求</span><button id=chooseWXPay class="btn btn_primary">
                chooseWXPay
            </button>
        </div>
    </div>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

    <script>
        /*
         * 註意:
         * 1. 所有的JS接口只能在公眾號綁定的域名下調用,公眾號開發者需要先登錄微信公眾平臺進入“公眾號設置”的“功能設置”裏填寫“JS接口安全域名”。
         * 2. 如果發現在 Android 不能分享自定義內容,請到官網下載最新的包覆蓋安裝,Android 自定義分享接口需升級至 6.0.2.58 版本及以上。
         * 3. 常見問題及完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
         *
         * 開發中遇到問題詳見文檔“附錄5-常見錯誤及解決辦法”解決,如仍未能解決可通過以下渠道反饋:
         * 郵箱地址:[email protected]
         * 郵件主題:【微信JS-SDK反饋】具體問題
         * 郵件內容說明:用簡明的語言描述問題所在,並交代清楚遇到該問題的場景,可附上截屏圖片,微信團隊會盡快處理你的反饋。
         */
        wx.config({
            debug: true,
            appId: ‘[appid的值]‘,
            timestamp: @ViewBag.timestamp,
            nonceStr: ‘@ViewBag.nonce_str‘,
            signature: ‘@ViewBag.signature‘,
            jsApiList: [
              ‘checkJsApi‘,
              ‘onMenuShareTimeline‘,
              ‘onMenuShareAppMessage‘,
              ‘onMenuShareQQ‘,
              ‘onMenuShareWeibo‘,
              ‘onMenuShareQZone‘,
              ‘hideMenuItems‘,
              ‘showMenuItems‘,
              ‘hideAllNonBaseMenuItem‘,
              ‘showAllNonBaseMenuItem‘,
              ‘translateVoice‘,
              ‘startRecord‘,
              ‘stopRecord‘,
              ‘onVoiceRecordEnd‘,
              ‘playVoice‘,
              ‘onVoicePlayEnd‘,
              ‘pauseVoice‘,
              ‘stopVoice‘,
              ‘uploadVoice‘,
              ‘downloadVoice‘,
              ‘chooseImage‘,
              ‘previewImage‘,
              ‘uploadImage‘,
              ‘downloadImage‘,
              ‘getNetworkType‘,
              ‘openLocation‘,
              ‘getLocation‘,
              ‘hideOptionMenu‘,
              ‘showOptionMenu‘,
              ‘closeWindow‘,
              ‘scanQRCode‘,
              ‘chooseWXPay‘,
              ‘openProductSpecificView‘,
              ‘addCard‘,
              ‘chooseCard‘,
              ‘openCard‘
            ]
        });
    </script>

    @*<script src="~/js/zepto.min.js"></script>*@
    <script src="~/js/demo.js"></script>
</body>
</html>
技術分享圖片

demo.js 代碼:

技術分享圖片
/*
 * 註意:
 * 1. 所有的JS接口只能在公眾號綁定的域名下調用,公眾號開發者需要先登錄微信公眾平臺進入“公眾號設置”的“功能設置”裏填寫“JS接口安全域名”。
 * 2. 如果發現在 Android 不能分享自定義內容,請到官網下載最新的包覆蓋安裝,Android 自定義分享接口需升級至 6.0.2.58 版本及以上。
 * 3. 完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
 *
 * 如有問題請通過以下渠道反饋:
 * 郵箱地址:[email protected]
 * 郵件主題:【微信JS-SDK反饋】具體問題
 * 郵件內容說明:用簡明的語言描述問題所在,並交代清楚遇到該問題的場景,可附上截屏圖片,微信團隊會盡快處理你的反饋。
 */
wx.ready(function () {
    // 1 判斷當前版本是否支持指定 JS 接口,支持批量判斷
    document.querySelector(‘#checkJsApi‘).onclick = function () {
        wx.checkJsApi({
            jsApiList: [
              ‘getNetworkType‘,
              ‘previewImage‘
            ],
            success: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 2. 分享接口
    // 2.1 監聽“分享給朋友”,按鈕點擊、自定義分享內容及分享結果接口
    document.querySelector(‘#onMenuShareAppMessage‘).onclick = function () {
        wx.onMenuShareAppMessage({
            title: ‘互聯網之子‘,
            desc: ‘在長大的過程中,我才慢慢發現,我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,註定如此的事,它們其實沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。‘,
            link: ‘http://www.baidu.com‘,
            imgUrl: ‘http://img14.360buyimg.com/n5/jfs/t1279/210/287600770/128026/75262bb3/5562e4deN53c98f09.jpg‘,
            
            //trigger: function (res) {
                // 不要嘗試在trigger中使用ajax異步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回
                //alert(‘用戶點擊發送給朋友‘);
            //},
            success: function (res) {
                alert(‘已分享‘);
            },
            cancel: function (res) {
                alert(‘已取消‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
        //alert(‘已註冊獲取“發送給朋友”狀態事件‘);
    };

    // 2.2 監聽“分享到朋友圈”按鈕點擊、自定義分享內容及分享結果接口
    document.querySelector(‘#onMenuShareTimeline‘).onclick = function () {
        wx.onMenuShareTimeline({
            title: ‘互聯網之子‘,
            link: ‘http://www.baidu.com‘,
            imgUrl: ‘http://img14.360buyimg.com/n5/jfs/t1279/210/287600770/128026/75262bb3/5562e4deN53c98f09.jpg‘,
            trigger: function (res) {
                // 不要嘗試在trigger中使用ajax異步請求修改本次分享的內容,因為客戶端分享操作是一個同步操作,這時候使用ajax的回包會還沒有返回
                alert(‘用戶點擊分享到朋友圈‘);
            },
            success: function (res) {
                alert(‘已分享‘);
            },
            cancel: function (res) {
                alert(‘已取消‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
        //alert(‘已註冊獲取“分享到朋友圈”狀態事件‘);
    };

    // 2.3 監聽“分享到QQ”按鈕點擊、自定義分享內容及分享結果接口
    document.querySelector(‘#onMenuShareQQ‘).onclick = function () {
        wx.onMenuShareQQ({
            title: ‘互聯網之子‘,
            desc: ‘在長大的過程中,我才慢慢發現,我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,註定如此的事,它們其實沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。‘,
            link: ‘http://movie.douban.com/subject/25785114/‘,
            imgUrl: ‘http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg‘,
            trigger: function (res) {
                alert(‘用戶點擊分享到QQ‘);
            },
            complete: function (res) {
                alert(JSON.stringify(res));
            },
            success: function (res) {
                alert(‘已分享‘);
            },
            cancel: function (res) {
                alert(‘已取消‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
        //alert(‘已註冊獲取“分享到 QQ”狀態事件‘);
    };

    // 2.4 監聽“分享到微博”按鈕點擊、自定義分享內容及分享結果接口
    document.querySelector(‘#onMenuShareWeibo‘).onclick = function () {
        wx.onMenuShareWeibo({
            title: ‘互聯網之子‘,
            desc: ‘在長大的過程中,我才慢慢發現,我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,註定如此的事,它們其實沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。‘,
            link: ‘http://movie.douban.com/subject/25785114/‘,
            imgUrl: ‘http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg‘,
            trigger: function (res) {
                alert(‘用戶點擊分享到微博‘);
            },
            complete: function (res) {
                alert(JSON.stringify(res));
            },
            success: function (res) {
                alert(‘已分享‘);
            },
            cancel: function (res) {
                alert(‘已取消‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
        //alert(‘已註冊獲取“分享到微博”狀態事件‘);
    };

    // 2.5 監聽“分享到QZone”按鈕點擊、自定義分享內容及分享接口
    document.querySelector(‘#onMenuShareQZone‘).onclick = function () {
        wx.onMenuShareQZone({
            title: ‘互聯網之子‘,
            desc: ‘在長大的過程中,我才慢慢發現,我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,註定如此的事,它們其實沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。‘,
            link: ‘http://movie.douban.com/subject/25785114/‘,
            imgUrl: ‘http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg‘,
            trigger: function (res) {
                alert(‘用戶點擊分享到QZone‘);
            },
            complete: function (res) {
                alert(JSON.stringify(res));
            },
            success: function (res) {
                alert(‘已分享‘);
            },
            cancel: function (res) {
                alert(‘已取消‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
        //alert(‘已註冊獲取“分享到QZone”狀態事件‘);
    };


    // 3 智能接口
    var voice = {
        localId: ‘‘,
        serverId: ‘‘
    };
    // 3.1 識別音頻並返回識別結果
    document.querySelector(‘#translateVoice‘).onclick = function () {
        if (voice.localId == ‘‘) {
            alert(‘請先使用 startRecord 接口錄制一段聲音‘);
            return;
        }
        wx.translateVoice({
            localId: voice.localId,
            complete: function (res) {
                if (res.hasOwnProperty(‘translateResult‘)) {
                    alert(‘識別結果:‘ + res.translateResult);
                } else {
                    alert(‘無法識別‘);
                }
            }
        });
    };

    // 4 音頻接口
    // 4.2 開始錄音
    document.querySelector(‘#startRecord‘).onclick = function () {
        wx.startRecord({
            cancel: function () {
                alert(‘用戶拒絕授權錄音‘);
            }
        });
    };

    // 4.3 停止錄音
    document.querySelector(‘#stopRecord‘).onclick = function () {
        wx.stopRecord({
            success: function (res) {
                voice.localId = res.localId;
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 4.4 監聽錄音自動停止
    wx.onVoiceRecordEnd({
        complete: function (res) {
            voice.localId = res.localId;
            alert(‘錄音時間已超過一分鐘‘);
        }
    });

    // 4.5 播放音頻
    document.querySelector(‘#playVoice‘).onclick = function () {
        if (voice.localId == ‘‘) {
            alert(‘請先使用 startRecord 接口錄制一段聲音‘);
            return;
        }
        wx.playVoice({
            localId: voice.localId
        });
    };

    // 4.6 暫停播放音頻
    document.querySelector(‘#pauseVoice‘).onclick = function () {
        wx.pauseVoice({
            localId: voice.localId
        });
    };

    // 4.7 停止播放音頻
    document.querySelector(‘#stopVoice‘).onclick = function () {
        wx.stopVoice({
            localId: voice.localId
        });
    };

    // 4.8 監聽錄音播放停止
    wx.onVoicePlayEnd({
        complete: function (res) {
            alert(‘錄音(‘ + res.localId + ‘)播放結束‘);
        }
    });

    // 4.8 上傳語音
    document.querySelector(‘#uploadVoice‘).onclick = function () {
        if (voice.localId == ‘‘) {
            alert(‘請先使用 startRecord 接口錄制一段聲音‘);
            return;
        }
        wx.uploadVoice({
            localId: voice.localId,
            success: function (res) {
                alert(‘上傳語音成功,serverId 為‘ + res.serverId);
                voice.serverId = res.serverId;
            }
        });
    };

    // 4.9 下載語音
    document.querySelector(‘#downloadVoice‘).onclick = function () {
        if (voice.serverId == ‘‘) {
            alert(‘請先使用 uploadVoice 上傳聲音‘);
            return;
        }
        wx.downloadVoice({
            serverId: voice.serverId,
            success: function (res) {
                alert(‘下載語音成功,localId 為‘ + res.localId);
                voice.localId = res.localId;
            }
        });
    };

    // 5 圖片接口
    // 5.1 拍照、本地選圖
    var images = {
        localId: [],
        serverId: []
    };
    document.querySelector(‘#chooseImage‘).onclick = function () {
        wx.chooseImage({
            success: function (res) {
                images.localId = res.localIds;
                alert(‘已選擇 ‘ + res.localIds.length + ‘ 張圖片‘);
            }
        });
    };

    // 5.2 圖片預覽
    document.querySelector(‘#previewImage‘).onclick = function () {
        wx.previewImage({
            current: ‘http://img5.douban.com/view/photo/photo/public/p1353993776.jpg‘,
            urls: [
              ‘http://img3.douban.com/view/photo/photo/public/p2152117150.jpg‘,
              ‘http://img5.douban.com/view/photo/photo/public/p1353993776.jpg‘,
              ‘http://img3.douban.com/view/photo/photo/public/p2152134700.jpg‘
            ]
        });
    };

    // 5.3 上傳圖片
    document.querySelector(‘#uploadImage‘).onclick = function () {
        if (images.localId.length == 0) {
            alert(‘請先使用 chooseImage 接口選擇圖片‘);
            return;
        }
        var i = 0, length = images.localId.length;
        images.serverId = [];
        function upload() {
            wx.uploadImage({
                localId: images.localId[i],
                success: function (res) {
                    i++;
                    //alert(‘已上傳:‘ + i + ‘/‘ + length);
                    images.serverId.push(res.serverId);
                    if (i < length) {
                        upload();
                    }
                },
                fail: function (res) {
                    alert(JSON.stringify(res));
                }
            });
        }
        upload();
    };

    // 5.4 下載圖片
    document.querySelector(‘#downloadImage‘).onclick = function () {
        if (images.serverId.length === 0) {
            alert(‘請先使用 uploadImage 上傳圖片‘);
            return;
        }
        var i = 0, length = images.serverId.length;
        images.localId = [];
        function download() {
            wx.downloadImage({
                serverId: images.serverId[i],
                success: function (res) {
                    i++;
                    alert(‘已下載:‘ + i + ‘/‘ + length);
                    images.localId.push(res.localId);
                    if (i < length) {
                        download();
                    }
                }
            });
        }
        download();
    };

    // 6 設備信息接口
    // 6.1 獲取當前網絡狀態
    document.querySelector(‘#getNetworkType‘).onclick = function () {
        wx.getNetworkType({
            success: function (res) {
                alert(res.networkType);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 7 地理位置接口
    // 7.1 查看地理位置
    document.querySelector(‘#openLocation‘).onclick = function () {
        wx.openLocation({
            latitude: 23.099994,
            longitude: 113.324520,
            name: ‘TIT 創意園‘,
            address: ‘廣州市海珠區新港中路 397 號‘,
            scale: 14,
            infoUrl: ‘http://weixin.qq.com‘
        });
    };

    // 7.2 獲取當前地理位置
    document.querySelector(‘#getLocation‘).onclick = function () {
        wx.getLocation({
            success: function (res) {
                alert(JSON.stringify(res));
            },
            cancel: function (res) {
                alert(‘用戶拒絕授權獲取地理位置‘);
            }
        });
    };

    // 8 界面操作接口
    // 8.1 隱藏右上角菜單
    document.querySelector(‘#hideOptionMenu‘).onclick = function () {
        wx.hideOptionMenu();
    };

    // 8.2 顯示右上角菜單
    document.querySelector(‘#showOptionMenu‘).onclick = function () {
        wx.showOptionMenu();
    };

    // 8.3 批量隱藏菜單項
    document.querySelector(‘#hideMenuItems‘).onclick = function () {
        wx.hideMenuItems({
            menuList: [
              ‘menuItem:readMode‘, // 閱讀模式
              ‘menuItem:share:timeline‘, // 分享到朋友圈
              ‘menuItem:copyUrl‘ // 復制鏈接
            ],
            success: function (res) {
                alert(‘已隱藏“閱讀模式”,“分享到朋友圈”,“復制鏈接”等按鈕‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 8.4 批量顯示菜單項
    document.querySelector(‘#showMenuItems‘).onclick = function () {
        wx.showMenuItems({
            menuList: [
              ‘menuItem:readMode‘, // 閱讀模式
              ‘menuItem:share:timeline‘, // 分享到朋友圈
              ‘menuItem:copyUrl‘ // 復制鏈接
            ],
            success: function (res) {
                alert(‘已顯示“閱讀模式”,“分享到朋友圈”,“復制鏈接”等按鈕‘);
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 8.5 隱藏所有非基本菜單項
    document.querySelector(‘#hideAllNonBaseMenuItem‘).onclick = function () {
        wx.hideAllNonBaseMenuItem({
            success: function () {
                alert(‘已隱藏所有非基本菜單項‘);
            }
        });
    };

    // 8.6 顯示所有被隱藏的非基本菜單項
    document.querySelector(‘#showAllNonBaseMenuItem‘).onclick = function () {
        wx.showAllNonBaseMenuItem({
            success: function () {
                alert(‘已顯示所有非基本菜單項‘);
            }
        });
    };

    // 8.7 關閉當前窗口
    document.querySelector(‘#closeWindow‘).onclick = function () {
        wx.closeWindow();
    };

    // 9 微信原生接口
    // 9.1.1 掃描二維碼並返回結果
    document.querySelector(‘#scanQRCode0‘).onclick = function () {
        wx.scanQRCode();
    };
    // 9.1.2 掃描二維碼並返回結果
    document.querySelector(‘#scanQRCode1‘).onclick = function () {
        wx.scanQRCode({
            needResult: 1,
            desc: ‘scanQRCode desc‘,
            success: function (res) {
                alert(JSON.stringify(res));
            }
        });
    };

    // 10 微信支付接口
    // 10.1 發起一個支付請求
    document.querySelector(‘#chooseWXPay‘).onclick = function () {
        // 註意:此 Demo 使用 2.7 版本支付接口實現,建議使用此接口時參考微信支付相關最新文檔。
        wx.chooseWXPay({
            timestamp: 1414723227,
            nonceStr: ‘noncestr‘,
            package: ‘addition=action_id%3dgaby1234%26limit_pay%3d&bank_type=WX&body=innertest&fee_type=1&input_charset=GBK&notify_url=http%3A%2F%2F120.204.206.246%2Fcgi-bin%2Fmmsupport-bin%2Fnotifypay&out_trade_no=1414723227818375338&partner=1900000109&spbill_create_ip=127.0.0.1&total_fee=1&sign=432B647FE95C7BF73BCD177CEECBEF8D‘,
            signType: ‘SHA1‘, // 註意:新版支付接口使用 MD5 加密
            paySign: ‘bd5b1933cda6e9548862944836a9b52e8c9a2b69‘
        });
    };

    // 11.3  跳轉微信商品頁
    document.querySelector(‘#openProductSpecificView‘).onclick = function () {
        wx.openProductSpecificView({
            productId: ‘pDF3iY_m2M7EQ5EKKKWd95kAxfNw‘,
            extInfo: ‘123‘
        });
    };

    // 12 微信卡券接口
    // 12.1 添加卡券
    document.querySelector(‘#addCard‘).onclick = function () {
        wx.addCard({
            cardList: [
              {
                  cardId: ‘pDF3iY9tv9zCGCj4jTXFOo1DxHdo‘,
                  cardExt: ‘{"code": "", "openid": "", "timestamp": "1418301401", "signature":"f54dae85e7807cc9525ccc127b4796e021f05b33"}‘
              },
              {
                  cardId: ‘pDF3iY9tv9zCGCj4jTXFOo1DxHdo‘,
                  cardExt: ‘{"code": "", "openid": "", "timestamp": "1418301401", "signature":"f54dae85e7807cc9525ccc127b4796e021f05b33"}‘
              }
            ],
            success: function (res) {
                alert(‘已添加卡券:‘ + JSON.stringify(res.cardList));
            }
        });
    };

    var codes = [];
    // 12.2 選擇卡券
    document.querySelector(‘#chooseCard‘).onclick = function () {
        wx.chooseCard({
            cardSign: ‘8ef8aa071f1d2186cb1355ec132fed04ebba1c3f‘,
            timestamp: 1437997723,
            nonceStr: ‘k0hGdSXKZEj3Min5‘,
            success: function (res) {
                res.cardList = JSON.parse(res.cardList);
                encrypt_code = res.cardList[0][‘encrypt_code‘];
                alert(‘已選擇卡券:‘ + JSON.stringify(res.cardList));
                decryptCode(encrypt_code, function (code) {
                    codes.push(code);
                });
            }
        });
    };

    // 12.3 查看卡券
    document.querySelector(‘#openCard‘).onclick = function () {
        if (codes.length < 1) {
            alert(‘請先使用 chooseCard 接口選擇卡券。‘);
            return false;
        }
        var cardList = [];
        for (var i = 0; i < codes.length; i++) {
            cardList.push({
                cardId: ‘pDF3iY9tv9zCGCj4jTXFOo1DxHdo‘,
                code: codes[i]
            });
        }
        wx.openCard({
            cardList: cardList
        });
    };

    var shareData = {
        title: ‘微信JS-SDK Demo‘,
        desc: ‘微信JS-SDK,幫助第三方為用戶提供更優質的移動web服務‘,
        link: ‘http://demo.open.weixin.qq.com/jssdk/‘,
        imgUrl: ‘http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0‘
    };
    wx.onMenuShareAppMessage(shareData);
    wx.onMenuShareTimeline(shareData);

    function decryptCode(code, callback) {
        $.getJSON(‘/jssdk/decrypt_code.php?code=‘ + encodeURI(code), function (res) {
            if (res.errcode == 0) {
                codes.push(res.code);
            }
        });
    }
});

wx.error(function (res) {
    alert(res.errMsg);
});
技術分享圖片

頁面,就是,這樣:

後臺代碼:

技術分享圖片
                    ...... 省略獲取access_token 
string url = Request.Url.ToString();//產生GUID //string nonce = Guid.NewGuid().ToString(); string nonce = createNonceStr(); ViewBag.nonce_str = nonce; //產生時間 int timestamp = TimeStampUtils.ConvertDateTimeInt(DateTime.Now); ViewBag.timestamp= timestamp;string getUrl3 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + AccessToken + "&type=jsapi";string jsonStr3 = HttpUtils.HttpGet(getUrl3, "");var jsapi_ticket = JsonConvert.DeserializeObject<JsapiTicket>(jsonStr3); string ticket = jsapi_ticket.ticket; string[] strArr = { "jsapi_ticket=" + ticket, "timestamp=" + timestamp, "noncestr=" + nonce, "url=" + url }; //進行字典排序 Array.Sort(strArr); string tempStr = string.Join("&", strArr);//sha1加密 string signature = SHA1_Hash(tempStr); ViewBag.signature = signature;
技術分享圖片

創建隨機字符串:

技術分享圖片
        //創建隨機字符串  
        private string createNonceStr()
        {
            int length = 16;
            string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string str = "";
            Random rad = new Random();
            for (int i = 0; i < length; i++)
            {
                str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
            }
            return str;
        }
技術分享圖片

DateTime時間格式轉換為Unix時間戳格式

技術分享圖片
        /// <summary>
        /// DateTime時間格式轉換為Unix時間戳格式
        /// </summary>
        /// <param name="time"> DateTime時間格式</param>
        /// <returns>Unix時間戳格式</returns>
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
技術分享圖片

微信jssdk分享功能開發