 
      
      2020-2-23 seo達(dá)人
最近在做一個(gè)手機(jī)站,要求點(diǎn)擊分享可以直接打開微信分享出去。而不是jiathis,share分享這種的點(diǎn)擊出來二維碼。在網(wǎng)上看了很多,都說APP能喚起微信,手機(jī)網(wǎng)頁實(shí)現(xiàn)不了。也找了很多都不能直接喚起微信。
總結(jié)出來一個(gè)可以直接喚起微信的。適應(yīng)手機(jī)qq瀏覽器和uc瀏覽器。
下面上代碼,把這些直接放到要轉(zhuǎn)發(fā)的頁面里就可以了:
html部分:
    - 
        
            
            
        
        
            
                <script src="mshare.js"></script>//引進(jìn)mshare.js
            
        
    
- 
        
            
            
        
        
            
                <button data-mshare="0">點(diǎn)擊彈出原生分享面板</button>
            
        
    
- 
        
            
            
        
        
            
                <button data-mshare="1">點(diǎn)擊觸發(fā)朋友圈分享</button>
            
        
    
- 
        
            
            
        
        
            
                <button data-mshare="2">點(diǎn)擊觸發(fā)發(fā)送給微信朋友</button>
            
        
    
js部分:
    - 
        
            
            
        
        
            
                <script>
            
        
    
- 
        
            
            
        
        
            
                var mshare = new mShare({
            
        
    
- 
        
            
            
        
        
            
                title: 'Lorem ipsum dolor sit.',
            
        
    
- 
        
            
            
        
        
            
                url: 'http://m.ly.com',
            
        
    
- 
        
            
            
        
        
            
                desc: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quaerat inventore minima voluptates.',
            
        
    
- 
        
            
            
        
        
            
                img: 'http://placehold.it/150x150'
            
        
    
- 
        
            
            
        
        
            
                });
            
        
    
- 
        
            
            
        
        
            
                $('button').click(function () {
            
        
    
- 
        
            
            
        
        
            
                // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接彈出原生
            
        
    
- 
        
            
            
        
        
            
                mshare.init(+$(this).data('mshare'));
            
        
    
- 
        
            
            
        
        
            
                });
            
        
    
- 
        
            
            
        
        
            
                </script>
            
        
    
下面是mshare.js的代碼分享,把這些代碼新建一個(gè)js文件放進(jìn)去,然后在頁面中引進(jìn)就ok了。
    - 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 此插件主要作用是在UC和QQ兩個(gè)主流瀏覽器
            
        
    
- 
        
            
            
        
        
            
                 * 上面觸發(fā)微信分享到朋友圈或發(fā)送給朋友的功能
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                'use strict';
            
        
    
- 
        
            
            
        
        
            
                var UA = navigator.appVersion;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 是否是 UC 瀏覽器
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 判斷 qq 瀏覽器
            
        
    
- 
        
            
            
        
        
            
                 * 然而qq瀏覽器分高低版本
            
        
    
- 
        
            
            
        
        
            
                 * 2 代表高版本
            
        
    
- 
        
            
            
        
        
            
                 * 1 代表低版本
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 是否是微信
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var wx = /micromessenger/i.test(UA);
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 瀏覽器版本
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
            
        
    
