手機網頁調用微信分享以及定位


var config = {
                title: 'hhhhhh哈哈哈',
                url: 'http://jzyp.bingying.ren/',
                desc: 'hhhhh哈哈哈',
                img: '',
                type: 2 // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接彈出原生 3==>QQ
            }
            let mshare = new mShare(config);
            mshare.init(2);
/**
 * mshare.js
 * 此插件主要作用是在UC和QQ兩個主流瀏覽器
 * 上面觸發微信分享到朋友圈或發送給朋友的功能
 * 代碼編寫過程中 參考:
 * http://mjs.sinaimg.cn/wap/module/share/201501261608/js/addShare.js
 * 此外,JefferyWang的項目對我也有一定啟示:
 * https://github.com/JefferyWang/nativeShare.js
 *
 * @revisor  angusfu1126@qq.com
 * @date     2015-07-22
 */

!(function(global) {

    'use strict';

    var UA, uc, qq, wx, tc, qqVs, ucVs, os, qqBridgeDone;

    UA = navigator.appVersion;

    // 是否是 UC 瀏覽器
    uc = UA.split('UCBrowser/').length > 1 ? 1 : 0;

    // 判斷 qq 瀏覽器
    // 然而qq瀏覽器分高低版本   2代表高版本  1代表低版本
    qq = UA.split('MQQBrowser/').length > 1 ? 2 : 0;

    // 是否是微信
    wx = ((UA.match(/MicroMessenger/i)) && (UA.match(/MicroMessenger/i).toString().toLowerCase() == 'micromessenger'));

    // 瀏覽器版本
    qqVs = qq ? parseFloat(UA.split('MQQBrowser/')[1]) : 0;
    ucVs = uc ? parseFloat(UA.split('UCBrowser/')[1]) : 0;

    //獲取操作系統信息  iPhone(1)  Android(2)
    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瀏覽器下面 是否加載好了相應的api文件
    qqBridgeDone = false;

    // 進一步細化版本和平台判斷
    // 參考: https://github.com/JefferyWang/nativeShare.js
    // http://mjs.sinaimg.cn/wap/module/share/201501261608/js/addShare.js
    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瀏覽器下面 根據不同版本 加載對應的bridge
     * @method loadqqApi
     * @param  {Function} cb 回調函數
     */
    function loadqqApi(cb) {
        if (!qq) { // qq == 0
            return cb && cb();
        }

        var qqApiScript = document.createElement('script');
        //需要等加載過qq的接口文檔之后,再去初始化分享組件
        qqApiScript.onload = function() {
            cb && cb();
        };
        qqApiScript.onerror = function() {};
        // qq == 1 低版本
        // qq == 2 高版本
        qqApiScript.src = (qq == 1) ? 'http://3gimg.qq.com/html5/js/qb.js' : 'http://jsapi.qq.com/get?api=app.share';

        document.body.appendChild(qqApiScript);
    }


    /**
     * UC瀏覽器分享
     * @method ucShare
     */
    function ucShare(config) {
        // ['title', 'content', 'url', 'platform', 'disablePlatform', 'source', 'htmlID']
        // 關於platform
        // ios: kWeixin || kWeixinFriend;
        // android: WechatFriends || WechatTimeline
        // uc 分享會直接使用截圖

        var platform = '',
            shareInfo;

        // 指定了分享類型
        if (config.type) {
            if (os == 2) {
                if (config.type == 1) {
                    platform = 'WechatTimeline';
                } else if (config.type == 2) {
                    platform = 'WechatFriends';
                } else if (config.type == 3) {
                    platform = 'QQ';
                } else if (config.type == 4) {
                    platform = 'SinaWeibo';
                } else if (config.type == 0) {
                    platform = 'undefined';
                }
            } else if (os == 1) {
                if (config.type == 1) {
                    platform = 'kWeixinFriend';
                } else if (config.type == 2) {
                    platform = 'kWeixin';
                } else if (config.type == 3) {
                    platform = 'kQQ';
                } else if (config.type == 4) {
                    platform = 'kSinaWeibo';
                } else if (config.type == 0) {
                    platform = 'undefined';
                }
            }
        }

        shareInfo = [config.title, config.desc, config.url, platform, '', '', ''];
        // android
        if (window.ucweb) {
            ucweb.startRequest && ucweb.startRequest('shell.page_share', shareInfo);
        }
        // ios
        else if (window.ucbrowser) {
            ucbrowser.web_share && ucbrowser.web_share.apply(null, shareInfo);
        }

    };


    /**
     * qq瀏覽器分享函數
     * @method qqShare
     */
    function qqShare(config) {
        var type = '';

        //微信好友1, 微信朋友圈8,QQ 4
        if (config.type == 1) {
            type = 8;
        } else if (config.type == 2) {
            type = 1;
        } else if (config.type == 3) {
            type = 4;
        } else if (config.type == 4) {
            type = 11;
        } else if (config.type == 0) {
            type = 'undefined';
        }

        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 (qqBridgeDone) {
            share();
        } else {
            loadqqApi(share);
        }
    };

    /**
     * 對外暴露的接口函數
     * @method mShare
     * @param  {Object} config 配置對象  參數見示例
     *     var config = {
     *          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://img1.40017.cn/cn/s/c/2015/loading.gif'
     *        , type  : type // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接彈出原生 3==>QQ
     *     }
     */
    function mShare(config) {
        this.check = function(succssFn, wxFn, failFn) {
            if (uc) {
                succssFn();
            } else if (qq && !wx) {
                succssFn();
            } else if (wx) {
                wxFn();
            } else {
                failFn();
            }
        }
        this.config = config;
        this.init = function(type) {
            if (type) {
                this.config.type = type;
            }
            if (uc) {
                ucShare(this.config);
            } else if (qq && !wx) {
                qqShare(this.config);
            }
        }
    }

    // 預加載 qq bridge
    loadqqApi(function() {
        qqBridgeDone = true;
    });

    // 方法暴露給全局變量
    global.mShare = mShare;
    global.isWX = wx;

})(this);

定位

// 獲取定位
        getLocation: function() {
            // alert('aaa');
            if (navigator.geolocation) {
                // alert('瀏覽器支持定位');
                //瀏覽器支持geolocation
                navigator.geolocation.getCurrentPosition(function(position) {
                    //經度
                    var longitude = position.coords.longitude;
                    //緯度
                    var latitude = position.coords.latitude;
                    alert('long' + longitude + 'lat' + latitude);
                });
            } else {
                alert('瀏覽器不支持定位');
            }
        },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM