用户登录
用户注册

分享至

HTML5中外部浏览器唤起微信分享

  • 作者: 灬跪求粉色木耳灬
  • 来源: 51数据库
  • 2021-10-19

最近在做一个手机站,要求点击分享可以直接打开微信分享出去。而不是jiathis,share分享这种的点击出来二维码。在网上看了很多,都说app能唤起微信,手机网页实现不了。也找了很多都不能直接唤起微信。

总结出来一个可以直接唤起微信的。适应手机qq浏览器和uc浏览器。

下面上代码,把这些直接放到要转发的页面里就可以了:

html部分:

<script src="mshare.js"></script>//引进mshare.js
<button data-mshare="0">点击弹出原生分享面板</button>
<button data-mshare="1">点击触发朋友圈分享</button>
<button data-mshare="2">点击触发发送给微信朋友</button>

js部分:

<script>
var mshare = new mshare({
    title: 'lorem ipsum dolor sit.',
    url: 'http://www.51sjk.com/Upload/Articles/1/0/265/265699_20210708011253404.com',
    desc: 'lorem ipsum dolor sit amet, consectetur adipisicing elit. quaerat inventore minima voluptates.',
    img: 'http://www.51sjk.com/Upload/Articles/1/0/265/265699_20210708011253419.jpg'
});
$('button').click(function () {
    // 1 ==> 朋友圈  2 ==> 朋友  0 ==> 直接弹出原生
    mshare.init(+$(this).data('mshare'));
});
</script>

下面是mshare.js的代码分享,把这些代码新建一个js文件放进去,然后在页面中引进就ok了。

 

/**
 * 此插件主要作用是在uc和qq两个主流浏览器
 * 上面触发微信分享到朋友圈或发送给朋友的功能
 */
'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;
 
/**
 * 获取操作系统信息  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浏览器下面 是否加载好了相应的api文件
 */
var qqbridgeloaded = false;
 
// 进一步细化版本和平台判断
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) {
    // 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']
    // 关于platform
    // ios: kweixin || kweixinfriend;
    // android: wechatfriends || wechattimeline
    // uc 分享会直接使用截图
    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 浏览器分享函数
 * @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);
    }
}
/**
 * 对外暴露的接口函数
 * @method mshare
 * @param  {object} config 配置对象
 */
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) {}
    }
}
// 预加载 qq bridge
loadqqapi(function () {
    qqbridgeloaded = true;
});
if (typeof module === 'object' && module.exports) {
    module.exports = mshare;
} else {
    window.mshare = mshare;
}

好了,这样就可以直接唤起微信进行分享啦

总结

以上所述是小编给大家介绍的html5中外部浏览器唤起微信分享,希望对大家有所帮助

软件
前端设计
程序设计
Java相关