- 
        
            
            
        
        
            
                var ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 獲取操作系統(tǒng)信息  iPhone(1)  Android(2)
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var os = (function () {
            
        
    
- 
        
            
            
        
        
            
                var ua = navigator.userAgent;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                if (/iphone|ipod/i.test(ua)) {
            
        
    
- 
        
            
            
        
        
            
                return 1;
            
        
    
- 
        
            
            
        
        
            
                } else if (/android/i.test(ua)) {
            
        
    
- 
        
            
            
        
        
            
                return 2;
            
        
    
- 
        
            
            
        
        
            
                } else {
            
        
    
- 
        
            
            
        
        
            
                return 0;
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }());
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * qq瀏覽器下面 是否加載好了相應(yīng)的api文件
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                var qqBridgeLoaded = false;
            
        
    
- 
        
            
            
        
        
            
            
        
    
- 
        
            
            
        
        
            
                // 進(jìn)一步細(xì)化版本和平臺(tái)判斷
            
        
    
- 
        
            
            
        
        
            
                if ((qq && qqVs < 5.4 && os == 1) || (qq && qqVs < 5.3 && os == 1)) {
            
        
    
- 
        
            
            
        
        
            
                qq = 0;
            
        
    
- 
        
            
            
        
        
            
                } else {
            
        
    
- 
        
            
            
        
        
            
                if (qq && qqVs < 5.4 && os == 2) {
            
        
    
- 
        
            
            
        
        
            
                qq = 1;
            
        
    
- 
        
            
            
        
        
            
                } else {
            
        
    
- 
        
            
            
        
        
            
                if (uc && ((ucVs < 10.2 && os == 1) || (ucVs < 9.7 && os == 2))) {
            
        
    
- 
        
            
            
        
        
            
                uc = 0;
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * qq瀏覽器下面 根據(jù)不同版本 加載對(duì)應(yīng)的bridge
            
        
    
- 
        
            
            
        
        
            
                 * @method loadqqApi
            
        
    
- 
        
            
            
        
        
            
                 * @param  {Function} cb 回調(diào)函數(shù)
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                function loadqqApi(cb) {
            
        
    
- 
        
            
            
        
        
            
                // qq == 0 
            
        
    
- 
        
            
            
        
        
            
                if (!qq) {
            
        
    
- 
        
            
            
        
        
            
                return cb && cb();
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                var script = document.createElement('script');
            
        
    
- 
        
            
            
        
        
            
                script.src = (+qq === 1) ? '//3gimg.qq.com/html5/js/qb.js' : '//jsapi.qq.com/get?api=app.share';
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 需要等加載過 qq 的 bridge 腳本之后
            
        
    
- 
        
            
            
        
        
            
                 * 再去初始化分享組件
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                script.onload = function () {
            
        
    
- 
        
            
            
        
        
            
                cb && cb();
            
        
    
- 
        
            
            
        
        
            
                };
            
        
    
- 
        
            
            
        
        
            
                document.body.appendChild(script);
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * UC瀏覽器分享
            
        
    
- 
        
            
            
        
        
            
                 * @method ucShare
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                function ucShare(config) {
            
        
    
- 
        
            
            
        
        
            
                // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
            
        
    
- 
        
            
            
        
        
            
                // 關(guān)于platform
            
        
    
- 
        
            
            
        
        
            
                // ios: kWeixin || kWeixinFriend;
            
        
    
- 
        
            
            
        
        
            
                // android: WechatFriends || WechatTimeline
            
        
    
- 
        
            
            
        
        
            
                // uc 分享會(huì)直接使用截圖
            
        
    
- 
        
            
            
        
        
            
                var platform = '';
            
        
    
- 
        
            
            
        
        
            
                var shareInfo = null;
            
        
    
- 
        
            
            
        
        
            
                // 指定了分享類型
            
        
    
- 
        
            
            
        
        
            
                if (config.type) {
            
        
    
- 
        
            
            
        
        
            
                if (os == 2) {
            
        
    
- 
        
            
            
        
        
            
                platform = config.type == 1 ? 'WechatTimeline' : 'WechatFriends';
            
        
    
- 
        
            
            
        
        
            
                } else if (os == 1) {
            
        
    
- 
        
            
            
        
        
            
                platform = config.type == 1 ? 'kWeixinFriend' : 'kWeixin';
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];
            
        
    
- 
        
            
            
        
        
            
                // android 
            
        
    
- 
        
            
            
        
        
            
                if (window.ucweb) {
            
        
    
- 
        
            
            
        
        
            
                ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
            
        
    
- 
        
            
            
        
        
            
                return;
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                if (window.ucbrowser) {
            
        
    
- 
        
            
            
        
        
            
                ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
            
        
    
- 
        
            
            
        
        
            
                return;
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * qq 瀏覽器分享函數(shù)
            
        
    
- 
        
            
            
        
        
            
                 * @method qqShare
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                function qqShare(config) {
            
        
    
- 
        
            
            
        
        
            
                var type = config.type;
            
        
    
- 
        
            
            
        
        
            
                //微信好友 1, 微信朋友圈 8
            
        
    
- 
        
            
            
        
        
            
                type = type ? ((type == 1) ? 8 : 1) : '';
            
        
    
- 
        
            
            
        
        
            
                var share = function () {
            
        
    
- 
        
            
            
        
        
            
                var shareInfo = {
            
        
    
- 
        
            
            
        
        
            
                'url': config.url,
            
        
    
- 
        
            
            
        
        
            
                'title': config.title,
            
        
    
- 
        
            
            
        
        
            
                'description': config.desc,
            
        
    
- 
        
            
            
        
        
            
                'img_url': config.img,
            
        
    
- 
        
            
            
        
        
            
                'img_title': config.title,
            
        
    
- 
        
            
            
        
        
            
                'to_app': type,
            
        
    
- 
        
            
            
        
        
            
                'cus_txt': ''
            
        
    
- 
        
            
            
        
        
            
                };
            
        
    
- 
        
            
            
        
        
            
                if (window.browser) {
            
        
    
- 
        
            
            
        
        
            
                browser.app && browser.app.share(shareInfo);
            
        
    
- 
        
            
            
        
        
            
                } else if (window.qb) {
            
        
    
- 
        
            
            
        
        
            
                qb.share && qb.share(shareInfo);
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                };
            
        
    
- 
        
            
            
        
        
            
                if (qqBridgeLoaded) {
            
        
    
- 
        
            
            
        
        
            
                share();
            
        
    
- 
        
            
            
        
        
            
                } else {
            
        
    
- 
        
            
            
        
        
            
                loadqqApi(share);
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                /**
            
        
    
- 
        
            
            
        
        
            
                 * 對(duì)外暴露的接口函數(shù)
            
        
    
- 
        
            
            
        
        
            
                 * @method mShare
            
        
    
- 
        
            
            
        
        
            
                 * @param  {Object} config 配置對(duì)象
            
        
    
- 
        
            
            
        
        
            
                 */
            
        
    
- 
        
            
            
        
        
            
                function mShare(config) {
            
        
    
- 
        
            
            
        
        
            
                this.config = config;
            
        
    
- 
        
            
            
        
        
            
                this.init = function (type) {
            
        
    
- 
        
            
            
        
        
            
                if (typeof type != 'undefined') this.config.type = type;
            
        
    
- 
        
            
            
        
        
            
                try {
            
        
    
- 
        
            
            
        
        
            
                if (uc) {
            
        
    
- 
        
            
            
        
        
            
                ucShare(this.config);
            
        
    
- 
        
            
            
        
        
            
                } else if (qq && !wx) {
            
        
    
- 
        
            
            
        
        
            
                qqShare(this.config);
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                } catch (e) {}
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
- 
        
            
            
        
        
            
                // 預(yù)加載 qq bridge
            
        
    
- 
        
            
            
        
        
            
                loadqqApi(function () {
            
        
    
- 
        
            
            
        
        
            
                qqBridgeLoaded = true;
            
        
    
- 
        
            
            
        
        
            
                });
            
        
    
- 
        
            
            
        
        
            
                if (typeof module === 'object' && module.exports) {
            
        
    
- 
        
            
            
        
        
            
                module.exports = mShare;
            
        
    
- 
        
            
            
        
        
            
                } else {
            
        
    
- 
        
            
            
        
        
            
                window.mShare = mShare;
            
        
    
- 
        
            
            
        
        
            
                }
            
        
    
好了,這樣就可以直接喚起微信進(jìn)行分享啦
藍(lán)藍(lán)設(shè)計(jì)的小編 http://www.ynkmey.cn